//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
时间: 2024-10-17 20:31:06