简单的问题,请进!

purerain 2003-05-20 04:46:28
下面我编的一个统计文本行数,单词数,字母数的程序,但只可以统计英文,如果想统计汉字,怎么办?
能否详细解释一下?

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
fstream f;
f.open("10.txt",ios::out|ios::in);
if(!f)
{
cout<<"can not open the file ,L am sorry!"<<endl;
abort();
}
char ch;
int i=0;
int j=0;
int k=0;
while(f.get(ch))
{
if(ch=='\n')
{
i++;
continue;
}
if(ch==' ')
{
j++;
continue;
}
else
k++;
}
cout<<"hang shu is"<<i+1<<endl;
cout<<"dang ci shu is"<<(j+i)<<endl;
cout<<"zhi mu shu is": "<<k+1<<endl;
}
...全文
30 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiya0 2003-06-30
  • 打赏
  • 举报
回复
能举个例子吗?比如要判断用户输入是否为“是”该怎么写
idontlikenickname 2003-06-29
  • 打赏
  • 举报
回复


一个汉字是两个字符,"对"也一样,
用memcmp()就行~,不要用strcpy(),因为没有'\0'结尾~除非你自己加上~~

yiya0 2003-06-29
  • 打赏
  • 举报
回复
那判断一个汉字和指定汉字是否相同的程序怎么写,
比如,我要判断用户输入的是否是“对”字
idontlikenickname 2003-06-29
  • 打赏
  • 举报
回复


以前写过的一个函数,把其中有用的部分写出来了~~
汉字的ASCII码都大于HZ_CODE(一个宏),且一个汉字占两个ascii码.

#define HZ_CODE 160

//
// 取得中英文混合字符串以英文字符为单位的长度.
// pString - 中英文混合字符串.
// pnELen - 返回英文字符的个数,不能为null.
// pnCLen - 返回中文字符的个数,不能为null.
//
void _far GetStringLengthEx(char *pString, uint *pnELen, uint *pnCLen)
{
uchar ucByteL, ucByteR;
uchar *pByte = pString;
uint nELen = 0, nCLen = 0;

while((ucByteL = *pByte ++) != '\0')
{
if(ucByteL > HZ_CODE)
{
// 取下一个字符串.
ucByteR = *pByte ++;
if(ucByteR == '\0')
{
// 字符串结束.
nELen ++;
break;
}
if(ucByteR > HZ_CODE)
{
// 按汉字处理
nCLen ++;
}
else
{
// 不按汉字处理
nELen += 2;
}
}
else
{
nELen ++;
}
}

*pnELen = nELen; // 返回其中英文字符的个数.
*pnCLen = nCLen; // 返回其中中文字符的个数.
}

自己看吧~

haosjt 2003-06-29
  • 打赏
  • 举报
回复
高手们写一个可以吧,让我们这些人也学习学习!
goldly 2003-06-29
  • 打赏
  • 举报
回复
我也不懂,UP!
three1103 2003-06-29
  • 打赏
  • 举报
回复
我真的不懂,关注
zteliubin 2003-06-25
  • 打赏
  • 举报
回复
判断连续两个字节高位为1 ch & 0x80 == 0x80
purerain 2003-06-25
  • 打赏
  • 举报
回复
up

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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