20221320 2022-2023-2 《Python程序设计》实验二报告

20221320 2022-2023-2 《Python程序设计》实验二报告

课程:《Python程序设计》
班级: 2213
姓名: 冯泰瑞
学号:20221320
实验教师:王志强
实验日期:2023年3月23日
必修/选修: 公选课

1.实验内容

设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
考核基本语法、判定语句、循环语句、逻辑运算等知识点

2. 实验过程及结果

源代码如下:

import math
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):
    return (a/b)
def square(a):
    return (a**2)
def radical(a):
    return (math.sqrt(a))
def module(a,b):
    return (a%b)
def abs(a):
    if a>0:
        return a
    else:
        return (-a)
def factorial(a):
    return math.factorial(a)
def power(a,b):
    return (a**b)
def triangle(tri,a):
    if tri=="sin":
        return math.sin(a)
    elif tri=="cos":
        return math.cos(a)
    elif tri=="tan":
        return math.tan(a)
flag=1
while flag==1:
    a=int(input("Please input the number a:\n"))
    b=int(input("Please input the number b:\n"))
    operation=input("Please input the operation that you want to do:\n")
    if operation=="+":
        print("The sum of a and b is",sum(a,b))
    elif operation=="-":
        print("The different of a and b is",sub(a,b))
    elif operation=="*":
        print("The product of a and b is",mul(a,b))
    elif operation=="/":
        if b==0:
            print("The divisor can't be zero!")
        else:
            print("The quotient is",div(a,b))
    elif operation=="**":
        print("The result is",power(a,b))
    elif operation=="*2":
        print("The a's square is",square(a))
        print("The b's square is",square(b))
    elif operation=="*1/2":
        print("The a's radical is",radical(a))
        print("Yhe b's radical is",radical(b))
    elif operation=="mod":
        print("The mod is",module(a,b))
    elif operation=="abs":
        print("The absolute number a is",abs(a))
        print("The absolute number b is",abs(b))
    elif operation=="fac":
        print("The factorial of a is",factorial(a))
        print("The factorial of b is",factorial(b))
    elif operation=="sin":
        print("The sin a is",triangle(operation,a))
        print("The sin b is",triangle(operation,b))
    elif operation=="cos":
        print("The cos a is",triangle(operation,a))
        print("The cos b is",triangle(operation,b))
    elif operation=="tan":
        print("The tan a is",triangle(operation,a))
        print("The tan b is",triangle(operation,b))
    flag=int(input("Do you want to calculate other number?Yes:1.No:0\n"))

运行结果如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
码云上代码上传:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

问题1:怎么实现三角函数运算操作合并在一个函数里面
问题1解决方案:使用选择分支结构,用变量tri记录输入的三角函数运算符,并进行比对,选择合适的库里写好的函数即可。

其他(感悟、思考等)

Python里面的计算器实现让我了解了很多Python库里写好的函数,增强了利用已知函数解决问题的能力。

posted @ 2023-06-22 00:47  20221320冯泰瑞  阅读(18)  评论(0编辑  收藏  举报