#include <stdio.h>
#include "SqList.h" // 线性表的存储与操作
int
pickword (
FILE
*f,
char
*fword );
int
main()
{
FILE
*f1, *f2;
const
int
MAX_FILENAME = 20;
char
fname1[MAX_FILENAME] =
"TUT.txt"
;
char
fname2[MAX_FILENAME] =
"result.txt"
;
printf
(
"请指定源文件名(不超过%d个字符):"
, MAX_FILENAME );
scanf
(
"%s"
, fname1 );
printf
(
"请指定要输出结果的文件名(不超过%d个字符):"
, MAX_FILENAME );
scanf
(
"%s"
, fname2 );
if
( ( f1 =
fopen
( fname1,
"r"
) ) == NULL )
{
printf
(
"打开文件%s失败!
"
, fname1 );
return
0;
}
else
printf
(
"打开文件%s成功!
"
, fname1 );
SqList L;
SqListInit ( &L );
char
fword[MAX_CHARACTER];
fword[MAX_CHARACTER - 1] =
' '
;
int
i = -1;
while
( !
feof
( f1 ) )
{
int
judge = pickword ( f1, fword );
if
( -1 == judge )
{
printf
(
"存在单词字符长度超过数组界限
"
);
return
-1;
}
if
( SqListBSearch ( &L, fword, i ) )
{
L.elem[i].count++;
}
else
{
SqListInsert ( &L, i, fword );
}
}
if
( ( f2 =
fopen
( fname2,
"w"
) ) == NULL )
{
printf
(
"写入文件%s失败!
"
, fname2 );
return
0;
}
else
printf
(
"文件已写入%s!
"
, fname2 );
SqListPrint ( f2, fname1, &L );
fclose
( f1 );
fclose
( f2 );
}
int
pickword (
FILE
*f,
char
*fword )
{
char
ch;
for
(
int
j = 0 , flag = 0 ; !
feof
( f ) ; )
{
if
( j >= MAX_CHARACTER )
{
return
-1;
}
ch =
fgetc
( f );
if
( ch >=
'A'
&& ch <=
'Z'
)
{
fword[j++] = ch + 32;
flag = 1;
}
if
( ( ch >=
'a'
&& ch <=
'z'
) )
{
fword[j++] = ch;
flag = 1;
}
if
(
'-'
== ch && fword[j - 1] >=
'a'
&& fword[j - 1] <=
'z'
)
{
fword[j++] = ch;
}
if
( ! ( ( ch >=
'A'
&& ch <=
'Z'
) || ( ch >=
'a'
&& ch <=
'z'
) ||
'-'
== ch )
&& flag == 1 )
{
if
( fword[j - 1] ==
'-'
)
fword[j - 1] =
' '
;
fword[j] =
' '
;
return
0;
}
}
}