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();
    }
    
  • 相关阅读:
    最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现
    动态规划算法(后附常见动态规划为题及Java代码实现)
    2个字符串的最长公共子串
    VS2010常用快捷键
    错误代码errno值的含义
    几个常用I/O函数用法(printf,fprintf等)
    查看CPU位数的方法
    关于函数指针的总结
    日本标点符号的输入总结
    共享内存及信号量的几个函数介绍
  • 原文地址:https://www.cnblogs.com/KOKODA/p/11029001.html
Copyright © 2011-2022 走看看