20181127-1附加作业 软件工程原则的应用实例
此作业的要求参见:【https://edu.cnblogs.com/campus/nenu/2018fall/homework/2446】
在软件工程中存在很多原则,在我的代码作业和非代码作业中均有体现。
一.首先分析我的代码作业:
在我的代码作业中,满足了一些原则。代码要满足用户的需求,满足用户的需求是我们书写出正确代码的前提。在完成用户需求是,我也犯了很多错误,没有按照输入的要求进行输入,虽然得到了正确的结果,但这也算不符合用户需求;在进行结对编程时,要提前制定代码编写的代码规范,然后在进行代码的编写;在代码书写之后,要进行再次测试,以保证代码的精准性。
代码的连接如下:
词频统计的代码地址:https://git.coding.net/sunsss/work.git
代码如下:
import re
class WF():
def __init__(self):
self.file_name = input('please input the name of book:')
self.mappings = {}
self.get_words()
self.console_print()
self.__init__()
def get_words(self):
file_tail = '' if '.txt' in self.file_name else '.txt'
with open (self.file_name + file_tail, 'r', encoding = 'utf-8') as f:
for line in f:
for i in re.sub(r'[^ws]','', line).replace('
','').split(' '):
if i not in self.mappings:
self.mappings[i] = 1
else:
self.mappings[i] += 1
def console_print(self):
for k, w in self.mappings.items():
print(k, w)
print('
total: %swords' % len(self.mappings))
WF()
该代码没有完成功能三和功能四,在之后的效能测试中,也没有办法完成。
四则运算的代码地址:https://git.coding.net/sunsss/two.git
代码如下:
(1)功能1
1 import random
2 ops = ['+','-','*','/']
3 com = input('>') #用户输入
4 cot = 0 #答对的题
5 x = 0
6 while x < 20 :
7 s1 = random.randint(1,10)
8 s2 = random.randint(1,10)
9 s3 = random.randint(1,10)
10 s4 = random.randint(1,10)
11 op1 = random.choice(ops) #随机运算符
12 op2 = random.choice(ops)
13 op3 = random.choice(ops)
14 while op1 == op2 == op3:
15 op1 = random.choice(ops) #随机运算符
16 op2 = random.choice(ops)
17 op3 = random.choice(ops)
18 eq = (str(s1)+op1+str(s2)+op2+str(s3)+op3+str(s4))
19 res = eval(eq)
20 if len(str(res) ) > 5:
21 continue
22 x += 1
23 print(eq)
24 in_res =eval( input('?'))
25 if in_res == res:
26 print('算对啦,你真是个天才!')
27 cot += 1
28 else:
29 print('再想想吧,答案似乎是%s喔!'%res)
30
31 print('你一共答对%s道题,共20道题'%cot)
(2)功能2
1 from random import randint
2 from random import choice
3 ops = ['+','-','*','/']
4 bra = ['(', '', ')']
5 com = input('>') #用户输入
6 cot = 0 #答对的题
7 x = 0
8 while x < 20 :
9 s1 = randint(1,10)
10 s2 = randint(1,10)
11 s3 = randint(1,10)
12 s4 = randint(1,10)
13 op1 = choice(ops) #随机运算符
14 op2 = choice(ops)
15 op3 = choice(ops)
16 """括号"""
17 bra_1 = ['' , '' ,'']
18 bra_2 = ['' , '' , '']
19 i = ii =0
20 while (i ==0 and ii ==2) or abs(i-ii)==1
21 or ii < i :
22 i = randint(0,2)
23 ii = randint(0,2)
24
25 bra_1[i] = '('; bra_2[ii]=')'
26
27 while op1 == op2 == op3 :
28 op1 = choice(ops) #随机运算符
29 op2 = choice(ops)
30 op3 = choice(ops)
31
32 eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) +
33 bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3
34 + str(s4) + bra_2[2]
35 res = eval(eq)
36 if len(str(res) ) > 5:
37 continue
38 x += 1
39 print(eq)
40 in_res =eval( input('?'))
41 if in_res == res:
42 print('算对啦,你真是个天才!')
43 cot += 1
44 else:
45 print('再想想吧,答案似乎是%s喔!'%res)
46
47 print('你一共答对%s道题,共20道题'%cot)
(3)功能3
1 from random import randint
2 from random import choice
3 import os
4 ops = ['+','-','*','/']
5 bra = ['(', '', ')']
6 com = input('>') #用户输入
7 com_list = com.split()
8 while com_list[2].isdigit() == False:
9 print('题目数量必须是正整数')
10 com = input('>') #用户输入
11 com_list = com.split()
12
13 def xx():
14 s1 = randint(1,10)
15 s2 = randint(1,10)
16 s3 = randint(1,10)
17 s4 = randint(1,10)
18 op1 = choice(ops) #随机运算符
19 op2 = choice(ops)
20 op3 = choice(ops)
21 """括号"""
22 bra_1 = ['' , '' ,'']
23 bra_2 = ['' , '' , '']
24 i = ii =0
25 while (i ==0 and ii ==2) or abs(i-ii)==1
26 or ii < i :
27 i = randint(0,2)
28 ii = randint(0,2)
29
30 bra_1[i] = '('; bra_2[ii]=')'
31
32 while op1 == op2 == op3 :
33 op1 = choice(ops) #随机运算符
34 op2 = choice(ops)
35 op3 = choice(ops)
36
37 eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) +
38 bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3
39 + str(s4) + bra_2[2]
40 res = eval(eq)
41 return [eq,res]
42
43
44
45 eq = []; res = []
46 while len(res) < int(com_list[2]):
47 a = xx()
48 if a[1] in res or len((str(a[1])) ) >6: #结果一样的直接就不要
49 continue
50 eq.append(a[0])
51 res.append(a[1])
52
53 f= open('题目.txt','w')
54 for i in range(len(eq)):
55 print('{0:15}'.format(eq[i]),end = '')
56 print(res[i])
57 xxx = 17 - len(eq[i])
58 f.write(str(eq[i]+' '*xxx))
59 f.write(str(res[i])+'
')
60 f.close()
61 os.system('题目.txt') #决定是否打开txt
(4)功能4
1 import os
2 from random import randint
3 from random import choice
4 from fractions import Fraction
5 ops = ['+','-','*','/']
6 bra = ['(', '', ')']
7 com = input('>') #用户输入
8 com_list = com.split()
9 while com_list[2].isdigit() == False:
10 print('题目数量必须是正整数')
11 com = input('>') #用户输入
12 com_list = com.split()
13
14 def xx():
15 s1 = randint(1,10)
16 s2 = randint(1,10)
17 s3 = randint(1,10)
18 s4 = randint(1,10)
19 op1 = choice(ops) #随机运算符
20 op2 = choice(ops)
21 op3 = choice(ops)
22 """括号"""
23 bra_1 = ['' , '' ,'']
24 bra_2 = ['' , '' , '']
25 i = ii =0
26 while (i ==0 and ii ==2) or abs(i-ii)==1
27 or ii < i :
28 i = randint(0,2)
29 ii = randint(0,2)
30
31 bra_1[i] = '('; bra_2[ii]=')'
32
33 while op1 == op2 == op3 :
34 op1 = choice(ops) #随机运算符
35 op2 = choice(ops)
36 op3 = choice(ops)
37
38 eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) +
39 bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3
40 + str(s4) + bra_2[2]
41 res = Fraction(eval(eq))
42 return [eq,res]
在这次代码书写中,我和我的同学结对完成,我们提出彼此的想法,提出不同的意见,这次编程是我们两个人都得到了进步。我们在满足客户需求的同时,还根据我们的想法对代码进行了优化。
二.其次分析我的非代码作业
第一周的博文作业,算是接触软件工程的第一课,存在着这样一项原则,在回答你的优点是什么,你是如何达到今天的进步的问题?我们回答问题时,要给出具体努力的过程,要有能进行衡量标准你的努力。这个原则让我颇为受用;每周例行报告的书写,进行一切实时记录,使我联想到了git版本控制,这样做的目的是是是存储代码,防止出现意外代码消失;吉林市两日游,对事先会发生的情况未雨绸缪,提出两套及以上的方案来应对。