最简单的C++制作家默神器的方法

现在,有许多学校的英语老师为了学生巩固所学重点,布置了家默作业
为了照顾那些父母不在身边的同学,便想要用程序解决该问题。
大家应该都知道Windows中的VBS文件是有朗读功能的。所以为了简单,不装第三方库,今天就得用到它!

上代码:

#include 
#include 
#include  
using namespace std;
int main()  
{  
    ifstream in("dicn.txt");  
    ofstream out("start.vbs");  
    string filename;
    string line;
    if(in) // 有该文件  
    {
        while (getline (in, line)) // line中不包括每行的换行符  
        {
            out << "CreateObject(\"SAPI.SpVoice\").Speak \"" << line << "\"" << endl;
            out << "wscript.sleep 1000*8" << endl;//每报一个单词等待时间
        }
        out << "CreateObject(\"SAPI.SpVoice\").Speak \"" << "默写结束" << "\"" << endl;
    }
    else //没有该文件
    {
        cout <<"[Error] No such file" << endl;
    }
    return 0;
}

编译后运行效果为:

dicn.txt中的内容:

economy
departure time
arrival time
travel to the airport
What time should we arrive at the airport?

-----------------------------------

生成出的start.vbs中的内容应该是这样的:

CreateObject("SAPI.SpVoice").Speak "economy"
wscript.sleep 1000*8
CreateObject("SAPI.SpVoice").Speak "departure time"
wscript.sleep 1000*8
CreateObject("SAPI.SpVoice").Speak "arrival time"
wscript.sleep 1000*8
CreateObject("SAPI.SpVoice").Speak "travel to the airport"
wscript.sleep 1000*8
CreateObject("SAPI.SpVoice").Speak "What time should we arrive at the airport?"
wscript.sleep 1000*8
CreateObject("SAPI.SpVoice").Speak "默写结束"

作者亲测,绝对好用!

你可能感兴趣的:(windows)