20232311 2023-2024-2 《Python程序设计》实验二报告
课程:《Python程序设计》
班级: 2323
姓名: 朱峰正
学号:20232311
实验教师:王志强
实验日期:2024年3月27日
必修/选修: 公选课
实验内容
设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
考核基本语法、判定语句、循环语句、逻辑运算等知识点
实验过程及结果
- 步骤1.根据实验要求,编写以下程序:
import math
def sum(a,b):
return (a+b)
def sub(a,b):
return (a-b)
def div(a,b):
if b==0:
print("除数不能为0")
return b
return (a/b)
def mul(a,b):
return(a*b)
def pysin(a):
return math.sin(math.radians(a))
def fs(a,b):
fsoperator=input("请输入复数的运算(+-*/):")
if fsoperator=="+":
return a+b
elif fsoperator=="-":
return a-b
elif fsoperator=="*":
return a*b
elif fsoperator=="/":
return a/b
else:
print("输入有误")
def pylog(a,b):
return math.log(b,a)
flag=True
while flag:
choice=int(input("请选择计算类型:1.四则运算;2.三角运算;3.复数运算;4.对数运算"))
a = eval(input("请输入一个数"))
b = eval(input("请输入一个数"))
if choice==1:
operator=input("请输入运算类型(+-*/)")
if operator=="+":
print("a+b=",sum(a,b))
elif operator=="-":
print("a-b=",sub(a,b))
elif operator=="*":
print("a*b=",mul(a,b))
elif operator=="/":
print("a/b=",div(a,b))
elif choice==2:
print("sin(a)=",pysin(a),"sin(b)=",pysin(b))
elif choice==3:
print(fs(a,b))
elif choice==4:
print("log a b=",pylog(a,b))
else:
print("NO")
flag=False if input("是否继续?Y or N:")=="N" else True
print("谢谢使用")
- 步骤2.调试结果
- 步骤3.代码托管到git
实验过程中遇到的问题和解决过程
- 问题1:不会操作三角函数弧度转换
- 问题1解决办法:Python有关于三角函数弧度制转换的函数,直接引用
- 问题2:不知道如何重复进行
- 问题2解决办法:设置flag实现循环
其他(感悟)
- Python的功能强大,尤其是它丰富的函数库,使用起来非常方便。
参考资料
- 实验二 计算器设计-CSDN社区
- https://blog.csdn.net/sinat_38682860/article/details/111051711?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171178718816800192264084%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=171178718816800192264084&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-2-111051711-null-null.142v100pc_search_result_base4&utm_term=%E5%B8%B8%E7%94%A8%E7%9A%84math%E5%87%BD%E6%95%B0py&spm=1018.2226.3001.4187