zoukankan      html  css  js  c++  java
  • 实验7流类库和输入输出

    基础练习:

    11-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);//设置输出宽度为10
        cout << 813 << 815 << '
    ';
        cout.unsetf(ios_base::adjustfield);//取消之前调用setf()的效果
        cout.precision(2);
        cout.setf(ios_base::uppercase | ios_base::scientific);//使用科学技术法输出,且对于16进制输出时,使用大写字母,E表示。
        cout << 831.0;
        cout.flags(original_flags);//恢复初始参数
        return 0;
    }

    11-3-4:

    源代码:

    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;
    int main(){
        ofstream os("test1.txt");
        if (os) {
            os << "已成功写入文件!";
        }
        os.close();
        ifstream is("test1.txt");
        if (is) {
            string s1;
            getline(is, s1);
            cout << s1;
        }
        is.close();
        return 0;
    }

    运行效果:

    应用练习:

    (1)

    源代码:

    #include<iostream>
    #include<fstream>
    #include<string>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    struct stu {
        int num;
        string num2, name, room;
    }student[100];
    int main() {
        ifstream is("list.txt");
        if (!is) {
            cout << "ERROR" << endl;
            return 1;
        }
        int i = 0;
        while (is >> student[i].num >> student[i].num2 >> student[i].name >> student[i].room) {
            i++;
        }
        is.close();
        ofstream os("roll.txt");
        srand((unsigned)time(NULL));
        for (int i = 0; i < 5; i++) {
            int m = (rand() % 83);
            cout << student[m].num << " " << student[m].num2 << " " << student[m].name << " " << student[m].room << endl;
            os << student[m].num << " " << student[m].num2 << " " << student[m].name << " " << student[m].room << endl;
        }
        os.close();
        return 0;
    }

    运行效果:

    (2)用getline()函数得出行数line;对每行用getline()得到的字符串s1测长度,即s1.size(),然后将每行的s1长度都加起来,得到字符数;求单词数则可以对每行的字符逐个检查,如果是字母且后一个字符为空格或标点符号,则是单词,将每行的单词数相加,即可得到单词总数。

    源代码:

    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;
    int main() {
        ifstream is("article.txt");
        int line = 0; int num1 = 0; int word = 0;
        if (is) {
            string s1; int m;
            while (getline(is, s1)) {
                m = s1.size();
                line++;
                num1 = num1 + m;
                for (int i = 0; i < m; i++) {
                    if (s1[i] >= 'A'&&s1[i] <= 'Z' || s1[i] >= 'a'&&s1[i] <= 'z') {
                        if (s1[i + 1] >= '!'&&s1[i + 1] <= '/'||s1[i+1]==' '||s1[i+1]>=':'&&s1[i+1]<='?') {
                            word++;
                        }
                    }
                }
            }
        }
        is.close();
        cout <<"行数为:"<< line << " 字符数为:" << num1 <<" 单词数为:"<<word<< endl;
        return 0;
    }

    运行效果:

    文件:

    总结与体会:

    此次实验是目前为止我感觉最为棘手的一次,困扰了整整一天。可能是因为以前没怎么接触过吧,刚开始感觉一筹莫展。我在应用练习的第一题花了大量时间,一开始无论如何都无法输入;我研究了一整个下午,没办法去参考别的同学的答案,换了四五种,却毫无进展;当我最后解决了问题之后,反而忘了最初自己的想法,实在是悲哀。在此我要感谢“MINt超”同学,我看到了他对另一个同学的评论后找到了解决办法,即自己新建一个list.txt,不直接用附件里的list,虽然现在仍不知道为何会出问题。而出了这么一档子事之后对于实验的选做部分我也实在是有心无力了,或许以后能力足够了会回来完善……

  • 相关阅读:
    MongoDB分页处理方案(适用于一般数据库的分页方法)
    linux java cpu 100%
    ueditor
    mysql深入,nginx原理
    spring mvc ajax file upload
    shiro session timeout ajax
    spring 源码解析 pdf
    linux下Nginx+tomcat整合的安装与配置
    Mac 环境下搭建Nginx + Tomcat集群
    easymock 搭建
  • 原文地址:https://www.cnblogs.com/hero-1/p/9201231.html
Copyright © 2011-2022 走看看