一、简单实现“四则运算”——对现代软件工程讲义学习的实践和心得体会

一、谈谈自己对专业的看法

  我是跨考的计算机专业,也比较喜欢软件这一块,但是基础较为薄弱,依然在艰难而快乐的学习着,大学四年的课程要说最感兴趣的时候还是敲代码的时候,总觉得一步一步在电脑上实现一些功能会很开心,虽然一边查资料一边敲代码的学习过程挺累人,但是最后看着跑出来的代码还是挺开心的。能学习一些自己感兴趣的知识还是会乐在其中。现在的状态是越来越谦虚,因为慢慢知道自己和别人差距悬殊,所以做事、学习更加脚踏实地、谦虚,不能自傲也不能自卑,体验学习的乐趣最重要。

二、讲义概述部分的实践——简单实现 “四则运算”

讲义原文:https://www.cnblogs.com/xinz/p/7417960.html

目前正在学习python相关的内容,所以课程的实践就用python来做了

 

因为我没有记录自己代码编写的情况,都是想到哪写到哪,所以直接上代码吧

  1 import random
  2 from docx import Document
  3 from docx.shared import Inches
  4 
  5 class gradeOne(object):
  6     def __init__(self,grade,cycleIndex):
  7         self.operator=['+','-']
  8         self.cycleIndex=cycleIndex
  9         self.grade=grade
 10     def formula(self):
 11         formula=str(random.randint(1,10))+self.operator[random.randint(0,1)]+str(random.randint(1,10))+self.operator[random.randint(0,1)]+str(random.randint(1,10))
 12         answer=eval(formula)
 13 
 14         return answer,formula
 15             
 16     def creatDocx(self):
 17         a=0
 18         switch=0
 19         switch2=0
 20         if self.grade=='':
 21             switch3=3
 22         elif  self.grade=='':
 23             switch3=2
 24         else:
 25             switch3=3
 26         document = Document()
 27         document2=Document()
 28         head=document.add_heading('小学{}年级四则运算'.format(self.grade), 0)      # 添加标题
 29         head.alignment = 1
 30         head=document2.add_heading('小学{}年级四则运算答案'.format(self.grade), 0)
 31         head.alignment = 1
 32         while a!=self.cycleIndex:
 33             try:
 34                 answer,formula=self.formula()
 35             
 36                 if answer >= 0 and float(answer).is_integer() == True:
 37                     print(formula+'='+str(int(answer)))
 38                     if switch == 0:
 39                         p=document.add_paragraph('{}: {}='.format(a+1,formula))    # 增加一段
 40                         p2=document2.add_paragraph('{}: {}'.format(a+1,int(answer)))
 41                         p.alignment = 1
 42                         p2.alignment = 1
 43                         a+=1
 44                         switch=1
 45                         continue
 46                     else:
 47                         p.add_run('                  ')
 48                         p.add_run('{}: {}='.format(a+1,formula))
 49                         p2.add_run('                  ')
 50                         p2.add_run('{}: {}'.format(a+1,int(answer)))
 51                         
 52                         switch2+=1
 53                         if switch2 == switch3:
 54                             switch=0
 55                             switch2=0
 56                         a+=1
 57             except ZeroDivisionError:
 58                 a-=1
 59         document.save('./小学{}年级四则运算.docx'.format(self.grade))              # 保存文件
 60         document2.save('./小学{}年级四则运算答案.docx'.format(self.grade))  
 61 
 62 class gradeTwo(gradeOne):
 63     def __init__(self,grade,cycleIndex):
 64         self.operator=['+','-','*','/']
 65         self.operator2=['+','-']
 66         self.grade=grade
 67         self.cycleIndex=cycleIndex
 68     def formula(self):
 69         formula=str(random.randint(1,10))+self.operator[random.randint(0,3)]+str(random.randint(1,10))+self.operator2[random.randint(0,1)]+str(random.randint(1,10))
 70         answer=eval(formula)
 71 
 72         return answer,formula
 73             
 74 class gradeThree(gradeOne):
 75     def __init__(self,grade,cycleIndex):
 76         self.operator=['+','-','*','/']
 77         self.operator2=['(','N']  #判断是否要加括号
 78         self.grade=grade
 79         self.cycleIndex=cycleIndex
 80         
 81     def formula(self):
 82         
 83         op_1=self.operator[random.randint(0,3)]
 84         op_2=self.operator[random.randint(0,3)]
 85         op_3=self.operator2[random.randint(0,1)]
 86         if (op_1 == '+' or op_1 == '-') and  op_3 == '(' and (op_2 == '*' or op_2 == '/'):  #判断是否要加括号
 87             formula='('+str(random.randint(1,100))+op_1+str(random.randint(1,100))+')'+op_2+str(random.randint(1,100))
 88         elif (op_1 == '*' or op_1 == '/') and  op_3 == '(' and (op_2 == '+' or op_2 == '-'):
 89             formula=str(random.randint(1,100))+op_1+'('+str(random.randint(1,100))+op_2+str(random.randint(1,100))+')'
 90         else :
 91             formula=str(random.randint(1,100))+op_1+str(random.randint(1,100))+op_2+str(random.randint(1,100))
 92         answer=eval(formula)
 93 
 94         return answer,formula
 95             
 96 class gradeFour(gradeThree):
 97     
 98     def creatDocx(self):
 99         a=0
100         switch=0
101         switch2=0
102         document = Document()
103         document2=Document()
104         head=document.add_heading('小学{}年级四则运算'.format(self.grade), 0)      # 添加标题
105         head.alignment = 1
106         head=document2.add_heading('小学{}年级四则运算答案'.format(self.grade), 0)
107         head.alignment = 1
108         while a!=self.cycleIndex:
109             try:
110                 answer,formula=self.formula()
111                 formula=formula+self.operator[random.randint(0,3)]+str(random.randint(1,100))
112                 answer=eval(formula)
113                 if answer >= 0 and float(answer).is_integer() == True:
114                     print(formula+'='+str(int(answer)))
115                     if switch == 0:
116                         p=document.add_paragraph('{}: {}='.format(a+1,formula))    # 增加一段
117                         p2=document2.add_paragraph('{}: {}'.format(a+1,int(answer)))
118                         p.alignment = 1
119                         p2.alignment = 1
120                         a+=1
121                         switch=1
122                         continue
123                     else:
124                         p.add_run('                  ')
125                         p.add_run('{}: {}='.format(a+1,formula))
126                         p2.add_run('                  ')
127                         p2.add_run('{}: {}'.format(a+1,int(answer)))
128                         
129                         switch2+=1
130                         if switch2 == 2:
131                             switch=0
132                             switch2=0
133                         a+=1
134             except ZeroDivisionError:
135                 a-=1
136         document.save('./小学{}年级四则运算.docx'.format(self.grade))                 # 保存文件
137         document2.save('./小学{}年级四则运算答案.docx'.format(self.grade))  
138 
139 class gradeFive(gradeThree):
140 
141     def formula(self):
142         
143         op_1=self.operator[random.randint(0,3)]
144         op_2=self.operator[random.randint(0,3)]
145         op_3=self.operator2[random.randint(0,1)]
146         if (op_1 == '+' or op_1 == '-') and  op_3 == '(' and (op_2 == '*' or op_2 == '/'):  #判断是否要加括号
147             formula='('+format(random.uniform(1,100),'.1f')+op_1+format(random.uniform(1,10),'.1f')+')'+op_2+format(random.uniform(1,100),'.1f')
148         elif (op_1 == '*' or op_1 == '/') and  op_3 == '(' and (op_2 == '+' or op_2 == '-'):
149             formula=format(random.uniform(1,100),'.1f')+op_1+'('+format(random.uniform(1,10),'.1f')+op_2+format(random.uniform(1,100),'.1f')+')'
150         else :
151             formula=format(random.uniform(1,100),'.1f')+op_1+format(random.uniform(1,10),'.1f')+op_2+format(random.uniform(1,100),'.1f')
152         answer=eval(formula)
153         
154         return answer,formula
155 
156 class gradeSix(gradeFive):
157 
158     def formula(self):
159         
160         op_1=self.operator[random.randint(0,3)]
161         op_2=self.operator[random.randint(0,3)]
162         op_3=self.operator2[random.randint(0,1)]
163         if (op_1 == '+' or op_1 == '-') and  op_3 == '(' and (op_2 == '*' or op_2 == '/'):  #判断是否要加括号
164             formula='('+format(random.uniform(1,100),'.1f')+op_1+format(random.uniform(1,100),'.1f')+')'+op_2+format(random.uniform(1,100),'.1f')
165         elif (op_1 == '*' or op_1 == '/') and  op_3 == '(' and (op_2 == '+' or op_2 == '-'):
166             formula=format(random.uniform(1,100),'.1f')+op_1+'('+format(random.uniform(1,100),'.1f')+op_2+format(random.uniform(1,100),'.1f')+')'
167         else :
168             formula=format(random.uniform(1,100),'.1f')+op_1+format(random.uniform(1,100),'.1f')+op_2+format(random.uniform(1,100),'.1f')
169         answer=eval(formula)
170         
171         return answer,formula
172 
173     def creatDocx(self):
174         a=0
175         switch=0
176         switch2=0
177         document = Document()
178         document2=Document()
179         head=document.add_heading('小学{}年级四则运算'.format(self.grade), 0)      # 添加标题
180         head.alignment = 1
181         head=document2.add_heading('小学{}年级四则运算答案'.format(self.grade), 0)
182         head.alignment = 1
183         while a!=self.cycleIndex:
184             try:
185                 answer,formula=self.formula()
186                 answer2,formula2=self.formula()
187                 formula=formula+self.operator[random.randint(0,3)]+formula2
188                 answer=eval(formula)
189                 if   float(answer).is_integer() == True:
190                     print(formula+'='+str(int(answer)))
191                     if switch == 0:
192                         p=document.add_paragraph('{}: {}='.format(a,formula))     # 增加一段
193                         p2=document2.add_paragraph('{}: {}'.format(a+1,int(answer)))
194                         p.alignment = 1
195                         p2.alignment = 1
196                         a+=1
197                         switch=1
198                         continue
199                     else:
200                         p.add_run('                  ')
201                         p.add_run('{}: {}='.format(a+1,formula))
202                         p2.add_run('                  ')
203                         p2.add_run('{}: {}'.format(a+1,int(answer)))
204 
205                         switch2+=1
206                         if switch2 == 1:
207                             switch=0
208                             switch2=0
209                         a+=1
210             except ZeroDivisionError:
211                 a-=1
212         document.save('./小学{}年级四则运算.docx'.format(self.grade))               # 保存文件
213         document2.save('./小学{}年级四则运算答案.docx'.format(self.grade))  
214 
215 if __name__ == '__main__':
216     while 1:
217         grade=input('请输入年级(一年级为1):')
218         cycleIndex=input('请输入题目数量:')
219         if int(grade) == 1:
220             grade_One=gradeOne('',int(cycleIndex))
221             grade_One.creatDocx()
222 
223         elif int(grade) == 2:
224             grade_Two=gradeTwo('',int(cycleIndex))
225             grade_Two.creatDocx()
226 
227         elif int(grade) == 3:
228             grade_Three=gradeThree('',int(cycleIndex))
229             grade_Three.creatDocx()
230 
231         elif int(grade) == 4:
232             grade_Four=gradeFour('',int(cycleIndex))
233             grade_Four.creatDocx()
234 
235         elif int(grade) == 5:
236             grade_Five=gradeFive('',int(cycleIndex))
237             grade_Five.creatDocx()
238             
239         elif int(grade) == 6:
240             grade_Six=gradeSix('',int(cycleIndex))
241             grade_Six.creatDocx()

刚写的时候单纯的实现减价乘除加三位数的运算,后来看了原博,发现到后面的信息量越来越大,比如不同年级不同难度、可以一次成多道题目等,然后我想着用一些更直观、易扩展的方法写,就用上了class,分别写了六个年级的类,看上去更加明了,易于补充别的功能。

到目前为止这些代码可以实现不同年级的选择,六个年级六个难度,还可以选择生成题目数量。后来我想着直接生成word文档就能打印了岂不是更好,就加入了一些生成本地docx文件的操作,分别生成一份试卷、一份答案。下面是程序运行界面

下面是本地生成的试卷和答案

到此很基本很基本的功能算是勉强实现了,后面还有更加复杂的一些功能还要完善,比如做一个UI界面、移植到移动端等等。

posted @ 2020-02-18 16:51  认真学习的小朋友  阅读(298)  评论(1编辑  收藏  举报