数据结构 C++实现 基于不同策略的英文单词的词频统计和检索系统( 四 )

<< "单词总数为:" << sum << endl;fout << "词频" << "\t" << "单词" << endl;for (int i = 0; i< sum; i++){fout << wf[i].frequency << "\t" << wf[i].word << endl; } //forfout.close(); //关闭文件} //ifif (n == 2){ofstream fout; //文件写操作 内存写入存储设备 fout.open("outfile3.txt");fout << "单词总数为:" << sum << endl;fout << "词频" << "\t" << "单词" << endl;for (int i = 0; i < sum; i++) {fout << wf[i].frequency << "\t" << wf[i].word << endl;} //forfout.close(); //关闭文件} //ifcout << "单词总数为:" << sum << endl;cout << "词频" << "\t" << "单词" << endl;for (int i = 0; i < sum; i++)cout << wf[i].frequency << "\t" << wf[i].word << endl;if (n == 1)cout << "单词以及词频已经保存到文件outfile1.txt文件中" << endl;else if (n == 2)cout << "单词以及词频已经保存到文件outfile3.txt文件中" << endl;system("pause"); //暂停}
4、链表类
//链表类class LinkList{public:LinkList(datatype a[], int n) { //有参构造函数,建立有n个元素的单链表Head = new Node; //生成头结点Node* r = Head, * s = NULL; //尾指针初始化,并定义存储指针for (int i = 0; i < n; i++){s = new Node; s->data = http://www.kingceram.com/post/a[i]; //数据域赋值r->next = s; //将存储节点s插入链表r = s; //更新尾指针} //forr->next = NULL; //单链表建立完毕,将终端结点的指针域置空}~LinkList() { //析构函数Node* temp = NULL; //定义临时节点while (Head != NULL){ //释放单链表的每一个结点的存储空间temp = Head; //暂存被释放结点Head = Head->next; // Head指向被释放结点的下一个结点delete temp;} //while}int Empety(); //判断链表是否为空int Locate(string word); //按值查找,返回下标void PrintList(); //遍历操作,按序号依次输出各元素datatype getdata(int n);private:Node* Head;//单链表的头指针};//返回数据域datatype LinkList::getdata(int n) {Node* t = Head->next; //指针初始化for (int i = 1; i < n; i++)t = t->next;return t->data;}//判空int LinkList::Empety(){if (Head->next)return 1; //链表非空,正常返回return 0; //链表为空,返回-1}//输出单链表void LinkList::PrintList(){system("cls"); //清屏Node* p = Head->next;//工作指针p初始化ofstream fout; //文件写操作 内存写入存储设备 fout.open("outfile2.txt"); //打开文件fout << "单词总数为:" << sum << endl;fout << "词频" << "\t" << "单词" << endl;while (p != NULL){fout << p->data.frequency << "\t" << p->data.word << endl;p = p->next; //指针p后移} //whilefout.close(); //关闭文件cout << "单词总数为:" << sum << endl;cout << "词频" << "\t" << "单词" << endl;Node* p1 = Head->next;//工作指针p初始化while (p1){ //p <--> p != NULLcout << p1->data.frequency << "\t" << p1->data.word << endl;p1 = p1->next;//工作指针p后移,注意不能写作p++} //whilecout << "单词以及词频已经保存到文件outfile2.txt文件中" << endl;system("pause"); //暂停}//按值查找,返回下标int LinkList::Locate(string word){Node* p = Head->next;//指针p初始化int count = 1; //计数器count初始化,表示查找次数while (p != NULL){if (p->data.word == word) return count;//查找成功,结束函数并返回下标p = p->next; //p指针后移count++;} //whilereturn -1; //退出循环表明查找失败}

数据结构 C++实现 基于不同策略的英文单词的词频统计和检索系统

文章插图
5、线性表菜单
// 线性表菜单void ListMenu(){while(true) {system("cls"); //清屏cout << "*******************基于不同策略的英文单词的词频统计和检索系统*******************" << endl;cout << "---基于线性表的查找---" << endl;cout << "1.顺序查找" << endl;cout << "2.折半查找" << endl;cout << "3.返回上一级" << endl;cout << "请按相应的数字键进行选择:" << endl;int n;cin >> n;switch (n){case 1 : SequenceMenu(); //顺序查找菜单break;case 2 : HalfSortMenu(); //顺序表折半查找菜单break;case 3 : return; //结束函数default: cout