源代码
https://download.csdn.net/download/qq_41948288/85170411
总体要求
一、软件开发目的
实现简单电子英汉词典的功能,具体管理操作包括单词的添加、显示、查找、删除、修改和保存等。
二、数据结构
采用结构体数组,每个数据的结构应当包括:单词的英文拼写,单词的中文释义。
三、软件功能说明
1.词条录入:即添加单词记录。
2.信息显示:将所有的单词按字母顺序显示。
3.词条修改:对已经输入的单词信息进行修改。
4.词条删除:删除某个单词记录。
5.单词查询: 输入单词英文拼写,输出该单词的中文释义。
6.信息保存:将单词信息保存到文件。
7.退出系统
四、软件验收标准
1.有较为美观简洁大方的菜单,能保证用户方便、直观、快捷的熟悉并使用软件的各项功能。
系统菜单功能项:
1、词条录入
2、信息显示
3、词条修改
4、词条删除
5、单词查询
6、信息保存
7、退出系统
注意:要求每执行一个具体的功能之后,程序将重新显示菜单。
2.有严密正确的数据验证功能和数据处理功能,能够实现各功能模块。
3.系统要有一定的可靠性、稳定性。
字典类
#include <vector>
#include <string>
using namespace std;
#define DICTIONARY "dictionary.txt"
#define NEWWORDBOOK "newWordBook.txt"
class dictionary
{
public:
vector<string> dictionaryWords;
vector<string> newWordBookWords;
vector<string>::iterator iter;
public:
dictionary();
//~dictionary();
void showDictionary();
void exitSystem();
void findWord(string word);
void addWord(string word);
void deleteWord(string word);
void modifyWord(string word);
};
主要函数介绍
构造函数
dictionary::dictionary() {
string dictionaryWord;
string newWordBookWord;
ifstream ifsd(DICTIONARY, ios::in);
ifstream ifsn(NEWWORDBOOK, ios::in);
assert(ifsd.is_open());
while (ifsd >> dictionaryWord)
{
this->dictionaryWords.push_back(dictionaryWord);
}
assert(ifsn.is_open());
while (ifsn >> newWordBookWord)
{
this->newWordBookWords.push_back(newWordBookWord);
}
ifsd.close();
ifsn.close();
}
将字典和生词本中的内容分别读入dictionaryWords
和newWordBookWords
中,以vector<string>
保存在内存中。
显示生词本
void dictionary::showDictionary() {
for (vector<string>::iterator i = this->newWordBookWords.begin();
i != this->newWordBookWords.end(); i++)
{
cout << *i << endl;
}
}
(认为显示整个字典无意义,修改题目为显示生词本)
使用迭代器遍历并显示生词本中的每个元素。
查找单词
void dictionary::findWord(string word) {
if (word == "")
{
cerr << "没有输入要查询的单词" << endl;
return;
}
vector<string>::iterator iterDictionary = find(this->dictionaryWords.begin(), this->dictionaryWords.end(), word);
vector<string>::iterator iterNewWordBook = find(this->newWordBookWords.begin(), this->newWordBookWords.end(), word);
char YorN = 'n';
if (iterDictionary == this->dictionaryWords.end() && iterNewWordBook == this->newWordBookWords.end())
{
cout << "字典中还没有这个单词 , 是否自己保存到生词本?[y / n]" << endl;
cin >> YorN;
if (YorN == 'y') {
addWord(word);
}
else
{
return;
}
}
else
{
cout << "\n释义: " << endl;
if (iterDictionary == this->dictionaryWords.end()) {
cout << "\n" << *(iterNewWordBook + 1) << endl;
}
if (iterNewWordBook == this->newWordBookWords.end()) {
cout << "\n" << *(iterDictionary + 1) << endl;
}
}
cout << "\n" << endl;
}
生词本添加生词
void dictionary::addWord(string word) {
string interpretation = "";
ofstream ofs(NEWWORDBOOK, ios::out | ios::app);
cout << "请输入释义: \n" << endl;
cin >> interpretation;
ofs << word << "\n" << interpretation << endl;
ofs.close();
}
生词本删除生词
void dictionary::deleteWord(string word) {
char YorN = 'n';
vector<string>::iterator iterNewWordBook =
find(this->newWordBookWords.begin(), this->newWordBookWords.end(), word);
if (iterNewWordBook != this->newWordBookWords.end())
{
cout << "查找到的要删除的单词释义为:" << endl;
this->findWord(word);
cout << "是否确认删除? [y / n]" << endl;
cin >> YorN;
if (YorN == 'y')
{
this->newWordBookWords.erase(iterNewWordBook, iterNewWordBook + 2);
ofstream ofs(NEWWORDBOOK, ios::out);
for (vector<string>::iterator iter = this->newWordBookWords.begin();
iter != this->newWordBookWords.end(); iter++) {
ofs << *iter << endl;
}
cout << "单词已从生词本移除." << endl;
}
}
else {
cerr << "提示:" << endl;
cerr << "只允许删除自己创建的生词本" << endl;
cerr << "单词不存在或存在于系统词库" << endl;
}
}
修改生词本中的单词
void dictionary::modifyWord(string word)
{
char YorN = 'n';
vector<string>::iterator iterNewWordBook = find(this->newWordBookWords.begin(), this->newWordBookWords.end(), word);
if (iterNewWordBook != this->newWordBookWords.end())
{
cout << "查找到的要删除的单词释义为:" << endl;
this->findWord(word);
cout << "是否确认修改? [y / n]" << endl;
cin >> YorN;
if (YorN == 'y') {
cout << "修改单词: " << endl;
cin >> *iterNewWordBook;
cout << "修改释义: " << endl;
cin >> *(iterNewWordBook + 1);
cout << "修改的结果: " << *iterNewWordBook << "\t" << *(iterNewWordBook + 1) << endl;
ofstream ofs(NEWWORDBOOK, ios::out);
for (vector<string>::iterator iter = this->newWordBookWords.begin(); iter != this->newWordBookWords.end(); iter++) {
ofs << *iter << endl;
}
}
}
}
退出系统
void dictionary::exitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}