zoukankan      html  css  js  c++  java
  • 实验6 流类库与I/O

    实验结论

    随机抽取

    源码

    getTime.h
    //
    // Created by KOKODA on 2019/6/11.
    //
    
    #ifndef SHIYAN6_GETTIME_H
    #define SHIYAN6_GETTIME_H
    
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    string getTime();
    
    #endif //SHIYAN6_GETTIME_H
    
    getTime.cpp
    //
    // Created by KOKODA on 2019/6/11.
    //
    
    #include "getTime.h"
    
    string getTime() {
        time_t now = time(nullptr);
        tm *ltm = localtime(&now);
        if (1 + ltm->tm_mon < 10 && ltm->tm_mday < 10)
            return to_string(1900 + ltm->tm_year) + '0' + to_string(1 + ltm->tm_mon) + '0' + to_string(ltm->tm_mday);
        else if (1 + ltm->tm_mon < 10 && ltm->tm_mday >= 10)
            return to_string(1900 + ltm->tm_year) + '0' + to_string(1 + ltm->tm_mon) + to_string(ltm->tm_mday);
        else if (1 + ltm->tm_mon >= 10 && ltm->tm_mday < 10)
            return to_string(1900 + ltm->tm_year) + to_string(1 + ltm->tm_mon) + '0' + to_string(ltm->tm_mday);
        else
            return to_string(1900 + ltm->tm_year) + to_string(1 + ltm->tm_mon) + to_string(ltm->tm_mday);
    }
    
    students.h
    //
    // Created by KOKODA on 2019/6/11.
    //
    
    #ifndef SHIYAN6_STUDENTS_H
    #define SHIYAN6_STUDENTS_H
    
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class students {
    private:
        int serial;
        string studentID, name, Class;
    public:
        students(int serial, const string &studentId, const string &name, const string &Class);
    
        void print(ofstream &p);
    
        void print();
    };
    
    
    #endif //SHIYAN6_STUDENTS_H
    
    students.cpp
    //
    // Created by KOKODA on 2019/6/11.
    //
    
    #include "students.h"
    
    students::students(int serial, const string &studentId, const string &name, const string &Class) : serial(serial),studentID(studentId),name(name),Class(Class) {}
    
    void students::print(ofstream &p) {
        p << serial << ' ' << studentID << ' ' << name << ' ' << Class << endl;
    }
    
    void students::print() {
        cout << serial << ' ' << studentID << ' ' << name << ' ' << Class << endl;
    }
    
    main.cpp
    //
    // Created by KOKODA on 2019/6/11.
    //
    
    #include "getTime.h"
    #include "students.h"
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <random>
    
    std::random_device rd;
    std::mt19937 gen(rd());
    
    using namespace std;
    
    int main() {
        vector<students> students;
        string listName;
        int amount;
        ofstream out;
        ifstream in;
        cout << "Please enter the name of list:";
        cin >> listName;
        cout << "How many people you want to choose:";
        cin >> amount;
        in.open(listName);
        if (!in.is_open()) {
            cerr << "fail to open file " << listName << endl;
            system("pause");
            exit(0);
        }
    
        int serial;
        string studentID, name, Class;
    
        while (!in.eof()) {
            in >> serial >> studentID >> name >> Class;
            students.emplace_back(serial, studentID, name, Class);
        }
    
        out.open(getTime() + ".txt");
    
        int number;
        for (int i = 0; i < amount; ++i) {
            std::uniform_int_distribution<> dis(0, students.size() - 1);
            number = dis(gen);
            students[number].print(out);
            students.erase(students.begin() + number);
        }
        in.close();
        out.close();
        return 0;
    }
    

    运行截图

    统计

    懒得做很好了,就这样吧。

    源码

    main.cpp
    //
    // Created by KOKODA on 2019/6/11.
    //
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
        string filename;
        ifstream in;
        int words = 0, character = 0, lines = 0;
        cout << "File name:";
        cin >> filename;
        in.open(filename);
        char ch;
        while (in.get(ch)) {
            if (ch == ' ')
                words++;
            if (ch == '
    ')
                lines++;
            character++;
        }
        cout<<words+lines+1<<' '<<character-lines<<' '<<lines+1<<endl;
        cin.get();
        cin.get();
    }
    
  • 相关阅读:
    Java基础教程:对象比较排序
    算法:深度优先搜索
    微信小程序开发:学习笔记[5]——JavaScript脚本
    微信小程序开发:学习笔记[4]——样式布局
    设计模式:学习笔记(8)——装饰器模式
    设计模式:学习笔记(7)——原型模式
    设计模式——单例模式解析
    玩转Android之数据库框架greenDAO3.0使用指南
    Android开发工具——Android Studio调试技巧
    VR开发的烦恼——范围限制
  • 原文地址:https://www.cnblogs.com/KOKODA/p/11029001.html
Copyright © 2011-2022 走看看