20183215 实验二《Python程序设计》实验报告

20183215 2019-2020-2 《Python程序设计》实验二报告

课程:《Python程序设计》
班级: 1832
姓名: 董振龙
学号: 20183215
实验教师:王志强
实验日期:2020年4月20日
必修/选修: 公选课

1.实验内容

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

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

2. 实验过程及结果

def add(a,b): #加法函数
    print(a,"+",b,"=",a+b)
    return a+b
def sub(a,b): #减法函数
    print(a,"-",b,"=",a-b)
    return a-b
def mul(a,b): #乘法函数
    print(a,"×",b,"=",a*b)
    return a*b
def div(a,b): #除法函数
    if b==0:
        b=float(input("除数为零,请重新输入:"))
        print(a,"÷",b,"=",a/b)
        return a/b
    else:
        print(a,"÷",b,"=",a/b)
        return a / b
def mod(a,b): #模运算
    if b==0:
        b=float(input("除数为零,请重新输入:"))
        print(a,"Mod",b,"=",a%b)
        return a%b
    else:
        print(a,"Mod",b,"=",a%b)
        return a%b

首先定义了最常见的5个运算函数,加减乘除模。在考虑其他运算时,想到了一点,就是有的运算需要两个数参与,而有的运算只需要一个数参与,比如三角函数运算以及lg等。
于是我编写了一个判断语句

if(input("1、单数运算
"
         "2、多数运算
")=="2"):
    a = float(input("第一个数:"))
    b = float(input("第二个数:"))
else:
    a=float(input("请输入数值:"))

在之后,便加入了其他的一些比较常见的运算函数

def log(a,b): #普通对数运算函数
    print("log(a,b)=",math.log(a,b))
    return math.log(a,b)
def exp(a,b): #幂运算
    print(a,"^",b,"=",a**b)
def lg(a): #lg运算函数
    print("lg(a)=",math.log(a,10))
    return math.log(a,10)
def ln(a): #ln运算函数
    print("ln(a)=",math.log(a))
    return math.log(a)
def exp_e(a): #e的n次方
    print("e^",a,"=",math.e**a)
def sin(a): #sin函数
    print("sin",str(a)+"°","=",math.sin(math.radians(a)))
    return math.sin(math.radians(a))
def cos(a): #cos函数
    print("cos",str(a)+"°","=",math.cos(math.radians(a)))
    return math.cos(math.radians(a))
def tan(a): #tan函数
    if math.radians(a)==math.pi/2:
        a=float(input("不可以为Π/2,请重新输入:"))
        print("tan", str(a) + "°", "=", math.tan(math.radians(a)))
        return math.tan(math.radians(a))
    else:
        print("tan", str(a) + "°", "=", math.tan(math.radians(a)))
        return math.tan(math.radians(a))

共13个常见运算。
在那之后,便是主函数了,在之前判断语句的基础上,添加了更为细分的判断语句,同时加入了循环判断,便于多次计算

while(i==1):
    if(input("1、单数运算
"
             "2、多数运算(若为对数运算,第二个数为底数;若为幂运算,第二个数为指数)
")=="2"):
        a = float(input("第一个数:"))
        b = float(input("第二个数:"))
        c =int(input("运算操作为:
"
                "1.+
"
                "2.-
"
                "3.×
"
                "4.÷
"
                "5.Mod
"
                "6.log
"
                "7.exp
"))
        if c==1:
            add(a,b)
        elif c==2:
            sub(a,b)
        elif c==3:
            mul(a,b)
        elif c==4:
            div(a,b)
        elif c==5:
            mod(a,b)
        elif c==6:
            log(a,b)
        elif c==7:
            exp(a,b)
    else:
        a=float(input("请输入数值:"))
        c=int(input("运算操作为:
"
                "1.sin
"
                "2.cos
"
                "3.tan
"
                "4.lg
"
                "5.ln
"
                "6.e^
"))
        if c==1:
            sin(a)
        elif c==2:
            cos(a)
        elif c==3:
            tan(a)
        elif c==4:
            lg(a)
        elif c==5:
            ln(a)
        elif c==6:
            exp_e(a)
    i=int(input("继续进行计算?
"
                "1.是
"
                "2.否
"))

至此,代码已经完成---完整的代码
在pycharm中简单运行如下

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

  • 问题:在设置三角函数运算时,不知道怎么将输入的角度转化为弧度
  • 问题解决方案:百度之后,找到了菜鸟教程,根据其中的解释,成功做到了输入角度实现计算

其他(感悟、思考等)

这次简单计算器的制作让我想到了很多,一开始我的设想是直接输入表达式然后便完成计算,但是发现我现在学习到的还不够,于是只能是制作简易计算器,但是,接下来的学习中,我会根据我新学习到的知识,一步步的完善这个计算器,希望最终可以完成科学计算器基本功能的实现,同时加入GUI美化,奥里给!

参考资料

原文地址:https://www.cnblogs.com/mo-xiao-qi/p/12741107.html