主题:为啥我的文学研究助手会有这种问题??求救~~~~~
#include "string.h"
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define N 10
#define M 10
struct linelist
{ int linenum;
struct linelist *next;
};
struct keyword
{ struct string *string1;
int num;
struct linelist *head,*tail;
}keyword1[N];
struct string
{
char string2[M];
}
string3[N]={'\0'};
FILE *fp=NULL;
int row=0;
void init()
{ int i;
for (i=0;i<N;i++)
{
keyword1[i].num=0;
keyword1[i].string1=NULL;
keyword1[i].head=NULL;
keyword1[i].tail=NULL;
}
}
void fileopen()
{
char filename[30];
FILE *stream;
for ( ; ; )
{
printf("Please input the file name:");
scanf("%s",filename);
if ((stream=fopen(filename,"r"))==NULL)
{printf("File not found!Please input again:\n");}
else {break;}
}
}
void inputkeyword()
{
int i;
for (i=0;i<N;i++)
{
printf("Please input the words to search:");
scanf("%s",string3[i].string2);
}
}
void compare(char Array[])
{
int i;
struct linelist *p,*q;
for (i=0;i<N;i++)
{
if(!strcmp(Array,string3[i].string2))
{
keyword1[i].num++;
if (keyword1[i].num==1)
{
p=(struct linelist *)malloc(sizeof(struct linelist));
p->linenum=row;
p->next=NULL;
keyword1[i].head=p;
keyword1[i].tail=p;
} else if (row!=keyword1[i].tail->linenum)
{
q=(struct linelist *)malloc(sizeof(struct linelist));
q->linenum=row;
q->next=NULL;
keyword1[i].tail->next=q;
keyword1[i].tail=q;
}
}
}
}
void loadcmp(char Array[])
{
int i,j;
char *p,*q,temp[20];
q=Array;
while (q!='\0')
{
p=q;
while (q!='\0')
{
if (*q==' ')
{ p++;
q++;
}
if (*q!=' ')
{
q++;
if (*q==' ')break;
}
}
j=q-p;
for (i=0;i<j;i++)
{
temp[i]=*p;
p++;
}
temp[j]='\0';
compare(temp);
}
}
void readline()
{
char buffer[41];
while (!feof(fp))
{
row++;
fgets(buffer,40,fp);
loadcmp(buffer);
}
}
void showresult()
{
int i;
struct linelist *p;
printf("\n\n\n");
for (i=0; i<N; i++)
{
printf("The word ");
printf("%s",string3[i].string2);
printf(" ");
printf("appeared in the file %d times.\n",keyword1[i].num);
printf(" ");
printf("They are in Line:");
p=keyword1[i].head;
while (p!=NULL)
{
printf("%3d",p->linenum);
p=p->next;
} printf("\n");
}
}
void destroy()
{
int i;
struct linelist *p,*q;
for (i=0; i<N; i++)
{
p=keyword1[i].head;
while (p!=NULL)
{ q=p;
p=p->next;
free(q);
}
}
fclose(fp);
}
void main()
{
init();
fileopen();
inputkeyword();
readline();
showresult();
destroy();
}
在运行时输入要查找的单词后,到最后一个单词之后出现 .exe文件有问题!!
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define N 10
#define M 10
struct linelist
{ int linenum;
struct linelist *next;
};
struct keyword
{ struct string *string1;
int num;
struct linelist *head,*tail;
}keyword1[N];
struct string
{
char string2[M];
}
string3[N]={'\0'};
FILE *fp=NULL;
int row=0;
void init()
{ int i;
for (i=0;i<N;i++)
{
keyword1[i].num=0;
keyword1[i].string1=NULL;
keyword1[i].head=NULL;
keyword1[i].tail=NULL;
}
}
void fileopen()
{
char filename[30];
FILE *stream;
for ( ; ; )
{
printf("Please input the file name:");
scanf("%s",filename);
if ((stream=fopen(filename,"r"))==NULL)
{printf("File not found!Please input again:\n");}
else {break;}
}
}
void inputkeyword()
{
int i;
for (i=0;i<N;i++)
{
printf("Please input the words to search:");
scanf("%s",string3[i].string2);
}
}
void compare(char Array[])
{
int i;
struct linelist *p,*q;
for (i=0;i<N;i++)
{
if(!strcmp(Array,string3[i].string2))
{
keyword1[i].num++;
if (keyword1[i].num==1)
{
p=(struct linelist *)malloc(sizeof(struct linelist));
p->linenum=row;
p->next=NULL;
keyword1[i].head=p;
keyword1[i].tail=p;
} else if (row!=keyword1[i].tail->linenum)
{
q=(struct linelist *)malloc(sizeof(struct linelist));
q->linenum=row;
q->next=NULL;
keyword1[i].tail->next=q;
keyword1[i].tail=q;
}
}
}
}
void loadcmp(char Array[])
{
int i,j;
char *p,*q,temp[20];
q=Array;
while (q!='\0')
{
p=q;
while (q!='\0')
{
if (*q==' ')
{ p++;
q++;
}
if (*q!=' ')
{
q++;
if (*q==' ')break;
}
}
j=q-p;
for (i=0;i<j;i++)
{
temp[i]=*p;
p++;
}
temp[j]='\0';
compare(temp);
}
}
void readline()
{
char buffer[41];
while (!feof(fp))
{
row++;
fgets(buffer,40,fp);
loadcmp(buffer);
}
}
void showresult()
{
int i;
struct linelist *p;
printf("\n\n\n");
for (i=0; i<N; i++)
{
printf("The word ");
printf("%s",string3[i].string2);
printf(" ");
printf("appeared in the file %d times.\n",keyword1[i].num);
printf(" ");
printf("They are in Line:");
p=keyword1[i].head;
while (p!=NULL)
{
printf("%3d",p->linenum);
p=p->next;
} printf("\n");
}
}
void destroy()
{
int i;
struct linelist *p,*q;
for (i=0; i<N; i++)
{
p=keyword1[i].head;
while (p!=NULL)
{ q=p;
p=p->next;
free(q);
}
}
fclose(fp);
}
void main()
{
init();
fileopen();
inputkeyword();
readline();
showresult();
destroy();
}
在运行时输入要查找的单词后,到最后一个单词之后出现 .exe文件有问题!!