【发布时间】:2012-02-22 21:14:14
【问题描述】:
这是我的代码:
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
int duze[26];
int male[26];
int n;
//cout<<int('a')<<endl<<int('A');
cin>>n;
char temp;
for (int i=0; i<27; i++)
male[i]=0;
for (int i=0; i<27; i++)
duze[i]=0;
while (n>=0)
{
cin.get(temp);
if (temp=='\n') { n--; continue;}
if (temp==' ') continue;
if (temp>='a' && temp<='z')
male[temp-'a']++;
else if (temp>='A' && temp<='Z')
duze[temp-'A']++;
}
for (int i=0; i<27; i++)
if (male[i]>0) cout<<char(i+'a')<<" "<<male[i]<<endl;
for (int i=0; i<27; i++)
if (duze[i]>0) cout<<char(i+'A')<<" "<<duze[i]<<endl;
//system("pause");
return 0;
}
程序计算给定 n 行文本中的字母。不存在的字母被跳过。 当我在控制台中运行它时,它看起来不错,但我知道字符之前有 EOF 字符...... 如何避免?
【问题讨论】:
-
注意数组索引从
0到N-1,其中N是元素的数量:所有for循环访问1太多。 -
“我知道字符前有EOF字符”。你怎么知道的?
-
哦,没看到 ;)thanx。添加回复,以便我将其标记为正确。 :D
-
@Rob 好吧,相信我,我知道,我 100% 确定:P
-
EOF 字符是什么意思? EOF 不是字符。