单词助手
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
class Word//单词的类
{
public:
string en;
string ch;
Word()
{
en="";
ch="";
}
void Set(string e,string c)
{
en=e;
ch=c;
}
void Show()
{
cout<<"英文:"<<en<<" 中文:"<<ch<<endl;
}
};
int n;//词库的单词数量
int test;//本次测试次数
int grade[100];//学生成绩
Word word[1000];//单词数量
void Read()
{
n=0;
ifstream f;
f.open("Vocabulary.txt",ios::in);
if(!f.is_open())
{
cout << "打开文件错误";
exit (1);
}
else {}
f.clear();
f.seekg(0);
string e,c;
while (!f.eof())
{
f>>e;
f>>c;
word[n].Set(e,c);
n++;
}
f.close();
}
void Write()
{
int m=0;
ofstream f;
f.open("Vocabulary.txt",ios::out | ios::trunc);
if(!f.is_open())
{
cout << "打开文件错误"<<endl;
exit (0);
}
else {}
while (m<n)
{
f<<endl;
f<<word[m].en;
f<<" ";
f<<word[m].ch;
m++;
}
f.close();
Read();
}
void menu()//菜单界面
{
int j=1;
while(j){
system("cls");
cout<<endl<<endl<<" ***********************************欢迎使用单词助手***********************************"<<endl;
cout<<" * *"<<endl;
cout<<" * *"<<endl<<" * 1.单词预览 *"<<endl;
cout<<" * *"<<endl<<" * 2.词库管理 *"<<endl;
cout<<" * *"<<endl<<" * 3.单词测试 *"<<endl;
cout<<" * *"<<endl<<" * 0.退出程序 *"<<endl;
cout<<" * *"<<endl<<" **************************************************************************************"<<endl;
void Show();
void Manage();
void Test();
char i;
i=getch();
switch(i)
{
case'1':
Show();
break;
case'2':
Manage();
break;
case'3':
Test();
break;
case'0':
exit(0);
return;
default:
cout<<"请选择0-3之间的数字"<<endl;
}
system("pause");
}
}
void Show()//单词预览
{
void menu();
system("cls");
cout<<endl<<endl;
if(n==0)
{
cout<<"词库目前为空"<<endl;
}
else
{
int m=0;
while(m<n)
{
cout<<m+1<<":";
word[m].Show();
m++;
}
}
cout<<"按任意键返回菜单!"<<endl;
getch();
menu();
}
void Manage()//词库管理
{
system("cls");
cout<<endl<<" **************************************************************************************"<<endl;
cout<<" * *"<<endl<<" * 1.添加单词 *"<<endl;
cout<<" * *"<<endl<<" * 2.删除单词 *"<<endl;
cout<<" * *"<<endl<<" * 3.修改单词 *"<<endl;
cout<<" * *"<<endl<<" * 4.查询单词 *"<<endl;
cout<<" * *"<<endl<<" * 0.退出程序 *"<<endl;
cout<<" * *"<<endl<<" **************************************************************************************"<<endl;
void Show();
void Add();
void Del();
void Modify();
void Search();
char i;
i=getch();
switch(i)
{
case'1':
Add();
break;
case'2':
Del();
break;
case'3':
Modify();
break;
case'4':
Search();
break;
case'0':
menu();
break;
}
}
void Test()//单词测试
{
void Grade();
system("cls");
cout<<endl<<" **************************************************************************************"<<endl;
cout<<" * *"<<endl<<" * 1.英译汉测试 *"<<endl;
cout<<" * *"<<endl<<" * 2.汉译英测试 *"<<endl;
cout<<" * *"<<endl<<" * 3.成绩查询 *"<<endl;
cout<<" * *"<<endl<<" * 0.返回菜单 *"<<endl;
cout<<" * *"<<endl<<" **************************************************************************************"<<endl;
void ENtoCH();
void CHtoEN();
char i;
i=getch();
switch(i)
{
case'1':
ENtoCH();
break;
case'2':
CHtoEN();
break;
case'3':
Grade();
break;
case'0':
menu();
break;
}
}
void Add()//添加单词
{
void menu();
system("cls");
string e,c;
cout<<endl<<endl;
cout<<endl<<"请输入您要添加单词的英文"<<endl;
cin>>e;
cout<<endl<<"请输入您要添加单词的中文"<<endl;
cin>>c;
word[n].Set(e,c);
n++;
Write();
cout<<"按任意键返回菜单!"<<endl;
getch();
menu();
}
void Del()//删除单词
{
void menu();
system("cls");
string e,c;
int m;
cout<<endl<<endl;
cout<<endl<<"请选择删除方法:"<<endl;
cout<<endl<<"1、按英文删除单词"<<endl;
cout<<endl<<"2、按中文删除单词"<<endl;
char i;
i=getch();
switch(i)
{
case '1':
cout<<endl<<"请输入您要删除单词的英文"<<endl;
cin>>e;
for(m=0;m<n;m++)
{
if(word[m].en==e)
{
break;
}
}
break;
case '2':
cout<<endl<<"请输入您要删除单词的中文"<<endl;
cin>>c;
for(m=0;m<n;m++)
{
if(word[m].ch==c)
{
break;
}
}
break;
default:
cout<<"您的按键有误,按任意键返回重新选择!"<<endl;
getch();
Del();
}
word[m].Show();
cout<<"该单词已经删除"<<endl;
for(;m<n;m++)
{
word[m]=word[m+1];
}
Write();
cout<<"按任意键返回菜单!"<<endl;
getch();
menu();
}
void Modify()//单词修改
{
void menu();
system("cls");
string e,c;
int m;
cout<<endl<<endl;
cout<<endl<<"请选择修改的单词查找的方法:"<<endl;
cout<<endl<<"1、按英文查找要修改的单词"<<endl;
cout<<endl<<"2、按中文查找要修改的单词"<<endl;
char i;
i=getch();
switch(i)
{
case '1':
cout<<endl<<"请输入您要查找单词的英文"<<endl;
cin>>e;
for(m=0;m<n;m++)
{
if(word[m].en==e)
{
break;
}
}
break;
case '2':
cout<<endl<<"请输入您要查找单词的中文"<<endl;
cin>>c;
for(m=0;m<n;m++)
{
if(word[m].ch==c)
{
break;
}
}
break;
default:
cout<<"您的按键有误,按任意键返回重新选择!"<<endl;
getch();
Del();
}
cout<<"您查找的单词为:";
word[m].Show();
cout<<"请编辑该单词!"<<endl;
cout<<endl<<"请输入您要修改单词的英文"<<endl;
cin>>e;
cout<<endl<<"请输入您要修改单词的中文"<<endl;
cin>>c;
word[m].Set(e,c);
Write();
cout<<"按任意键返回菜单!"<<endl;
getch();
menu();
}
void Search()//单词查询
{
void menu();
system("cls");
string e,c;
int m;
cout<<endl<<endl;
cout<<endl<<"请选择查找方法:"<<endl;
cout<<endl<<"1、按英文查找单词"<<endl;
cout<<endl<<"2、按中文查找单词"<<endl;
char i;
i=getch();
switch(i)
{
case '1':
cout<<endl<<"请输入您要查找单词的英文"<<endl;
cin>>e;
for(m=0;m<n;m++)
{
if(word[m].en==e)
{
break;
}
}
break;
case '2':
cout<<endl<<"请输入您要查找单词的中文"<<endl;
cin>>c;
for(m=0;m<n;m++)
{
if(word[m].ch==c)
{
break;
}
}
break;
default:
cout<<"您的按键有误,按任意键返回重新选择!"<<endl;
getch();
Del();
}
cout<<"您查找的单词为:";
word[m].Show();
cout<<"按任意键返回菜单!"<<endl;
getch();
menu();
}
void ENtoCH()//英译汉
{
void menu();
system("cls");
cout<<endl<<endl;
int m;
string e,c;
if(n==0)
{
cout<<"词库目前为空"<<endl;
}
else
{
test++;
for(m=0;m<n;m++)
{
cout<<word[m].en<<endl;
cin>>c;
if(word[m].ch==c)
{
cout<<"恭喜你,正确!"<<endl;
grade[test]++;
}
else
{
cout<<"很遗憾,错误!"<<endl;
}
}
}
cout<<"本次测试结果为:"<<endl;
cout<<"测试共:"<<n<<"个单词"<<endl;
cout<<"正确共:"<<grade[test]<<"个单词"<<endl;
cout<<"按任意键返回菜单!"<<endl;
getch();
menu();
}
void CHtoEN()//汉译英
{
void menu();
system("cls");
cout<<endl<<endl;
int m;
string e,c;
if(n==0)
{
cout<<"词库目前为空"<<endl;
}
else
{
test++;
cout<<endl<<"请输入您要查找单词的中文"<<endl;
cin>>c;
for(m=0;m<n;m++)
{
cout<<word[m].ch<<endl;
cin>>c;
if(word[m].en==e)
{
cout<<"恭喜你,正确!"<<endl;
grade[test]++;
}
else
{
cout<<"很遗憾,错误!"<<endl;
}
}
}
cout<<"本次测试结果为:"<<endl;
cout<<"测试共:"<<n<<"个单词"<<endl;
cout<<"正确共:"<<grade[test]<<"个单词"<<endl;
cout<<"按任意键返回菜单!"<<endl;
getch();
menu();
}
void Grade()//成绩查询
{
void menu();
system("cls");
if(test==0)
{
cout<<"本次还没有进行测试"<<endl<<endl;
}
else
{
int m;
for(m=1;m<=test;m++)
{
cout<<"第"<<m+1<<"次测试结果为:"<<endl;
cout<<"测试共:"<<n<<"个单词"<<endl;
cout<<"正确共:"<<grade[test]<<"个单词"<<endl<<endl;
}
}
cout<<"按任意键返回菜单!"<<endl;
getch();
menu();
}
void main()
{
test=0;
Read();
menu();
}