|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
main.cpp
- #include <sstream>
- #include <fstream>
- #include <cassert>
- #include<iostream>
- #include<string.h>
- #pragma warning(disable:4996)
- using namespace std;
- #define MAXWORD 25
- #define FILEN 100 //读取文件时一行数据的长度
- struct record
- {
- char word[MAXWORD+1];
- int length;
- };
- struct lnode //链表结点结构
- {
- struct record data;
- struct lnode* next;
- };
- void InsertList(struct lnode* list, struct lnode* n);
- void FileAdd(struct lnode*list,char*filename);
- void Add(struct lnode* list[]);
- void Search(struct lnode* list[]);
- void Delete(struct lnode* list[]);
- void Display(struct lnode* list[]);
- struct lnode* SearchPrimarykey(struct lnode* list, char* key);
- void FreeList(struct lnode* list[]);
- void DisplayTableHead();
- void DisplayRecord(struct lnode* r);
- /* 显示菜单 */
- void DisplayMenu()
- {
- cout << "*******************************************" << endl;
- cout << "****************欢迎使用字典***************" << endl;
- cout << "*****************0.添加单词****************" << endl;
- cout << "*****************1.搜索单词****************" << endl;
- cout << "*****************2.删除单词****************" << endl;
- cout << "*****************3.显示单词****************" << endl;
- cout << "*****************4.退出系统****************" << endl;
- cout << "*******************************************" << endl;
- cout << endl;
- }
- /* 将记录按首字母升序插入链表 */
- void InsertList(struct lnode* list, struct lnode* n)
- {
- struct lnode* p = list;
- while (p->next != NULL && strcmp((p->next->data).word, (n->data).word) < 0)
- {
- p = p->next;
- }
- n->next = p->next;
- p->next = n;
- }
- //从文件中添加单词
- void FileAdd(struct lnode*list,char*filename)
- {
- struct record t;
- struct lnode* n, * r;
- FILE* fp=NULL;
- char str[FILEN + 1];
- char word[30];
- int h = 0;
- while (fgets(str, FILEN, fp) != NULL)
- {
- h++;
- int i = 0;
- int j = 0;
- for (i; str[i] != ' '; i++)word[i] = str[i];
- word[i] = '\0';
- strcpy(t.word, word);
- }
- /* 判断记录是否已存在,若存在则显示记录,若不存在则添加记录 */
- if ((r = SearchPrimarykey(list, t.word)) == NULL)
- {
- // 申请lnode空间并初始化
- n = new struct lnode;
- if (n != NULL)
- {
- //* 复制记录
- strcpy_s((n->data).word, str);
- //* 插入链表
- InsertList(list, n);
- }
- }
- fclose(fp);
- }
- /* 添加 */
- void Add(struct lnode* list[])
- {
- struct record t;
- struct lnode* n, * r;
- struct lnode* onelist;
- /* 录入记录 */
- cout<<"Please input the word: "<<endl;
- getchar();
- gets_s(t.word);
- onelist = *(list + t.word[0] - 97);//通过单词首字母找到单词所属于的链表, a是97
- fflush(stdin);
- /* 判断记录是否已存在,若存在则显示记录,若不存在则添加记录 */
- if ((r = SearchPrimarykey(onelist, t.word)) == NULL)
- {
- /* 申请lnode空间并初始化 */
- n = new struct lnode;
- if (n != NULL)
- {
- /* 复制记录 */
- strcpy((n->data).word, t.word);
- /* 插入链表 */
- InsertList(onelist, n);
- }
- }
- else
- {
- cout << "Record Existed!" << endl;
- void DisplayTableHead();
- DisplayRecord(r);
- }
- }
- /* 查找 */
- void Search(struct lnode* list[])
- {
- char e[MAXWORD];
- struct lnode* r;
- struct lnode* onelist;
- cout << "Please input the word you want to search: " << endl;
- getchar();
- gets_s(e);
- onelist = *(list + (e[0] - 97));
- if ((r = SearchPrimarykey(onelist, e)) != NULL) //SearchPrimarykey 会返回一个p指针或者null ,返回p 说明查找成功,返回null 说明查找失败,单词不存在
- {
- DisplayTableHead();
- DisplayRecord(r);
- }
- else
- {
- cout << "Cannot find the word." << endl;
- }
- }
- /* 删除 */
- void Delete(struct lnode* list[])
- {
- char e[MAXWORD];
- struct lnode* q, * p;
- struct lnode* onelist;
- cout << "Please input the word you want to delete: " << endl;
- getchar();
- gets_s(e);
- onelist = *(list + e[0] - 97);
- q = onelist;
- p = onelist->next;
- while (p != NULL)
- {
- if (strcmp((p->data).word, e) == 0)
- {
- q->next = p->next;
- delete p; /* 释放空间 */
- return; /* 函数返回 */
- }
- q = p;
- p = p->next;
- }
- }
- /* 显示所有记录 */
- void Display(struct lnode* list[])
- {
- int c = 0;
- struct lnode* p;
- char charr[3];//存放a-z字符
- cout << "\n--------请输入a--z中任意一个字符---------\n" << endl;
- getchar();
- gets_s(charr);
- p = *(list + charr[0] - 97);
- p = p->next;
- cout << "\n--------- ReaderMessage Display ---------\n" << endl;
- void DisplayTableHead();
- while (p != NULL)
- {
- DisplayRecord(p);
- c++; /* 记录条数 */
- p = p->next;
- }
- cout << "\n--------- Total: %d Record(s) ---------\n" << endl;
- }
- /* 按主键查找 */
- struct lnode* SearchPrimarykey(struct lnode* list, char* key)//传进来的list是当前单词首字母那个链表,不是全部的
- {
- struct lnode* p = list->next; //p指向一个节点,通过比较看一下这个节点是不是要查的单词,如果是,把p指向的节点返回。
- while (p != NULL)
- {
- if (strcmp((p->data.word), key) == 0)
- {
- return p;
- }
- p = p->next;
- }
- return NULL;
- }
- /* 释放整个链表空间 */
- void FreeList(struct lnode* list[])
- {
- struct lnode* p, * onelist;
- for (int i = 0; i < 26; i++)
- {
- onelist = *(list + i);
- p = onelist;
- while (p->next != NULL)
- {
- p = p->next;
- free(onelist);
- onelist = p;
- }
- delete p;
- }
- }
- /* 显示表头 */
- void DisplayTableHead()
- {
- cout << "WORD" << endl;
- }
- /* 显示一条记录 */
- void DisplayRecord(struct lnode* r)
- {
- cout << (r->data).word << endl;
- }
- int main()
- {
- struct lnode* dictionary[26];
- struct lnode* p;
- char filename[20];//文件名字(用来读取a.txt b.txt...里面的单词文件, *.txt 名字是需要改变的)
- /* 头结点 */
- for (int i = 0; i < 26; i++)
- {
- dictionary[i] = new struct lnode;
- if (dictionary[i] != NULL)
- {
- dictionary[i]->next = NULL; //初始化
- }
- }
- for (int i = 0; i < 26; i++)
- {
- char charr[3];
- charr[0] = (char)(i + 97);
- charr[1] = ' ';
- charr[2] = '\0';
- strcpy_s(filename, "./dir/");
- strcat_s(filename, charr);
- strcat_s(filename, ".txt\0");
- p = *(dictionary + i);
- FileAdd(p,filename);
- }
- int choice;
- while (1)
- {
- DisplayMenu();
- cout << "请输入您的选择:" << endl;
- cin >> choice;
- switch (choice)
- {
- case 0:
- Add(dictionary);
- break;
- case 1:
- Search(dictionary);
- break;
- case 2:
- Delete(dictionary);
- break;
- case 3:
- Display(dictionary);
- break;
- case 4:
- exit(0);
- break;
- default:
- cout << "Input Error! Please input the right word." << endl;
- break;
- }
- }
- FreeList(dictionary);
- }
复制代码
|
|