4.27

题目;

题目 14 标准化考试系统设计 【设计要求】功能要求用文件保存试题库。(要求试题类型覆盖选择、判断、 填空等客观题类型)实现试题增加、删除、修改、查询等功能,并要求实现自动 组卷并输出到试卷文件 【界面要求】要求图形界面实现。

代码:

import os
import tkinter as tk
from collections import defaultdict
from tkinter import messagebox, simpledialog
import csv
import random

CSV_FILE_PATH = "questions.csv"


def init_csv_file():
if not os.path.exists(CSV_FILE_PATH):
with open(CSV_FILE_PATH, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["编号", "题干", "类型", "答案"])


def read_all_questions():
"""读取CSV文件中的所有试题信息"""
with open(CSV_FILE_PATH, mode='r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader) # 跳过表头
questions = [row for row in reader]
return questions


def add_question(id, question, qtype, answer):
"""向CSV文件中添加一条新的试题记录"""
with open(CSV_FILE_PATH, mode='a', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([id, question, qtype, answer])


def delete_question(qid):
"""根据试题编号从CSV文件中删除一条试题记录"""
questions = read_all_questions()
updated_questions = [row for row in questions if row[0] != qid]

# 清空原文件并写入更新后的数据
with open(CSV_FILE_PATH, mode='w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["编号", "题干", "类型", "答案"]) # 写入表头
writer.writerows(updated_questions)


def update_question(qid, new_content=None, new_qtype=None, new_answer=None):
"""根据试题编号更新试题内容"""
questions = read_all_questions()
updated = False

for row in questions:
if row[0] == qid:
if new_content is not None:
row[1] = new_content
if new_qtype is not None:
row[2] = new_qtype
if new_answer is not None:
row[3] = new_answer
updated = True

if updated:
# 更新后重新写入文件
with open(CSV_FILE_PATH, mode='w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["编号", "题干", "类型", "答案"]) # 写入表头
writer.writerows(questions)
else:
messagebox.showerror("错误", "未找到指定编号的试题")


def search_question(query):
"""根据查询内容搜索试题,支持题干、类型、答案的模糊搜索"""
questions = read_all_questions()
results = [row for row in questions if query.lower() in (row[1].lower() or '') or
query.lower() in (row[2].lower() or '') or
query.lower() in (row[3].lower() or '')]
return results


def add_question_gui():
question = simpledialog.askstring("新增试题", "请输入题干:")
answer = simpledialog.askstring("新增试题", "请输入答案:")
qtype = simpledialog.askstring("新增试题", "请输入题型:")
new_id = max([int(row[0]) for row in read_all_questions()] or [0]) + 1
add_question(str(new_id), question, qtype, answer)
messagebox.showinfo("成功", "试题添加成功!")


def delete_question_gui():
qid = simpledialog.askstring("删除试题", "请输入试题编号:")
delete_question(qid)
messagebox.showinfo("成功", "试题删除成功!")


def update_question_gui():
qid = simpledialog.askstring("更新试题", "请输入试题编号:")
question = simpledialog.askstring("更新试题", "请输入新的题干(留空则不修改):", initialvalue="")
answer = simpledialog.askstring("更新试题", "请输入新的答案(留空则不修改):", initialvalue="")
qtype = simpledialog.askstring("更新试题", "请输入新的题型(留空则不修改):", initialvalue="")
update_question(qid, new_content=question, new_qtype=qtype, new_answer=answer)
messagebox.showinfo("成功", "试题更新成功!")


def search_question_gui():
query = simpledialog.askstring("查询试题", "请输入查询内容:")
result = search_question(query)
if result:
messagebox.showinfo("查询结果", "\n".join([" ".join(row) for row in result]))
else:
messagebox.showinfo("查询结果", "未找到相关试题")


def generate_paper_gui():
# 用户输入题型和数量信息
question_quantities = defaultdict(int)
question_types = ["选择题", "判断题", "填空题"]
for qtype in question_types:
quantity = simpledialog.askinteger("生成试卷", f"请输入{qtype}的题数:")
question_quantities[qtype] = quantity

generate_paper(question_quantities)


def generate_paper(question_quantities):
questions = read_all_questions()
paper_questions = []
current_question_num = 1

for qtype, quantity in question_quantities.items():
type_questions = [q for q in questions if q[2] == qtype]

if len(type_questions) >= quantity:
selected_questions = random.sample(type_questions, quantity)
for q in selected_questions:
paper_questions.append([current_question_num, q[1]]) # 添加题号并加入试卷
current_question_num += 1
else:
messagebox.showwarning("警告", f"题库中{qtype}的数量不足{quantity},无法生成试卷。")
return

# 写入生成的试卷题干到新文件中
with open("generated_paper.csv", 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["题号", "题干"])
writer.writerows(paper_questions)


def create_widgets():
btn_add = tk.Button(root, text="添加试题", command=add_question_gui)
btn_add.pack(pady=10)

btn_delete = tk.Button(root, text="删除试题", command=delete_question_gui)
btn_delete.pack(pady=10)

btn_update = tk.Button(root, text="更新试题", command=update_question_gui)
btn_update.pack(pady=10)

btn_search = tk.Button(root, text="查询试题", command=search_question_gui)
btn_search.pack(pady=10)

btn_generate_paper = tk.Button(root, text="生成试卷", command=generate_paper_gui)
btn_generate_paper.pack(pady=10)


def gui_init():
global root
root = tk.Tk()
root.title("试题库管理系统")
init_csv_file()
create_widgets()
root.mainloop()


gui_init()
posted @ 2024-06-19 18:03  孙锺鸣  阅读(5)  评论(0编辑  收藏  举报