20182304张子正 2020-2021-2 《Python程序设计》实验二报告

20182304张子正 2020-2021-2 《Python程序设计》实验二报告

课程:《Python程序设计》
班级: 1823
姓名: 张子正
学号:20182304
实验教师:王志强
实验日期:2021年4月29日
必修/选修: 公选课

1.实验内容

(一)实验内容

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

2.实验过程及结果

设计与实现

  • 设计思想:用函数的形式分别实现不同功能模块,最后整合在一起。

  • 四则运算

    • 注意除数不能为0,用while循环进行检查。
def Cal1(a, b, operator):
    if operator == "+":
        return a + b
    elif operator == "-":
        return a - b
    elif operator == "*":
        return a * b
    elif operator == "/":
        return a / b

def operation1():
    print("现在进行基本四则运算")
    operator = input("请输入运算符号:+,-,*,/:")
    a = int(input("输入前一个数: "))
    b = int(input("输入后一个数: "))
    while b == 0 and operator =="/":
        print("输入错误!除数不能为0!")
        b = int(input("重新输入后一个数: "))
    print(str(a) + operator + str(b) + " = ", Cal1(a, b, operator))

  • 模运算
    • 注意运算性质,利用while判断,p必须为正整数,q为整数且不能为0。
def operation2():
    print("现在进行取模运算")
    print("请按照提示输入取模运算的两个操作数")
    x = int(input("请输入正整数p:"))
    y = int(input("请输入整数q:"))
    while y == 0 :
        print("输入错误!q不能为0!")
        y = int(input("请重新输入整数除数q:"))
    while x < 0 :
        print("输入错误!p必须为正整数!")
        x = int(input("请重新输入正整数p:"))
    print(str(x) + " Mod " + str(y) + " = ",x % y)
  • 幂运算
def operation3():
    print("现在进行求幂运算")
    a = float(input("请输入底数:"))
    b = float(input("请输入指数:"))
    print(str(a) + " ** " + str(b) +" = ", a ** b)
  • 求阶乘
def operation4():
    print("现在进行求阶乘运算")
    n = int(input("请输入一个整数:"))
    print(str(n),"! = ", math.factorial(n))
  • 解一元二次方程
    • 注意区分各种情况,要考虑虚数。
def operation5():
    print("现在进行解一元二次方程")
    a = float(input("请输入a:"))
    b = float(input("请输入b:"))
    c = float(input("请输入c:"))
    d = b*b - 4*a*c
    if d >= 0:
        x1 = (-b+math.sqrt(d)) / (2*a)
        x2 = (-b-math.sqrt(d)) / (2*a)
        if d == 0:
            print("X1 = X2 = "+str(x1))
        else:
            print("X1 = "+str(x1)+'\t'+"X2 = "+str(x2))
    else:
        x1 = str(-b/(2*a)) + '+' + str(math.sqrt(-d)/(2*a)) + 'i'
        x2 = str(-b/(2*a)) + '-' + str(math.sqrt(-d)/(2*a)) + 'i'
        print("X1 = "+x1+'\t'+"X2 = "+x2)
  • 主函数
if __name__ == "__main__":
 print("欢迎使用计算器!")
 flag = 1
 while flag == 1:
    print("-" * 30)
    print("本计算器提供的功能有:1.基本四则运算||2.取模运算||3.求幂运算||4.求阶乘")
    print("如您想退出使用,请输入0:")
    print("-" * 30)
    a = int(input("请从中选择您想要实现的功能,输入对应的数字:"))
    if a == 1:
        operation1()
    elif a == 2:
        operation2()
    elif a == 3:
        operation3()
    elif a == 4:
        operation4()
    elif a == 5:
        operation5()
    flag = int(input("继续请输入1,退出请输入0:"))

 print("成功退出计算器!")

实验结果

  • 四则运算

  • 模运算

  • 幂运算

  • 求阶乘

  • 解一元二次方程

码云链接

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

  • 问题1:没有将Python和之前学过的语言完全区分开,比如说Python与操作是and,Python没有数据类型,需要强制转换,Python中if\while要注意缩进问题与冒号。

    • 问题1解决方案:编译器报错后及时查找课本,参考正确语法。
  • 问题2:参考之前学过的Java与C,想用switch/case语句实现计算器功能,似乎不可行。

    • 问题2解决方案:查阅相关资料,发现与Java、C\C++等语言不同,Python中是不提供switch/case语句的,只能寻找代替方案,最终使用if…elif…elif…else 实现switch/case功能,感觉代码有点冗余。当然也可以用字典实现,但是程序简单,没有必要。
  • 问题3:一元二次方程实现相对困难,缺乏思路。

    • 问题3解决方案:将之前写过的C的一元二次方程改为Python即可。

其他(感悟、思考等)

  • 由于本人没有涉及过于复杂的功能,所以本次实验相对简单,要注意的是掌握每个运算的性质 ,保障正确的输入与输出,测试时要重视边界值测试。
  • Python与之前学过的语言稍有差异,应该多加练习,更好的掌握这门语言。
  • 使用函数与模块化设计思路能够使编程结构清晰,思路简洁。

参考资料

posted @ 2021-04-29 22:44  20182304张子正  阅读(65)  评论(0编辑  收藏  举报