一、关键是思路, 单词, 行,字符,数字, 他们有什么特点:
1、单词,标准的是遇到空格后,单词数,自动加一。
2、行是以\n结束的, 也就是说, 遇到\n行数加一,当然也视你的操作系统而言, windows的话,就是这样。
3、字符, 空格是否记在里面? 等因素需要考虑。
4、数字,这个的统计,通过ascll表, 或者,字符,都能很好的解决。
二、例程:
#include
#include
using namespace std;
class txt
{
public:
txt(vectorcha){ch = cha;}
void count_word(); //统计单词数
void count_line(); //统计行数
void count_ch(); //统计字符数
private:
vectorch;
};
void txt::count_line()
{
int count = 0;
for (int i=0;i < ch.size();i++)
{
if ('\012' == ch[i])
count++;
}
cout << "总行数:" << count <}
void txt::count_word()
{
int count = 0;
bool v=true;
for (int i=0;i < ch.size();i++)
{
if (!(ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
{
if (v)
count++;
v=false;
}
else
v=true;
}
cout << "单词数:" << count <}
void txt::count_ch()
{
int count = 0;
for (int i=0;i < ch.size();i++)
{
if ('\012' != ch[i])
count++;
}
cout << "字符数:" << count <}
int main()
{
vectorwords;
cout << "请输入多行文本,空行结束:" <char word;
//输入文本,不忽略空格和换行
cin.unsetf( ios::skipws );
while (cin >> word )
{
//判断如果输入的是数字,则返回
if (word >= '0' && word <= '9')
{
cout << "对不起,您输入的字符中含数字。" << endl;
return -1;
}
words.push_back(word);
int i = words.size();
if (words[i-2] == '\012' && words[i-1] == '\012')
{
break;
}
}
//删除最后一个空行,因空行代表结束
words.pop_back();
//定义类对象
txt t(words);
t.count_line();
t.count_word();
t.count_ch();
return 0;
}
#include
//判断是否换行
int isRow(char c,int &bz_r)
{
if(c=='\n')
{
bz_r++;
if(bz_r==1)
return 1;
else
return 0;
}
else
{
if(bz_r>0)
bz_r=0;
return 0;
}
}
//判断是否单词
int isWord(char c,int &bz_w)
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
{
bz_w++;
if(bz_w==1)
return 1;
else
return 0;
}
else
{
if(bz_w>0)
bz_w=0;
return 0;
}
}
//判断是否字符
int isLetter(char c)
{
if((c<'0'||c>'9')&&c!='\n')
return 1;
else
return 0;
}
int main ()
{
int row=0,word=0,letter=0;//初始化总行数 ,单词数 ,字符数
int bz_r=0,bz_w=0;//行连续出现次数 ,英文字符连续出现次数
char c;
while(bz_r<2)//如果不是空行则循环
{
c=getchar();
row+=isRow(c,bz_r);
word+=isWord(c,bz_w);
letter+=isLetter(c);
}
printf("总行数:%d\n",row);
printf("单词数:%d\n",word);
printf("字符数:%d",letter);
return 0;
}
#include
#include
using namespace std;
class txt
{
public:
txt(vector
void count_word(); //统计单词数
void count_line(); //统计行数
void count_ch(); //统计字符数
private:
vector
};
void txt::count_line()
{
int count = 0;
for (int i=0;i < ch.size();i++)
{
if ('\012' == ch[i])
count++;
}
cout << "总行数:" << count <
void txt::count_word()
{
int count = 0;
bool v=true;
for (int i=0;i < ch.size();i++)
{
if (!(ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
{
if (v)
count++;
v=false;
}
else
v=true;
}
cout << "单词数:" << count <
void txt::count_ch()
{
int count = 0;
for (int i=0;i < ch.size();i++)
{
if ('\012' != ch[i])
count++;
}
cout << "字符数:" << count <
int main()
{
vector
cout << "请输入多行文本,空行结束:" <
//输入文本,不忽略空格和换行
cin.unsetf( ios::skipws );
while (cin >> word )
{
//判断如果输入的是数字,则返回
if (word >= '0' && word <= '9')
{
cout << "对不起,您输入的字符中含数字。" << endl;
return -1;
}
words.push_back(word);
int i = words.size();
if (words[i-2] == '\012' && words[i-1] == '\012')
{
break;
}
}
//删除最后一个空行,因空行代表结束
words.pop_back();
//定义类对象
txt t(words);
t.count_line();
t.count_word();
t.count_ch();
return 0;
}