本来是1月20号以后提交的,所以我想把它做成图形界面,可是老师突然把截止日期提前到了1月初……为了赶工,只好写成Console的了,毕竟还有好多科目要复习><
所有的源代码加报告是一晚上搞定的,凑活着看吧。。。源代码和报告分在两篇不同的博客发吧,否则比较混乱。
字典库是一个事先处理过的文本文件(原始文本是从网上下载的),都是“英文 中文释义”的格式,9000多条,我就不贴出来了。
下面是源代码:
Main.cpp
/*
*
*Author: lwfcgz
*Student ID: it is secret, haha~~
*Date: 2012-12-27
*Environment: Microsoft Visual Studio 2008
*
*/
#include<iostream>
#include<string>
#include<vector>
#include "DataBase.h"
#include "WordNode.h"
#include "MyFrame.h"
using namespace std;
//主函数
int main(){
//生成一个界面对象
MyFrame frame;
//初始化界面并将控制转移到该对象内部
frame.initialize();
return 0;
}
WordNode.h
#ifndef WORDNODE_H
#define WORDNODE_H
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class WordNode{
public:
//默认空构造函数
WordNode();
//给出英文单词和中文解释的构造函数
WordNode(string english, string chinese);
//获取英文内容
string getEnglish();
//获取中文内容
string getChinese();
//重新设置英文内容
void setEnglish(string english);
//重新设置中文内容
void setChinese(string chinese);
private:
//英文单词
string english;
//对应的中文语义
string chinese;
};
#endif
WordNode.cpp
#include<iostream>
#include<string>
#include<vector>
#include "WordNode.h"
using namespace std;
//默认空构造函数
WordNode::WordNode(){
}
//给出英文单词和中文解释的构造函数
WordNode::WordNode(string english, string chinese){
this->english=english;
this->chinese=chinese;
}
//获取英文内容
string WordNode::getEnglish(){
return this->english;
}
//获取中文内容
string WordNode::getChinese(){
return this->chinese;
}
//重新设置英文内容
void WordNode::setEnglish(string english){
this->english=english;
}
//重新设置中文内容
void WordNode::setChinese(string chinese){
this->chinese=chinese;
}
DataBase.h
#ifndef DATABASE_H
#define DATABASE_H
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include "WordNode.h"
using namespace std;
//存放与单词相关的数据
class DataBase{
public:
//默认构造函数
DataBase();
//返回单词的总数目
int getDicSize();
//判断字典是否为空
bool isEmpty();
//返回下标为i的单词,i不合法则返回空
WordNode getWordNode(int i);
//修改第i个WordNode节点,失败则返回值为负数
int setWordNode(int i,WordNode newWordNode);
//添加一个新单词
int addWordNode(WordNode wordNode);
//删除第i个WordNode节点,失败则返回值为负数
int deleteWordNode(int i);
//根据单词二分查找相应的条目,查找失败返回负数
int binarySearch(string word);
//重新对单词按字典序进行排序
void sortWords();
private:
//存放单词,向量内的元素为WordNode类对象
vector<WordNode> words;
WordNode wordNode;
};
#endif
DataBase.cpp
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<fstream>
#include "DataBase.h"
#include "WordNode.h"
#include "myless.cpp"
using namespace std;
//默认构造函数
DataBase::DataBase(){
string english;
string chinese;
//打开词典文件
ifstream file("dic.txt");
while(file>>english>>chinese){
this->wordNode.setChinese(chinese);
this->wordNode.setEnglish(english);
//将相应节点插入数据库中