64,662
社区成员
发帖
与我相关
我的任务
分享
/*
发表于:2008-06-15 13:49:47 楼主
背单词软件开发
1.录入单词
China 中国
Japan 日本
…….
使用数组或文件存放。
2.随机测试
系统随机显示单词(用户可以选择中译英,或英译中),对回答错误的单词应该可重新回答。
3. 能够统计回答的正确单词的个数。
程序我已经写出来了,这个是下周一要交的。问题有一个:我在进行单词测试的时候无法把文件中曾经录入的单词调用出来。
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;
void wtest(); //单词测试函数
void bkuang(); //主界面边框
void Chinese(); //汉译英函数
void English(); //英译汉函数
void luru(); //录入单词函数
struct ciku
{
char english[10];
char chinese[10];
}ciku[100]={{"China","中国"},{"one","一个"},{"well","好的"},{"high","高的"},{"Japan","日本"},{"am","是"}};
void print() //界面函数
{ char ch;
bkuang();
cout <<setw(12) <<"#" <<"#####欢迎您进入背单词系统#####" <<"#" <<endl;
bkuang();
cout <<setw(12) <<"#" <<" 1、录入单词" <<setw(12) <<"#" <<endl;
cout <<setw(12) <<"#" <<" 2、单词测试" <<setw(12) <<"#" <<endl;
cout <<setw(12) <<"#" <<" 0、退出系统" <<setw(12) <<"#" <<endl;
bkuang();
cout <<"选择1.进入录入单词系统" <<endl;
cout <<"选择2.进入单词测试系统" <<endl;
cout <<"选择0.退出系统" <<endl;
cout <<"请输入您要进入的系统:" <<endl;
cin>>ch;
if(ch=='1')
{
luru();
}
if(ch=='2')
{
wtest();
}
else
{
cout <<"谢谢使用,再见!" <<endl;
exit(0);
}
}
void bkuang() //边框函数
{
cout <<setw(12);
for(int i=0;i <30;i++)
cout <<"*";
cout <<endl;
}
void luru() //单词录入系统
{
cout <<"****************************************" <<endl;
cout <<"**********欢迎进入单词录入系统**********" <<endl;
cout <<"****************************************" <<endl;
int i,n;
cout <<"如果要退出请直接输入#!" <<endl;
cout <<"请输入你要输入的单词个数:" <<endl;
cin>>n;
FILE *fp;
for(i=6;i <n+6;i++)
{
cout <<endl <<"请输入单词:" <<endl; //把单词输入到结构体当中
cout <<"英文:";
cin>>ciku[i].english;
cout <<"中文:";
cin>>ciku[i].chinese;
}
fp=fopen("ciku.txt","a+"); //将结构体中的单词写进文件
for(i=0;i <n+6;i++)
{
fwrite(&ciku[i],sizeof(ciku[i]),1,fp);
}
fclose(fp);
print();
}
void wtest() //单词测试系统
{
cout <<"****************************************" <<endl;
cout <<"**********欢迎进入单词测试系统**********" <<endl;
cout <<"****************************************" <<endl;
char n;
cout <<"请选择单词测试模式:" <<'\n';
cout <<"1英译汉" <<'\n' <<"2汉译英" <<endl;
cin>>n;
if(n=='1')
{
English();
}
if(n=='2')
{
Chinese();
}
}
void English() //英译汉函数
{
cout <<"###########欢迎进入单词测试——英译汉系统###########" <<endl;
cout <<"如果要退出请直接输入#!" <<endl;
FILE *fp;
char Chin[10];
int t=0,n=0,f=0,w=0,x;
{
srand(time(0)); //随机产生一个整数
x=rand()%5;
for(n=0;n <10;n++)
{
cout <<ciku[x].english <<endl;
cout <<"请您输入其汉语意思:" <<endl;
cin>>Chin;
if (strcmp(Chin,"#")==0) //输入的字符串如果为'#'则停止该程序
break;
if(strcmp(ciku[x].chinese,Chin)==0)
{
cout <<"恭喜您,答对了!请继续测试!" <<endl;
t++; //答对的题的个数
x=++x%5; //再随机产生一个单词
}
else
{
cout <<"您的答案有误,请重新输入:" <<endl;
cin>>Chin;
if (strcmp(Chin,"#")==0)
break;
if(ciku[x].chinese==Chin)
{
cout <<"恭喜您,答对了!请继续测试." <<endl;
t++;
x=++x%5;
}
else
{
cout <<"您的答案有误,请继续努力!" <<endl;
cout <<"正确答案是:" <<ciku[x].chinese <<endl;
f++;
x=++x%5;
}
}
}
}
cout <<"您答对了" <<t <<"道题" <<endl;
print();
}
void Chinese() //汉译英函数
{
cout <<"###########欢迎进入单词测试——汉译英系统###########" <<endl;
cout <<"如果要退出请直接输入#!" <<endl;
char eng[10];
int t=0,n=0,f=0,w=0,x;
srand(time(0));
x=rand()%5;
for(n=0;n <10;n++)
{
cout <<ciku[x].chinese <<'\n' <<"请您输入其英语意思:" <<endl;
cin>>eng;
if (strcmp(eng,"#")==0)
break;
if(strcmp(ciku[x].english,eng)==0)
{
cout <<"恭喜您,答对了!请继续测试!" <<endl;
t++;
x=++x%4;
}
else
{
cout <<"您的答案有误,请重新输入:" <<endl;
cin>>eng;
if (strcmp(eng,"#")==0)
break;
if(strcmp(ciku[x].english,eng)==0)
{
cout <<"恭喜您,答对了!请继续测试!" <<endl;
t++;
x=++x%5;
}
else
{
cout <<"您的答案有误,请继续努力!" <<endl;
cout <<"正确答案是:" <<ciku[x].english <<endl;
f++;
x=++x%5;
}
}
}
cout <<"您答对了" <<t <<"道题" <<endl;
print();
}
int main()
{
// 首先调入已经存在的文件
FILE* fp;
if( (fp=fopen("ciku.txt","r") ) != NULL)
{
int i = 0;
printf("%d\n",sizeof(ciku[0]));
while(fread(&ciku[i],sizeof(ciku[0]),1,fp) == 1 ) {i++;}
fclose(fp);
}
print();
system("pause");
}