本程序中的随机函数无法执行,不知道哪里出错了,希望高手们帮帮忙!多谢咯!!!#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
using namespace std;
void search_1();
void search_2();
void add();
void test();
int B,Y;
void kaitou();
void te_1();
void te_3();
void te_2();
void te_4();
int big();
void random();
string chinese;
string english;
struct word
{
char english[20];
char chinese[20];
};
int i;
word wor[100];
int main()
{
kaitou();
return 0;
}
void kaitou()
{
cout<<" ** ** "<<endl;
cout<<" ** ** ** ** ** ** ** ** "<<endl;
cout<<" **** ^_^ 欢迎来到单词记忆轩 ^_^ **** "<<endl;
cout<<"** ** ** ** ** **"<<endl;
cout<<"* * * 1.中文译英文 * * *"<<endl;
cout<<"** * 2.英文译中文 * **"<<endl;
cout<<" * * 3.添加新词 * * "<<endl;
cout<<" ** ** * 4.记词测试 * ** ** "<<endl;
cout<<" * * 5.退出系统 * * "<<endl;
cout<<" ** ** * ** ** "<<endl;
cout<<" ** * ** "<<endl;
cout<<" * * * * * "<<endl;
cout<<" *** "<<endl;
cout<<"请选择所需要的功能"<<":"<<endl;
char a;
cin>>a;
switch(a)
{
case'1':search_1();break;
case'2':search_2();break;
case'3':add();break;
case'4':test();break;
case'5':jiewei();break;
default:cout<<"请重新输入 ";break;
}
}
void add() //添加模块
{
void fan();
ofstream outfile("w1.txt",ios::app);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"输入中文和英语"<<endl;
cout<<"请输入想输入单词的个数n"<<endl;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个单词"<<endl;
cin>>wor[i].chinese;
outfile<<wor[i].chinese<<endl;
cin>>wor[i].english;
outfile<<wor[i].english<<endl;
}
outfile.close();
cout<<"单词输入完毕"<<endl;
fan();
}
void fan()
{
cout<<"请选择继续添加单词(Y/N)?"<<endl;
char b;
cin>>b;
if (b=='y')add();
else if(b=='n')main();
else abort();
}
void search_1() //查询模块
{
cout<<"请输入您要查询的中文:";
cin>>chinese;
ifstream infile("w1.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<100;i++)
{
infile>>wor[i].chinese;
infile>>wor[i].english;
if(chinese==wor[i].chinese)
cout<<wor[i].english<<endl;
}
cout<<"请选择您希望的选项:"<<"1.继续"<<"2.退出"<<endl;
int b;
cin>>b;
switch(b)
{
case 1:search_1();break;
case 2:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void search_2()
{
cout<<"请输入您要查询的英文:";
cin>>english;
ifstream infile("w1.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int j=2;j<100;j++)
{
infile>>wor[j].english;
if(english==wor[j].english)
cout<<wor[j-1].english<<endl;
}
int f;
cout<<"请选择您希望的选项:"<<"1.继续"<<"2.退出"<<endl;
cin>>f;
switch(f)
{
case 1:search_2();break;
case 2:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void jiewei() //退出模块
{cout<<"谢谢大家光临本单词记忆系统"<<endl;
cout<<"希望大家多提宝贵意见"<<endl;
cout<<"欢迎大家再次光临"<<endl;
}
void test() //测试模块
{
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<20;i++)
{
infile>>wor[i].chinese;
infile>>wor[i].english;
}
infile.close();
cout<<"测试开始咯!"<<endl;
cout<<"请选择"<<":"<<"1.输出英语译成汉语"<<"2.输出汉语译成英语"<<"3.随机英译汉测试"<<"4.随机汉译英测试"<<"5.回到主菜单"<<endl;
int p;
cin>>p;
switch(p)
{
case 1:te_1();break;
case 2:te_2();break;
case 3:te_3();break;
case 4:te_4();break;
case 5:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void te_1() //顺序测试模块开始
{
int j=0;
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
int n;
for(int i=1;i<20;i++);
{
infile>>wor[i].english;
infile>>wor[i].chinese;
}
infile.close();
cout<<"请输入你要测试的单词个数"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"第"<<i+1<<"个单词是"<<endl;
cout<<wor[i].english<<" ";
cout<<endl;
cin>>chinese;
if(chinese==wor[i].chinese)
cout<<"棒极啦!"<<endl;
else
{
cout<<" 错了没关系"<<endl;
cout<<"正确答案是"<<endl;
cout<<wor[i].chinese<<endl;
}
if(wor[i].chinese!=chinese) j++;
}
cout<<"测试结束"<<endl;
cout<<"您答错的单词个数有"<<endl;
cout<<j<<endl;
cout<<"1.再来"<<"2.后退"<<"3.回到主菜单"<<endl;
int a;
cin>>a;
switch(a)
{
case 1:te_1();break;
case 2:test();break;
case 3:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void te_2()
{
int j=0;
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
int n;
for(int i=0;i<20;i++);
{
infile>>wor[i].english;
infile>>wor[i].chinese;
}
infile.close();
cout<<"请输入你要测试的单词个数"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"第"<<i+1<<"个单词是"<<endl;
cout<<wor[i].chinese<<" ";
cout<<endl;
cin>>english;
if(english==wor[i].english)
cout<<"棒极啦!"<<endl;
else
{
cout<<" 错了没关系"<<endl;
cout<<"正确答案是"<<endl;
cout<<wor[i].english<<endl;
}
if(wor[i].english!=english) j++;
}
cout<<"测试结束"<<endl;
cout<<"您答错的单词个数有"<<endl;
cout<<j<<endl;
cout<<"1.再来"<<"2.后退"<<"3.回到主菜单"<<endl;
int a;
cin>>a;
switch(a)
{
case 1:te_2();break;
case 2:test();break;
case 3:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void te_3() //随机模块开始
{
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<100;i++)
{
infile>>wor[i].english;
infile>>wor[i].chinese;
}cout<<endl;
string m;int n;
for( i=0;i<100;i++)
{
if(wor[i].chinese=="")
{B=i;break;}
}//return B;
for(int j=0;;j++)
{
srand(time(NULL));
int number=rand()%B+1;
cout<<"随机数为:"<<number<<endl;
if(wor[number].chinese=="")kaitou();
else cout<<"※"<<wor[number].chinese<<"※"<<endl;
cout<<"请输入该中文的英语单词"<<endl;
cin>>m;
if(m==wor[number].english)
{cout<<"恭喜你,回答正确"<<endl;}
else
{cout<<"很抱歉,回答错误"<<endl;}
cout<<" 请选择 "<<endl;
cout<<"1:继续测试。2:返回菜单"<<endl;
cin>>n;
if(n==1)continue;
if(n==2)break;
}kaitou();
}
void te_4()
{
string k;int n;
for(int i=0;i<100;i++)
{
if(wor[i].chinese=="")
{B=i;break;}
}
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<100;i++)
{
infile>>wor[i].chinese;
infile>>wor[i].english;
}cout<<endl;
for(int j=0;;j++)
{
srand(time(NULL));
int number=rand()%B+1;
cout<<"随机数为:"<<number<<endl;
if(wor[number].english=="")kaitou();
else cout<<"※"<<wor[number].english<<"※"<<endl;
cout<<"请输入该中文的英语单词"<<endl;
cin>>k;
if(k==wor[number].chinese)
{cout<<"恭喜你,回答正确"<<endl;}
else
{cout<<"很抱歉,回答错误"<<endl;}
cout<<" 请选择 "<<endl;
cout<<"1:继续测试。2:返回菜单"<<endl;
cin>>n;
if(n==1)continue;
if(n==2)break;
}kaitou();
}