在线oj之试题模块

试题模块用于保存试题信息(编号,题目,保存路径,难度),提供接口用于server端调用。

保存题目的数据结构我们采用unordered_map,因为其底层采用hash结构,所以我们在查找题目的时候更快
<k, v> 对应 <题目id, 整个题目信息(question类)>

先介绍下一个具体的题目包含哪几个部分

  • desc.txt:题目描述
  • header.cpp:给出的头文件及函数入口
  • tail.cpp:主函数和测试函数

在server模块我们需要获取desc和header
在运行编译模块我们就需要获取header和tail并组成新的cpp文件

bool LoadQuestions(const std::string& configfile_path);//用于加载所有题目的基本信息
bool GetAllQuestions(std::vector<Question>* ques);//用于获取所有试题的基本信息
bool GetOneQuestion(const std::string& id, std::string* desc, std::string* header, Question* ques); //用于获取单个题目的详细信息,包括题目描述(desc), 给出的头文件(header)
#pragma once
#include "oj_log.hpp"
#include "tools.hpp"
#include <fstream>
#include <unordered_map>
#include <string>
#include <iostream>
#include <algorithm>

typedef struct Question
{

    std::string _id;
    std::string _name;
    std::string _path;
    std::string _star;
}Ques;


class OjModel
{
    public:

        OjModel()
        {
            LoadQuestions("./config_oj.cfg"); 
        }
        bool GetAllQuestions(std::vector<Question>* ques) {
        //将获取的试题信息保存在一个vector数组中
            for (const auto& kv : _model_map) {
                ques->push_back(kv.second);
            }
            //获取试题信息后,按照id编号排序
            std::sort(ques->begin(), ques->end(), [](const Question& le, const Question& ri){
                    return std::atoi(le._id.c_str()) < std::atoi(ri._id.c_str());
                    });
            return true;
        }


        bool GetOneQuestion(const std::string& id, std::string* desc, std::string* header, Question* ques)
        {
            //1.根据id去查找对应题目信息,在哪里加载
            auto iter = _model_map.find(id);
            if (iter == _model_map.end()) {
                LOG(ERROR, "Not Found Question id is") << " " << id << std::endl;
                return false;
            }

            *ques = iter->second;

            //iter->second._path;
            //加载具体的单个题目信息,从保存的路径上面去加载
            //从具体的题目文件当中去获取两部分信息,描述,header
            int ret = FileOpen::ReadDataFromFile(DescPath(iter->second._path), desc);
            if (ret == -1) {
                LOG(ERROR, "Read desc failed") << std::endl;
            }
            ret = FileOpen::ReadDataFromFile(HeaderPath(iter->second._path), header);
            if (ret == -1) {
                LOG(ERROR, "Header desc failed") << std::endl;
            }
            return true;
        }


        bool SplitingCode(std::string user_code, std::string ques_id, std::string* code)
        {
            //1.查找以下对应id的题目是否存在
            auto iter = _model_map.find(ques_id);
            if (iter == _model_map.end()) {
                LOG(ERROR, "can not find questions id is") << ques_id << std::endl;
                return false;
            }

            std::string tail_code;
            int ret = FileOpen::ReadDataFromFile(TailPath(iter->second._path), &tail_code);
            if (ret < 0) {
                LOG(ERROR, "Open tail.cpp failed") << std::endl;
                return false;
            }
            *code = user_code + tail_code;
            return true;
        }
    private:
      //传的参数是路径,用于到具体的路径下寻找对应文件
        std::string DescPath(const std::string& que_path) 
        {
            return que_path + "desc.txt";
        }
        std::string HeaderPath(const std::string& que_path)
        {
            return que_path + "header.cpp";
        }
        std::string TailPath(const std::string& que_path)
        {
            return que_path + "tail.cpp";
        }

        bool LoadQuestions(const std::string& configfile_path)
        {
            //使用C++当中的文件流来加载文件
            //iostream:处理控制台
            //fstream:处理命名文件
            //stringstream:处理string
            //ostream:output文件流
            //istream:input文件流,从文件当中读

            std::ifstream file(configfile_path.c_str());
            if(!file.is_open()){
                return false;
            }

            std::string line;
            while(std::getline(file, line)) {
                //切割字符串
                std::vector<std::string> vec;
                StringTools::Split(line, " ", &vec);
                if (vec.size() != 4) {
                    continue;
                }
                //将切割后的字符放到unordered_map
                Question ques;
                ques._id = vec[0];
                ques._name = vec[1];
                ques._path = vec[2];
                ques._star = vec[3];
                _model_map[ques._id] = ques;
            }
            file.close();
            return true;
        }

    private:
        //试题id,名称,路径,难度
        std::unordered_map<std::string, Question> _model_map;

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值