?? main.c
字號:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define maxb 2//文章數(shù)
typedef struct headnode headnode;
typedef struct key key;
typedef struct idxnode idxnode;
typedef struct normal normal;
struct idxnode
{//索引號結(jié)點
int idxnum[3];
idxnode *down;
};
struct key
{//關(guān)鍵詞結(jié)點
char word[25];//一個單詞最大長度定義為25
int idx;//索引號個數(shù)
idxnode *down;//指向索引號
key *right;//指向下一個關(guān)鍵詞
};
struct headnode
{//索引表頭結(jié)點
key *keyp;
int keynum;//關(guān)鍵詞個數(shù)
};
struct normal
{//平凡詞結(jié)點
char word[15]; //一個平凡單詞數(shù)組,長度定義為15
normal *p;
};
//-------------------------------(一)建立平凡詞表--readnormal()-----------------------------------------------------------------------
normal *readnormal(normal *L)
{//將平凡詞表讀入內(nèi)存
int i,j;//j用來標記單詞個數(shù)
char c;//檢查'\n'
normal *a;
FILE *normalfp;
normalfp=fopen("normal.txt","r");
if(normalfp==NULL)
{
printf("cannot open normal.txt\n");
exit(0);
}
j=0;
while((c=fgetc(normalfp))!=EOF)
{
i=0;
a=(normal *)malloc(sizeof(normal));
// a->p = NULL;
while(c!='\n')
{
a->word[i]=c;
c=fgetc(normalfp);
i++;
}
if(i!=0)
{
a->word[i]='\0';
a->p=L->p;
L->p=a;//插入頭結(jié)點之后
a=a->p;
j+=1;
}
}
fclose(normalfp);
/*a=L->p;
while(j>0 )
{
printf("%s\n",a->word);
a=a->p;
j-=1;
}*/
return L;
}
//------------------------------------(二)建立索引表-----------------------------------------------------------------------
int iskey(char *str,normal *norH)
{//判斷是否為關(guān)鍵詞
normal *h;
for(h=norH->p;h!=NULL;h=h->p)
{
if(!strcmp(h->word,str))
return 0;
}
return 1;
}//iskey
//-----------------------------------------------------------------------------------------------------------
key *locate(headnode *head,char *buf,int s)
{//s=0代表該關(guān)鍵詞不在索引表內(nèi),s=1則表示在索引表內(nèi),且返回該關(guān)鍵詞結(jié)點指針
int i,m;
key *q;
q=(key *)malloc(sizeof(key));
q=head->keyp;
for(i=0;i<head->keynum;i++,q=q->right)
{
m=strcmp(q->word,buf);
if(m==0)
break;
}
if(i==head->keynum)
{
s=0;
return NULL;
}
else
{
s=1;
return q;
}
}//locate
//-----------------------------------------------------------------------------------------------------------
headnode *build(normal *L,headnode *head)
{//返回索引表頭結(jié)點
FILE *f;
char c;//在文件中取字符
int i,j,k,s;//書號,段號,行號,s在buf[]內(nèi)做標記
char filename[maxb][6];//書名數(shù)組
char buf[25];//寄存單詞
idxnode *dp;
key *rp,*p;
head->keyp=NULL;
head->keynum=0;
for(i=0;i<maxb;i++)
{//書名從0.txt開始
filename[i][0]='1'+i;
filename[i][1]='.';
filename[i][2]='t';
filename[i][3]='x';
filename[i][4]='t';
filename[i][5]='\0';
}
i=0;
do
{
printf("%s",filename[i]);
j=k=s=0;
f=fopen(filename[i],"r");
if(!f)
{
printf("openfile err");
exit(0);
}
c=fgetc(f);
while(c!=EOF)
{//在一篇文章內(nèi)循環(huán)
if(c=='*')
{
j++;//'*'代表段首
}
else if(c=='\n')
k++;//一行結(jié)束
else if(isalpha(c))
{
if(isupper(c))//大寫字母轉(zhuǎn)換為小寫
c = tolower(c);
buf[s++]=c;
}
else if((c==' '||c=='\n'||c==','||c=='.')&&buf[0]!='\0')
{
buf[s]='\0';
k++;
if(iskey(buf,L))
{
dp=(idxnode *)malloc(sizeof(idxnode));
dp->idxnum[0]=i+1;
dp->idxnum[1]=j;
dp->idxnum[2]=k;
dp->down=NULL;
p=locate(head,buf,s);//s作為是否在索引表內(nèi)的標記
if(p == NULL)
{//不在索引表內(nèi)
rp=(key *)malloc(sizeof(key));
strcpy(rp->word,buf);
rp->down=dp;
rp->idx=1;
rp->right=head->keyp;
head->keyp=rp;
head->keynum++;
}
else
{//在索引表內(nèi)
dp->down=p->down;
p->down=dp;
p->idx++;
}
}
s=0;//buf為平凡詞,則重新取詞
buf[s]='\0';
}
c=fgetc(f);
}//while(c!=EOF)
if(fclose(f))
{
printf("close file error");
exit(0);//關(guān)閉未成功
}
i++;//下一本書
}while(i<maxb);
return head;
}
//-----------------------------------------------------------------------------------------------------------
void putout(headnode *head)
{
FILE *f;
int i,j;
idxnode *dp;
key *rp;
dp=(idxnode *)malloc(sizeof(idxnode));
rp=(key *)malloc(sizeof(key));
rp=head->keyp;
f=fopen("idxlist.txt","w");
for(i=0;i<(head->keynum);i++,rp=rp->right)
{
fprintf(f,"%s\n",rp->word);
dp=rp->down;
for(j=0;j<rp->idx;j++,dp=dp->down)
fprintf(f,"%d,%d,%d\n",dp->idxnum[0],dp->idxnum[1],dp->idxnum[2]);
}
if(fclose(f))
exit(0);//關(guān)閉未成功
}
//-----------------------------打印文章------------------------------------------------------
void prt(int i,int j)
{//打印出含有關(guān)鍵詞的文字段
char fn[6];
FILE *f;
char c;
int p;
fn[0]='0'+i;
fn[1]='.';
fn[2]='t';
fn[3]='x';
fn[4]='t';
fn[5]='\0';
f=fopen(fn,"r");
if(!f)
{
printf("openfile error");
exit(0);
}
for(p=0;p<j;)
{
c=fgetc(f);
if(c=='*')
p++;
}
for(c=fgetc(f);c!='*'&&c!=EOF;c=fgetc(f))
printf("%c",c);
if(fclose(f))
exit(0);//關(guān)閉未成功
return;
}
//--------------------------------(三)查找關(guān)鍵詞---------------------------------------------------------------------------
void searchkey(normal *L,headnode *head)
{
int need,i,num,b=0;
char qustword[25];//max 25
idxnode *dp;
key *rp;
dp=(idxnode *)malloc(sizeof(idxnode));
rp=(key *)malloc(sizeof(key));
printf("是否需要搜索關(guān)鍵詞(yes=1/no=0):");
scanf("%d",&need);
while(need)
{//在索引表中逐個查找
printf("請輸入要查找的單詞:");
scanf("%s",qustword);
if(iskey(qustword,L))
{
rp=locate(head,qustword,b);
if(rp == NULL )
printf("該詞不在關(guān)鍵詞詞庫內(nèi)\n");
else
{
i=rp->idx;
printf("%s有%d個搜索結(jié)果\n是否需要顯示索引號(yes=1/no=0):",qustword,i);
scanf("%d",&need);
if(need)
{
num=rp->idx;
dp=rp->down;
for(i=0;i<num;i++,dp=dp->down)
{
printf("\n書號,段號,行號\n%d,%d,%d\n",dp->idxnum[0],dp->idxnum[1],dp->idxnum[2]);
prt(dp->idxnum[0],dp->idxnum[1]);
}
}//if
}//else
}//if
else
printf("該詞為平凡詞\n");
printf("是否繼續(xù)搜索(yes=1/no=0):");
scanf("%d",&need);
}//while(need)
return;
}
//-----------------------------------------------------------------------------------------------------------
void main()
{
headnode *head;
normal *norH;
head=(headnode *)malloc(sizeof(headnode));
norH=(normal *)malloc(sizeof(normal));
norH->p = NULL;
norH=readnormal(norH);
head=build(norH,head);
putout(head);
searchkey(norH,head);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -