📄
字号:
#include <stdio.h>
#include <stdlib.h>
#include<alloc.h>
#include<process.h>
#include<malloc.h>
#include<string.h>
#include<conio.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREATEMENT 10;
int tcount=0;
typedef struct HString{
char *ch;
int length;
int account;
int next[100];
int j; }HString;
typedef struct Stack{
HString *base;
HString *top;
HString *temp;
int stacksize;
}Stack;
void InitStack(Stack &S){
S.base=(HString*)malloc(50* sizeof(HString));
if(!S.base)exit(0);
S.top=S.base;
S.stacksize=50;
}
void Push(Stack &S,HString string){
if(S.top-S.base>=S.stacksize){
S.base=(HString *)realloc(S.base,(S.stacksize+10)*sizeof(HString));
if(!S.base)exit(0);
S.top=S.base+S.stacksize;
S.stacksize+=10; }
*S.top++=string;
}
void get_nextval(Stack &stack)
{
stack.temp=stack.base;
while(stack.temp!=stack.top)
{
int i=1;int length=(*stack.temp).length;
(*stack.temp).next[0]=0;
int j=0;
while(i<=length)
{if(j==0||(*stack.temp).ch[i-1]==(*stack.temp).ch[j-1])
{++i;++j;
if((*stack.temp).ch[i-1]!=(*stack.temp).ch[j-1]) (*stack.temp).next[i-1]=j;
else (*stack.temp).next[i-1]=(*stack.temp).next[j-1];
}
else j=(*stack.temp).next[j-1];
}
stack.temp++;
}
}
void locate (char S[],HString &str,int i)
{int j=0,row=1;
str.account++;
while(j!=i+1)
{if(S[j]=='\n')
row++;j++;
}
printf("the word: %s appears the %d time,in row: %d\n",str.ch,str.account,row);
}
void Index_KMP(char S[],int n,Stack T,int pos)
{
int Slength=n;
int i =pos ;
T.temp=T.base;
while( i <Slength){
T.temp=T.base;
while(T.temp!=T.top)
{if((*T.temp).j != 0 &&S[i-1] !=(*T.temp).ch[(*T.temp).j-1])
{ (*T.temp).j = (*T.temp).next[(*T.temp).j-1]; }
T.temp++;
}
T.temp=T.base;
int count=0;
while(T.temp!=T.top)
{
if((*T.temp).j==0||S[i-1]==(*T.temp).ch[(*T.temp).j-1])
{count++;}
T.temp++;
}
T.temp=T.base;
if(count==tcount)
{
{++i;}
while(T.temp!=T.top)
{
++(*T.temp).j;
if((*T.temp).j > (*T.temp).length&&(S[i-1]==' '||S[i-1]=='\n')&&(S[i- (*T.temp).length-2]==' '||S[i- (*T.temp).length-2]=='\n'))
{ locate (S,(*T.temp),(i - (*T.temp).length)); (*T.temp).j=1; }
T.temp++;
}
}
}
}
void main()
{ Stack stack;
HString string;
InitStack(stack);
FILE *fp;
int n=0;
char ch, filename[10];
printf("\n");
printf("Please enter the filename you want to edit:\n");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{printf("cannot open file or the file does not exsist!\n");
exit(0);}
ch=fgetc(fp);
while(ch!=EOF)
{n++;
ch=fgetc(fp);
}
rewind(fp);
char *str=NULL;
str=(char *)calloc(n,1);
char *mainstring=str;
ch=fgetc(fp);
while(ch!=EOF)
{ *str=ch;
str++;
ch=fgetc(fp);
}
*str='\0';
fclose(fp);
printf("%s",mainstring);
printf("\n");
printf("how many words do you want to edit:\n");
scanf("%d",&tcount);
printf("please enter the word:\n");
struct Entry_struct
{
char name[20];
int length;
} entry[20];
for(int m=0;m<tcount;m++)
{
scanf("%s",entry[m].name);
string.ch=entry[m].name;string.length=strlen(entry[m].name);
string.account= 0;string.j=1;
Push(stack,string);
}
get_nextval(stack);
Index_KMP(mainstring,n,stack,1);
free(mainstring);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -