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;
}

最后把所有文件编译即可

相关推荐
FL1623863129几秒前
[C++]使用纯opencv部署yolov11-pose姿态估计onnx模型
c++·opencv·yolo
sukalot4 分钟前
windows C++-使用任务和 XML HTTP 请求进行连接(一)
c++·windows
ぃ扶摇ぅ21 分钟前
Windows系统编程(三)进程与线程二
c++·windows
Mr.Z.4111 小时前
【历年CSP-S复赛第一题】暴力解法与正解合集(2019-2022)
c++
Death2001 小时前
使用Qt进行TCP和UDP网络编程
网络·c++·qt·tcp/ip
郭二哈1 小时前
C++——list
开发语言·c++·list
黑不溜秋的2 小时前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光2 小时前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
￴ㅤ￴￴ㅤ9527超级帅3 小时前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
_GR3 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划