注册 登录
编程论坛 C++教室

[讨论]一道看似简单但不一定做得出的题目

icelake 发布于 2007-01-19 18:26, 1137 次点击
输入一串字符,分别统计其中英文字母\数字\和其他字符的个数?
要求用C++编; 有方法可以,但最好有具体的程序.
.
14 回复
#2
caiqiufu2007-01-20 10:46
/************************************************************************/
/* 输入一串字符,分别统计其中英文字母\数字\和其他字符的个数?
*/
/************************************************************************/
#include<iostream.h>
#include<stdio.h>
#include <string.h>
void main()
{
int i,ch,k;
int countC=0,countN=0,countA=0;
char str[81];
for( i = 0; (i < 80) && ((ch = getchar()) != EOF)
&& (ch != '\n'); i++ )
str[i] = (char)ch;
for (int j=0;j<i;j++)
{
if ((64<str[j])&&(str[j]<123))
{
countC++;
}
else if((47<str[j])&&(str[j]<58))
{
countN++;
}
else countA++;
}
cout<<countC<<endl;
cout<<countN<<endl;
cout<<countA;
}
#3
icelake2007-01-20 15:30
太强了!!!;能告诉我你的qq吗?(邮箱也行)
#4
caiqiufu2007-01-21 14:47

呵呵
我也是刚才好好开始学啊
QQ:407923240(注明)

#5
tyc6112007-01-21 17:24
以下是引用icelake在2007-1-19 18:26:19的发言:
输入一串字符,分别统计其中英文字母\数字\和其他字符的个数?
要求用C++编; 有方法可以,但最好有具体的程序.
.

写C++程序,请用标准C++写
比如:
头文件是 <iostream>(配合namespace),而不是<iostream.h>(估计你们看的书是国人的吧,老掉牙了)
main函数返回类型是int
用string代替char[],用cin代替getchar()
等等,找一本好的书学习比较重要,另外建议多用库函数

再把你的代码改写一下:
#include<iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
string str;
getline(cin, str);
cout<<"Inputted: \n"<<str<<endl;

size_t countChars = 0;
size_t countDigits = 0;
size_t countOthers = 0;

for (size_t i = 0; i < str.size(); ++i) {
if (isalpha(str[i]))
++countChars;
else if (isdigit(str[i]))
++countDigits;
else
++countOthers;
}
cout<<"Chars: "<<countChars<<", Digits: "<<countDigits
<<", Others: "<<countOthers<<endl;

return 0;
}

[此贴子已经被作者于2007-1-21 17:25:03编辑过]

#6
caiqiufu2007-01-22 08:37
能帮我推荐几篇文章看看关于namespace和main()的吗
我看见msdn上也是我那样写的啊
#7
maplelxf2007-01-22 09:06
学习
#8
dragonfly2007-01-22 09:38
以下是引用icelake在2007-1-19 18:26:19的发言:
输入一串字符,分别统计其中英文字母\数字\和其他字符的个数?
要求用C++编; 有方法可以,但最好有具体的程序.
.

我曾经面试时遇到这样的问题:统计某字符串中各字母重复的个数?要求用最简单最快速的方法,有兴趣的可以试试

#9
一二三四五2007-01-22 10:47
挺标准的
#10
tyc6112007-01-22 12:44
以下是引用caiqiufu在2007-1-22 8:37:58的发言:
能帮我推荐几篇文章看看关于namespace和main()的吗
我看见msdn上也是我那样写的啊

不知道你学习C++使用什么教材,建议使用C++ Primer

#11
raincom2007-01-24 10:44
以下是引用tyc611在2007-1-21 17:24:03的发言:

写C++程序,请用标准C++写
比如:
头文件是 <iostream>(配合namespace),而不是<iostream.h>(估计你们看的书是国人的吧,老掉牙了)
main函数返回类型是int
用string代替char[],用cin代替getchar()
等等,找一本好的书学习比较重要,另外建议多用库函数

再把你的代码改写一下:
#include<iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
string str;
getline(cin, str);
cout<<"Inputted: \n"<<str<<endl;

size_t countChars = 0;
size_t countDigits = 0;
size_t countOthers = 0;

for (size_t i = 0; i < str.size(); ++i) {
if (isalpha(str[i]))
++countChars;
else if (isdigit(str[i]))
++countDigits;
else
++countOthers;
}
cout<<"Chars: "<<countChars<<", Digits: "<<countDigits
<<", Others: "<<countOthers<<endl;

return 0;
}


增加一个提示输入的语句。呵呵。完美一点还是好的不是么?

#include<iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str;
cout<<"Input a word segment press to return to car be over"<<endl; //提示输入
getline(cin, str);
cout<<"Inputted: \n"<<str<<endl;

size_t countChars = 0;
size_t countDigits = 0;
size_t countOthers = 0;

for (size_t i = 0; i < str.size(); ++i) {
if (isalpha(str[i]))
++countChars;
else if (isdigit(str[i]))
++countDigits;
else
++countOthers;
}
cout<<"Chars: "<<countChars<<", Digits: "<<countDigits
<<", Others: "<<countOthers<<endl;

return 0;
}

#12
pinglideyu2007-01-26 17:17
晕哟~~~~~~~
#13
蓝鹰2007-01-26 19:55
赞同5楼的观点
#14
lixang2007-01-26 19:57
以下是引用raincom在2007-1-24 10:44:05的发言:

增加一个提示输入的语句。呵呵。完美一点还是好的不是么?

#include<iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str;
cout<<"Input a word segment press to return to car be over"<<endl; //提示输入
getline(cin, str);
cout<<"Inputted: \n"<<str<<endl;

size_t countChars = 0;
size_t countDigits = 0;
size_t countOthers = 0;

for (size_t i = 0; i < str.size(); ++i) {
if (isalpha(str[i]))
++countChars;
else if (isdigit(str[i]))
++countDigits;
else
++countOthers;
}
cout<<"Chars: "<<countChars<<", Digits: "<<countDigits
<<", Others: "<<countOthers<<endl;

return 0;
}

我在测试你的程序时输入“你好!”马上就不行了!
所以说你的程序能否对汉字也做判断?
但我不会所以想请教?

#15
dlcdavid2007-01-27 11:18
调试看看什么地方出错呢?
1