【making tools】:查找一篇文章里面中文有多少个?
2016-03-05 16:40
330 查看
这个程序起因来自于我自己的想法。
以前写作文要写满800字,但是符号也占了字数,所以我想统计是否是中文真的写了800字,因此写这个程序
思路:
思路是这样子的,通过文件依次读取,可以获得每一个字节,其中中文是占两个字节的,而中文的ASCII码通常都是负的,因此,可以据此写出统计代码,其中我的中文都是存储在结构体里面的,统计也是通过对结构体进行二重循环做的,牵涉的小技巧有布尔数组定义之类的。。。
txt文件:
源码:
结果:
统计出了各种汉字的分布,以及多少,还统计出了字节数以及符号
以前写作文要写满800字,但是符号也占了字数,所以我想统计是否是中文真的写了800字,因此写这个程序
思路:
思路是这样子的,通过文件依次读取,可以获得每一个字节,其中中文是占两个字节的,而中文的ASCII码通常都是负的,因此,可以据此写出统计代码,其中我的中文都是存储在结构体里面的,统计也是通过对结构体进行二重循环做的,牵涉的小技巧有布尔数组定义之类的。。。
txt文件:
源码:
[code]#include <iostream> #include <fstream> using namespace std; struct ch{ char temp[3]; int num; }C[10000]; bool trek[10000]; int select[10000]; int word[256]; void main(){ char num[10000]; int int_res[300]; int hanzi = 0; int fuhao = 0; memset(int_res,0,sizeof(int_res)); memset(trek,true,sizeof(trek)); memset(select,0,sizeof(select)); memset(word,1,sizeof(word)); char char_res[256]; int count = 0,ch_count=0; fstream f("d:\\郑勇军.txt",ios::in); if(!f) cout<<"mistake"; while(f>>num[count]&&!f.eof()){ // cout<<num[count]<<' '; int a = (int)num[count]; if(a>0){ int_res[a]++; // cout<<int_res[a]<<endl; //输出某位置 } count++; } cout<<endl; for(int i=0;i<count;i++){ //count代表了字节数 int k = (int)num[i]; if(k>0){ cout<<num[i]<<" "<<int_res[num[i]]<<endl; fuhao++; //统计符号 } else{ C[ch_count].temp[0] = num[i]; C[ch_count].temp[1] = num[i+1]; C[ch_count].temp[2] = '\0'; cout<<C[ch_count].temp<<endl; ch_count++; i++; hanzi++; for(int t=0;t<ch_count;t++){ if(strcmp(C[t].temp,C[ch_count-1].temp)==0 && t!=ch_count-1){ select[t]++; trek[ch_count-1] = false; break; } else if(t==ch_count-1){ select[ch_count-1]++; break; } } } //cout<<i<<" "<<(int)num[i]<<endl; } cout<<endl<<endl<<endl; for(int m=0;m<ch_count;m++){ if(trek[m]){ cout<<C[m].temp<<" "<<select[m]<<endl; } } cout<<endl<<endl<<endl; cout<<"汉字:"<<hanzi<<" 符号:"<<fuhao<<endl; cout<<endl<<"字节数:"<<count<<endl; //count代表了字节数 }
结果:
统计出了各种汉字的分布,以及多少,还统计出了字节数以及符号
相关文章推荐
- bzoj4419 发微博
- iOS绘图与滤镜
- BP神经网络的反向求导
- 【Android】侧滑菜单
- 蓝桥杯之平时小练三羊献瑞
- 51nod 1384 全排列 (dfs)
- OpenCV学习笔记_调用摄像头录制视频(无声音)
- cocos2d-x学习文章
- 问题
- 二进制 八进制 十进制 十六进制 之间的转换
- 加油站问题 (优先队列)
- PL/SQL database character set(AL32UTF8) and Client character set(ZHS16GBK) are different
- “笨办法学python”学习笔记-习题5
- 简单讲解AngularJS的Routing路由的定义与使用
- iOS多控制器-视图切换.
- [FAQ04528]如何在静音模式下也开启拍照音
- 出现报错duplicate sumbols for architecture armv7 或者linker command failed with exit code 1
- Java之矩阵转置
- 泛型理解
- h.264 scanning process for transform coefficients