/* 功能:单词查询、存储、删除 文件名:eng_sre.c 作者:flonlen 版本:1.1
*/ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define FILE_NAME "datamass.data" /********* 定义链表结点的具体内容 ***********/ typedef struct list_node { char data1[30]; char data2[60]; struct list_node *prov; struct list_node *next; }_eng_sea,*eng_sea; /********* 函数功能:初始化头结点 ***********/ void init_head(eng_sea list) { list->next = list; list->prov = list; } /********* 函数功能:插入结点 ***********/ void list_add(eng_sea node1,eng_sea prov,eng_sea next) { node1->next = next; node1->prov = prov; prov->next = node1; next->prov = node1; } /********* 函数功能:删除结点 ************/ void list_del(eng_sea node) { node->prov->next = node->next; node->next->prov = node->prov; free(node); } /******** 函数功能:将结点插入正确位置 **********/ void ins_node(eng_sea head,eng_sea node1) { eng_sea temp; temp = head->next; while(strcmp(node1->data1,temp->data1) > 0) temp = temp->next; list_add(node1,temp->prov,temp); } /****** 函数功能:将文件内容导入链表 *******/ void read_to_list(eng_sea head) { FILE *fp; int i ,j; eng_sea node1; char node_mass[90],node1_mass[90],data1[30],data2[60]; fp = fopen(FILE_NAME,"r"); if(fp == NULL) { } else { while(!feof(fp)) { fgets(node1_mass,90,fp); if(strcmp(node_mass,node1_mass) != 0) { strcpy(node_mass,node1_mass); i = 30; while(node_mass[i] == ' ') i--; for(j = 0;j <= i;j++) data1[j] = node_mass[j]; data1[j] = '\0'; for(j = 0,i = strlen(node_mass); (j + 30) <= i;j++) data2[j] = node_mass[31 + j]; data2[j-3] = '\0'; node1 = (eng_sea)malloc(sizeof(_eng_sea)); i = strlen(data1); strcpy(node1->data1,data1); strcpy(node1->data2,data2); if(node1->data1) ins_node(head,node1); } } fclose(fp); } } /********* 函数功能:重新输出到文件 ***********/ void print_file2(eng_sea head) { FILE *fp; eng_sea temp; unsigned int i; temp = head->next; fp = fopen(FILE_NAME,"w+"); while(temp != head) { fwrite(temp->data1,strlen(temp->data1),1,fp); for(i = 0;i <= (30 - strlen(temp->data1));i++) fwrite(" ",1,1,fp); fwrite(temp->data2,strlen(temp->data2),1,fp); fwrite("\n",1,1,fp); temp = temp->next; } fclose(fp); } /********* 函数功能:显示10个单词 ********/ void dis_10_word(eng_sea head,eng_sea temp) { int i; for(i = 0;i < 10;i++) { printf("%s %s\n",temp->data1,temp->data2); temp = temp->next; if(temp == head) temp = head->next; } } /********* 函数功能:单词查询 ***********/ void sea_eng(eng_sea head) { eng_sea temp; int i = 0; char c = '\0'; char word[30] = "\0"; SEARCH: temp = head->next; while(c != '\r') { system("cls"); printf("输入(方向键选择):%s\n",word); if(i != 0) dis_10_word(head,temp); c = getch(); if(c <= 'z' && c >= 'a' || c == ' ') { word[i] = c; while(word[i] > temp->data1[i]) temp = temp->next; } i++; if(c == '\r') word[i] = '\0'; switch(c) { case 72: case 75: temp = temp->prov; if(temp == head) temp = head->prov; strcpy(word,temp->data1); i = strlen(word); break; case 77: case 80: temp = temp->next; if(temp == head) temp = head->next; strcpy(word,temp->data1); i = strlen(word); break; } } system("cls"); if(strcmp(temp->data1,word) == 0) printf("%s %s\n",temp->data1,temp->data2); else printf("没有找到“%s”.",word); printf("从新查找输入1,输入0返回:"); c = getch(); printf("\77"); while(c != '0') { if(c == '1') { c = '\0'; for(i = 0;i < 30;i++) word[i] = '\0'; i = 0; goto SEARCH; } c = getch(); } } /********* 函数功能:插入新单词 ***********/ void inst_new_word(eng_sea head) { eng_sea node; int c; SEARCH: system("cls"); node = (eng_sea)malloc(sizeof(_eng_sea)); printf("请输入英语单词:"); scanf("%s",node->data1); printf("请输入中文意思:"); scanf("%s",node->data2); ins_node(head,node); printf("继续插入单词请输入1,返回输入0:"); while(c != '0') { c = getch(); if(c == '1') goto SEARCH; } } /********* 函数功能:删除单词 ***********/ void dec_word(eng_sea head) { eng_sea temp; int i = 0; char c = '\0'; char word[30] = "\0"; temp = head->next; while(c != '\r') { system("cls"); printf("输入要删除的单词(方向键选择,#:重新输入,*:退出):%s\n",word); if(i != 0) dis_10_word(head,temp); c = getch(); if(c <= 'z' && c >= 'a' || c == ' ') { word[i] = c; while(word[i] > temp->data1[i]) temp = temp->next; } i++; if(c == '\r') word[i] = '\0'; switch(c) { case 72: case 75: temp = temp->prov; if(temp == head) temp = head->prov; strcpy(word,temp->data1); i = strlen(word); break; case 77: case 80: temp = temp->next; if(temp == head) temp = head->next; strcpy(word,temp->data1); i = strlen(word); break; } if(c == '*') break; if(c == '#') { temp = head->next; for(i = 0;i < 30;i++) word[i] = '\0'; i = 0; } } if(strcmp(temp->data1,word) == 0) { if(c != '*') list_del(temp); system("cls"); } else { system("cls"); printf("没找到 %s\n",word); } printf("输入0返回:"); c = getch(); while(c != '0') c = getch(); } void act_sear(eng_sea head) { char act; ACTION: if(head->next == head) { printf("没找到:datamass.data,缺少单词库。\n\n"); printf("**************************************\n"); printf("******** 输入2:存储单词 **********\n"); printf("******** 输入4:退出 **********\n"); printf("**************************************\n"); printf("请输入:"); act = getch(); switch(act) { case '2': inst_new_word(head); goto ACTION; case '4': break; default : printf("输入错误,请重新输入。\n"); goto ACTION; } } else { system("cls"); printf("**************************************\n"); printf("******** 输入1:查询单词 **********\n"); printf("******** 输入2:存储单词 **********\n"); printf("******** 输入3:删除单词 **********\n"); printf("******** 输入4:退出 **********\n"); printf("**************************************\n"); printf("请输入:"); act = getch(); switch(act) { case '1': sea_eng(head); goto ACTION; case '2': inst_new_word(head); goto ACTION; case '3': dec_word(head); goto ACTION; case '4': break; default : printf("输入错误,请重新输入。\n"); goto ACTION; } } } /****** 主函数 *******/ int main(int argc,char *argv[]) { eng_sea head; head = (eng_sea)malloc(sizeof(_eng_sea)); init_head(head); read_to_list(head); act_sear(head); print_file2(head); }
注:datamass.data为文本文件,每一行的前30个字节存储单词,31个字节后存储单词的中文意思。
datamass.data 示例
air conditioning 空调