您好,欢迎来到99网。
搜索
您的当前位置:首页软件实验报告(4)

软件实验报告(4)

来源:99网
年级 专业 实验名称 实 验 目 的 或 要 求 2011级 计算机科学与技术 班号 计科 5班 学号 姓名 实验 类型 设计型 综合型 √ 创新型 实验二 T4 写一个程序,用他计算一个文件的以下各项统计信息:文件中的字符总数、非空白字符的总数和文件中的字母数。并将统计结果输出到屏幕和另一个文件。 实 验 原 理 ( 算 法 流 程 ) #include #include #include #include #include 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。 最后输出统计结果。 心得体会: 此程序目的是为了让我们更好的了解和运用字符判断函数,同时,降低我们以往对各种字符判断的难度,以方便我们的编程,提高编程效率。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 99spj.com 版权所有 湘ICP备2022005869号-5

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务