实验6 流类库与I/O

实验6 流类库与I/O_第1张图片

实验结论

随机抽取

源码

getTime.h
//
// Created by KOKODA on 2019/6/11.
//

#ifndef SHIYAN6_GETTIME_H
#define SHIYAN6_GETTIME_H

#include 
#include 

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 
#include 
#include 

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 
#include 
#include 
#include 

std::random_device rd;
std::mt19937 gen(rd());

using namespace std;

int main() {
    vector 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;
}

运行截图

实验6 流类库与I/O_第2张图片

实验6 流类库与I/O_第3张图片

统计

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

源码

main.cpp
//
// Created by KOKODA on 2019/6/11.
//
#include 
#include 
#include 

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 == '\n')
            lines++;
        character++;
    }
    cout<

你可能感兴趣的:(实验6 流类库与I/O)