您好,欢迎访问三七文档
【问题描述】文学研究人员要统计某篇英文小说中某些形容词的出现次数和位置.试写一个实现这一目标的文学统计表,称为文学研究助手【基本要求】a。英文小说存在于一个文本文件中.待统计的词汇集合要一次输入完毕,即统计工作必须在程序的一次运行之后完成.程序的输出结果是每个词的出现次数和出现位置所在行的行号,格式自行设计b。模式匹配要基于KMP算法c。整个统计过程中只对小说文字扫描一次,以提高效率【测试数据】以当前的源程序文件作为测试目标【实现提示】约定小说中的词汇一律不跨行【思路演示】【代码过程】1。//base.h//-------------------公用的常量和类型----------------------------#includestdio.h#includemalloc.h#includestdlib.h#includestring.h//函数结果状态代码#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1#defineOVERFLOW-2typedefintStatus;//函数的返回值//~2。//hstring.h//-----------------串的堆分配存储表示---------------------------typedefstruct{char*ch;//若是非空串,则按串长分配存储区,否则ch为NULLintlength;//串长度}HString;//-----------------栈的基本操作的算法实现--------------------------------StatusStrInit(HString&T){//初始化串T.ch=NULL;T.length=0;returnOK;}StatusStrAssign(HString&T,char*chars){//生成一个其值等于串常量chars的串T//if(T.ch)free(T.ch);//释放原有的串空间intlen;char*c;for(len=0,c=chars;*c;++len,++c);//求chars的长度i;if(!len){T.ch=NULL;T.length=0;}else{if(!(T.ch=(char*)malloc(len*sizeof(char))))exit(OVERFLOW);for(inti=0;ilen;i++)T.ch[i]=chars[i];T.ch[i]='/0';//结尾加上/0T.length=len;}returnOK;}intStrLength(HStrings){//返回串长度returns.length;}voidStrPrint(HStrings){//打印串for(inti=0;is.length;i++)printf(%c,s.ch[i]);//printf(/t);}intIndex(HStrings,HStringt,intpos){//返回子串t在主串s中第pos个字符之后的位置。如不存在,则返回0inti=pos,j=1;while(i=s.length&&j=t.length){if(s.ch[i-1]==t.ch[j-1])i++,j++;elsei=i-j+2,j=1;}if(jt.length)returni-t.length;elsereturn0;}intNext(HStrings,intj){//KMP模式匹配的next函数if(j==1)return0;for(intk=j-1;k1;k--){for(inti=1;ik;i++){if(s.ch[i-1]!=s.ch[j-k+i-1])break;}if(i==k)break;}returnk;}intIndex_KMP(HStrings,HStringt,intpos){//KMP算法inti=pos,j=1;while(i=s.length&&j=t.length){if(j==0||s.ch[i-1]==t.ch[j-1])i++,j++;elsej=Next(t,j);}if(jt.length)returni-t.length;elsereturn0;}intIsNotCharactor(charch){//判断该字符是不是字母return!(ch='a'&&ch='z'||ch='A'&&ch='Z');}intStrCount_KMP(HStrings,HStringt,intpos){//求串t在s中出现的次数inti=pos,j=1;intcount=0;while(i=s.length){if(j==0||s.ch[i-1]==t.ch[j-1])i++,j++;elsej=Next(t,j);if(jt.length){//if(s.ch[i-1]==''||s.ch[i-1]=='/0')//如果下一个字符为'',或者为串的结尾//if(s.ch[i-t.length-2]==''||i-t.length-20)//并且前一个字符为'',或者为串的开头if(IsNotCharactor(s.ch[i-1])&&IsNotCharactor(s.ch[i-t.length-2]))//如果该字符串的前一和后一字符不是字母,说明找到了count++;//次数++j=1;}}returncount;}3。//linklist.h//--------------------行数据链表--------------------------------------typedefstructLRowDataElem{introw;//所在行intcount;//次数}LRowDataElem;typedefstructLRowNode{LRowDataElemdata;structLRowNode*next;}LRowNode,*RowLink;typedefstruct{RowLinkhead,tail;intlen;}RowLinkList;//行数据链表//-----------------行数据链表的基本操作的算法实现--------------------------------StatusInitList(RowLinkList&L){L.head=(RowLink)malloc(sizeof(RowLink));L.tail=(RowLink)malloc(sizeof(RowLink));if(!L.head||!L.tail)exit(OVERFLOW);L.head-next=L.tail-next=NULL;L.len=0;returnOK;}StatusCopyElem(LRowDataElem&e1,LRowDataEleme2){e1.count=e2.count;e1.row=e2.row;returnOK;}StatusCreateNode(RowLink&rl,LRowDataElemelem){//创建节点rl=(RowLink)malloc(sizeof(RowLink));if(!rl)exit(OVERFLOW);CopyElem(rl-data,elem);rl-next=NULL;returnOK;}StatusAppend(RowLinkList&ls,RowLinklink){//附加节点if(ls.head-next==NULL)ls.head-next=link;elsels.tail-next-next=link;ls.tail-next=link;ls.len++;returnOK;}StatusPrintList(RowLinkListL){//打印列表RowLinkh=L.head-next;while(h){printf(%d:%d,h-data.row,h-data.count);h=h-next;}printf();returnOK;}4。//stringLinklist.h//--------------------串数据链表--------------------------------------typedefstructStringNode{HStringstr;//要查找的串RowLinkListrowlist;//串匹配的行数据列表,每个串对应一个StringNode*next;}StringNode,*StringLink;typedefstruct{StringLinkhead,tail;intlen;}StringLinkList;//-----------------串数据链表的基本操作的算法实现--------------------------------StatusInitList(StringLinkList&L){L.head=(StringLink)malloc(sizeof(StringLink));L.tail=(StringLink)malloc(sizeof(StringLink));if(!L.head||!L.tail)exit(OVERFLOW);L.head-next=L.tail-next=NULL;L.len=0;returnOK;}StatusCreateNode(StringLink&sl,HStringstr){sl=(StringLink)malloc(sizeof(StringLink));if(!sl)exit(OVERFLOW);StrInit(sl-str);StrAssign(sl-str,str.ch);InitList(sl-rowlist);sl-next=NULL;returnOK;}StatusAppend(StringLinkList&ls,StringLinklink){if(ls.head-next==NULL)ls.head-next=link;elsels.tail-next-next=link;ls.tail-next=link;ls.len++;returnOK;}StatusStringListCount(StringLinkList&stringLinkList,HStringhstrLine,introw){//在串数据链表stringLinkList,读出查找的串strkey,与传入的串hstrLine匹配//如果成功将匹配的次数与行数row,写入相对应的行行数据链表StringLinkstringLink=stringLinkList.head-next;//找出第一个串while(stringLink){HStringstrkey=stringLink-str;intcount=StrCount_KMP(hstrLine,strkey,1);//求匹配的次数if(count0){RowLinkrLink;LRowDataElemdata;data.count=count;data.row=row;CreateNode(rLink,data);Append(stringLink-rowlist,rLink);//写入相对应的行行数据链表}stringLink=stringLink-next;//找下一个}returnOK;}StatusPrintList(StringLinkListL){//打印串数据链表的信息StringLinkh=L.head-next;while(h){printf(字符串:);StrPrint(h-str);printf(出现的位置和次数是:);PrintList(h-rowlist);h=h-next;}printf();returnOK;}//~5。//test.cpp#includebase.h#includehstring.h#includelinklist.h#includestri
本文标题:文学助手研究
链接地址:https://www.777doc.com/doc-6049388 .html