首页 > 代码库 > calculate the number of characters-统计文件中的字符数,非空白字符数,字母数,输入到文件和屏幕:
calculate the number of characters-统计文件中的字符数,非空白字符数,字母数,输入到文件和屏幕:
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
calculate the number of characters-统计文件中的字符数,非空白字符数,字母数,输入到文件和屏幕:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。