1. 程式人生 > 其它 >20212115 實驗二 《python程式設計》實驗報告

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)
-  ...