| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1046 人关注过本帖, 1 人收藏
标题:统计文件中的字符出现频率,并按照频率出现的高低排序<目前还只能处理ansi字 ...
只看楼主 加入收藏
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
结帖率:99.76%
收藏(1)
已结贴  问题点数:50 回复次数:18 
统计文件中的字符出现频率,并按照频率出现的高低排序<目前还只能处理ansi字符,有测试的朋友请用纯ANSI文件测试><那个T版呢,解释一下那个
程序代码:
#include <stdio.h>

int  main()
{
    struct Count
    {
        int c;
        int count;
    };
    struct Count counter[256];
    int i=0;
    for(i=0;i<256;i++) {counter[i].c=i;counter[i].count=0;}
    FILE* fp;
    fp=fopen("d:\\b.txt","r");
    char flag=0xA;
    int lines=0;
    char tline[256]={'\0'};
    char* cp=tline;
    char tmp;
    while(!feof(fp))
    {
        tmp=getc(fp);
        counter[tmp].count++;
    }
    //sort
    int m,n,tmp2,tmp3;
    for(m=0;m<256;m++)
    {
        for(n=m+1;n<256;n++)
        {
            if(counter[m].count>=counter[n].count)
            {
                tmp2=counter[m].count;
                counter[m].count=counter[n].count;
                counter[n].count=tmp2;
                tmp3=counter[m].c;
                counter[m].c=counter[n].c;
                counter[n].c=tmp3;
            }
        }
    }
    //sort end
    int _chars=0;
    for(i=0;i<256;i++)
    {
        if(counter[i].count>0)
        {
            _chars+=counter[i].count;
            if(counter[i].c==0xA)   //\n
            {
                printf("\\n  Number of occurrences=%d\n",counter[i].count);
            }
            else
                if(counter[i].c==0x20)  //space
                {
                    printf("space Number of occurrences=%d\n",counter[i].count);
                }
                else
                    if(counter[i].c==0x9)  //\t
                    {
                        printf("\\t  Number of occurrences=%d\n",counter[i].count);
                    }
                    else
                        if(counter[i].c==0x0)  //NULL
                        {
                            printf("NULL  Number of occurrences=%d\n",counter[i].count);
                        }
                        else
                            if(counter[i].c==0xD)  //
                            {
                                printf("OxD  Number of occurrences=%d\n",counter[i].count);
                            }
                            else  /*Visible characters*/printf("%c  Number of occurrences=%d\n",counter[i].c,counter[i].count);
        }
    }
    printf("File the total number of characters=%d\n",_chars);
    fclose(fp);
    return 0;
}

/*
w  Number of occurrences=1
V  Number of occurrences=1
O  Number of occurrences=1
I  Number of occurrences=1
E  Number of occurrences=1
:  Number of occurrences=1
9  Number of occurrences=1
1  Number of occurrences=1
#  Number of occurrences=1
!  Number of occurrences=1
g  Number of occurrences=2
U  Number of occurrences=2
F  Number of occurrences=2
D  Number of occurrences=2
C  Number of occurrences=2
A  Number of occurrences=2
'  Number of occurrences=2
_  Number of occurrences=3
>  Number of occurrences=3
3  Number of occurrences=3
*  Number of occurrences=4
L  Number of occurrences=5
<  Number of occurrences=5
6  Number of occurrences=6
5  Number of occurrences=6
x  Number of occurrences=8
N  Number of occurrences=8
%  Number of occurrences=8
b  Number of occurrences=9
2  Number of occurrences=10
d  Number of occurrences=11
h  Number of occurrences=12
,  Number of occurrences=12
+  Number of occurrences=12
}  Number of occurrences=15
{  Number of occurrences=15
l  Number of occurrences=15
a  Number of occurrences=16
\  Number of occurrences=16
/  Number of occurrences=16
0  Number of occurrences=18
"  Number of occurrences=18
)  Number of occurrences=24
(  Number of occurrences=24
p  Number of occurrences=25
s  Number of occurrences=26
m  Number of occurrences=27
]  Number of occurrences=29
[  Number of occurrences=29
.  Number of occurrences=29
f  Number of occurrences=35
=  Number of occurrences=39
;  Number of occurrences=42
\t  Number of occurrences=45
i  Number of occurrences=55
u  Number of occurrences=64
o  Number of occurrences=71
e  Number of occurrences=75
r  Number of occurrences=76
\n  Number of occurrences=78
t  Number of occurrences=90
n  Number of occurrences=94
c  Number of occurrences=95
space Number of occurrences=744
File the total number of characters=2094
*/



[ 本帖最后由 wp231957 于 2013-1-4 22:38 编辑 ]
搜索更多相关主题的帖子: count 测试 
2013-01-04 22:31
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
文件的实际字节数=2094+78=2172
但是c代码读出来的就是2094  包含了78个0AH 丢弃了78个0DH

DO IT YOURSELF !
2013-01-04 22:33
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:10 
插楼失败。。。


[fly]存在即是合理[/fly]
2013-01-04 22:35
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
以下是引用azzbcc在2013-1-4 22:35:04的发言:

插楼失败。。。
别光顾茶楼啊   留点意见再走

DO IT YOURSELF !
2013-01-04 22:39
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:10 
很好很强大,等接分

认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2013-01-04 22:45
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
OAH? ODH? 什么东东? 不清楚。。。

else 和 if 中间没必要换行吧,这样看起来像走楼梯。


[fly]存在即是合理[/fly]
2013-01-04 22:49
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
以下是引用azzbcc在2013-1-4 22:49:28的发言:

OAH? ODH? 什么东东? 不清楚。。。

else 和 if 中间没必要换行吧,这样看起来像走楼梯。
走楼梯是正常的吧  平齐才不对

DO IT YOURSELF !
2013-01-04 22:53
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:0 
万版主,你说的其他字符指什么啊?

认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2013-01-04 22:54
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
回车是一个字符
换行也是一个字符
但是我只能统计到换行字符(\n) 回车字符却统计不到

DO IT YOURSELF !
2013-01-04 22:57
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
收藏
得分:10 
茶楼2号失败...

I have not failed completely
2013-01-04 22:58
快速回复:统计文件中的字符出现频率,并按照频率出现的高低排序<目前还只能处理 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037878 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved