zoukankan      html  css  js  c++  java
  • 【C++ 流类库与输入输出 】实验七

    1. 1. 基础练习 (1)教材习题 11-7 (2)教材习题 11-3 (3)教材习题 11-4

      2. 应用练习 (1)已知有班级名单文件 list.txt(见实验 7 附件包)。编写一个应用程序实现随机抽点 5 位同学,在屏幕上显示结果,同时,也将结果写入文件 roll.txt。 ① 编写程序实现题目基本功能要求。(必做) ② ******选做******) 对①中实现的基本功能进行完善、扩充,使得这个点名应用程序更灵活、更方便。比如: a) 从键盘输入班级文件名,支持对不同班级的点名操作; b) 从键盘输入用于保存点名结果的文件名。更灵活地,自动获取当前系统日期作为文 件名,比如 20180612.txt。(如果希望更细粒度,文件名可以到小时和分钟这一层级); c) 随机抽点人数不固定,通过键盘按键控制何时抽点结束; d) 通过菜单及程序的函数模块划分,或类的设计与实现,做成一个更完善的应用,等 等。 (2)统计英文文本文件字符数、单词数、行数,文件名由键盘输入。 ① 编写 C++程序实现题目基本功能要求。(必做) ② ******选做****** a) 提供菜单,由用户选择统计内容; b) 思考当文本内容数量级偏大,①处已实现的程序能否胜任,实现快速统计?在算法 和处理逻辑上是否存在进一步改进的部分?

    2. 解答:
      1. 11-3
         1 #include<iostream>
         2 #include<fstream>
         3 using namespace std;
         4 int main(){
         5     ofstream out("test1.txt");     
         6     if(!out) {
         7         cout << "fail to open." << endl;
         8         return 1;
         9     }
        10      out<<"已成功写入文件!"<<endl;
        11      out.close(); 
        12     
        13     return 0;
        14 } 

      2.  11-4:

         1 #include <iostream>
         2 #include <fstream>
         3 #include <string>
         4 using namespace std;
         5 int main() {
         6 
         7     string s;
         8     ifstream in("test1.txt");
         9     if(!in) {
        10         cout << "fail to open." << endl;
        11         return 1;
        12     }
        13     in >>  s;
        14     cout << s;
        15     in.close();
        16     return 0;
        17 }

      3. 11-7

        // p510, ex11-7
        #include <iostream> 
        using namespace std;
        
        int main() {
        
            ios_base::fmtflags original_flags=cout.flags();//保存现在的参数设置 
            
            cout << 812 << '|';
            cout.setf(ios_base::left, ios_base::adjustfield);//输出左对齐 
            
            cout.width(10);//设置域宽,但是只对第一个输出起作用 
            cout << 813 << 815 << '
        ';
            
            cout.unsetf(ios_base::adjustfield);//清除左对齐格式 
            cout.precision(2);// 两位浮点数 
            cout.setf(ios_base::uppercase|ios_base::scientific);// 科学计数法 
            cout << 831.0;
            cout.flags(original_flags);    // 恢复原来的格式设置 
        
            
            return 0;
        } 

      4. 这道题我分别采用三种方式读入老师所给的文件,但是读不进来。测试输出的都是空格乱码。我将list.txt文件和代码放在同一目录下,但是,就是读入不了啊。然后我就自己新写了一个test1.txt文件,可以正常读取输出。老师所给的文件有什么特殊输入,特殊字符吗?一定要文件读入这个list.txt文件是不是在考查哪个知识?遗憾的是,我并没有成功读入文件啊!!!下面给出我自己创建的文件运行
         1 #include<iostream>
         2 #include<string>
         3 #include<cstdlib>
         4 #include<fstream>
         5 using namespace std;
         6 struct student{
         7         string order;
         8       string xuehao;
         9         string name;
        10         string classname;        
        11 };
        12 int main(){
        13     student s[83];
        14     ifstream infile("test1.txt");
        15     if(!infile)                 //测试是否成功打开  
        16     {  
        17         cerr<<"open error!"<<endl;  
        18        return 0;  
        19     }  
        20     /*利用行读取来获取文件信息*/
        21     /*int i=0;
        22      char c[83];
        23      
        24      while(!infile.eof()){
        25          infile.getline(c,83);
        26          cout<<c[i]<<endl;
        27          i++;
        28      }
        29      infile.close();  //利用行读入进行文件读取*/ 
        30      
        31      
        32       /*仿照课件员工工资读取方式读取*/  
        33   /*  while(!infile.eof())  
        34     {  
        35         infile.read(reinterpret_cast<char*>(&s), sizeof(s));  //只有二进制才能这样读取吗? 
        36      if(infile) {
        37             cout << s.order << " " << s.xuehao << endl; 
        38         }
        39     }  
        40     infile.close(); */
        41     
        42      int i=0;
        43     while(infile>>s[i].order>>s[i].xuehao>>s[i].name>>s[i].classname)
        44     {
        45         i++;
        46     } 
        47     infile.close(); 
        48     int a=1;
        49     ofstream out("roll.txt");
        50     for(int i=1;i<=5;++i)
        51     {
        52          a=rand()%i+1;
        53         cout<<s[a].order<<" "<<s[a].xuehao<<" "<<s[a].name<<" "<<s[a].classname<<endl;
        54         out<<s[a].order<<" "<<s[a].xuehao<<" "<<s[a].name<<" "<<s[a].classname<<endl;
        55     }
        56     out.close();
        57     
        58    return 0;
        59 }

        1. 最后的蓝色部分也是一行输出,由于我定义的s数组为100,但是文件里面只有5个名字,所以输出了其中没有值的一项。

        2. 写入的文件截图如下
        3. 利用实验七里面的list.txt生成截图如下:
        4. SYSTEMTIME sys;
          GetLocalTime(&sys);
          cout<<sys.wYear<<sys.wMonth<<sys.wDay<<sys.wHour<<sys.wMinute<<sys.wSecond<<endl;可以读取当前时间,头文件#include<windows.h>,我的想法是将时间强制转化为string,进行字符串的连接之后,形成一个string h,用h 命名文件名字。强制类型转化失败了。

      5. 读取单词
        #include<string.h>
        #include<fstream>
        #include<iostream>
        using namespace std;
        int main(){
            ofstream out("words");
        
           out<<"you are so much kind!"<<endl;
           out<<"guss this can be successd"<<endl;
           out.close(); //手动创建文件
           
           /*cout<<"Please Enter filename:
        ";//在命令行窗口输入文件 
             string filename;
             cin>>filename;
              ifstream in(filename.c_str()) ;*/ 
           
           
           
        ifstream in("words");
            long linenum=0,chnum=0,wordnum=0;
            char str[1000];
            while(in.getline(str,1000)){
                for(int i=0;i<strlen(str);i++)
                {
                    chnum++;
                    if(str[i]==' '||str[i]==','||str[i]=='!')
                    wordnum++;
                }
                linenum++;
            }
            cout<<"行数:"<<linenum<<endl<<"字符数:"<<chnum<<endl<<"单词数:"<<wordnum<<endl; 
            in.close();
            return 0;
        } 

         

  • 相关阅读:
    【前端基础】webpack 概述
    Temporary Post Used For Theme Detection (1f3683c2-c37f-4fb8-ba3f-6a33842053d2
    分布式场景实用技术
    Linq中的SingleOrDefault和FirstOrDefault
    笔记:SpringBoot教程快速入门-基础-SpringBoot简介&快速入门案例(一)
    使用nacos的配置中心报错Ignore the empty nacos configuration and get it based on dataId
    Shell 命令
    django orm 笔记
    memoize
    once函数
  • 原文地址:https://www.cnblogs.com/yitou13/p/9204554.html
Copyright © 2011-2022 走看看