-
/*********************************************************** * 版权所有 (C)2016,WuHao * * 项目名称:单词检索统计程序 * 其它说明: 各个模块函数 * 作 者: 武昊 * 完成日期: 20161230 ********************************************************** hao.h: /********************************************************* * 版权所有 (C)2016,wuhao * * 内容摘要:对各种参数进行初始化 * 文件名称:hao.h * 文件标识:hao.h * 其它说明:可以读取txt文件 * 当前版本: V8.0 * 作 者: 武昊 * 完成日期: 20161230 * ************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_INIT_SIZE 500 /*线性表存储空间的初始分配量*/ #define LISTINCREMENT 10 /*线性表存储空间的分配增量*/ #define FILE_NAME_LEN 20 /*文件名长度*/ #define WORD_LEN 20 /*单词长度*/ #define MaxStrSize 256 #define llength 110 /*规定一行有110个字节*/ #define MaxStr 258 #define WORD 21 typedef struct { char ch[MaxStr]; /* ch是一个可容纳256个字符的字符数组 */ int length; } string; /* 定义顺序串类型 */ typedef struct { char word[WORD]; /*存储单词,不超过20个字符*/ int count; /*单词出现的次数*/ } elem_type; typedef struct{ elem_type *elem; /*存储空间基址*/ int length; /*当前长度*/ int listsize; /*当前分配的存储容量*/ } sqlist; /********************************************************* * 版权所有 (C)2016,wuhao * * 文件名称:word.cpp * 文件标识:word.cpp * 内容摘要:单词检索 * 其它说明:无 * 当前版本: V8.0 * 作 者: 武昊 * 完成日期: 20161230 * ************************************************************/ #include"hao.h" int sqlist_init(sqlist *sq, elem_type *et) { sq->elem = et; sq->length = 0; return 0;} /********************************************************* * 功能描述:单词添加 * 输入参数:变量i,j,结构体:sqlist,elem_type * 输出参数:无 * 返回值 :0 * 其它说明:无 ************************************************************/ int sqlist_add(sqlist *sq, elem_type *et, char *word) { int i; int j; for (i = 0; i < sq->length; i++) { /*当前单词与加入的单词相同,直接统计,不做插入 */ if (strcmp(et[i].word, word) == 0) { et[i].count++; return 0; } if (strcmp(et[i].word, word) > 0) { break; } } if (sq->length == LIST_INIT_SIZE) { printf("空间不足,单词[%s]插入失败\n", word); return 0; } for (j = sq->length; j > i; j--) { memcpy(et+j, et+j-1, sizeof(elem_type)); } sq->length++; strcpy(et[i].word, word); et[i].count = 1; return 0; } /********************************************************* * 功能描述:单词输出 * 输入参数:变量i,j,结构体:sqlist,elem_type * 输出参数:无 * 返回值 :j * 其它说明:无 ************************************************************/ int sqlist_count(sqlist *sq, elem_type *et) { int i, j = 0; for(i=0;i<sq->length;i++) j=j+et[i].count; return j; } /********************************************************* * 功能描述:建立文本文档 * 输入参数:文件变量:fp,结构体:sqlist,elem_type,字符串:file_name * 输出参数:无 * 返回值 :0 * 其它说明:无 ************************************************************/ int creat_text_file() /*建立文本文档*/ { elem_type w; sqlist s; char file_name[FILE_NAME_LEN + 1],yn; FILE *fp; printf("输入要建立的文件名:"); scanf("%s",file_name); fp=fopen(file_name,"w"); yn='n'; /* 输入结束标志初值 */ while(yn=='n'||yn=='N') { printf("请输入一行文本:"); gets(w.word); gets(w.word); s.length=strlen(w.word); fwrite(&w,s.length,1,fp); fprintf(fp,"%c",10); /* 是输入换行 */ printf("结束输入吗?y or n :");yn=getchar(); } fclose(fp); /* 关闭文件 */ printf("建立文件结束!\n"); return 0; } /********************************************************* * 功能描述:文本单词总汇 * 输入参数:字符:file_name[],word[],文件变量:fp * 输出参数:无 * 返回值 :0 * 其它说明:无 ************************************************************/ int substrsum()/*文本单词汇总 */ { char file_name[FILE_NAME_LEN + 1]; char word[WORD_LEN+1]; FILE *fp; int i; int j,q=0; int w,x,y=0; elem_type et[LIST_INIT_SIZE]; sqlist sq; sqlist_init(&sq, et); printf("请输入文件名:"); scanf("%s", file_name); fp = fopen(file_name, "r"); if (fp == NULL) { printf("打开文件失败!\n"); return 0; } while (fscanf(fp, "%s", word) != EOF) { sqlist_add(&sq, et, word); } fclose(fp); printf(">>>>>>>>>>>>>>>>单词<<<>>>>个数<<<<<<<<<<<\n"); for (i = 0; i < sq.length; i++) { x=strlen(et[i].word); for(w=x-1;w>=0;w--) if(et[i].word[w]<65||(et[i].word[w]>90&&et[i].word[w]<97)||et[i].word[w]>122) { et[i].word[w]=' '; } for(w=0;w<x;w++) if (et[i].word[w]==' ') y++; if(y==x) { et[i].count=0; y=0; } else y=0; if(et[i].count!=0) printf("%20s%10d\n", et[i].word, et[i].count);else q++; } j=sqlist_count(&sq, et); printf("\n>>>>>>>>>>>>>>>>>>%s的单词总数为%d个\n",file_name,j); printf("\n>>>>>>>>>>>>>>>>>>%s的非单词个数为%d种\n",file_name,q); printf("\n"); return 0; } /********************************************************* * 功能描述:串匹配算法 * 输入参数:变量i,j,串s1,s2,字符长度:length * 输出参数:无 * 返回值 :0 * 其它说明:无 ************************************************************/ int partposition (string s1,string s2,int k) /*串匹配算法*/ { int i,j; i=k-1; /* 扫描s1的下标,因为c中数组下标是从0开始,串中序号相差1 */ j=0; /* 扫描s2的开始下标 */ while(i<s1.length && j<s2.length) { if(s1.ch[i]==s2.ch[j]) { i++;j++; /* 继续使下标移向下一个字符位置 */ } else { i=i-j+1; j=0; } } if (j>=s2.length) return i-s2.length; else return -1; /* 表示s1中不存在s2,返回-1 */ /* 表示s1中存在s2,返回其起始位置 */ } /* 函数结束 */ /********************************************************* * 功能描述:统计单次出现的个数 * 输入参数:串变量:s,t文件变量:fp字符量:fname[] * 输出参数:无 * 返回值 :0 * 其它说明:无 ************************************************************/ int substrcount() /* 统计单词出现的个数 */ { FILE *fp; string s,t; /* 定义两个串变量 */ char fname[10]; int i=0,j,k; printf("输入文本文件名:"); scanf("%s",fname); fp=fopen(fname,"r"); printf("输入要统计计数的单词:"); scanf("%s",t.ch); t.length=strlen(t.ch); while(!feof(fp)){ memset(s.ch,'\0',110); fgets(s.ch,110,fp); s.length=strlen(s.ch); k=0; /* 初始化开始检索位置 */ while(k<s.length-1) /* 检索整个主串S */ { j=partposition(s,t,k); /* 调用串匹配函数 */ if(j<0 ) break; else { i++; /* 单词计数器加1 */ k=j+t.length; /* 继续下一字串的检索 */ } } } printf("\n单词%s在文本文件%s中共出现%d次\n",t.ch,fname,i); return 0; } /********************************************************* * 功能描述:检索单词出现的行号次数及位置 * 输入参数:串变量:s,t文件变量:fp * 输出参数:无 * 返回值 :0 * 其它说明:无 ************************************************************/ int substrint() /* 检索单词出现在文本文件中的行号、次数及其位置 */ { FILE *fp; string s,t; /* 定义两个串变量 */ char fname[10]; int wz[20]; /* 存放一行中字串匹配的多个位置 */ printf("输入文本文件名:"); int i,j,k,l,m; scanf("%s",fname); fp=fopen(fname,"r"); printf("输入要检索的单词:"); scanf("%s",t.ch); t.length=strlen(t.ch); l=0; /* 行计数器置0 */ while(!feof(fp)) { /* 扫描整个文本文件 */ memset(s.ch,'\0',110); fgets(s.ch,110,fp); s.length=strlen(s.ch); l++; /* 行计数器自增1 */ k=0; /* 初始化开始检索位置 */ i=0; /* 初始化单词计数器 */ while(k<s.length-1) /* 检索整个主串S */ { j=partposition(s,t,k); /* 调用串匹配函数 */ if(j<0) break; else { i++; /* 单词计数器加1 */wz[i]=j; /* 记录匹配单词位置 */ k=j+t.length; /* 继续下一字串检索 */ } } if(i>0) { printf("行号:%d,次数:%d,起始位置分别为:",l,i); for(m=1;m<=i;m++) printf("第%4d个字符",wz[m]+1); } printf("\n"); } printf("\n本软件自定义110个字节为一行\n\n"); return 0; } /********************************************************* * 功能描述:定位统计 * 输入参数:字符变量:t,switch case语句 * 输出参数:无 * 返回值 :0 * 其它说明:无 ************************************************************/ int substrio() { int substrcount(),substrint(); char t; //while(1) //{ printf("===============================================\n"); printf("||文本文件单词字串的定位统计及定位||\n"); printf("||================================||\n"); printf("|| a. 单词出现次数 ||\n"); printf("|| ||\n"); printf("|| ||\n"); printf("|| b. 单词出现位置 ||\n"); printf("|| ||\n"); printf("====================================\n"); printf("请输入a或b:"); scanf("%c%*c",&t); switch(t) { case 'a': substrcount();break; case 'b': substrint();break; default: return 0; } //} return 0; } int main(void) { int creat_text_file(),substrsum(),substrio(); int a; int t = 1; while(t) { printf("****************************************************\n"); printf("**************文本文件单词的检索与计数**************\n"); printf("╔━━━═━━━═━━━═━━━═━━━═━━━═╗\n"); printf("┋ ╭─────╮ ╰★【1】建立文本文档 ┋\n"); printf("┋ │● ●│ ╰★【2】文本单词汇总 ┋\n"); printf("┋ ╱ ╭▼▼▼╮ ╲ ╰★【3】单词定位 ┋\n"); printf("┋ ╰┐│ │┌╯ ╰★【4】退出 ┋\n"); printf("┋ │╰▲▲▲╯│ ┋\n"); printf("┋┗━ ┗━ ┋\n"); printf("╚━━━═━━━═━━━═━━━═━━━━━━━━╝\n"); printf("请选择(1~4):"); scanf("%d%*c",&a); switch(a) { case 1: creat_text_file();break; case 2: substrsum();break; case 3: substrio();break; case 4: return 0; default:printf("选择错误,重新选 \n"); } } return 0; }
数据结构实训——单词检索统计程序
最新推荐文章于 2023-11-15 17:38:03 发布