| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1137 人关注过本帖
标题:[讨论]一道看似简单但不一定做得出的题目
只看楼主 加入收藏
raincom
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-4-15
收藏
得分:0 
以下是引用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;
}


2007-01-24 10:44
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 
晕哟~~~~~~~

~~我的明天我知道~~
2007-01-26 17:17
蓝鹰
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-8-26
收藏
得分:0 
赞同5楼的观点
2007-01-26 19:55
lixang
Rank: 1
等 级:新手上路
帖 子:231
专家分:0
注 册:2006-7-15
收藏
得分:0 
以下是引用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;
}

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

2007-01-26 19:57
dlcdavid
Rank: 3Rank: 3
来 自:成都
等 级:新手上路
威 望:6
帖 子:193
专家分:0
注 册:2005-12-23
收藏
得分:0 
调试看看什么地方出错呢?

为了C++,我放弃了课本
为了高考,我又放弃了C++
现在而今眼目下,我能做什么?www.
2007-01-27 11:18
快速回复:[讨论]一道看似简单但不一定做得出的题目
数据加载中...
 
   



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

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