20212115 实验二 《python程序设计》实验报告

实验二 计算器设计

#20212115 2021-2022-2 《python程序设计》 实验报告二

课程:

课程:《Python程序设计》
班级: 2121
姓名: 朱时鸿
学号:20212115
实验教师:王志强
实验日期:2022年3月31日
必修/选修: 公选课

(一)实验内容

  • 设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。

  • 考核基本语法、判定语句、循环语句、逻辑运算等知识点

(二)实验要求

创建工程项目,使用Python语言实现具体的操作运算,并完成程序调试和运行,代码托管到码云。

注:在华为ECS服务器(OpenOuler系统)和物理机(Windows/Linux系统)上使用VIM、IDLE、Pycharm等工具编程实现。

(三)实验报告

## 1.实验内容

1,学习到python语言中“+”作用为拼接,重复字符串为*

>>> s4="hello world"
>>> s4[:5:2]
'hlo'

2.学会使用split

>>> sl = "hello world 你好 世界"

>>> s1 = "hello world 你好 世界"
>>> s1.split()
['hello', 'world', '你好', '世界']

3.学会使用split(,)来消除字符

>>> s2="hello#world#你好#shijie"
>>> s2.split("#",1)
['hello', 'world#你好#shijie']

4.学会使用count find 函数来判断一个有几个字符

>>>s1.count("#")
>>>0

5.用upper 和 lower来时字母变大写或变小写

6.去掉字符串收尾的字符:strip(),lstrip(),rstrip();

默认去掉 空格 、\n  \t   \r回车等特殊字符

7.学会使用%操作符

-:使左对齐

+:右对齐

0:右对齐,复数前面加负号,用零填充

m:可选参数,表示占有宽度

8.学会了怎样表示复数

a=complex(input("第一个复数:\n"))
b=complex(input("第二个复数:\n"))

9.输出

print(complex1,op,complex2,"=",result)
print("a "+op+"b =",result,"\n")




10.循环

while(flag == True):
    choice = input("请选择你要选择的计算器类型:0为普通计算器,1为复数计算器:\n");
    op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");

11.判断语句

   if op == "0":
        flag = False;
        print("已退出,祝你生活愉快");
        break;
    if choice == "0":
        a = int(input("请输入操作数1:"));
        b = int(input("请输入操作数2:"));




## 2. 实验过程及结果
实验编程计算器的代码

                   

import math;
flag = True;
print("besti专属计算器!");
while(flag == True):
    choice = input("请选择你要选择的计算器类型:0为普通计算器,1为复数计算器:\n");
    op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");
    if op == "0":
        flag = False;
        print("已退出,祝你生活愉快");
        break;
    if choice == "0":
        a = int(input("请输入操作数1:"));
        b = int(input("请输入操作数2:"));
    elif choice == "1":
        a = complex(input("请输入操作数1:"));
        b = complex(input("请输入操作数2:"));
    result = 0;
    if op == "+":
        result = a + b;
    elif op == "-":
        result = a - b;
    elif op == "*":
        result = a * b;
    elif op == "/":
        result = a / b;
    else:
        print("输入错误,请重新输入\n");
        continue;
    print(str(a)+str(op)+str(b)+"="+str(result)+"\n");
    '''
    elif choice == "1":
        op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");
        if op == "0":
            flag = False;
            print("已退出,祝你生活愉快");
            break;
        
        a1 = input("请输入第一个数字的实部:\n");
        a2 = input("请输入第一个数字的虚部:\n");
        b1 = input("请输入第二个数字的实部:\n");
        b2 = input("请输入第二个数字的虚部:\n");
        
        complex1 = complex(input("请输入第一个数:\n"));
        complex2 = complex(input("请输入第二个数:\n"));
        if op == "+":
            result = complex1 + complex2;
        if op == "-":
            result = complex1 - complex2;
        if op == "*":
            result = complex1 * complex2;
        if op == "/":
            result = complex1 / complex2;
        print(str(complex1)+op+str(complex2)+"="+str(result)+"\n");
        if op == "+":
            result1 = a1 + b1;
            result2 = b1 + b2;
        if op == "-":
            result1 = a1 - b1;
            result2 = b1 - b2;
        if op == "*":
            result1 = a1*b1 - a2*b2;
            result2 = a1*b2 + b1*a2;
        print(str(a1)+"+"+str(a2)+"i"+str(op)+str(b1)+"+"+str(b2)+"i="+str(result1)+"+"+str(result2));
    '''

 再本次实验中的收获与重要的知识点在“内容板块”,最后的结果:成功编出计算器的代码并成功运行

  

## 3. 实验过程中遇到的问题和解决过程
- 问题1:出现缩进的问题
- 问题1解决方案:使用tap键
- 问题2:编程思路出现问题

if choice=="0":
a=int(input("输入a\n"))
b=int(input("输入b\n"))
elif choice=="1":
a=complex(input("第一个复数:\n"))
b=complex(input("第二个复数:\n"))


- 问题2解决方案:请教同学
- ...


## 其他(本次课上所写的计算器代码)

import math;
flag = True;
print("besti专属计算器!");
while(flag == True):
    choice = input("请选择你要选择的计算器类型:0为普通计算器,1为复数计算器:\n");
    op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");
    if op == "0":
        flag = False;
        print("已退出,祝你生活愉快");
        break;
    if choice == "0":
        a = int(input("请输入操作数1:"));
        b = int(input("请输入操作数2:"));
    elif choice == "1":
        a = complex(input("请输入操作数1:"));
        b = complex(input("请输入操作数2:"));
    result = 0;
    if op == "+":
        result = a + b;
    elif op == "-":
        result = a - b;
    elif op == "*":
        result = a * b;
    elif op == "/":
        result = a / b;
    else:
        print("输入错误,请重新输入\n");
        continue;
    print(str(a)+str(op)+str(b)+"="+str(result)+"\n");
    '''
    elif choice == "1":
        op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");
        if op == "0":
            flag = False;
            print("已退出,祝你生活愉快");
            break;
       
        a1 = input("请输入第一个数字的实部:\n");
        a2 = input("请输入第一个数字的虚部:\n");
        b1 = input("请输入第二个数字的实部:\n");
        b2 = input("请输入第二个数字的虚部:\n");
       
        complex1 = complex(input("请输入第一个数:\n"));
        complex2 = complex(input("请输入第二个数:\n"));
        if op == "+":
            result = complex1 + complex2;
        if op == "-":
            result = complex1 - complex2;
        if op == "*":
            result = complex1 * complex2;
        if op == "/":
            result = complex1 / complex2;
        print(str(complex1)+op+str(complex2)+"="+str(result)+"\n");
        if op == "+":
            result1 = a1 + b1;
            result2 = b1 + b2;
        if op == "-":
            result1 = a1 - b1;
            result2 = b1 - b2;
        if op == "*":
            result1 = a1*b1 - a2*b2;
            result2 = a1*b2 + b1*a2;
        print(str(a1)+"+"+str(a2)+"i"+str(op)+str(b1)+"+"+str(b2)+"i="+str(result1)+"+"+str(result2));


    '''

感想:想学好python不易,需要自己付出努力以及遇见一位好老师,很幸运由王老师教课


下为码云的地址

https://gitee.com/zhu-shihong/zhu-shihongs-warehouse/commit/b84aafa140baa5847c177ea314ec0fc23ef7519e
## 参考资料

-  [《Java程序设计与数据结构教程(第二版)》](https://book.douban.com/subject/26851579/)

-  [《Java程序设计与数据结构教程(第二版)》学习指导](http://www.cnblogs.com/rocedu/p/5182332.html)
-  ...

 

posted @ 2022-03-31 23:39  六月的鸿  阅读(229)  评论(0)    收藏  举报