求教:文本统计问题

wjn161 2007-11-18 03:19:08
请教一个文本统计的问题,用C++编程,要求:
1.从键盘输入一段文字,以‘@’作结束符号
2.统计文字中的文本行数,字母,数字以及其他符号的数量,并在屏幕上显示
...全文
74 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryfdizuo 2007-11-18
  • 打赏
  • 举报
回复
ls的大哥真早啊,哈哈,c语言版:
#include <cstdio>
#include <cctype>
#include <cstdlib>

int main()
{
char ch;
int CharNum = 0, LineNum = 0, digitNum = 0, otherNum = 0;
while ( (ch = getchar()) != '@')
{
switch(ch)
{
case '\n':
LineNum++;
break;
case '1':case '2':case '3':case '4':case '5':
case '6':case '7':case '8':case '9':case '0':
digitNum++;
break;
default:
if (isalpha(ch))
{
CharNum++;
}
else
otherNum++;
break;
}
}

printf("Lines: %d, digits :%d, chars :%d\n", LineNum, digitNum, CharNum);
printf("Others: %d\n", otherNum);

return 0;
}
jixingzhong 2007-11-18
  • 打赏
  • 举报
回复

#include <string>
#include <cctype>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
string line;
int cLine=0, cNum=0, cChar=0, cOther=0, i;

do
{
getline(cin, line);
cLine++;
for (i=0; i<line.length(); i++)
{
if (isdigit(line[i]))
cNum++;
else if(isalpha(line[i]))
cChar++;
else cOther++;
}
} while(line[line.length()-1] != '@');

cout<<"You've input:"<<endl
<<"Lines = "<<cLine<<endl
<<"Number = "<<cNum<<endl
<<"Charactor = "<<cChar<<endl
<<"Others = "<<cOther-1<<endl; // @字符 计数去掉

return 0;
}
wjn161 2007-11-18
  • 打赏
  • 举报
回复
to jixingzhong
谢谢,我明白了。 原来一直弄不懂的就是如何用‘@’结束。
to dizuo
也谢谢你啊,我不早啊,是在通宵弄呢~

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧