python 做乙個簡單的計算器

2021-09-12 11:32:27 字數 1672 閱讀 2701

def jisuanqi():   

#實現加減乘除運算

a, b, c=shuru()

if c == "+":

z = a + b

print("%s+%s 的和為%s"%(a, b, z))

elif c == "-":

z = a - b

print("%s-%s 的差為%s" % (a, b, z))

elif c == "*":

z = a * b

print("%s*%s 的積為%s" % (a, b, z))

elif c == "/":

z = a / b

print("%s/%s 的商為%s" % (a, b, z))

else:

print("error operate")

def shuru():

#int是強轉為整數型 str為了強轉為字串

a= int(input("輸入數字1:"))

b= int(input("輸入數字2:"))

operate= str(input("輸入運算法則:"))

return a,b,operate

while 1:

#讓計算器可以迴圈使用

p=input("請輸入p的值:")

if p == 'y':

jisuanqi()

else:

break

class jisuanqi():

#通過類的方法啊

def __init__(self, a, b):

self.num1 = a

self.num2 = b

def add(self):

#加法z = int(self.num1) +int(self.num2)

print("a+b的和為:%s"%z)

def sub(self):

#減法z = int(self.num1) -int(self.num2)

print("a-b的差為:%s"%z)

def mul(self):

#乘法z = int(self.num1) *int(self.num2)

print("a*b的積為:%s"%z)

def div(self):

#除法z = int(self.num1) /int(self.num2)

print("a/b的商為:%s"%z)

while 1 :

#讓計算器可以迴圈使用

p = str(input("請輸入p值:"))

if p =="y":

a = int(input("請輸入數字1:"))

b = int(input("請輸入數字2:"))

jisuanqi = jisuanqi(a, b)

c = str(input("請輸入算術法則"))

if c=="+":

jisuanqi.add()

elif c =="-":

jisuanqi.sub()

elif c =="*":

jisuanqi.mul()

elif c =="/":

jisuanqi.div()

else:

break

做乙個iOS的簡單計算器 二

定義乙個處理基本操作的類 class calculatetool nsobject 初始化運算子棧 func initoptrstack 壓入運算數 func pushoperand operand string 壓入運算子 func pushoperator optr string 彈出乙個運算數...

乙個簡單的計算器

乙個非常簡單的計算器 來自sololearn 只能進行單一計算 while true print 設定 print enter 增加 to 增加 two numbers print enter 減去 to 減去 two numbers print enter 相乘 to 相乘 two numbers...

20 12 17 基於棧做乙個計算器

package stack public class calculator else else else else index if index expression.length while true num1 numberstack.pop num2 numberstack.pop oper o...