四则运算题目生成器
软件工程大作业的结对项目–四则运算题目生成器
写的是需求分析中的功能需求第四阶段的过程和内容
需求分析阶段的链接如下
软件工程项目 四则运算表达式生成----1
网页之前的功能需求和设计的链接如下
软件工程项目 四则运算表达式生成----3
网页的后端实现过程如下,贴上队友的链接
软件工程–四则运算表达式(4)
网页的前端实现过程的链接如下
软件工程项目 四则运算表达式生成----4
做题和历史记录的设计过程
做题部分的功能是:
1.接受登录用户的做题请求
2.回传给用户一个表单用于填写做题信息
3.接收用户的表单,从数据库中取出相应的数据并且新建一个History记录保存用户的做题信息
4.给用户回传一个界面用于做题
5.接收用户做题信息并且对用户做题情况进行记录
6.返回用户答题成绩界面
7.可以让用户看到自己做错的题的信息
用例图
类图
顺序图
代码
控制器
from django.urls import path
from . import views
app_name = "Quest_Generator"
urlpatterns = [
path('quest_generator/', views.generator, name="Generator"),
path('answer/<int:num>/<int:answer_id>', views.confirm, name="Answer"),
path('history/', views.youtube, name="History"),
]
用户在前端调用这些函数,跳转到对应页面
表单
from django import forms
class DSform(forms.Form):
qunt = (
('5', "给爷先来5个"),
('10', "少废话再来5个"),
('20', "20个让爷做到爽"),
)
quantity = forms.ChoiceField(label="想来几个题爽", choices=qunt)
optr = (
('2', "简单难度2"),
('3', "简单难度3"),
('4', "简单难度4"),
('5', "中等难度5"),
('6', "中等难度6"),
('7', "中等难度7"),
('8', "小学生都哭了的难度8"),
('9', "小学生都哭了的难度9"),
)
operators = forms.MultipleChoiceField(label="爷来几个符号", choices=optr)
if_pw = (
('True', "要"),
('False', "不要"),
('Both', "爷都要"),
)
if_ng = (
('True', "要"),
('False', "不要"),
('Both', "爷都要"),
)
if_fa = (
('True', "要"),
('False', "不要"),
)
potyp = (
('True', "^"),
('False', "**"),
)
if_pow = forms.ChoiceField(label="乘方", choices=if_pw)
if_neg = forms.ChoiceField(label="负数", choices=if_ng)
if_fra = forms.ChoiceField(label="分数", choices=if_fa)
pow_type = forms.ChoiceField(label="类型", choices=potyp)
class Form5(forms.Form):
Kt1 = forms.CharField(max_length=128)
Kt2 = forms.CharField(max_length=128)
Kt3 = forms.CharField(max_length=128)
Kt4 = forms.CharField(max_length=128)
Kt5 = forms.CharField(max_length=128)
class Form10(forms.Form):
Kt1 = forms.CharField(max_length=128)
Kt2 = forms.CharField(max_length=128)
Kt3 = forms.CharField(max_length=128)
Kt4 = forms.CharField(max_length=128)
Kt5 = forms.CharField(max_length=128)
Kt6 = forms.CharField(max_length=128)
Kt7 = forms.CharField(max_length=128)
Kt8 = forms.CharField(max_length=128)
Kt9 = forms.CharField(max_length=128)
Kt10 = forms.CharField(max_length=128)
class Form20(forms.Form):
Kt1 = forms.CharField(max_length=128)
Kt2 = forms.CharField(max_length=128)
Kt3 = forms.CharField(max_length=128)
Kt4 = forms.CharField(max_length=128)
Kt5 = forms.CharField(max_length=128)
Kt6 = forms.CharField(max_length=128)
Kt7 = forms.CharField(max_length=128)
Kt8 = forms.CharField(max_length=128)
Kt9 = forms.CharField(max_length=128)
Kt10 = forms.CharField(max_length=128)
Kt11 = forms.CharField(max_length=128)
Kt12 = forms.CharField(max_length=128)
Kt13 = forms.CharField(max_length=128)
Kt14 = forms.CharField(max_length=128)
Kt15 = forms.CharField(max_length=128)
Kt16 = forms.CharField(max_length=128)
Kt17 = forms.CharField(max_length=128)
Kt18 = forms.CharField(max_length=128)
Kt19 = forms.CharField(max_length=128)
Kt20 = forms.CharField(max_length=128)
这个部分主要是用户选择题量和题目参数,提交给后端,查询数据库,返回题目给用户
视图
from django.shortcuts import render, HttpResponse
from login.models import User
from .forms import DSform, Form5, Form10, Form20
from SouSouSou.models import History
from SouSouSou.views import get_ls, string_change
import random
from .models import Answer, WrongAnswer
# Create your views here.
def generator(request):
if not request.session.get('is_login', None):
return HttpResponse('您尚未登陆,无法操作')
if request.method == "POST":
form = DSform(request.POST)
if form.is_valid():
quantity = form.cleaned_data['quantity']
if_pow = form.cleaned_data['if_pow']
if_fra = form.cleaned_data['if_fra']
if_neg = form.cleaned_data['if_neg']
potp = form.cleaned_data['pow_type']
operator = form.cleaned_data['operators']
quest = get_ls(operator=operator, if_pow=if_pow, if_neg=if_neg)
user = User.objects.get(id=request.session.get('user_id', None))
i = 0
ot_ls = []
history = History.objects.create()
history.user = user
history.quantity = quantity
history.if_fra = if_fra
history.save()
id = history.id
while i < int(quantity):
rand = random.randint(0, quest.__len__() - 1)
quest[rand].question = string_change(quest[rand].question, potp)
ot_ls.append(quest[rand])
quest.pop(rand)
i += 1
i = 0
while i < int(quantity):
answer = Answer.objects.create()
answer.answer_id = history
answer.question = ot_ls[i]
answer.save()
i = i + 1
if int(quantity) == 5:
g_form = Form5
elif int(quantity) == 10:
g_form = Form10
else:
g_form = Form20
context = {'output_list': ot_ls, 'form': g_form, 'answer_id': id, 'quantity': quantity}
return render(request, 'question/Question_list.html', context)
form = DSform()
context = {'form': form}
return render(request, 'question/Question.html', context)
def confirm(request, num, answer_id):
if request.method == "POST":
if num == 5:
form = Form5(request.POST)
if form.is_valid():
answer = {}
answer[0] = form.cleaned_data['Kt1']
answer[1] = form.cleaned_data['Kt2']
answer[2] = form.cleaned_data['Kt3']
answer[3] = form.cleaned_data['Kt4']
answer[4] = form.cleaned_data['Kt5']
history = History.objects.get(id=answer_id)
if history.end_time != None:
return HttpResponse('您已经提交过,请勿重复提交!')
history.__set_end_time__()
time = history.end_time - history.start_time
as_list = Answer.objects.filter(answer_id=history)
i = 0
s = num
ot_ls = []
while i < answer.__len__():
if history.if_fra == True:
if not answer[i] == as_list[i].question.answer_Fraction:
if WrongAnswer.objects.filter(question=as_list[i].question):
s = s - 1
else:
wrong = WrongAnswer.objects.create()
wrong.question = as_list[i].question
wrong.answer_id = history
ot_ls.append(as_list[i].question)
wrong.save()
s = s - 1
else:
if not float(answer[i]) == float(as_list[i].question.answer_float):
if WrongAnswer.objects.filter(question=as_list[i].question):
s = s - 1
else:
wrong = WrongAnswer.objects.create()
wrong.question = as_list[i].question
wrong.answer_id = history
wrong.save()
ot_ls.append(as_list[i].question)
s = s - 1
i = i + 1
history.score = s
history.save()
context = {'output_list': ot_ls, 'score': s, 'time': time, 'if_fra': history.if_fra}
return render(request, 'question/your_score.html', context)
return HttpResponse('请重新填写表单')
elif num == 10:
form = Form10(request.POST)
print(request.POST)
if form.is_valid():
answer = {}
answer[0] = form.cleaned_data['Kt1']
answer[1] = form.cleaned_data['Kt2']
answer[2] = form.cleaned_data['Kt3']
answer[3] = form.cleaned_data['Kt4']
answer[4] = form.cleaned_data['Kt5']
answer[5] = form.cleaned_data['Kt6']
answer[6] = form.cleaned_data['Kt7']
answer[7] = form.cleaned_data['Kt8']
answer[8] = form.cleaned_data['Kt9']
answer[9] = form.cleaned_data['Kt10']
history = History.objects.get(id=answer_id)
if history.end_time != None:
return HttpResponse('您已经提交过,请勿重复提交!')
history.__set_end_time__()
time = history.end_time - history.start_time
as_list = Answer.objects.filter(answer_id=history)
i = 0
s = num
ot_ls = []
while i < answer.__len__():
if history.if_fra == True:
if not answer[i] == as_list[i].question.answer_Fraction:
if WrongAnswer.objects.filter(question=as_list[i].question):
s = s - 1
else:
wrong = WrongAnswer.objects.create()
wrong.question = as_list[i].question
wrong.answer_id = history
ot_ls.append(as_list[i].question)
wrong.save()
s = s - 1
else:
if not float(answer[i]) == float(as_list[i].question.answer_float):
if WrongAnswer.objects.filter(question=as_list[i].question):
s = s - 1
else:
wrong = WrongAnswer.objects.create()
wrong.question = as_list[i].question
wrong.answer_id = history
wrong.save()
ot_ls.append(as_list[i].question)
s = s - 1
i = i + 1
history.score = s
history.save()
context = {'output_list': ot_ls, 'score': s, 'time': time, 'if_fra': history.if_fra}
return render(request, 'question/your_score.html', context)
return HttpResponse('请重新填写表单')
if request.method == "POST":
if num == 20:
form = Form5(request.POST)
print(request.POST)
if form.is_valid():
answer = {}
answer[0] = form.cleaned_data['Kt1']
answer[1] = form.cleaned_data['Kt2']
answer[2] = form.cleaned_data['Kt3']
answer[3] = form.cleaned_data['Kt4']
answer[4] = form.cleaned_data['Kt5']
answer[5] = form.cleaned_data['Kt6']
answer[6] = form.cleaned_data['Kt7']
answer[7] = form.cleaned_data['Kt8']
answer[8] = form.cleaned_data['Kt9']
answer[9] = form.cleaned_data['Kt10']
answer[10] = form.cleaned_data['Kt11']
answer[11] = form.cleaned_data['Kt12']
answer[12] = form.cleaned_data['Kt13']
answer[13] = form.cleaned_data['Kt14']
answer[14] = form.cleaned_data['Kt15']
answer[15] = form.cleaned_data['Kt16']
answer[16] = form.cleaned_data['Kt17']
answer[17] = form.cleaned_data['Kt18']
answer[18] = form.cleaned_data['Kt19']
answer[19] = form.cleaned_data['Kt20']
history = History.objects.get(id=answer_id)
if history.end_time != None:
return HttpResponse('您已经提交过,请勿重复提交!')
history.__set_end_time__()
time = history.end_time - history.start_time
as_list = Answer.objects.filter(answer_id=history)
i = 0
s = num
ot_ls = []
while i < answer.__len__():
if history.if_fra == True:
if not answer[i] == as_list[i].question.answer_Fraction:
if WrongAnswer.objects.filter(question=as_list[i].question):
s = s - 1
else:
wrong = WrongAnswer.objects.create()
wrong.question = as_list[i].question
wrong.answer_id = history
ot_ls.append(as_list[i].question)
wrong.save()
s = s - 1
else:
if not float(answer[i]) == float(as_list[i].question.answer_float):
if WrongAnswer.objects.filter(question=as_list[i].question):
s = s - 1
else:
wrong = WrongAnswer.objects.create()
wrong.question = as_list[i].question
wrong.answer_id = history
wrong.save()
ot_ls.append(as_list[i].question)
s = s - 1
i = i + 1
history.score = s
history.save()
context = {'output_list': ot_ls, 'score': s, 'time': time, 'if_fra': history.if_fra}
return render(request, 'question/your_score.html', context)
return HttpResponse('请重新填写表单')
return HttpResponse('非法的提交操作')
def youtube(request):
if not request.session.get('is_login', None):
return HttpResponse('您尚未登录,无法操作')
user = User.objects.get(id=request.session.get('user_id', None))
history = History.objects.filter(user=user)
i = 0
ot_ls = []
while i < history.__len__():
wa = WrongAnswer.objects.filter(answer_id=history[i])
k = 0
while k < wa.__len__():
ot_ls.append(wa[k].question)
k = k + 1
i = i + 1
return render(request, 'question/history.html', context={'output_list': ot_ls})
分为用户选择题目属性的部分和答题部分。
generator:向服务器发送get请求。如果用户没有登录,提示用户还没有登录。如果request的方法是POST,用户提交表单,跳转到题目页面。用user记录当前的user_id,并创建history对象,记录这次的答题情况,其user属性为上面提到的user。
这里是复用的sousousou的get_ls来获得出题的列表quest
从quest中随机选取quantity个数据插入ot_ls中返回给用户
为了防止二次抽取,每次抽取后从quest弹出这道题
为每个题创造一个answer对象,记录用户的答案
confirm:用户发送POST请求,提供给用户5道题的表单,用history的answer_id属性判断用户是不是重复提交,以及记录答题时间
答案分为分数形式和小数形式,如果答案错误,在WrongAnswer的model中找这道题,如果没有找到,创建WrongAnswer对象,其question属性是当前的错题,并加入ot_ls错题列表中 ,然后扣分。跳转到分数页面,显示分数,错题和用时。
youtube用于显示用户的做题历史记录,首先判断用户是否存在,存在的话从数据库中取出用户的信息,并且在history中查询相应的主键,然后再去WrongAnswer表中查询对应的错题记录,将相应的内容保存在output_list中,从相应的界面上返回给用户