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:

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

#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

#include <iostream>
#include <cstdlib>
using namespace std;

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

 

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

windows:

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

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

最后把所有文件编译即可

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿Eason

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值