20242317 2024-2025-2 《Python程序设计》实验二报告

20242317 2024-2025-2 《Python程序设计》实验二报告

课程:《Python程序设计》
班级:2423
姓名:林楚皓
学号:20242317
实验教师:王志强
实验日期:2025年3月26日
必修/选修:公选课

一、实验内容

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

二、实验过程及结果

(一)设计思路与实现功能

1.本次实验主要是熟悉函数的使用,故定义多个函数分别实现加减乘除等运算;
2.该计算器可实现实数或复数下的加、减、乘、除、指数、对数、除模、三角函数等运算;
3.该计算器可实现多轮运算,直至用户退出为止。

(二)具体实现步骤

1.编写代码

import math

def sum(num1, num2):
    return num1 + num2

def minus(num1, num2):
    return num1 - num2

def times(num1, num2):
    return num1 * num2

def divided(num1, num2):
    return num1 / num2

def power(num1, num2):
    return num1 ** num2

def modular(num1, num2):
    return num1 % num2

print("""★★★★★★★★★★★★★★★★★★★★★★★★★★★
  Welcome to Lin's calculator. Wish you a nice day!
  """)

flag = "Y"

while flag == "Y":
    ch = int(input("Please input the operation (complex operation[1], real number operation[2]):"))
    if ch == 1:
        num1 = complex(input("Please input a number:"))
        num2 = complex(input("Please input another number:"))
    if ch == 2:
        num1 = float(input("Please input a number:"))
        num2 = float(input("Please input another number:"))
    choice = input("Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):")
    if choice == "+":
        print(num1, choice, num2, "=", sum(num1, num2))
    if choice == "-":
        print(num1, choice, num2, "=", minus(num1, num2))
    if choice == "*":
        print(num1, choice, num2, "=", times(num1, num2))
    if choice == "/":
        if num2 == 0:
            print("The divisor cannot be zero!")
        else:
            print(num1, choice, num2, "=", divided(num1, num2))
    if choice == "%":
        print(num1, choice, num2, "=", modular(num1, num2))
    if choice == "^":
        print(num1, choice, num2, "=", power(num1, num2))
    if choice == "log":
        print(f"The logarithm of {num2} to the base {num1}" " =", math.log(num2, num1))
    if choice == "sin":
        print(choice, num1, "=", math.sin(num1))
    if choice == "cos":
        print(choice, num1, "=", math.cos(num1))
    if choice == "tan":
        print(choice, num1, "=", math.tan(num1))
    flag = input("Continue? (Y/N)")

print("""
   Copyright ©Lin Chuhao 2025. All rights reserved.
★★★★★★★★★★★★★★★★★★★★★★★★★★★""")

2.调试过程

设置断点,进入调试模式。

3.运行结果

(1)实数运算(依次为加、减、乘、除、指数、对数、除模、三角函数运算)

★★★★★★★★★★★★★★★★★★★★★★★★★★★
  Welcome to Lin's calculator. Wish you a nice day!
  
Please input the operation (complex operation[1], real number operation[2]):2
Please input a number:23
Please input another number:5326
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):+
23.0 + 5326.0 = 5349.0
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):2
Please input a number:345
Please input another number:3213
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):-
345.0 - 3213.0 = -2868.0
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):2
Please input a number:324
Please input another number:24
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):*
324.0 * 24.0 = 7776.0
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):2
Please input a number:13.2
Please input another number:5.3
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):/
13.2 / 5.3 = 2.490566037735849
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):2
Please input a number:45
Please input another number:8
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):%
45.0 % 8.0 = 5.0
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):2
Please input a number:4
Please input another number:7
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):^
4.0 ^ 7.0 = 16384.0
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):2
Please input a number:27
Please input another number:3
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):log
The logarithm of 3.0 to the base 27.0 = 0.33333333333333337
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):2
Please input a number:233
Please input another number:22
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):sin
sin 233.0 = 0.49873928180328125
Continue? (Y/N)N

   Copyright ©Lin Chuhao 2025. All rights reserved.
★★★★★★★★★★★★★★★★★★★★★★★★★★★

(2)复数运算(仅演示加、减、乘、除四则运算)

★★★★★★★★★★★★★★★★★★★★★★★★★★★
  Welcome to Lin's calculator. Wish you a nice day!
  
Please input the operation (complex operation[1], real number operation[2]):1
Please input a number:3+2j
Please input another number:4+5j
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):+
(3+2j) + (4+5j) = (7+7j)
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):1
Please input a number:65+32j
Please input another number:21-39j
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):-
(65+32j) - (21-39j) = (44+71j)
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):1
Please input a number:43+2j
Please input another number:19-93j
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):*
(43+2j) * (19-93j) = (1003-3961j)
Continue? (Y/N)Y
Please input the operation (complex operation[1], real number operation[2]):1
Please input a number:34+2j
Please input another number:14-9j
Please choose an operation (+, -, *, /, %, ^, log, sin(num1), cos(num1), tan(num1)):/
(34+2j) / (14-9j) = (1.6534296028880868+1.2057761732851986j)
Continue? (Y/N)N

   Copyright ©Lin Chuhao 2025. All rights reserved.
★★★★★★★★★★★★★★★★★★★★★★★★★★★

4.上传至Gitee

实验二:计算器

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

  • 问题1:不知如何在Markdown格式中插入代码块、插入链接等
  • 问题1解决方案:参考CSDN文章《markdown常用语法》,了解到连续三个反引号是插入代码块的语法、[链接名称文字](链接地址)是插入链接的语法。
  • 问题2:进行复数运算时,输入复数使用a+bi的形式会报错
  • 问题2解决方案:参考CSDN文章《Python中的复数》,了解到在Python中复数用a+bj来表示,虚数单位用j不用i的原因是i在Python中移作他用;且在电气工程和其他工程领域,也习惯用j来代表虚数单位,因为i习惯用于表示电流。

四、其它方面

参考CDSN文章python格式化输出方法汇总f字符串及其常见用法,学会了如何用f字符串进行字符串格式化:只需要在字符串前面加上f或F,并用花括号{}在字符串中表示要被替换的变量,其中花括号{}内直接填入要替换的变量。
如代码中的这一段:

    if choice == "log":
    print(f"The logarithm of {num2} to the base {num1}" " =", math.log(num2, num1))

就使用了f字符串,这样就避免了把一句话分割成多个引号,简化了语法。

参考资料

posted @ 2025-04-01 22:21  Howardlch123  阅读(8)  评论(0)    收藏  举报