using namespace std; void CountChar(ifstream& in_stream,ofstream& out_stream); int main() { char readFileName[20]={'\\0'}; char writeFileName[20]={'\\0'}; ifstream input; ofstream output; cout << \"\\The programming is Statistics character's number in a file!\\n\\n\"; cout << \"Please input read file's name : \"; cin >> readFileName; cout << \"Please input wrote file's name : \"; cin >> writeFileName; input.open(readFileName); if (input.fail()) { cout << \"Read file opening failed\\n\"; exit (1); } output.open(writeFileName); if (output.fail()) { cout << \"Write file opening failed;\\n\";exit(1); } CountChar(input,output); input.close(); output.close(); cout << \"Statistics character success!\\n\" << \"Please check up in \" << writeFileName << endl; return 0; } void CountChar(ifstream& in_stream,ofstream& out_stream) { 实 char ch; 验 原 int char_number=0,no_space=0,alphabetic=0; //用于计算各个类型字符的数理 量 ( in_stream.get(ch); 算 while(!in_stream.eof()) 法 { 流 程 char_number++; //字符加1 ) if(!isspace(ch)) //如果不为空白字符则加1 no_space++; if(isalpha(ch)) //如果为字母则加1 alphabetic++; in_stream.get(ch); } out_stream << \"The sum of char is \" << char_number << endl; out_stream << \"The number of no space is \" << no_space <组 内 分 工 ( 可 选 ) 1. 屏幕显示结果如下: 从文件2_4.txt中读取数据,在d.txt文件中输出。 如果程序成功运行,屏幕上会出现\" Statistics character success!”语 2. 文件读取数据如下: 实 验 结 果 分 析 及 心 得 体 会 任意字符文件即可,用于提供被统计字符。 3. 数据存储文件结果如下:实 验 原 理 ( 算 法 流 程 ) 此函数的主要部分是void CountChar(ifstream& in_stream,ofstream& out_stream)函数程序用它对文件进行处理。 此程序用变量char_number,no_space,alphabetic来分别统计总字符数,非空白字符数和字母数。其中用到了cstdlib 库函数中的空白字符判断函数isspace(),字母判断函数isalpha()。进入While循环,则char_number的值加1。然后用判断与据对读入的字符进行判断,是非空白字符则no_space加1,是字母则alphabetic加1。 最后输出统计结果。 心得体会: 此程序目的是为了让我们更好的了解和运用字符判断函数,同时,降低我们以往对各种字符判断的难度,以方便我们的编程,提高编程效率。