PupilOJ - Chinese
2025-03-23 18:13:33
发布于:北京
PupilOJ
代码
import tkinter as tk
from tkinter import ttk, scrolledtext
import io
import sys
import os
import time
__version__ = "1.0.0"
class OnlineJudgeApp:
def __init__(self, root):
self.root = root
self.root.title("PupilOJ - Easy Online Judge")
self.root.geometry("700x750")
self.name = "add"
self.tinum = "A0001"
self.ti = f"{self.tinum}.A+B problem\n编写一个程序计算两个数的和\n输入格式:\na b\n输出格式:\nans\n输入输出样例:\n输入样例:\n1 2\n输出样例:\n3\n数据范围:\n-10^20 <= a,b <= 10^20"
# 创建主框架
self.main_frame = ttk.Frame(self.root, padding="10")
self.main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# 问题描述区域
self.problem_label = ttk.Label(self.main_frame, text=self.ti)
self.problem_label.grid(row=0, column=0, columnspan=2, pady=5)
# 代码输入区域
self.code_label = ttk.Label(self.main_frame, text="代码: |Python3|")
self.code_label.grid(row=1, column=0, sticky=tk.W, pady=5)
self.code_text = scrolledtext.ScrolledText(self.main_frame, width=80, height=20, wrap=tk.WORD)
self.code_text.grid(row=2, column=0, columnspan=2, pady=5)
# 结果显示
self.output_label = ttk.Label(self.main_frame, text="测试点结果:")
self.output_label.grid(row=3, column=0, sticky=tk.W, pady=5)
self.output_text = scrolledtext.ScrolledText(self.main_frame, width=80, height=5)
self.output_text.grid(row=4, column=0, columnspan=2, pady=5)
# 提交按钮
self.submit_button = ttk.Button(self.main_frame, text="提交代码", command=self.evaluate_code)
self.submit_button.grid(row=5, column=0, columnspan=2, pady=10)
# 状态标签
self.status_label = ttk.Label(self.main_frame, text="状态: NA")
self.status_label.grid(row=6, column=0, columnspan=2)
def load_test_cases(self):
test_files = []
i = 1
while True:
in_file = f"{self.name}{i}.in"
ans_file = f"{self.name}{i}.ans"
if os.path.exists(in_file) and os.path.exists(ans_file):
with open(in_file, "r") as f_in:
inputs = f_in.read().strip().split('\n')
with open(ans_file, "r") as f_ans:
outputs = f_ans.read().strip().split('\n')
test_files.append((in_file, inputs, outputs))
i += 1
else:
break
return test_files
def evaluate_code(self):
self.status_label.config(text="状态: JD")
self.root.update()
time.sleep(0.5)
code = self.code_text.get("1.0", tk.END).strip()
self.output_text.delete("1.0", tk.END)
# 加载所有测试用例文件
test_files = self.load_test_cases()
if not test_files:
self.output_text.insert(tk.END, "Error: no test cases found,please in the pupilOJ folder.\n")
self.status_label.config(text="状态: Unaccepted")
return
all_correct = True
score = 0
for file_name, test_cases, expected_outputs in test_files:
try:
results = []
start = time.time()
flag = False
for test_case in test_cases:
if test_case.strip():
output_buffer = io.StringIO()
sys.stdout = output_buffer
local_vars = {}
a, b = map(int,test_case.split())
local_vars['a'] = a
local_vars['b'] = b
if 'input' in code:
local_vars['input'] = lambda: test_case # 模拟input()函数
exec(code, local_vars)
mid = time.time()
if mid - start > 1:
all_correct = False
flag = True
self.output_text.insert(tk.END, f"{file_name[:-3]}: TLE\n")
break
sys.stdout = sys.__stdout__
result = output_buffer.getvalue().strip()
results.append(result if result else "")
end = time.time()
# 比较结果与标准答案
if end - start > 1 and not flag:
all_correct = False
self.output_text.insert(tk.END, f"{file_name[:-3]}: TLE\n")
elif len(results) != len(expected_outputs) and not flag:
all_correct = False
self.output_text.insert(tk.END, f"{file_name[:-3]}: WA\n")
else:
for i, (result, expected) in enumerate(zip(results, expected_outputs)):
if result.strip() != expected.strip():
all_correct = False
self.output_text.insert(tk.END, f"{file_name[:-3]}: WA\n")
if results == expected_outputs:
self.output_text.insert(tk.END, f"{file_name[:-3]}: AC\n")
score += 10
except Exception as e:
all_correct = False
self.output_text.insert(tk.END, f"{file_name} Error: {str(e)}\n")
self.status_label.config(text="状态: Unaccepted")
break
finally:
sys.stdout = sys.__stdout__
# 更新最终状态
if all_correct:
self.status_label.config(text="状态: Accepted, 得分: %d" % score)
else:
self.status_label.config(text="状态: Unaccepted, 得分: %d" % score)
def main():
root = tk.Tk()
app = OnlineJudgeApp(root)
root.mainloop()
if __name__ == "__main__":
main()
你如何使用它?
你可以使用Python来运行它,但你需要创建一个文件夹,在这个文件夹里将上面的代码复制过去且创建 个文件,名为:add1.in,add1.ans,add2.in,add2.ans,...,add.in,add.ans
代码样图
这里空空如也
有帮助,赞一个