【问题标题】:c++ scoring program using python for the classc++评分程序使用python进行类
【发布时间】:2020-06-27 11:53:21
【问题描述】:

我正在尝试制作 python 程序来为用 c++ 编写的作业评分。 我的笔记本电脑操作系统是窗口,所以我安装了 g++ 以使用子进程命令。 这是我的代码。

Python 代码:

import subprocess
import os
import glob
from openpyxl import Workbook



path='C:\\auto_scoring\\'
def write_csv(tc_num,ans):
    score_book=Workbook()

def score(testcases):#scroing function
    correct=True
    for i,testcase in enumerate(testcases):
        if correct:
            print(i+1,":correct")
        else :
            print(i+1,":wrong")


def exe_maker(filename):
    ret=filename.rstrip('.cpp')+'.exe'
    return ret

def execute(file):
    ret=subprocess.check_output('check')


def get_testcase():
    global path
    text_name='test.txt'
    r=open(text_name,mode='r',encoding='utf8')
    text=r.read()
    print(text)

def run():
    file_list = glob.glob('*.cpp*')#cpp

    for file in file_list:
        exe_file=exe_maker(file)
        command='g++ -o '+path+exe_file+" "+path+file
        print(command)
        subprocess.call(command)
        print("start!")
        ret=subprocess.check_output(file.rstrip('.cpp')+" 1").decode('ascii')
        print(ret)


def main():

    run()

def exp():
    ret=subprocess.check_output('check').decode('ascii')

    if ret=='1':
        print('correct')
    else:
        print('wrong')



if __name__=='__main__':
    main()

但我必须使用传递参数,例如 C++ 示例:

int main(int argc,char*argv){

    cout<<argv[1];
}

我不想用这种东西。没有 int argc 和 char argv 的程序。 我只是想检查答案是否正确。 例如)输入:我的名字是k -> 输出:Hello K。 如果我不能制作程序,我必须每 100 个 c++ 文件运行一次...... 我真的需要帮助。 谢谢。

【问题讨论】:

    标签: python c++ scoring


    【解决方案1】:

    尝试使用subprocess.run,将命令行中的每个项目作为列表传递:

    command=['g++', '-o' , exe_file, path + file]
    subprocess.call(command)
    

    【讨论】:

    • 感谢您的评论!调用和运行有什么区别?而且我很好奇有什么方法可以在不使用 C++ 文件的 main(int argc,char*argv) 的情况下传递参数??
    • “我很好奇有什么方法可以在不使用 C++ 文件的 main(int argc,char*argv) 的情况下传递参数?”恐怕我不明白……你是什么意思?
    • 太棒了,我很高兴!随意投票和/或接受答案
    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 2018-09-03
    • 2014-02-02
    • 2023-03-14
    • 2019-08-11
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多