一个简单的英文排版系统 实现
作为此学期的一个作业:
一种简单的英文单词排版系统实现 一、功能 英文单词排版及查询系统基本功能(大小写敏感):录入单词和释义、修改单词、修改释义、文件录入、输出到屏幕和输出到文件、输入单词查询已有释义。 二、实现约定 1、单词结构 typedef struct dictWord { char word[101]; char meaning[201]; } DictWord; 2、录入单词和释义 录入规则: A) 一行只能录入一个单词或词组和释义, B) 可以只录入单词 C) 每一次录入得单词总长度包含空格不超过100字符 D) 不能只录入释义 E) 单词和释义之间必须有空格 F) 单词是连续的或有空格的字母,不能有其他字符 G) 录入异常打印对应的错误信息。 H) 录入时单词已经存在且释义不同,则视为修改释义,增加修改计数 I) 释义应只包含汉字、阿拉伯数字和汉字标点符号且长度不超过100字 3、修改单词 修改规则: A) 修改的关键字是”<<”,修改示例:worr << word B) << 必须在两个单词的中间,且和前后单词有空格隔开 C) 修改错误打印错误信息 D) 单词应仅包含空格和字母,不能有其他字符 4、修改释义 修改释义同2 5、文件录入 文件录入规则: A) 录入时输入相对文件名或者绝对文件名 B) 文件名只能是英文和‘/’,其他字符视为输入错误。 C) 文件名输入错误或者未找到文件名打印对应的错误信息 D) 文件内单词规则(一行录入一个单词和单词的释义):单词 释义\n E) 录入时单词已经存在且释义不同,则视为修改释义,增加修改计数 F) 录入时单词和释义都相同,视为录入成功,增加单词相同的计数 G) 录入时单词不存在,则录入,增加录入成功计数 H) 录入完成时打印总单词数目、录入成功目、单词相同的数据和修改数目 6、屏幕输出 输出规则: A) 按照字母序排序输出 7、文件输出 文件输出规则: A) 按照字母序排序输出 B) 输入需要保存的文件名,文件名是相对文件名或者绝对文件名(只能是英文字母和’/’) 8、使用命令提示符 A) 使用两个连续的右尖括号作为命令提示符 >> 来表明后面是一条命令而不是一个单词 例:>>menu 将回到菜单; C) 每一次输入只能录入 查询 或执行一条命令 所以, 下面这个例子, >>menu word 将转到菜单并忽略后面的word >>menuword 将打印如下错误信息“无法识别的指令” D) 使用>>help 打印帮助信息 E) 命令不区分大小写 F) 您可以使用的命令有; i. H 打印帮助 ii. M 显示菜单 iii. FF 从文件录入 iv. << 修改单词,用法 A << B 将A 修改为B ,释义保持不变 该命令可单独使用,不需要提示符 >> v. View [-t] 该命令用来以t秒间隔连续打印已有数据的单词及其对应释义。 T默认为0. 按任意键退出该模式。 vi. BB 进入连续录入模式 改模式下,你可以连续从屏幕输入以录入单词。录入规则如下: 1. 单词在前,注释在后,单词与注释间用空格分隔,每一个单词或词组总长度不超过100字,空格占用一字符。 2. 单词应只包含空格及字母,包含其他字符将被认定为释义部分 3. 注释只能以汉字或阿拉伯数字开头,总长度限制在200字,标点符号和数字占用一个字。 4. 每输入完一次回车以进行下一次录入 5. 一次只能输入一个单词或词组及其对应释义 6. 录入模式下可使用提示符>> 进行操作 二、功能实现 A) 主函数调用menu显示菜单,初始化参数,然后调用输入函数,传入保存单词和释义的数组参数,并等待用户输入 B) 输入函数 1、输入函数从屏幕读取并分析是否包含命令提示符>> ,然后判断当前所属模式, I、默认模式将输入单词存入单词数组,遇到非法字符或换行符结束。完成返回0 Ii、录入模式将输入单词,和释义存入相应数组,释义为空则忽略,完成返回0 Iii、如果开头包含命令提示符,包含命令提示符,则录入后面的命令及其参数,并返回主函数对应命令的相应值。 Iiii、输入函数会自动忽略开头空格,并删除输入字符之间多余空格,输入 C) 排序函数 D) 写入文件函数 E) 顺序输出函数 F) 查询函数
简单字典使用手册 一、 功能 1、英文单词排版及查询系统基本功能:录入单词和释义、修改单词/释义、文件录入、输出到屏幕和输出到文件、查询释义。 二、实现约定 1、通用约定 按照终端提示输入,即可正常使用该软件 单词是由连续的字母组成,且大小写敏感,长度不大于50 文件名可以是相对文件名和绝对文件名,文件名只能有字母和’/’组成 回车代表输入完成,把输入数据交给程序执行 特定模式下输入字符’q’,退出当前模式 命令模式提示符”$”,二级模式提示符:”>>” 输入不符合约定会打印错误信息 2、关键字约定 1、search:进入单词搜索模式 2、enter:录入单词和释义 3、update:修改单词或者释义 4、file:从文件录入 5、output:输出到屏幕 6、outfile:输出到文件 7、help:输出命令关键字和含义 3、命令概述 搜索模式 A) 命令模式输入命令”search”进入查询模式 B) 输入单词 回车 C) 打印单词释义 录入单词和释义: A) 命令模式输入命令”enter”进入单词和释义的录入 B) 一行只能录入一个单词和释义,可以只录入单词,不能只录入释义、单词和释义之间必须至少有一个空格 修改单词和释义: A) 命令模式输入命令”update”,进入修改模式 B) 输入要更新的单词 C) 输入修改后的单词和释义(同录入) 文件录入: A) 文件录入模式输入命令” file”,进入文件录入模式 B) 输入文件名 C) 文件内单词规则:同录入 D) 单词已经存在且释义不同,则视为修改释义,增加修改计数 E) 录入时单词和释义都相同,视为录入成功,增加单词相同计数 F) 录入时单词不存在,则录入,增加成功计数 G) 录入时单词不符合规则,增加错误记录 H) 录入完成时打印总单词数目、录入成功计数、修改计数、单词相同计数和错误计数。 屏幕输出 A) 命令模式输入命令”output”,进入屏幕输出模式 B) 输出打印规则,输入数字表示打印多少个单词和释义 文件输出 A) 命令模式输入命令” outfile”,进入屏幕输出模式 B) 输入文件名
#include "main.h" /***** 全局变量 ***************/ char beauty[600]; char DictFile[200]; /**** 全局变量END ************/ struct wtree root; extern int getAndCheckWord(char *pWord,char *exp);//0是返回命令 -1错误 1返回单词 2替换单词 -2未知错误*/ void clean_Print( const char *str ); /**** 清空缓存函数*/ void lprint(); void temprint(); void strprint(char str[],int t); void miniDict(); void Menu(); int checkCmd( int opt ); int getOption( char str[] ) ; int cmdHelp(); int cmdCls(); int cmdMenu() ; int fun_search(); int searchAndReplaceWord(char w[],char r[],struct wtree *pt); int fun_enter(); int fun_ffile(); int getOption_ffile( char str[]); int get_from_file(FILE *fp, char *pWord,char *exp );/**** 输入函数 -1错误 1返回单词 -2未知错误*/ int fun_ofile(); int fun_outprint(); int fun_help(); int word_tree_traveal(struct wtree* pt,char *str,FILE *fp); bool word_tree_delete(char *str,struct wtree *pt); bool word_tree_child_enum(struct wtree* pt); struct wtree* word_tree_search(char *str,struct wtree *pt); struct wtree* word_tree_insert(char *str,char *exp,struct wtree *pt); struct wtree* creatchild(); int alpcompare(const char a,const char b); int ptlen(struct wtree* pt[]); int exchange(char str[]); char DictFile[200]; int main(int argc, char *argv[]) { #ifdef _WIN32 SetConsoleTitle("MiniDict"); system("mode con cols=80 lines=50"); system("color B0"); #endif //__WIN32 temprint(); strcpy(DictFile,"minidict.mi"); // printf("E%sE",DictFile); strprint(beauty, 2); if (access(DictFile,F_OK) == 0) { strcpy(cmdOption,DictFile); load_file(); cmdOption[0] = '\0'; } system( "cls" ); miniDict(); Menu(); fun_tip = help; while(1) { switch (fun_tip) { case help: fun_help(); fun_tip = search; break; case search: fun_search(); break; case enter: fun_enter(); break; case ffile: fun_ffile(); break; case ofile: fun_ofile(); break; // case info: // fun_info(); // break; case outprint: fun_outprint(); break; } } return(0); } int load_file() { samewords = 0; int coun = 0; printf("loading from file...please wait ...\n"); FILE *read; char word[WORD_NUMBER] = {0}; char wordExp[EXP_NUMBER] = {0}; int flag; read = fopen(cmdOption,"r"); if (read == NULL) { printf("open file error!\n"); fun_tip = search; return 0; } else { while (1) { flag = get_from_file(read,word,wordExp); if (flag == -1) { //printf("This word will be ignore\n"); continue; } else if (flag == 1) { char tempword[WORD_NUMBER]; printf("%s",tempword); strcpy(tempword,word); exchange(tempword); word_tree_insert(tempword,wordExp,&root); coun++; printf("%s\n",word); continue; } else if (flag == 0) { if( fclose(read) != 0) { perror("fclosed"); } return 0; } } } }
/************************************************************************* > File Name: main.h > Author: > Mail: > Created Time: Thu 30 Jan 2020 06:50:54 AM HKT ************************************************************************/ #ifndef _AJA_H #define _AJA_H #define DEF_SLEEP_TIMES 5 #define ERRORMESSAGE_LENTH 30 #ifndef TRUE #define FALSE 0 #define TRUE 1 #endif // TRUE #if _WIN32 #include <dos.h> #include <windows.h> #else #include <unistd.h> #endif // _linux_ #include <stdbool.h> #include "common.h" #include "wtree.h" #include "func.h" extern int load_file(); #endif
#include "wtree.h" static struct wtree defstr; int samewords; char TempFile[200] = "minidicttemp"; static struct wtree *creatchild(void) { struct wtree *p = (struct wtree *)malloc(sizeof(struct wtree)); if (p == NULL) { printf("Error,out of memory!\n"); return NULL; } *p = defstr; return p; } int word_tree_traveal_delete(struct wtree* pt) { int i = 0; while (1) { if (pt->child[i] == NULL) { return 0; } else if (pt->child[i] != NULL) { word_tree_traveal_delete(pt->child[i]); free(pt->child[i]); pt->child[i] = NULL; i++; continue; } } } int word_tree_traveal(struct wtree* pt,char *str,FILE *fp) { int i = 0; int flag = 0; if (fp == NULL) { FILE *fp1 = fopen(TempFile,"w"); if (fp1 == NULL) { printf("Failed, insufficient permissions\n"); return -1; } flag = 1; fp = fp1; } if (str == NULL) { char str1[WORD_NUMBER]; if (str1 == NULL) { printf("Failed\n"); return -1; } str = str1; str[0] = '\0'; flag = 1; } int j = strlen(str); int len = ptlen(pt->child); //printf("pt %d\n%d",len,strlen(pt->child)); while (i < len) { // printf("%s\ni = %d\nj = %d\n",str,i,j); if (pt->child[i] == NULL) { break; } else if (pt->child[i] != NULL) { str[j] = (pt->child[i])->c; str[j + 1] = '\0'; if ((pt->child[i])->self == TRUE) { fputs(str,fp); if (strlen((pt->child[i])->exp) == 0 ) { fputs("\n",fp); } else { fputs(" ",fp); fputs((pt->child[i])->exp,fp); fputs("\n",fp); } } if(word_tree_traveal(pt->child[i],str,fp) == -1) { return -1; } else { i++; continue; } } } if (flag == 1 && i == (len - 1)) fclose(fp); return 0; } bool word_tree_child_enum(struct wtree* pt) { int i; int len = ptlen(pt->child); if (len == 0) { return FALSE; } else { for (i = 0; i < len; i++) { if (pt->child[i] != NULL) { return TRUE; } } return FALSE; } } bool word_tree_delete(char *str,struct wtree *pt) { int len = strlen(str); int i = 0; while (1) { if (pt->child[i] == NULL) { return FALSE; } else if(alpcompare((pt->child[i])->c,str[len - 1]) < 0) /**** a < b **/ { i++; continue; } else if (alpcompare((pt->child[i])->c,str[len - 1]) == 0) { str[len - 1] = '\0'; if (len == 1) { if ((pt->child[i])->self == TRUE) { (pt->child[i])->self = NULL; (pt->child[i])->exp[0] = '\0'; } break; } else { word_tree_delete(str,pt->child[i]); break; } } else if (alpcompare((pt->child[i])->c,str[len - 1]) > 0) { return FALSE; } } if (!word_tree_child_enum(pt->child[i])) /****无用节点删除 ***/ { int templen1 = ptlen(pt->child); for (free(pt->child[i]); i < templen1 - 2; i++) { pt->child[i] = pt->child[i + 1]; } pt->child[templen1 - 1] = NULL; } return TRUE; } struct wtree* word_tree_search(char *str,struct wtree *pt) { int len = strlen(str); int i = 0; while (1) { if (pt->child[i] == NULL) { return NULL; } else if (alpcompare((pt->child[i])->c,str[len - 1]) < 0) /**** a < b **/ { i++; continue; } else if (alpcompare((pt->child[i])->c,str[len - 1]) == 0) { str[len - 1] = '\0'; if (len == 1) { return (pt->child[i]); } else { return word_tree_search(str,pt->child[i]); } } else if (alpcompare((pt->child[i])->c,str[len - 1]) > 0) { return NULL; } } } struct wtree* word_tree_insert(char *str,char *exp,struct wtree *pt) { int len = strlen(str); if (len == 0) { printf("没有什么需要插入的!\n"); return NULL; } int i = 0; while (1) { if (pt->child[i] == NULL) { pt->child[i] = creatchild(); *(pt->child[i]) = defstr; (pt->child[i])->c = str[len - 1]; str[len - 1] = '\0'; if (len == 1) { strcpy((pt->child[i])->exp,exp); (pt->child[i])->self = TRUE; //printf("return %p",pt->child[i]); return (pt->child[i]); } else { return word_tree_insert(str,exp,pt->child[i]); } } else if (alpcompare((pt->child[i])->c,str[len - 1]) < 0) /**** a < b **/ { i++; continue; } else if (alpcompare((pt->child[i])->c,str[len - 1]) == 0) { str[len - 1] = '\0'; if (len == 1) { samewords++; strcpy((pt->child[i])->exp,exp); (pt->child[i])->self = TRUE; return (pt->child[i]); } else { return word_tree_insert(str,exp,pt->child[i]); } } else if (alpcompare((pt->child[i])->c,str[len - 1]) > 0) { int tempint; int templen = ptlen(pt->child); for (tempint = templen - 1; tempint > i; tempint--) { pt->child[tempint] = pt->child[tempint - 1]; } pt->child[i] = creatchild(); (pt->child[i])->c = str[len - 1]; str[len - 1] = '\0'; if (len == 1) { strcpy((pt->child[i])->exp,exp); (pt->child[i])->self = TRUE; return (pt->child[i]); } else { return word_tree_insert(str,exp,pt->child[i]); } } } } int alpcompare(const char a,const char b) { if (toupper(a) == toupper(b)) { if (a == b) { return 0; } else if (a > b) /***** A isLOWER ********/ { return 1; } else if (a < b) { return -1; } else if (a == b) { return 0; } } else if (toupper(a) != toupper(b)) { return (toupper(a) - toupper(b)); } return 0; } int ptlen(struct wtree* pt[]) { int i = 0; while (pt[i] != NULL) i++; return i + 1; } int exchange(char str[]) { int left = 0; int right = strlen(str) - 1; char temp; while(1) { if ( left <= right) { temp = str[left]; str[left++] = str[right] ; str[right--] = temp; } else { break; } } return 0; }
/************************************************************************* > File Name: wtree.h > Author: > Mail: > Created Time: Thu 30 Jan 2020 07:25:31 AM HKT ************************************************************************/ #ifndef _WTREE_H #define _WTREE_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #ifndef FALSE #define FALSE 0 #define TRUE 1 #endif // FALSE #define WORD_NUMBER 101 struct wtree { char c; char exp[201]; bool self; struct wtree *child[46]; }; extern int alpcompare(const char, const char); extern int word_tree_traveal_delete(struct wtree *); extern struct wtree *word_tree_search(char *, struct wtree *); extern int exchange(char str[]); extern int ptlen(struct wtree* pt[]); extern int alpcompare(const char a,const char b); extern struct wtree* word_tree_insert(char *str,char *exp,struct wtree *pt); extern bool word_tree_delete(char *str,struct wtree *pt); extern bool word_tree_child_enum(struct wtree* pt); extern int word_tree_traveal(struct wtree* pt,char *str,FILE *fp); extern int word_tree_traveal_delete(struct wtree* pt); extern int samewords; extern char TempFile[]; #endif
#include "func.h" #define COMANDOPT_LENTH 200 #define REPLACE_WORD_LENTH 100 #define fromFileEnterAndCheck(c) do{fread(&c,1,1,fp); }while ( ( c == ' ') && c != '\n' ) extern int getOption(char str[]); static struct wtree root; static char cmdInput[CMD_INPUT_LENTH]; static int outPutSleep; static char replaceWord[REPLACE_WORD_LENTH]; char cmdOption[COMANDOPT_LENTH]; int fun_search() { char words[WORD_NUMBER] = {0}; char wordExp[EXP_NUMBER] = {0}; /* 声明单词词组和释义词组 */ int opt = 1; while ( 1 ) { // printf("%s %s %s\n",words,wordExp,replaceWord); lprint(); opt = getAndCheckWord( words,wordExp ); /*****0是返回命令 -1错误 1返回单词 2替换单词 -2未知错误*/ /* 显示菜单*/ if ( opt == 0 ) /* 识别对应命令并调用对应函数 */ { return checkCmd(opt); } else if ( opt == 1 ) /*搜索查询单词*/ { char tempchar[WORD_NUMBER]; strcpy(tempchar,words); exchange(tempchar); struct wtree *res = word_tree_search(tempchar,&root); if (res != NULL && res->self == TRUE) { printf(" \t%s : ",words); printf("%s\n",res->exp); } else { printf(" \tNot Found!\n"); } } else if ( opt == 2 ) /*替换单词*/ { strcpy(root.exp,wordExp); if (searchAndReplaceWord( words, replaceWord,&root) != -1) printf( " \tUpdate Sucessfully! \n" ); } else if ( opt == 3 ) { return 0; } else if ( opt == -1 || opt == -2 ) { ; } } } int searchAndReplaceWord(char w[],char r[],struct wtree *pt) { exchange(w); exchange(r); char temp1[WORD_NUMBER]; strcpy(temp1,w); struct wtree *temp = word_tree_search(temp1,&root); if (pt->exp[0] == '\0' && temp != NULL) { strcpy(pt->exp,temp->exp); } if (temp == NULL) { if (!word_tree_insert(r,&(pt->exp),pt)) return -1; } else { word_tree_delete(w,pt); word_tree_insert(r,pt->exp,pt); } pt->exp[0] = '\0'; return 0; } int fun_enter() { char words[WORD_NUMBER] = {0}; char wordExp[EXP_NUMBER] = {0}; /**** 声明单词词组和释义词组 */ int opt = 1; while ( 1 ) { lprint(); //printf("%s %s %s\n",words,wordExp,replaceWord); opt = getAndCheckWord( words,wordExp ); /* 显示菜单*/ if ( opt == 0 ) /*** 识别对应命令并调用对应函数 */ { return checkCmd(opt); } else if ( opt == 1 ) /***** 写到树里面 ***/ { exchange(words); word_tree_insert(words,wordExp,&root); printf(" \tSuccess!\n"); continue; } else if ( opt == 2 ) /***替换单词*/ { strcpy(root.exp,wordExp); searchAndReplaceWord( words, replaceWord,&root); printf( "\tUpdate Sucessfully! \n" ); } else if ( opt == -1 || opt == -2 ) { printf( " \tInput error!\n" ); } } } int fun_ffile() { samewords = 0; int coun = 0; printf(" \tloading from file...please wait and do not exit...\n"); FILE *read; char word[WORD_NUMBER] = {0}; char wordExp[EXP_NUMBER] = {0}; int flag; read = fopen(cmdOption,"r"); if (read == NULL) { printf(" \topen file error!\n"); fun_tip = search; return 0; } else { while (1) { flag = get_from_file(read,word,wordExp); if (flag == -1) { printf("This word will be ignore\n"); continue; } else if (flag == 1) { char tempword[WORD_NUMBER]; strcpy(tempword,word); exchange(tempword); word_tree_insert(tempword,wordExp,&root); coun++; printf("%s\n",word); continue; } else if (flag == 0) { if( fclose(read) != 0) { perror("fclosed"); } printf("加载完毕!\n共输入%d个单词,其中%d为重复输入。你想将数据排序输出到文件吗?[Y/n]:",coun,samewords); fflush(stdin); char c = getchar(); while (c == '\n' || c == 'Y' || c =='y') { cPrint("\n请输入文件路径:"); int flagfile = getOption_ffile(cmdOption); if (flagfile == -1 || isrightfile()) { if (strlen(cmdOption) == 0 && flagfile != -1) { printf(" \t没有输入任何路径!\n"); } //else // { // printf(" \t错误的路径!\n"); // } break; } fun_tip = ofile; fun_ofile(); return 0; } fflush(stdin); fun_tip = search; return 0; } } } } int getOption_ffile( char str[] ) { int c; int i = 0; EnterAndCheck(c); while ( 1 ) { if ( c == '\n' || c == EOF || c == ' ' ) { str[i] = '\0'; return(0); } else if ( isalpha( c ) || c == ':' || c == '\\' || c == '/' || c == '.' || c == '_' || isdigit( c ) ) { str[i++] = c; c = getchar(); continue; } else { str[0] = '\0'; cPrint( " \tIllegal file names\n" ); return(-1); } } return 0; } int get_from_file(FILE *fp, char *pWord,char *exp ) { char c; int i = 0; //int j = 0;//|warning: unused variable 'j' [-Wunused-variable]| pWord[0] = '\0'; exp[0] = '\0'; replaceWord[0] = '\0'; if (feof(fp)) //虽然现在这个语句看上去没用,可能是有一个换行符,但是如果就真的一个字节都没有呢 { return 0; } fromFileEnterAndCheck(c); /* */ if (feof(fp)) { return 0; } if ( c == '\n') { //printf( "Invalid input\n" ); return(-1); /**** 未检测到有效内容 */ } while ( 1 ) { if (feof(fp)) { return 0; } if ( c == '\n' || c == EOF ) { /* 结束 */ pWord[i] = '\0'; while ( pWord[i - 1] == ' ' ) { pWord[i - 1] = '\0'; i--; } i = 0; if (c == EOF) { return 0; } return(1); } else if ( i == WORD_NUMBER ) { printf( " \tword too long\n" ); fread(&c,1,1,fp); while (c != '\n' && c != EOF); pWord[0] = '\0'; return(-1); } else if ( c == ' ' ) /**** 空格 */ { /* 单词遇到多空格只保留一个 */ pWord[i++] = c; fread(&c,1,1,fp); while (c == ' ' ) /** ***空格后还是字母则认为是词组,继续 */ fread(&c,1,1,fp); continue; } else if ( isalpha( c ) || c == '.' ) { pWord[i++] = c; c = fgetc(fp); continue; } else if ( !isalpha( c ) && c != '.' ) /***不是字母也不是。输入错误 */ { if (pWord[i-1] == ' ') { pWord[i-1] = '\0'; ungetc(c,fp); fgets(exp,200,fp); return 1; } else { // printf("%d",c); printf( " \t%c is a bad char!\n", c ); i = 0; pWord[0] = '\0'; while (c != '\n' && c != EOF && !feof(fp)) fread(&c,1,1,fp); return(-1); } } else { /*** * 是字母 也不是space \n eof * 正常情况,永远不执行这一块 */ printf( "error Input Word ,OH GOD ERROR" ); } } return(-2); } int fun_ofile(void) { FILE *fp = fopen(cmdOption,"a"); if (fp == NULL) { printf(" \t文件打开失败!\n"); fun_tip = search; return 0; } word_tree_traveal(&root,NULL,fp); printf(" \t文件输出完毕!\n"); fclose(fp); fun_tip = search; return 0; } void system_sleep(int sleepTime) { #ifdef _WIN32 Sleep(sleepTime); // Sleep(sleepTime); #else sleep(sleepTime); #endif //_WIN32 } int fun_outprint() { int i = 1; int flag; FILE* fp; int c; char w[WORD_NUMBER] = {0}; char e[EXP_NUMBER] = {0}; if (strlen(cmdOption) == 0) outPutSleep = DEF_PRINT_TIME; else if (strlen(cmdOption) == 1) { outPutSleep = atof(cmdOption) * 1000; } //printf("%d",outPutSleep); if (access(TempFile, F_OK) == 0) remove(TempFile); fp = fopen(TempFile,"w"); if (fp == NULL) { printf(" \t文件打开失败!\n"); return 0; } else { word_tree_traveal(&root,NULL,fp); fseek(fp,0L,SEEK_SET); fclose(fp); } fp = fopen(TempFile,"r"); if (fp == NULL) { printf(" \t文件打开失败!\n"); return 0; } while(1) { flag = get_from_file(fp,w,e); if (flag == -1) { printf("\r\r\r\r\r\r"); continue; } else if (flag == 1) { if (strlen(w) == 0 || w[0] == '\n') continue; printf("\n \tNO.%d: %s : ",i,w); printf("%s",e); i++; system_sleep(outPutSleep); if(kbhit()) /**** WARNING implicit declaration of function 'kbhit' ***/ { if (c = getch() == 'q') /**** warning: suggest parentheses around assignment used as truth value [-Wparentheses]| ***/ { printf(" \t用户取消操作!\n"); fclose(fp); remove(TempFile); fun_tip = search; return 0; } } continue; } else if (flag == 0) { if( fclose(fp) != 0) { perror("fclosed"); } printf(" \t全部输出完毕!\n"); //fclose(fp); remove(TempFile); fun_tip = search; return 0; } } } int fun_help() { cmdHelp(); return 0; } static void clean_stdin_buffer(void) { scanf("%*[^\n]"); scanf("%*c"); } bool isrightfile() { return (strlen(cmdOption) == 0 || cmdOption[strlen(cmdOption) - 1] == '\\' || cmdOption[strlen(cmdOption) - 1] == '/'); } bool isrighttime() { return (strlen(cmdOption) == 0 || strlen(cmdOption) > 2); } int checkCmd( int opt ) { int flag; /*** enter option *****/ if (!strcmp( "ofile", cmdInput ) || !strcmp( "ffile", cmdInput ) ) { flag = getOption(cmdOption); if (flag == -1 || isrightfile()) { fun_tip = search; if (strlen(cmdOption) == 0) { printf(" \t请输入文件路径!\n"); } else if (!isdigit(cmdOption)) { printf(" \t请检查文件路径是否正确!\n"); } return 0; } } else if ( !strcmp( "outprint", cmdInput )) { flag = getOption(cmdOption); if (flag == -1 || isrighttime()) { fun_tip = search; if (strlen(cmdOption) == 0) { printf(" \t请正确输入时间\n"); } else if (!isdigit(cmdOption[0])) { printf(" \t请检查时间是否正确!(1 -99)\n"); } return 0; } } else { fflush(stdin); } if ( !strcmp( "clean", cmdInput ) ) { word_tree_traveal_delete(&root); printf(" \t数据清理完毕!\n"); return(0); } else if ( !strcmp( "outprint", cmdInput ) ) { fun_tip = outprint; return(0); } else if ( !strcmp( "ofile", cmdInput ) ) { fun_tip = ofile; return(0); } else if ( !strcmp( "ffile", cmdInput ) ) { fun_tip = ffile; return(0); } else if ( !strcmp( "help", cmdInput ) ) { fun_tip = help; return(0); } else if ( !strcmp( "exit", cmdInput ) ) { word_tree_traveal_delete(&root); exit( 0 ); return(0); } else if ( !strcmp( "search", cmdInput ) ) { fun_tip = search; return(0); } else if ( !strcmp( "enter", cmdInput ) ) { fun_tip = enter; return(0); } else if ( !strcmp( "cls", cmdInput ) ) { cmdCls(); return(0); } else if ( !strcmp( "menu", cmdInput ) ) { cmdMenu(); return(0); } else if ( !strcmp( "update", cmdInput ) ) { /* cmdUpdate(); */ printf( "update words please use \" << \"\n" ); return(0); } else { printf( " \tcan not found the conmand: %s!\n", cmdInput); return(-1); } } int getOption( char str[] ) { int c; int i = 0; EnterAndCheck(c); if ( c != '-' && c != 'f' ) { str[0] = '\0'; ungetc(c,stdin); fgets(cmdOption,300,stdin); if (access(cmdOption,F_OK) == 0) { return 0; } else if(isdigit(c)) { if ( strlen(cmdOption) == 1) return 0; else if (isdigit(cmdOption[1])) return 0; else return -1; } cPrint( " \tWrong argument\n" ); return -1; } else if ( (c = getchar() ) == '=' ) { c = getchar(); while ( 1 ) { if ( c == '\n' || c == EOF || c == ' ' ) { str[i++] = '\0'; return(0); } else if ( isalpha( c ) || c == ':' || c == '\\' || c == '/' || c == '.' || c == '_' || isdigit( c ) ) { str[i++] = c; c = getchar(); continue; } else { str[0] = '\0'; cPrint( " \tIllegal file names\n" ); return(-1); } } } else if ( isdigit( c ) ) { str[i++] = c; c = getchar(); if ( isdigit( c ) ) { str[i++] = c; if ( isdigit( c = getchar() ) ) { str[0] = '\0'; cPrint( " \tTime must be within 1-99.\n" ); return(-1); } else { str[i] = '\0'; return(0); } } else if ( c == '\n' || c == ' ' ) { str[i] = '\0'; return(0); } else { str[0] = '\0'; fflush(stdin); printf( " \tWrong argument\n" ); return(-1); } } else { printf( "%c", c ); fflush(stdin); printf( " \tWrong argument\n" ); } return 0; } int cmdHelp() { system( "cls" ); printf( "\n" ); printf( "在主界面直接输入单词以查询已有数据\n默认数据将保存到程序目录minidict.mi文件\n" ); printf( "* 使用命令提示符 >> 进行操作\n" ); printf( " 例 >>help 将显示本页面\n" ); printf( "\n" ); printf( "* FFILE f=[file] 从file文件输入数据\n" ); printf( "* ENTER 进入连续录入模式,该模式下可手动连续输入数据\n" ); printf( "* MENU 回到主界面\n" ); printf( "* CLS 清空屏幕\n" ); printf( "* [wors] << [word] 将wors 替换为 word\n" ); printf( "* OUTPRINT -[t] 将已有数据以t秒为间隔输出到屏幕 留空为5秒\n" ); printf( "* OFILE f=[file] 将数据输出到file\n" ); printf( "* HELP 显示本页\n" ); printf( "* EXIT 退出程序\n" ); printf( "\n" ); printf( "回车退出" ); int c; while ( c = getchar() != '\n' ) /**** warning: suggest parentheses around assignment used as truth value [-Wparentheses]| ***/ ; system( "cls" ); miniDict(); Menu(); return(0); } int cmdMenu() { system( "cls" ); miniDict(); Menu(); return(0); } int cmdCls() { system( "cls" ); miniDict(); Menu(); return(0); } int getAndCheckWord( char *pWord,char *exp ) { int c; int i = 0; //int j = 0; /***** unused j ****/ /****** 清空数组 */ pWord[0] = '\0'; exp[0] = '\0'; replaceWord[0] = '\0'; EnterAndCheck(c); /* */ if ( c == '\n' || c == EOF ) /* 第一个字符就无效 */ { printf( "\n" ); return(-8); /**** 未检测到有效内容 */ } /**** 命令输入 */ if ( c == '>' && (c = getchar() ) == '>' && i == 0 ) { EnterAndCheck(c); while ( 1 ) { if ( c == '\n' || c == ' ' ) { cmdInput[i] = '\0'; return(0); } else if ( i == CMD_INPUT_LENTH ) { clean_Print( "\ttoo long arg\n" ); cmdInput[0] = '\0'; i = 0; return(-1); } else if ( !isalpha( c ) ) { printf( " \t' %c 'bad conmand\n", c ); cmdInput[0] = '\0'; fflush(stdin); i = 0; return(-1); } cmdInput[i++] = tolower( c ); /* /将命令转换为小写 */ c = getchar(); continue; } } /*** * 命令输入完毕 * 正常 单词输入开始 */ while ( 1 ) { if ( c == '\n' || c == EOF ) { /* 结束 */ pWord[i] = '\0'; while ( pWord[i - 1] == ' ' ) { pWord[i - 1] = '\0'; i--; } i = 0; return(1); } else if ( i == WORD_NUMBER ) { cPrint( " \tword too long\n" ); pWord[0] = '\0'; return(-1); } else if ( c == '<') { if ((c = getchar() ) != '<' ) { if (fun_tip == enter && pWord[i-1] == ' ') { pWord[i - 1] = '\0'; ungetc(c,stdin); ungetc('<',stdin); fgets(exp,200,stdin); return 1; } else { ungetc(c,stdin); fgets(exp,200,stdin); printf(" \terror: '%s<%s ' is not a word!\n",pWord,exp); fflush(stdin); pWord[0] = '\0'; exp[0] = '\0'; return -1; } } int errorTemp = getAndCheckWord( replaceWord,exp); if ( errorTemp == -1 ) { pWord[0] = '\0'; return -1; /**** 如果替换单词录入出错,那么清空前面录入的单词 */ } pWord[i] = '\0'; while ( pWord[i - 1] == ' ' ) { pWord[i - 1] = '\0'; i--; } return(2); } else if ( c == ' ' ) /**** 空格 */ { /* 单词遇到多空格只保留一个 */ pWord[i++] = c; while ( (c = getchar() ) == ' ' ) /** ***空格后还是字母则认为是词组,继续 */ ; continue; } else if ( isalpha( c ) || c == '.' ) { pWord[i++] = c; c = getchar(); continue; } else if (( !isalpha( c ) && c != '.')) /***不是字母也不是。输入错误 */ { int lrn; if (pWord[i-1] == ' ' && fun_tip == enter) { pWord[i-1] = '\0'; ungetc(c,stdin); fgets(exp,200,stdin); return 1; } else { pWord[i++] = c; pWord[i] = '\0'; char tempfile2[300]; fgets(tempfile2,300,stdin); strcpy(cmdOption,pWord); strcat(cmdOption,tempfile2); lrn = strlen(cmdOption); cmdOption[lrn - 1] = '\0'; // printf("%s %s %s",pWord,tempfile2,cmdOption); if (access(cmdOption,F_OK) == 0 && cmdOption[lrn - 2] != '\\' &&cmdOption[lrn - 2] != '/') { int temp = fun_tip; fun_tip = ffile; fun_ffile(); fun_tip = temp; return 3; } } cmdOption[5] = '\0'; strcpy(pWord,cmdOption); strlwr(pWord); if (strcmp(pWord,"ffile") == 0 || strcmp(pWord,"ofile")) { int co; for (co = 6; co < lrn - 1; co++) { cmdOption[co - 6] = cmdOption[co]; } cmdOption[co - 6] = '\0'; lrn = strlen(cmdOption); } if (access(cmdOption,F_OK) == 0 && cmdOption[lrn - 2] != '\\' &&cmdOption[lrn - 2] != '/') { int temp = fun_tip; fun_tip = ffile; fun_ffile(); fun_tip = temp; return 3; } printf( " \terror:' %s ' is not a word!\n",cmdOption ); fflush(stdin); i = 0; cmdOption[0] = '\0'; pWord[0] = '\0'; exp[0] = '\0'; return(-1); } else { /*** * 是字母 也不是space \n eof * 正常情况,永远不执行这一块 */ printf( " \terror Input Word ,OH GOD ERROR" ); } } return(-2); /**** 测试临时,正常永远不会返回-2 */ }
/************************************************************************* > File Name: func.h > Author: > Mail: > Created Time: Thu 30 Jan 2020 08:01:53 AM HKT ************************************************************************/ #ifndef _FUNC_H #define _FUNC_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <windows.h> #include <conio.h> #include "common.h" #include "wtree.h" #define DEF_PRINT_TIME 1 #define CMD_INPUT_LENTH 30 #define EXP_NUMBER 201 #define EnterAndCheck(c) while ( ( (c = getchar() ) == ' ') && c != '\n' ) extern char cmdOption[]; extern int getOption_ffile(char []); extern bool isrightfile(); extern bool isrighttime(); extern int getAndCheckWord( char *pWord,char *exp ); extern int cmdCls(); extern int cmdMenu() ; extern int cmdHelp(); extern int checkCmd( int opt ); extern int fun_outprint(); extern void system_sleep(int sleepTime); extern int fun_ofile(void); extern int get_from_file(FILE *fp, char *pWord,char *exp ); extern int getOption_ffile( char str[] ); extern int fun_ffile(); extern int fun_enter(); extern int searchAndReplaceWord(char w[],char r[],struct wtree *pt); extern int fun_search(); #endif
/************************************************************************* > File Name: common.c > Author: > Mail: > Created Time: Thu 30 Jan 2020 06:57:03 AM HKT ************************************************************************/ #include "common.h" char beauty[600]; enum funtip fun_tip; void clean_Print(const char *str) { int ch; printf("%s", str); while ((ch = getchar() != '\n') && ch != EOF); } void cPrint(const char *str) { // char temp[1024]; // fgets(temp, sizeof(temp), stdin); fflush(stdin); printf("%s", str); } void lprint(void) { switch (fun_tip) { case search: printf( "Serach $ " ); break; case enter: printf( "Enter > " ); break; case ffile: printf( "File > " ); break; case help: printf( "Help > " ); break; case outprint: printf( "Output > " ); break; default: printf( "prefix变量异常改变!" ); } } void temprint(void) { strcpy(beauty, " \t .::::. \n* .::::::::. \n* ::::::::::: FUCK YOU \n* ..:::::::::::' \n* ':::::::::::' \n* .:::::::::: \n* '::::::::::::::.. \n* ..::::::::::::. \n* ``:::::::::::::::: \n* ::::``:::::::::' .:::. \n* ::::' ':::::' .::::::::. \n* .::::' :::: .:::::::'::::. \n* .:::' ::::: .:::::::::' ':::::. \n* .::' :::::.:::::::::' ':::::. \n* .::' ::::::::::::::' ``::::. \n* ...::: ::::::::::::' ``::.\n* ```` ':. ':::::::::' ::::..\n* \'.:::::\' \':'````.."); } void strprint(char str[],int t) { int i; printf("\n"); for (i = 0; i < strlen(str); i++) { if (str[i] == '\n') { printf("\n"); } else if (str[i] == '\0') { i = strlen(str); } else { printf("%c",str[i]); } } printf("\n"); system_sleep(t * 1000); } void miniDict(void) { printf( " \n ,, ,, ,,\n" ); printf( " 7MMM. ,MMF' db db `7MM\"\"\"Yb. db mm \n" ); printf( " MMMb dPMM MM `Yb. MM \n" ); printf( " M YM ,M MM `7MM `7MMpMMMb. `7MM MM `Mb `7MM ,p6\"bo mmMMmm \n" ); printf( " M Mb M' MM MM MM MM MM MM MM MM 6M' OO MM \n" ); printf( " M YM.P' MM MM MM MM MM MM ,MP MM 8M MM \n" ); printf( " zM `YM' MM MM MM MM MM MM ,dP' MM YM. , MM \n" ); printf( " .JML. `' .JMML.JMML.JMML JMML.JMML.JMMmmmdP' .JMML.YMbmd' `Mbmo\n" ); printf( "\n" ); } void Menu() { printf( "\n直接输入单词回车以查询单词。 \n" ); printf( "\t\t*1 直接输入单词以查询 \n"); printf( "\t\t*2 >>ffile 从文件录入 \n"); printf( "\t\t*3 >>enter 进入录入模式 \n"); printf( "\t\t*4 >>help 显示帮助信息 \n"); //printf( "\t\t*5 >>menu 退出当前操作回到主菜单 \n"); printf( "\t\t*5 >>exit 退出程序 \n"); printf( "\n" ); printf( "\n" ); }
/************************************************************************* > File Name: common.h > Author: > Mail: > Created Time: Thu 30 Jan 2020 06:57:21 AM HKT ************************************************************************/ #ifndef _COMMON_H #define _COMMON_H #include <stdio.h> #include <string.h> extern void clean_Print(const char *); extern void cPrint(const char *); extern void lprint(void); extern void temprint(void); extern void strprint(char [], int); extern void miniDict(void); extern void Menu(void); extern char beauty[]; /** 枚举 命令标记 ***/ enum funtip { help = 0, search, enter, ffile, ofile, outprint }; extern enum funtip fun_tip; #endif