c语言统计汉字重复次数,求助,c语言统计汉字字频最高的字

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#include "frequency.c"

main(){

char article[12000], string[12000];

int n, maxf[1000];

int i;

gets(article);

scanf("%d", &n);

frequency(article, n, string, maxf);

puts(string);

for(i = 0; i < n; i++)printf("%d, ", maxf[i]);

printf("%d", n);

}

frequency.c如下

int frequency(char *article, int n, char *string, int *maxf){

int i, j, k, temp;

int len;/*计算文章总汉字数*/

int max[1000];/*存储由大到小排列的数*/

int count[12000];/*计算每个字的字频*/

char ChineseOnly[12000];/*仅存储汉字*/

char NewChinese[12000];/*存储处理过的汉字*/

char c_temp;

for(i = 0; i < 12000; i++){

count[i] = 0;/*将count全体初始化为0,避免由大到小排序时出现错误*/

ChineseOnly[i] = 0;

NewChinese[i] = 0;

}

strcpy(ChineseOnly, article);

for(i = 0; ChineseOnly[i] != 0; i=i+2){/*过滤全角符号*/

while(ChineseOnly[i] < 0xA0) i++;/*跳过半角字符*/

if(ChineseOnly[i] == 161 && ChineseOnly[i+1] == 161 || ChineseOnly[i] == 163 && ChineseOnly[i+1] == 172 || ChineseOnly[i] == 161 && ChineseOnly[i+1] == 163)

for(i = 0; ChineseOnly[i+1] != 0; i++)

ChineseOnly[i] = ChineseOnly[i+2];/*如果是全角空格,句号,逗号,则去掉,全体向前进位*/

}

for(i = 0; ChineseOnly[i] != 0; i=i+1){/*过滤半角字符*/

if(ChineseOnly[i] < 0xA0){/*如果不是全角字符,则去掉该字符并向前进一位*/

for(j = i; ChineseOnly[j] != 0; j++)

ChineseOnly[j] = ChineseOnly[j+1];

i--;/*退一位判断该位是否为全角字符*/

}

}

strcpy(NewChinese, ChineseOnly);

for(i = 0; NewChinese[i] != 0; i=i+2){

count[i] = 1;

for(j=i+2; NewChinese[j] != 0; j=j+2){

if(NewChinese[i] == NewChinese[j] && NewChinese[i+1] == NewChinese[j+1]){

count[i]++;

for(k = 0; NewChinese[k+1] != 0; k++)/*去掉已被判断的字符并进位*/

NewChinese[k] = NewChinese[k+2];

}

}

}

len = i;/*记录无重复字符串NewChinese中字节总数*/

for(i = 0; i < len; i=i+2){

k = i;

for(j=i+2; j < len; j=j+2)

if(count[i] < count[j])k = j;

temp = count[i]; count[i] = count[j]; count[j] = temp;

c_temp = NewChinese[ i ]; NewChinese[ i ] = NewChinese[ j ]; NewChinese[ j ] = c_temp;

c_temp = NewChinese[i+1]; NewChinese[i+1] = NewChinese[j+1]; NewChinese[j+1] = c_temp;

}/*将count[i]从大到小排列,并对应排列NewChinese*/

while(count[2*n-2] == count[2*n]) n++;/*统计实际n的大小*/

for(i = 0; i < 2*n; i=i+2){

string[i] = NewChinese[i];string[i+1] = NewChinese[i+1];

maxf[i] = count[2*i];

}

string[i-1] = 0;

puts(string);

for(i = 0; i < n; i++)printf("%d, ", maxf[i]);

return n;

}

总是运行不通过,是怎么回事啊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值