20192415 2020-2021-2 《Python程序设计》实验2报告 20192415 2020-2021-2 《Python程序设计》实验2报告

  • 课程:《Python程序设计》
  • 班级: 1924
  • 姓名: 邢质斌
  • 学号: 20192415
  • 实验教师:王志强
  • 实验日期:2021年4月26日
  • 选修: 公选课

一.实验内容

1.设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善;

2.考核基本语法、判定语句、循环语句、逻辑运算等知识点。

二. 实验过程及结果

Ⅰ 该计算器主要具有以下几种运算功能,将一一分析介绍:

1.加、减、乘、除、取模、次方、开方

2.累加、累乘

3.阶乘

4.三角函数

1.加、减、乘、除、取模、次方、开方

1.1 设计思路

该部分共含有7种运算。

加减乘除取模次方使用python运算符即可完成;

开方则利用math模块中的log函数完成。

1.2 程序代码

list1 = ('+','-','*','/','%','**','k')
def operation1(a,b,c):
	if c == '+':
    	return a + b
	elif c == '-':
    	return a - b
	elif c == '*':
    	return a * b
	elif c == '/':
    	return a / b
	elif c == '%':
    	return a % b
	elif c == '**':
    	return a ** b
	elif c == 'k':
    	return cmath.log(a[b])

while(flag == 1):
	operator = input("请输入你要做的运算(+、-、*、/、%、**(次方)、k(开方)):")
	a = int(input("请输入一个数:"))
	b = int(input("请输入一个数:"))
	if list1.count(operator) > 0:
		if operator != 'k':
			print(a,operator,b," = ",operation1(a,b,operator))
		elif operator == 'k':
			print("以", b, "为底的", a, "的对数为:", math.log(a,b))
	else:
            	print("运算符号输入有误!")
	flag = int(input("继续(1)or其它运算(0)?"))

1.3 运行结果

20192415 2020-2021-2 《Python程序设计》实验2报告
20192415 2020-2021-2 《Python程序设计》实验2报告

2.累加、累乘

2.1 设计思路

根据输入确定累加/累乘的数的范围。

利用列表的append()方法,使需要累加/累乘的每个数进入列表。

再利用循环进行累加/累乘运算。

2.2 程序代码

list2 = []
def list_build(a,b):
	if a>b:
    	temp=a
    	a=b
    	b=temp
	b=b+1
	for x in range(a, b):
    	list2.append(x)
def operation2(*para):
	sum1 = 0
	for item in para:
    	sum1 += item
	print("多个数的和:",sum1)
def operation3(*para):
	sum1 = 1
	for item in para:
    	sum1 = item * sum1
	print("多个数的积:",sum1)

while(flag == 1):
	operator = input("请输入你要做的运算(n+(累加)、n*(累乘)):")
	a = int(input("请输入一个数:"))
	b = int(input("请输入一个数:"))
	list_build(a,b)
	if operator == 'n+':
		operation2(*list2)
	elif operator == 'n*':
		operation3(*list2)
	else:
		print("运算符号输入有误!")
	list2.clear()

	flag = int(input("继续(1)or其它运算(0)?"))

2.3 运行结果

20192415 2020-2021-2 《Python程序设计》实验2报告
20192415 2020-2021-2 《Python程序设计》实验2报告

3.阶乘

3.1 设计思路

阶乘和累乘的设计思路相似,阶乘相当于累乘中的大数为所输入的数,小数则总为1。

3.2 程序代码

def list_build(a,b):
	if a>b:
    	temp=a
    	a=b
    	b=temp
	b=b+1
	for x in range(a, b):
    	list2.append(x)
def operation3(*para):
	sum1 = 1
	for item in para:
    	sum1 = item * sum1
	print("多个数的积:",sum1)

while(flag == 1):
	a = int(input("请输入需要计算阶乘的数:"))
	list_build(1,a)
	operation3(*list2)
	list2.clear()

	flag = int(input("继续(1)or其它运算(0)?"))

3.3 运行结果

20192415 2020-2021-2 《Python程序设计》实验2报告
20192415 2020-2021-2 《Python程序设计》实验2报告

4.三角函数

4.1 设计思路

计算三角函数利用math模块中的三角函数即可完成。

需要注意的是角度需要转化为弧度,可以借助math模块中的radians函数完成。

4.2 程序代码

while (flag == 1):
	c = int(input("请输入一个角度(0~360):"))
	d = input("请输入你要做的运算(sin,cos,tan):")
	if d == 'sin':
		print(c,"的",d,"为",math.sin(math.radians(c)))
	elif d == 'cos':
		print(c,"的",d,"为",math.cos(math.radians(c)))
	elif d == 'tan':
		print(c,"的",d,"为",math.tan(math.radians(c)))
	else:
		print("运算符号输入有误!")
                    
	flag = int(input("继续(1)or其它运算(0)?"))

4.3 运行结果

20192415 2020-2021-2 《Python程序设计》实验2报告
20192415 2020-2021-2 《Python程序设计》实验2报告

Ⅱ 将代码push至码云

https://gitee.com/besti2021python/Xing20192415/blob/master/Python实验二0426.py)

20192415 2020-2021-2 《Python程序设计》实验2报告
20192415 2020-2021-2 《Python程序设计》实验2报告

三. 实验过程中遇到的问题和解决过程

  • 问题1:range()左开右闭,列表中生成的数比实际需要的少一个

  • 问题1解决方案:range(a,b),使原本的b变为b+1。

    同时,我发现range()左开右闭是有一定便利和道理的:

    ①上下界之差等于元素的数量

    ②易于表示两个相邻子序列,一个子序列的上界就是另一个子序列的下界

    ③序列从零(最小自然数)开始计数时,下界的下标不是 -1(非自然数)

    ④表达空集时,不会使得上界小于下界

  • 问题2:混淆 math 模块与 cmath 模块

  • 问题2解决方案:经过查找资料区分两者不同。

    Python math 模块提供了许多对浮点数的数学运算函数;Python cmath 模块包含了一些用于复数运算的函数。cmath 模块的函数跟 math 模块函数基本一致,区别是 cmath 模块运算的是复数,math 模块运算的是数学运算。

  • 问题3: math 模块中的三角函数需使用弧度制

  • 问题3解决方案:利用 math 模块中的 radians 函数将角度转化为弧度。

    为便于用户使用计算器,设计的输入是角度制(0~360),但 math 模块中的三角函数需使用弧度制。一开始考虑利用“pi * 角度 / 360”进行运算,但实际得到的结果与理想不符。后发现 math 模块有专门的函数进行二者转化。

四. 其他(感悟、思考等)

网络上有一些关于可视化的教程,可以将编写的代码变为按键式的计算器。有时间有机会要试一试。

五. 参考资料