这个代码是哪里出问题了?
十分感谢
#include<stdlib.h>
#include<stdio.h>
#include <conio.h>
#include<string.h>
#include<windows.h>
#include<malloc.h>
#include<iostream>
using namespace std;
typedef struct position //文件编号存储
{
int p; //当前文件编号
struct position *np; //下一个文件编号指针
}position,*Position;
typedef struct word
{
char key[20]; //关键词
int last; // 关键词长度
position *fp; // 第一个文档编号指针
struct word *next; // 下一个关键词
struct word *front; // 上一个关键词
}word,*Word;
typedef struct head
{
int num; //目前关键词数量
word *first; //第一个关键词指针
word *last;//最后一个关键词指针
}head;
void init(head *h)//初始化索引表存储结构体
{
h->first=h->last=(word*)malloc(sizeof(word));
h->first->next=h->last;
h->last->front=h->first;
h->num=0;
}
int n=0; //文件编号
int file=3; // 文件夹编号
char p[100]; // 路径
void main()
{
void creatlist();
void search();
int f; //选择功能标记
char a; //无用的
cout<<" 1.建立索引表"<<endl;
cout<<" 2.检索关键词"<<endl;
cout<<"\n选择功能:";
cin>>f;
cout<<"输入文件路径(eg:D:\\fengci ): ";
cin>>p;
if(f==1)
creatlist(); //建立索引表
else if(f==2)
search(); //检索关键词
a=getch();
}
void path(char p[],char s[]) //生成每个文件路径
{
int i;
strcpy(s,p);
if(file==1)
strcat(s,"\\C000007\");
if(file==2)
strcat(s,"\\C000008\");
if(file==3)
strcat(s,"\\C000010\");
if(file==4)
strcat(s,"\\C000013\");
if(file==5)
strcat(s,"\\C000014\");
if(file==6)
strcat(s,"\\C000016\");
if(file==7)
strcat(s,"\\C000020\");
if(file==8)
strcat(s,"\\C000022\");
if(file==9)
strcat(s,"\\C000023\");
if(file==10)
strcat(s,"\\C000024\");
//printf("%s",p);
i=strlen(s);
if(n>999)
{
s[i]=n/1000+48;
i++;
}
if(n>99)
{
s[i]=n/100-n/1000*10+48;
i++;
}
if(n>9)
{
s[i]=n/10-n/100*10+48;
i++;
}
s[i++]=n%10+48;
s[i]='\0';
strcat(s,".txt");
printf("%s\n",s);
}
void getchars(FILE *fp,char k[])//获取一个关键词
{
int i;
i=0;
k[i]=fgetc(fp);
while(k[i]!=EOF&&k[i]!=' '&&k[i]!='\n')
{
//putchar(k[i]);
i++;
k[i]=fgetc(fp);
}
k[i]='\0';
}
void newiteam(head *h,char k[]) //建立关键词新节点
{
word *w;
w=(word*)malloc(sizeof(word));
w->next=w->front=(word*)malloc(sizeof(word));
w->fp=(position*)malloc(sizeof(position));
strcpy(w->key,k);
w->last=strlen(k);
w->fp->p=n;
w->front=h->last;
w->fp->np=NULL;
w->next=NULL;
if(h->num==0)
{
h->first=w;
h->last=w;
}
else
{
h->last->next=w;
h->last=w;
}
h->num++;
//cout<<k<<endl;
//cout<<h->first->key<<endl;
}
void show(head *h) //测试节点内容函数
{
word *w;
position *p;
w=h->first;
while(w!=NULL)
{
cout<<w->key<<endl;
p=w->fp;
while(p!=NULL)
{
cout<<p->p;
p=p->np;
}
cout<<endl;
w=w->next;
}
}
int compare(FILE *fp,char k[]) //索引关键词时候比较内容
{
char temp[100];
int i;
rewind(fp);
getchars(fp,temp);
while(strcmp(k,temp)!=0)
{
if((i=feof(fp))!=0)
{
return(0);
}
getchars(fp,temp);
}
return(1);
}
Word compare(head *h,char k[]) //建立索引表时候比较内容
{
word *w;
w=h->first;
while(w!=NULL)
{
if(strcmp(w->key,k)==0)
{
return(w);
}
w=w->next;
}
w=NULL;
return(w);
}
int select(FILE *fp3,char k[]) //消除停用词
{
if(strlen(k)<=2)
{
return(0);
}
if(compare(fp3,k)==1)
{
return(0);
}
return(1);
}
void insert(word *w) //对某个关键词插入新编号
{
position *pp;
position *p;
p=(position*)malloc(sizeof(position));
p->np=NULL;
p->p=n;
pp=w->fp;
while(pp->np!=NULL)
{
pp=pp->np;
}
if(pp->p!=n)
{
pp->np=p;
}
}
void make(FILE *fp1,FILE *fp3,head *h) //建立索引表循环体
{
char k[50];
word *w;
getchars(fp1,k);
newiteam(h,k);
//show(h);
while(feof(fp1)==0)
{
getchars(fp1,k);
if(select(fp3,k)==0)
continue;
if((w=compare(h,k))!=NULL)
{
insert(w);
}
else
{
newiteam(h,k);
}
}
//show(h);
cout<<"关键词:"<<h->num<<endl;
}
void write(head *h) //把结构体写入文件
{
FILE *fp;
char s[20];
char k[]="\n";
word *w;
position *p;
w=h->first;
if(file==1)
strcpy(s,"索引表1.txt");
if(file==2)
strcpy(s,"索引表2.txt");
if(file==3)
strcpy(s,"索引表3.txt");
if(file==4)
strcpy(s,"索引表4.txt");
if(file==5)
strcpy(s,"索引表5.txt");
if(file==6)
strcpy(s,"索引表6.txt");
if(file==7)
strcpy(s,"索引表7.txt");
if(file==8)
strcpy(s,"索引表8.txt");
if(file==9)
strcpy(s,"索引表9.txt");
if(file==10)
strcpy(s,"索引表10.txt");
if((fp=fopen(s,"w+"))==NULL)//创建索引表
{
printf("不能创建\n\n");
exit(1);
}
fclose(fp);
fp=fopen(s,"r+"); //打开索引表
while(w!=NULL)
{
fprintf(fp,"%s ",w->key);
p=w->fp;
while(p!=NULL)
{
fprintf(fp,"%d ",p->p);
p=p->np;
}
fprintf(fp,"%s ",k);
w=w->next;
}
fclose(fp);
}
void creatlist() //建立索引表
{
FILE *fp1,*fp3;
char s[100];
int l=0;
SYSTEMTIME start;//开始时间
SYSTEMTIME end;//结束时间
head H[10];
head *h;
fp3=fopen("select.txt","r"); //打开停用词表
//strcpy(p,"D:/fengci");
while(file<11)
{
h=&H[file-1];
init(h);
n=0;
while(n<8000)
{
path(p,s);
if((fp1=fopen(s,"r"))==NULL) //打开文档
{
printf("不能读取\n\n");
exit(1);
}
//printf("读取成功\n");
make(fp1,fp3,h);//创建fp1的索引表循环体
cout<<"新增:"<<h->num-l<<endl;
l=h->num;
n++;
fclose(fp1);
}
cout<<"索引表建立中..."<<endl;
write(h);
cout<<"索引表建立完成!"<<endl;
file++;
}
fclose(fp3);
}
void searchlist(char key[],int list[][1000],int o) //索引表寻找关键词
{
int i=1;
int j=0;
int l;
char temp[40];
char s[15];
char k;
FILE *fp;
while(i<11)
{
list[i-1+o*10][0]=-1;
l=0;
if(i==1)
strcpy(s,"索引表1.txt");
if(i==2)
strcpy(s,"索引表2.txt");
if(i==3)
strcpy(s,"索引表3.txt");
if(i==4)
strcpy(s,"索引表4.txt");
if(i==5)
strcpy(s,"索引表5.txt");
if(i==6)
strcpy(s,"索引表6.txt");
if(i==7)
strcpy(s,"索引表7.txt");
if(i==8)
strcpy(s,"索引表8.txt");
if(i==9)
strcpy(s,"索引表9.txt");
if(i==10)
strcpy(s,"索引表10.txt");
fp=fopen(s,"r");
fseek(fp,1,1);
getchars(fp,temp);
while(feof(fp)==0)
{
if(strcmp(key,temp)==0)
{
//fseek(fp,1,1);
k=fgetc(fp);
list[i-1+o*10][l]=k-48;
k=fgetc(fp);
while(k!='\n')
{
if(k==' ')
{
l++;
list[i-1+o*10][l]=-2;
}
else
{
if(list[i-1+o*10][l]==-2)
list[i-1+o*10][l]=0;
list[i-1+o*10][l]=list[i-1+o*10][l]*10+k-48;
}
k=fgetc(fp);
}
break;
}
getchars(fp,temp);
}
fclose(fp);
i++;
}
/*
for(i=0;i<10;i++)
{
j=0;
while(list[i+o*10][j]!=-1&&list[i+o*10][j]!=-2)
{
cout<<list[i+o*10][j]<<" ";
j++;
}
}
*/
}
void result(int list[][1000],int num) //结果写入文件中
{
int i;
int j;
int o;
int k;
int f;
char s[100];
//="E:\corpus\ClassFile";
FILE *fp;
if(num>1)
{
for(i=0;i<10;i++)
{
j=0;
while(list[i][j]!=-1&&list[i][j]!=-2)
{
f=0;
for(o=1;o<num;o++)
{
k=0;
while(list[i+o*10][k]!=-1&&list[i+o*10][k]!=-2)
{
if(list[i][j]==list[i+o*10][k])
f=1;
k++;
}
if(f==0)
{
list[i][j]=-3;
}
}
j++;
}
}
}
if((fp=fopen("result.txt","w+"))==NULL)//创建结果列表
{
printf("不能创建\n\n");
exit(1);
}
fclose(fp);
fp=fopen("result.txt","r+"); //打开结果列表
for(i=0;i<10;i++)
{
j=0;
strcpy(s,p);
if(i==0)
strcat(s,"\\C000007\");
if(i==1)
strcat(s,"\\C000008\");
if(i==2)
strcat(s,"\\C000010\");
if(i==3)
strcat(s,"\\C000013\");
if(i==4)
strcat(s,"\\C000014\");
if(i==5)
strcat(s,"\\C000016\");
if(i==6)
strcat(s,"\\C000020\");
if(i==7)
strcat(s,"\\C000022\");
if(i==8)
strcat(s,"\\C000023\");
if(i==9)
strcat(s,"\\C000024\");
while(list[i][j]!=-1&&list[i][j]!=-2)
{
if(list[i][j]!=-3)
{
fprintf(fp,"%s",s);
fprintf(fp,"%d",list[i][j]);
fprintf(fp,"%s",".txt");
fprintf(fp,"%s","\n");
}
j++;
}
}
fclose(fp);
}
void search() //关键词检索
{
int num;
char key[100];
char temp[20];
int i;
int j;
int k=0;
int list[100][1000];
SYSTEMTIME start;//开始时间
SYSTEMTIME end;//结束时间
cout<<endl<<"输入关键词的数量:";
cin>>num;
key[0]='\0';
for(i=0;i<num;i++)
{
cout<<endl<<"输入关键词:";
cin>>temp;
//cout<<temp;
strcat(key,temp);
strcat(key," ");
}
//cout<<key;
i=-1;
cout<<"检索开始..\n";
while(k<num)
{
i++;
j=0;
while(key[i]!=' ')
{
temp[j]=key[i];
i++;
j++;
}
temp[j]='\0';
searchlist(temp,list,k);
k++;
}
cout<<"检索结束.."<<endl;
result(list,num);
}
[解决办法]