字符统计(HuffmanCode的辅助代码)

Code:
  1. //C++file.cpp  
  2. /*为实现统计字符 
  3.  给定字符集G{大小写字母∪( ) , . 和space}共26*2+5=57种字符 
  4.  先用标准字符集给G赋初值 
  5.  将待统计的文章放入article中,用*号标识文章结尾 
  6.  统计时用标准字符集与待统计字母集比对相等时 
  7. */  
  8. #include <fstream>  
  9. #include <iostream>  
  10. #define MAX_CHARS_NUM 58  
  11. #define MAX_ROW 100  
  12. #define MAX_COL 100  
  13. using namespace std;  
  14. char chars[MAX_CHARS_NUM];//存储文章中出现的字符,字符集  
  15. char article[MAX_ROW][MAX_COL];//存储待统计文章  
  16. int frequence[MAX_CHARS_NUM] = {0};//各字符出现的频度,为计算字符概率  
  17. double probty[MAX_CHARS_NUM];//各字符出现的概率  
  18.   
  19. void initchars(ifstream &_fin)  
  20. {  
  21.     for(int i = 0; i < MAX_CHARS_NUM; i++)  
  22.         _fin.get(chars[i]);  
  23. }  
  24.   
  25. void show(char *ch)  
  26. {  
  27.     for(int i = 0; i < MAX_CHARS_NUM; i++)  
  28.         cout << ch[i];  
  29.     cout << endl;  
  30. }  
  31.   
  32. void show(char *ch, int *fre)  
  33. {  
  34.     for(int i = 0; i <MAX_CHARS_NUM; i++)  
  35.         cout << ch[i] << "/t" << fre[i] << endl;  
  36. }  
  37. void show(char (*art)[MAX_COL])  
  38. {  
  39.     for (int i = 0; i < MAX_ROW; i++)  
  40.     {  
  41.         for (int j = 0; j < MAX_COL; j++)  
  42.         {     
  43.             cout << art[i][j];  
  44.             if(art[i][j] == '*')break;//遇到文章结尾  
  45.         }  
  46.         if(art[i][j] == '*')break;//遇到文章结尾  
  47.     }  
  48.     cout << endl;  
  49. }  
  50. void initart(ifstream &_fin)  
  51. {     
  52.     char ch;  
  53.     for(int i = 0; i < MAX_ROW; i++)  
  54.     {  
  55.         for(int j = 0; j < MAX_COL; j++)  
  56.         {  
  57.           _fin.get(ch);  
  58.           article[i][j] = ch;  
  59.         }  
  60.     }  
  61. }  
  62.   
  63.  void statistic(char (*art)[MAX_COL], char *ch)  
  64.  {     
  65.     for(int k = 0; k < MAX_CHARS_NUM; k++)  
  66.      {  
  67.         for (int i = 0; i < MAX_ROW; i++)  
  68.         {  
  69.             for (int j = 0; j < MAX_COL; j++)  
  70.             {     
  71.                 if(art[i][j] == ch[k])  
  72.                     frequence[k]++;  
  73.                 if(art[i][j] == '*')break;//遇到文章结尾  
  74.             }  
  75.             if(art[i][j] == '*')break;//遇到文章结尾  
  76.         }  
  77.      }  
  78.  }  
  79. /*void main() 
  80. { 
  81.     ifstream fin("data.dat"); 
  82.     char ch; 
  83.     while (fin.get(ch)) 
  84.     { 
  85.         cout << ch; 
  86.     } 
  87.     fin.close(); 
  88. }*/  
  89. void main()  
  90. {  
  91.     ifstream fin;  
  92.     fin.open("initdata.dat");  
  93.     initchars(fin);  
  94.     cout << "The Standard Character Set: " << endl;  
  95.     show(chars);  
  96.     fin.close();  
  97.     ifstream _fin;  
  98.     _fin.open("data.dat");  
  99.     initart(_fin);  
  100.     cout << "The article is : " << endl;   
  101.     show(article);  
  102.     statistic(article,chars);  
  103.     cout << "The frequence is: " << endl;  
  104.     show(chars,frequence);  
  105. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值