实验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 == '\n')
            lines++;
        character++;
    }
    cout<<words+lines+1<<' '<<character-lines<<' '<<lines+1<<endl;
    cin.get();
    cin.get();
}
优质内容筛选与推荐>>
1、haproxy+keepalived实现web集群高可用性[转]
2、使用dom4j做简单的xml操作
3、PHP 在Swoole中使用双IoC容器实现无污染的依赖注入
4、数据库类型(八)
5、北大光华管理学院院长蔡洪滨:商学院需要有灵魂_网易财经


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号