20232416 2023-2024-2 《python程序设计》实验二报告
课程:《Python程序设计》
班级: 2324
姓名: 石泽伟
学号:20232416
实验教师:王志强
实验日期:2024年3月27日
必修/选修: 公选课
1.实验内容
设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
考核基本语法、判定语句、循环语句、逻辑运算等知识点
2. 实验过程及结果
import math
def ldtriangle(a,b):
triangleop = input("请输入操作: ")
if triangleop == "sin":
print("sin(a) = ",math.sin(math.radians(a)),"sin(b) = ",math.sin(math.radians(b)))
elif triangleop == "cos":
print("cos(a) = ",math.cos(math.radians(a)),"cos(b) = ",math.cos(math.radians(b)))
elif triangleop == "tan":
print("tan(a) = ",math.tan(math.radians(a)),"tan(b) = ",math.tan(math.radians(b)))
elif triangleop == "sec":
print("sec(a) = ",1.0/math.sin(math.radians(a)),"sec(b) = ",1.0/math.cos(math.radians(b)))
elif triangleop == "csc":
print("csc(a) = ",1.0/math.cos(math.radians(a)),"cos(b) = ",1.0/math.cos(math.radians(b)))
def sum(a,b):
return (a+b)
def sub(a,b):
return (a-b)
def mul(a,b):
return (a*b)
def div(a,b):
if b == 0:
print("ZerodivisionError")
return None
return (a/b)
def ldcomplex(a,b):
complexoper = input("请输入复数运算符(+-*/):")
if complexoper == "+":
print("a+b=",a+b)
elif complexoper == "-":
print("a-b=", a - b)
elif complexoper == "*":
print("a*b=", a * b)
elif complexoper == "/":
print("a/b=", a / b)
def ldlog(a,b):
if a <= 0 or b <= 0:
print("a b 需大于0")
return
result = math.log(b) / math.log(a)
print("logaB = ",result)
print("**=========欢迎使用积极计算器=========**")
print("|| 作者: ||")
print("|| 开发时间:2024.3.27 ||")
print("|| 联系方式:laoda@los angles.com ||")
print("**================================**")
flag = True
while flag:
a = eval(input("请输入a: "))
b = eval(input("请输入b: "))
operate = input("请输入运算符(+-*/ tri com lg): \n")
if operate == "+":
print("a+b=",sum(a,b))
elif operate == "-":
print("a-b=",sub(a,b))
elif operate == "*":
print("a*b=",mul(a,b))
elif operate == "/":
print("a/b=",div(a,b))
elif operate == "tri":
ldtriangle(a,b)
elif operate == "com":
ldcomplex(a,b)
elif operate == "lg":
ldlog(a,b)
else:
print("您输入有误,请重新输入: ")
flag = False if input("是否继续?y or n\n") == "n" else True
这里展示一部分的功能,所有功能均可正常运行。
3.实验过程中遇到的问题和解决过程
- 1.问题:一开始的程序中判断是否会继续输入,
- 解决方案:使用一个简化式,就像C语言中的这个程序一样。
max = a > b ? a:b;