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

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

课程:《Python程序设计》
班级: 2232
姓名: 黄湛
学号:20223210
实验教师:王志强
实验日期:2023年3月23日
必修/选修: 公选课

(一)实验内容和实验要求

设计并完成一个完整的应用程序,完成加减乘除模等运算
创建工程项目,使用Python语言实现具体的操作运算,并完成程序调试和运行,代码托管到码云

(二)实验过程及结果

  • calculator
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 mod(a, b):
    return a % b
def ediv(a, b):
    return a // b
def pow(a, b):
    return a ** b
def logg(a, b):
    return math.log(b) / math.log(a)
def sinn(a):
    return math.sin(a)
def coss(a):
    return math.cos(a)
def tann(a):
    return math.tan(a)
def asinn(a):
    return math.asin(a)
def acoss(a):
    return math.acos(a)
def atann(a):
    return math.atan(a)
def factorial(a):
    v = 1
    for i in range(1, a + 1, 1):
        v *= i
    return v
def maxx(a, b):
    return a if a > b else b
def minn(a, b):
    return a if a < b else b
def abss(a):
    return a if a > 0 else -a
def floorr(a):
    return math.floor(a)
def ceill(a):
    return math.ceil(a)
while True:
    a = float(input("Please enter the number a:"))
    b = float(input("Please enter the number b: "))
    print("+ - * / % // ** log max min abs floor ceil sin cos tan asin acos atan !")
    op = input("Please enter the operator you want: ")
    if op == "+":
        print(a,op,b,"=",sum(a, b))
    elif op == "-":
        print(a,op,b,"=",sub(a, b))
    elif op == "*":
        print(a,op,b,"=",mul(a, b))
    elif op == "/":
        if b == 0:
            print("Wrong")
        else:
            print(a,op,b,"=",div(a, b))
    elif op == "%":
        print(a,op,b,"=",mod(a, b))
    elif op == "//":
        print(a,op,b,"=",ediv(a, b))
    elif op == "**":
        print(a,op,b,"=",pow(a, b))
    elif op == "log":
        print("log",a,"(",b,") = ",logg(a, b),sep = '')
    elif op == "max":
        print("max(",a,", ",b,") = ",maxx(a, b),sep = '')
    elif op == "min":
        print("min(",a,", ",b,") = ",minn(a, b),sep = '')
    elif op == "abs":
        print("abs(",a,") = ",abss(a))
    elif op == "floor":
        print("floor(",a,") = ",floorr(a),sep = '')
    elif op == "ceil":
        print("ceil(",a,") = ",ceill(a),sep = '')
    elif op == "sin":
        print("sin(",a,") = ",sinn(a),sep = '')
    elif op == "cos":
        print("cos(",a,") = ",coss(a),sep = '')
    elif op == "tan":
        print("tan(",a,") = ",tann(a),sep = '')
    elif op == "asin":
        print("asin(",a,") = ",asinn(a),sep = '')
    elif op == "acos":
        print("acos(",a,") = ",acoss(a),sep = '')
    elif op == "atan":
        print("atan(",a,") = ",atann(a),sep = '')
    elif op == '!':
        print(a,"! = ",factorial(int(a)),sep = '')
    print("1:Yes / 0:No")
    flag = int(input("Please select whether you want to continue the game: "))
    if flag == 0:
        break
    print("")

部分代码截图

运行结果展示

上传到码云截图

  • caculator plus
    tkinter (用了又不完全会用 寄)
    只是弄出了界面图 (呜呜呜)
from tkinter import *
import tkinter as tk
cal = Tk()
cal.title("calculator plus")
cal.geometry("340x455") #窗口大小 字母x


btn = ("sin","cos","tan","asin","acos","atan",
        "(",")","<-","->","AC","CE",
        "abs",7,8,9,"**","*",
        "pi",4,5,6,"/","-",
        "e",1,2,3,"//","+",
        "!","log",0,".","%","=")
class work:
    def __init__(self):
        self.var = tk.StringVar()
        e = tk.Entry(cal, textvariable = self.var, relief = "sunken", font = (20), width = 30, bg = "white")
        e = tk.Entry(cal, textvariable = self.var, relief = "sunken",  width=30, bg="white")
        e.grid(row = 1, columnspan = 30, pady =8 , ipady = 20)
        for i in range(len(btn)):
              if btn[i] in range(0, 10):
                  b = tk.Button(cal, text = btn[i], font = (20), width = 4, height = 2, command = lambda : self.add(btn[i]) )
                  b.grid(row = 10 + i // 6, column = i % 6, padx = 3, pady = 3)
              else:
                  b = tk.Button(cal, text = btn[i], font = (20), width = 4,height = 2)
                  b.grid(row = 10 + i // 6, column = i % 6, padx = 3, pady = 3)
    def add(self, x):
          self.var.set(self.var.get() + str(x))
   #             b = tk.Button(cal,)
  #      elif btn[i] == "AC":
    #            b = tk.Button(command = delete())
    #    elif btn[i] == "CE":

    #    elif btn[i] == "=":


work()
cal.mainloop()

运行界面图展示

部分代码截图

就像这样 python真是太有趣辣

改日接着研究~ 敲电子木鱼哒哒哒
没写完的削代码就不用传gitee了叭

(三)实验过程中遇到的问题和解决过程

阿问题真是太多了呢一时不知从何说起也记不太清了呢

问题1:奇怪的button布局

问题1解决方案:

虽然但是并没真正解决,而是手动调合适的button长宽间距 电子木鱼敲敲敲

问题2:command函数自动执行了

问题2解决方案:
Tkinter中button按钮未按却主动执行command函数问题

问题3:点击数字按钮后entry上显示的都是等号

问题3解决方案:

不懂啊
开始加class、self、__init__什么的 没理解阿 悲
小停摆一会

其他(感悟、思考等)

python真是太有趣辣*n
删除线真是太好玩辣^n

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值