怎样用python开发一个c++的本地OJ练习系统

 

由于在做c++信奥教学,出于某种考虑,可能会用到本地的OJ练习系统。略作修改也可以成为测试系统。用Python开发是比较简单的,不像网络的OJ平台,本地OJ不用考虑太复杂的用户情况,只需处理好最基本的几个问题即可,下面简单介绍一下。

一、源代码编译

源代码编译,是通过直接调用MinGW64实现的。

def makeExe(cpp):
    command= r'MinGW64\bin\g++.exe %s -o %s -m32'
    # command= r'gcc\bin\g++.exe %s -o %s -m32'
    exe= filetool.changeExt(cpp, '.exe')
    command= p1+ "\\"+ command%(cpp, exe)
    # print(command)
    cmd= command.split()
    # print(cmd)
    sp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)      
    out, err = sp.communicate()
    ret= "" 
    if out:      
       ret+= out.decode("utf-8")
    if err:      
        ret+= err.decode("gbk")
    ret= ret.strip()
    return ret

二、运行EXE,给与输入并得到结果

# 测试exe文件的执行,并用对应的测试文件.in/.out进行测试
def testExe(exe, din, dout):
    cmd=[exe]
    f1= open(din)
    txtin= filetool.readText(din)
    f2= filetool.readText(dout).rstrip()
    try:
        sp = subprocess.Popen(cmd, shell=True, stdin= f1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)      
        out, err = sp.communicate(timeout=2)
        if err:      
            ret= err.decode("gbk")
            return 2, ret
    except Exception as e:
        sp.kill()
        str1= "cmd /c taskkill /f /im src.exe"
        subprocess.Popen(str1)
        ret="------ 输入 ------\n"+txtin
        ret+= "\n------标准输出------\n"+f2
        ret+= "\n------用户输出------\n超时!"
        return 1, ret
    ret= "" 
    out= out.decode("gbk").rstrip()
    s1= out.split('\n')
    s2= f2.split('\n')
    a1= len(s1)
    a2= len(s2)
    same= True
    if a1!= a2:
        same= False
    else:
        for i in range(a1):
            l1= s1[i].rstrip()
            l2= s2[i].rstrip()
            if l1!=l2:
                same= False
                break
    if same:
        return 0, ""
    else:
        ret="------ 输入 ------\n"+txtin
        ret+= "\n------标准输出------\n"+f2
        ret+= "\n------用户输出------\n"+out
        return 3, ret

三、界面的管理

界面尽量简单明确即可

 

系统提供多个题单,每个题单包含多道题目,每个题目包括内容描述,代码,当前状态等参数。 

 核心逻辑简述如上,有兴趣的朋友可以直接下载完整的源代码和辅助文件(CSDN下载,锁定5分)。

用python开发的本地OJ练习系统(附几道练习题)-Python文档类资源-CSDN下载开发一个简单的OJ系统,包括编译源代码,运行EXE,得到结果并比较等内容。可以供python爱好者更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/xiaorang/69861928

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

圣手书生肖让

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

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

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

打赏作者

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

抵扣说明:

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

余额充值