-
大小: 5KB文件類型: .c金幣: 1下載: 0 次發(fā)布日期: 2021-06-11
- 語(yǔ)言: 其他
- 標(biāo)簽: 單詞統(tǒng)計(jì)??鏈表??單詞排序??
資源簡(jiǎn)介
對(duì)讀入的某個(gè)文本文件input.txt中,拆出英文單詞,輸出一個(gè)按字典順序排列的單詞表,
結(jié)果輸出在文本文件output.txt中,每個(gè)單詞一行,并在單詞后輸出該單詞出現(xiàn)的個(gè)數(shù),
兩個(gè)字段之間用逗號(hào)分隔。
約定單詞僅由英文字母組成,單詞間由非英文字母分隔,相同單詞只輸出一個(gè),大小寫(xiě)不區(qū)分。
例如文本文件input.txt為:
Hello world.
Hello every one.
Let us go.
則輸出文本文件output.txt為:
every,1
go,1
hello,2
let,1
one,1
us,1
world,1
代碼片段和文件信息
/*版權(quán):福建星網(wǎng)銳捷網(wǎng)絡(luò)有限公司
*作者:XXX?
*功能:實(shí)現(xiàn)從文件中讀取沒(méi)一個(gè)單詞,并對(duì)單詞進(jìn)行統(tǒng)計(jì),
*按字母順序排列存入另一個(gè)文件中
*時(shí)間:2010-9-2?到?2010-9-3*/
#include
#include
#include
typedef?struct?Str{
????struct?Str?*next;
????char???????*str;
????int??????????num;
}f_str;
char?*?p_strs;???????????/*input文件中是所有單詞*/
int????isf_len;??????????/*input文件的長(zhǎng)度*/
/*從文件input中讀取文件內(nèi)容,并返回給函數(shù)f_get_string*/
char?*?f_get_string()
{
????FILE?*?in_file?=?NULL;
????
????in_file?=?fopen(“d:/input.txt“?“rb“);
????if?(in_file?==?NULL)?{
????????printf(“error!“);
????}?else?{
????????fseek(in_file?0?SEEK_END);
????????isf_len?=?ftell(in_file);??????????????????/*得到文件的長(zhǎng)度*/
????????if?(isf_len?!=?-1)?{
????????????fseek(in_file?0?SEEK_SET);??????
????????????p_strs?=?(char?*)malloc(isf_len+100);
????????????memset(p_strs?0?isf_len+100);
????????????fread(p_strs?sizeof(char)?isf_len?in_file);????/*將文件的全部?jī)?nèi)容給p_strs*/
?????????}
????}
????return?p_strs;
}
/*將input文件中所有處理后的單詞存入output文件中*/
void?f_out_string(f_str?*?head)
{
????FILE?*?out_file?=?NULL;
????f_str?*?hp;
????char?*?sstr;
????char?*?sss?=?““;
????
????out_file?=?fopen(“d:/output.txt“?“wb+“);
????if?(out_file?==?NULL)?{
????????printf(“error!“);
????}?else?{
????????hp?=?head;
????????while?(hp?!=?NULL)?{
????????????sstr?=?malloc(20);
????????????if?(strcmp(hp->str?sss)?!=?0)?{??????/*strcmp比較兩字符串的大小*/
????????????????sprintf(sstr?“%s????%d\r\n“?hp->str?hp->num);???/*將字符串型hp->str和整形的hp->num加到字符串sstr*/
????????????????fwrite(sstr?sizeof(char)?strlen(sstr)?out_file);???/*輸出字符串到out_file*/
????????????}
????????????hp?=?hp->next;
????????????free(sstr);
????????}
????????fclose(out_file);
????}
}
/*對(duì)文件中每個(gè)單詞統(tǒng)計(jì),并按按字母順序存儲(chǔ)到鏈表中*/
f_str?*?order_str(f_str?*?head?f_str?*?fstr?int?i)
{
????f_str??*?hp?=?NULL;
????
????if?(i?==?1)?{
????????head?=?fstr;
????????fstr->next?=?NULL;
????}?else?{
????????hp?=?head;
????????while?(hp?!=?NULL)?{
????????????if?(strcmp(hp->str?fstr->str)?>?0)?{?????/*strcmp比較兩字符串的大小*/
????????????????head?=?fstr;
????????????????fstr->next?=?hp;
????????????????break;
????????????}?else?if?(strcmp(hp->str?fstr->str)?0
????????????????&&?strcmp
評(píng)論
共有 條評(píng)論