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

20203322 冷骏 2020-2021-2 《Python程序设计》实验二报告

简单计算器

课程:《Python程序设计》
班级: 2033
姓名: 冷骏
学号:20203322
实验教师:王志强
实验日期:2021年5月8日
必修/选修: 公选课

1.1实验内容

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

本次计算器包含内容:基本加减乘除四则运算,取余,取商,三角函数运算,图形面积与周长运算并在运算完成之后询问是否继续。

2实验过程及结果

2.1基本的四则运算

def operation(a,b,operator):#函数
    '''
        计算器
        输入两个数a和b,计算a和b的操作运算(+,—,*,/,//,%,**)
    '''
    if operator == "+":
        return a+b
    if operator == "-":
        return a-b
        if operator == "/":
            if b==0:
               print(“Error!”)
            else:
               return a/b
    if operator == "*":
        return a*b                    

 2.2求余,求商,幂运算

def operation(a,b,operator):
     if operator == "**":
         return a**b
     if operator == "%":
         return a%b
     if operator == "//":
         return a//b

 2.3三角函数

while (choice1 == 3):
        number = int(input("1.sinx  2.cosx  3.tanx\n"))  # 三角函数种类算术
        c = float(input("请输入一个值(角度):\n"))
        d = math.radians(c)
        if number == 1:
            answer = round(math.sin(d),2)
        elif number == 2:
            answer = round(math.cos(d),2)
        elif number == 3:
            answer = round(math.tan(d),2)

 2.4求图形面积与周长

def area(a,b,c,d,e,graph):#求图形面积
    PI = 3.1415926535
    if graph == 1:# 长方形
        return a * b
    elif graph == 2:# 圆形
        return 2 * PI * a * a
    elif graph == 3:# 梯形
        return (a+b) * c / 2
    elif graph == 4:# 平行四边形
        return a * e
def perimeter(a,b,c,d,e,graph):#求图形周长
        PI = 3.1415926535
        if graph == 1:
            return (a + b) * 2
        elif graph == 2:# 圆形
            return 2 * a * PI
        elif graph == 3:# 梯形
            return a + b + c + d
        elif graph == 4:# 平行四边形
            return a + b + c + d

 2.5完整代码

  1 def operation(a,b,operator):#函数
  2     if operator == "+":
  3         return a+b
  4     if operator == "-":
  5         return a-b
  6     if operator == "**":
  7         return a**b
  8     if operator == "%":
  9         return a%b
 10     if operator == "/":
 11         return a/b
 12     if operator == "//":
 13         return a//b
 14     if operator == "*":
 15         return a*b
 16 def area(a,b,c,d,e,graph):#求图形面积
 17     PI = 3.1415926535
 18     if graph == 1:# 长方形
 19         return a * b
 20     elif graph == 2:# 圆形
 21         return 2 * PI * a * a
 22     elif graph == 3:# 梯形
 23         return (a+b) * c / 2
 24     elif graph == 4:# 平行四边形
 25         return a * e
 26 def perimeter(a,b,c,d,e,graph):#求图形周长
 27         PI = 3.1415926535
 28         if graph == 1:
 29             return (a + b) * 2
 30         elif graph == 2:# 圆形
 31             return 2 * a * PI
 32         elif graph == 3:# 梯形
 33             return a + b + c + d
 34         elif graph == 4:# 平行四边形
 35             return a + b + c + d
 36 import math
 37 
 38 flag = 1
 39 while flag == 1:
 40     print("请输入你要做的运算类型:\n【四则运算(1)】\n【计算面积(2)】\n【计算sin,cos,tan(3)】\n")
 41     choice1 = int(input())
 42     
 43     while (choice1 == 1):
 44         operator = input("请输入你要做的运算:+,-,*,/,//,%,**\n")
 45         a=int(input("请输入一个数a:"))
 46         b=int(input("请输入一个数b:"))
 47         if operator    == "-":
 48             print("a"+ operator +"b=",operation(a,b,operator))
 49         elif operator    ==    "+":
 50             print("a"+ operator +"b=",operation(a,b,operator))
 51         elif operator    == "**":
 52             print("a"+ operator +"b=",operation(a,b,operator))
 53         elif operator    == "/":
 54             print("a"+ operator +"b=",operation(a,b,operator))
 55         elif operator    == "//":
 56             print("a"+ operator +"b=",operation(a,b,operator))
 57         elif operator    == "*":
 58             print("a"+ operator +"b=",operation(a,b,operator))
 59         print("还要继续使用计算器么(继续输入1,退出输入2)")
 60         flag = int(input())
 61         break
 62     while (choice1 == 2):
 63             print("请输入所需计算的图形序号(1.长方形 2.圆形 3.梯形 4.平行四边形)")
 64             print("\n请选择您要计算的类型序号:")
 65             graph = int(input())
 66             a=0.0
 67             b=0.0
 68             c=0.0
 69             d=0.0
 70             e=0.0
 71             if graph==1:#长方形
 72                     print("请输入:")
 73                     a = float(input("长:"))
 74                     b = float(input("宽:"))
 75             elif graph==2:#圆形
 76                     print("请输入:")
 77                     a = float(input("半径:"))
 78             elif graph==3:#梯形
 79                     print("请输入:")
 80                     a = float(input("上底:"))
 81                     b = float(input("下底:"))
 82                     c = float(input("高:"))
 83                     d = float(input("左腰长:"))
 84                     e = float(input("右腰长:"))
 85             elif graph==4:#平行四边形
 86                     print("请输入:")
 87                     a = float(input("上边长:"))
 88                     b = float(input("下边长:"))
 89                     c = float(input("左边长:"))
 90                     d = float(input("右边长:"))
 91                     e = float(input("高:"))
 92             print("周长为:",perimeter(a,b,c,d,e,graph))
 93             print("面积为:",area(a,b,c,d,e,graph))
 94             print("还要继续使用计算器么(继续输入1,退出输入2)")
 95             choice2 = int(input())
 96             break
 97     while (choice1 == 3):
 98         number = int(input("1.sinx  2.cosx  3.tanx\n"))  # 三角函数种类算术
 99         c = float(input("请输入一个值(角度):\n"))
100         d = math.radians(c)
101         if number == 1:
102             answer = round(math.sin(d),2)
103         elif number == 2:
104             answer = round(math.cos(d),2)
105         elif number == 3:
106             answer = round(math.tan(d),2)
107         else:
108             answer = "错误,请重新输入"
109         print("答案为:", answer)
110         print("还要继续使用计算器么(继续输入1,退出输入2)")
111         choice2 = int(input())
112         break

 3.1运行结果

3.1.1四则运算

 

 3.1.2面积运算

 

3.1.3三角函数运算 

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

  问题1:知识点记不牢,通过多方面查询才知道计算三角函数要调用math函数库

  问题2:对python的缩进机制还不熟悉,在做循环的时候将跳出的代码缩进多了导致运行无限循环

5.1其他(感悟,思考等)

  本次试验设计思路比较清晰,通过def进行各种运算的设计,然后主函数实现输入,输出。但是每做一次运算都要从新选择运算方式的循环系统非常麻烦,还需要改进,并且在这种结构的基础上也能够做出更加丰富的计算器系统,甚至还能够进行界面设计;但是关键还在于我们在有想法的同时加强自身的编程能力,打铁还需自身硬,只有对各种算法,结构,等各个方面都掌握熟练,才能够更好的实现自己的想法。

6.1码云链接

 https://gitee.com/Lengjunnnn/a-simple-calculator-.git

posted @ 2021-05-10 11:30  好吧。  阅读(97)  评论(0编辑  收藏  举报