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

最后把所有文件编译即可

相关推荐
liulilittle19 小时前
并发与内存安全术语:定义、分类与关联
c++·安全·并发·术语
_wyt00119 小时前
从图到树:9道洛谷基础题带你入门树形结构
c++·
Titan202420 小时前
Linux网络基础知识
linux·服务器·网络·c++·学习
Titan20241 天前
Linux网络学习:套接字、UDP的封装与应用
linux·服务器·开发语言·网络·c++
Felven1 天前
C2. Potions (Hard Version)
c++
Tyler_TXZ1 天前
C++C语言之——树
c语言·c++·算法·
hansang_IR1 天前
【记录】「COCI 2024/2025」四道模拟赛/8.13
c++·算法
旖旎夜光1 天前
LeetCode 30:串联所有单词的子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
星恒随风1 天前
C++ 红黑树封装(二):迭代器、const_iterator 与 operator[] 完整实现 mymap 和 myset
开发语言·c++·状态模式
小小龙学IT1 天前
oneTBB 开源并行编程库深度解析:从工作窃取调度器到 flow graph 实战
c++·开源