windows/mac/linux 用C++搭建一个刷题模拟器

一个刷题模拟器,一定要有题,题目从这里下载

欢迎大家提供新题目(请把题目文件夹压缩成zip后发给我chengyixuan130812@163.com),我会定期更新的。

题目格式:

题目编号

nums.txt(评测点个数)

题目描述.txt(题目描述)

1_in.txt(第1个测试点的输入)

1_out.txt(第1个测试点的输出)

......

n_in.txt(例如n=10,文件名为10_in.txt)

n_out.txt(例如n=10,文件名为10_out.txt)

下面是正文:

我把这个项目分成了几块,分别是:显示题目描述、编译程序、评测

最后我还做了一个"开始做题"的程序

先在你的工程里建一个文件夹,把题目和程序放在这个目录下。注意,最后编译的时候请在目录下编译

先把要做的题号放在了"题号.txt"文件里

这样显示题目描述就简单了,只要把题号读出来,再找到文件夹里的"题目描述.txt"并输出就好了

题目描述.cpp

windows:

cpp 复制代码
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream fin("\\题号.txt");
    string num;
    fin >> num;
    fin.close();
    fin.open("\\" + num + "\\题目描述.txt");
    string s;
    while(getline(fin,s))
    {
        cout << s << endl;
    }
    return 0;
}

mac/linux:

cpp 复制代码
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream fin("./题号.txt");
    string num;
    fin >> num;
    fin.close();
    fin.open("./" + num + "/题目描述.txt");
    string s;
    while(getline(fin,s))
    {
        cout << s << endl;
    }
    return 0;
}

我把题目程序放在了test.cpp文件里

如果想要在终端编译程序,可以输入g++ -o main main.cpp(这里要编译的文件是main.cpp,而编译出来的文件是main/main.exe)

在程序里只要使用system("g++ -o main main.cpp");就能编译成功了

make.cpp

cpp 复制代码
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    system("g++ -o test test.cpp");
    return 0;
}

最后评测环节,把评测点读入到程序里,把它存在一个文件里,与评测点的输出做比较,如果相同那么就对了,我结果记录在一个"检测结果.txt"文件里

windows:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ifstream fin("题号.txt");
    string ti;
    fin >> ti;
    fin.close();
    fin.open(ti + "\\nums.txt");
    int nums;
    fin >> nums;
    fin.close();
    int cnt = 0;
    system("./make.exe");
    ofstream fout("检测情况.txt",ios::trunc);
    for(int i = 1;i <= nums;i++)
    {
        string dic = "test_" + to_string(i) + ".out";
        freopen(dic.c_str(),"w",stdout);
        string s = ti + "\\" + to_string(i) + "_in.txt";
        freopen(s.c_str(),"r",stdin);
        system("./test");
        fclose(stdin);
        ifstream fin1(dic);
        ifstream fin2(ti + "\\" + to_string(i) + "_out.txt");
        string s1,s2;
        bool flag = true;
        while(getline(fin1,s1) && getline(fin2,s2))
        {
            if(s1 != s2)
            {
                flag = false;
            }
        }
        fout << to_string(i) << ":";
        if(flag)
        {
            fout << "AC" << endl;
            cnt++;
        }
        else
            fout << "WA" << endl;
        fclose(stdout);
    }
    printf("你得到了%d分",(int)floor(1.0 * cnt / nums));
    return 0;
}

mac/linux:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ifstream fin("./题号.txt");
    string ti;
    fin >> ti;
    fin.close();
    fin.open(ti + "/nums.txt");
    int nums;
    fin >> nums;
    fin.close();
    int cnt = 0;
    system("./make");
    ofstream fout("./检测情况.txt",ios::trunc);
    for(int i = 1;i <= nums;i++)
    {
        string dic = "./test_" + to_string(i) + ".out";
        freopen(dic.c_str(),"w",stdout);
        string s = "./" + ti + "/" + to_string(i) + "_in.txt";
        freopen(s.c_str(),"r",stdin);
        system("./test");
        fclose(stdin);
        ifstream fin1(dic);
        ifstream fin2("./" + ti + "/" + to_string(i) + "_out.txt");
        string s1,s2;
        bool flag = true;
        while(getline(fin1,s1) && getline(fin2,s2))
        {
            if(s1 != s2)
            {
                flag = false;
            }
        }
        fout << to_string(i) << ":";
        if(flag)
        {
            fout << "AC" << endl;
            cnt++;
        }
        else
            fout << "WA" << endl;
        fclose(stdout);
    }
    printf("你得到了%d分",(int)floor(1.0 * cnt / nums));
    return 0;
}

最后把所有文件编译即可

相关推荐
郭涤生几秒前
C++中设置函数与回调函数设值的性能差异及示例
开发语言·c++
mjhcsp几秒前
C++ 爬山算法(Hill Climbing):局部搜索(Local Search)的核心解析
c++·爬山算法
柏木乃一3 分钟前
Linux线程(7)基于策略模式的日志模块
linux·运维·服务器·c++·线程·策略模式
TrueDei3 分钟前
linux-C/C++主子进程同时占用主进程文件描述符问题
linux·c语言·c++
仰泳的熊猫9 分钟前
题目2266:蓝桥杯2015年第六届真题-打印大X
数据结构·c++·算法·蓝桥杯
cui_ruicheng28 分钟前
C++ 数据结构:AVL树原理与实现
数据结构·c++
小龙报40 分钟前
【数据结构与算法】环与相遇:链表带环问题的底层逻辑与工程实现
c语言·数据结构·c++·物联网·算法·链表·visualstudio
mmz12071 小时前
贪心算法(c++)
c++·贪心算法
vx-程序开发1 小时前
springboot具备推荐和预警机制的大学生兼职平台的设计与实现-计算机毕业设计源码17157
java·c++·spring boot·python·spring·django·php
tankeven1 小时前
HJ127 小红的双生串
c++·算法