标题:[答案]统计字母的使用频率.简易版
只看楼主
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
得分:0 
回复:(野比)[答案]统计字母的使用频率.简易版

Using a map is a very good approach for handling this kind of stuff. However, if you don't want to use STL, you can use an array.

/*---------------------------------------------------------------------------
File name: LetterStats.cpp
Author: HJin
Created on: 7/2/2007 20:36:21
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:
===========================================================================

Analysis:
---------------------------------------------------------------------------

Sample output:
---------------------------------------------------------------------------
A: 1
B: 1
C: 1
D: 0
E: 0
F: 0
G: 1
H: 2
I: 2
J: 0
K: 0
L: 2
M: 1
N: 4
O: 1
P: 1
Q: 0
R: 0
S: 2
T: 1
U: 0
V: 0
W: 2
X: 0
Y: 0
Z: 0
a: 8
b: 3
c: 8
d: 7
e: 18
f: 7
g: 3
h: 5
i: 16
j: 2
k: 0
l: 2
m: 6
n: 12
o: 18
p: 3
q: 0
r: 14
s: 11
t: 16
u: 7
v: 2
w: 1
x: 0
y: 4
z: 0
Press any key to continue . . .

Reference:

*/

#include <iostream>
#include <fstream>

using namespace std;

/** content of a.txt

WASHINGTON (CNN) -- President Bush commuted Monday the prison term of
former White House aide I. Lewis "Scooter" Libby, facing 30 months in
prison after a federal court convicted him of perjury, obstruction of
justice and lying to investigators.

*/


int main(int argc, char** argv)
{
/**
c[0..25] for letter A..Z
c[26..51] for letter a..z
*/
int c[52];
int i;

for(i=0; i<52; ++i)
c[i] = 0;

ifstream ifs("a.txt");
if(!ifs)
{
cout<<"cannot open file a.txt.\n";
exit(0);
}

char ch;

while(ifs.get(ch))
{
if(ch>='A' && ch <= 'Z')
++c[ch-'A'];

if(ch>='a' && ch <= 'z')
++c[ch-'a'+26];
}
ifs.close();

for(i=0; i<26; ++i)
{
cout<<char('A'+i)<<": "<<c[i]<<endl;
}

for(i=26; i<52; ++i)
{
cout<<char('a'+i-26)<<": "<<c[i]<<endl;
}

return 0;
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-03 11:39
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
得分:0 
呵呵, 使用map肯定完全超出了这题考试的范围, 不过对学习C++的考生来说是一个拓宽思路, 提高能力的好机会

女侠,约吗?
2007-07-04 12:42
hehe214
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-6-28
得分:0 
哥我用了你这个程序 链接之后输入字母 使用频率表里字母频率都是0  这是什么问题 麻烦你告诉我一下怎么改  明天就用了 !!谢谢了
2010-06-28 10:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-151674-1-1.html




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

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