您的位置:首页 > 其它

calculate the number of characters-统计文件中的字符数,非空白字符数,字母数,输入到文件和屏幕:

2014-09-02 20:41 471 查看
calculate the number of characters-统计文件中的字符数,非空白字符数,字母数,输入到文件和屏幕:
//calculate the number of characters-统计文件中的字符数,非空白字符数,字母数,输入到文件和屏幕:
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cmath>

int main()
{
using namespace std;
ifstream fin;
ofstream fout;

double ch1 = 0,ch2 = 0,letter = 0;
char tem;

fin.open("infile.dat");
if(fin.fail())
{
cout<<"Input file opening failed.\n";
exit(1);
}
fout.open("outfile.dat");
if(fin.fail())
{
cout<<"Output file opening failed.\n";
exit(1);
}

while(fin.get(tem))
{
ch1 ++;
ch2 ++;
if(tem == ' ')
ch2--;
if((tem >= 'a' && tem <= 'z') || (tem >= 'A' && tem <= 'Z'))
letter++;

}
ch1--;
ch2--;
cout<<"The numbers of character is "<<ch1<<endl;
cout<<"The numbers of character except empty characters is "<<ch2<<endl;
cout<<"The numbers of letter is "<<letter<<endl;

fout<<"The numbers of character is "<<ch1<<endl;
fout<<"The numbers of character except empty characters is "<<ch2<<endl;
fout<<"The numbers of letter is "<<letter<<endl;

fin.close();
fout.close();

return 0;

}
文件:
1 2 3 4 5 6 7 8 9 10 a
输出文件:
To outfile.dat
The numbers of character is 21
The numbers of character except empty characters is 11
The numbers of letter is 1
输出屏幕:
The numbers of character is 21
The numbers of character except empty characters is 11
The numbers of letter is 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐