當前位置:成語大全網 - 書法字典 - 詞典編程

詞典編程

//我就不寫字典了。這是我前幾天寫的,供妳參考。

/*實現:刪除重復單詞

從同壹目錄中讀取文本wordin.txt,對其進行處理,並將其輸出到wordout.txt

*/

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltwindows.h & gt

int delc word(char * temp,char * newstr)//temp是原始字符串,newstr是經過處理的字符串。

{//# include & lt;stdlib.h & gt# include & ltwindows.h & gt//要求頭文件存在壹個隱患:未檢測到newstr的大小。

typedef結構Mystr

{ char * str

Mystr * next

} mystr

char last【100】=““,chlast

int n=0,k=0,flag = 1;

Mystr *head=NULL、*st、*delpt、* lastpt

newstr【0】=“\ 0“;

{ chlast = * temp++;

//從第壹個字母倒數保存,如果遇到非字母則獲取壹個單詞。

if(isalpha(chlast)| | isalnum(chlast)| |‘-‘= = chlast)

{ last【k++】= chlast;最後壹個【k】=‘\ 0‘;}

else { st = head

白色(st)

{ if(!strcmp(ST-& gt;str,last)} { flag = 0;打破;}//與現有單詞比較

ST = ST-& gt;接下來;

}

if(1 = = flag & amp;& ampk & gt0)//如果沒有重復項,它們將存儲在鏈表中。

{ Mystr * pt =(Mystr *)malloc(sizeof(Mystr));

pt-& gt;str =(char *)malloc(sizeof(char)*(k+1);

pt-& gt;next = NULL

strcpy(pt-& gt;str,last);

if(0 = = n)lastpt = head = pt;

else { lastpt-& gt;next = ptlastpt = pt}

n++;

}

flag = 1;//重置標誌位,默認情況下不會與現有字符串重復。

k = 0;

}

} while(chlast);

st =頭部;

while(ST )//復制到字符串

{ strncat(newstr,ST-& gt;str,strlen(ST-》;str);strncat(newstr,““,1);

ST = ST-& gt;接下來;

}

st =頭部;

while(ST)//清理垃圾

{ delpt = ST-& gt;接下來;免費(ST-& gt;str);免費(ST);

st = delpt

}

返回n;

}

void main()

{ char *s2、*s3,ch;

FILE * fp

int I = 0;

if((FP = fopen(“wordin . txt“,“r“)!= NULL)//從文本輸入

{ f SEEK(FP,0,SEEK _ SET);

f SEEK(FP,0,SEEK _ END);

int longBytes = ft ell(FP);//獲取文件長度

f SEEK(FP,0,SEEK _ SET);

S3 =(char *)malloc(sizeof(char)* longBytes+1);

S2 =(char *)malloc(sizeof(char)* longBytes+1);

//fread(S3,longBytes,1,FP);

I = 0;

while((ch = fgetc(FP))!=EOF)

S3【i++】= ch;

S3【I】=‘\ 0‘;

}

fclose(FP);

printf(“\ n元字符串:%s\n“,S3);

printf(“% d:\ n % s“,delc word(S3,s2),S2);//調用函數刪除重復單詞並輸出結果。

if((FP = fopen(“word out . txt“,“w+“)!= NULL)//輸出到文本

fwrite(S2,strlen(S2),1,FP);

fclose(FP);

printf(" \ n ");

免費(S2);

免費(S3);

}