⽤Python实现⼩学⽣四则运算⼀、需求分析
根据⼩学学的四则元算法则,对输⼊的字符串实现⽐较⼤⼩,加法,减法,乘法,除法和求余等运算。⼆、实现环境
coffee是什么意思
本程序是在Python3.7、pycharm下实现
⼆、代码
class StringArithmetic:
# 输⼊的x,y皆为⾮负
# ⽐较⼤⼩ x>y retuen 1,x==y return 0, x<y resurn -1
def Compare(lf,x,y):
m=len(x)
n=len(y)
if m>n:
return 1
杨明学if m<n:
return -1
for i in range(m):
if x[i]>y[i]:
return 1
if x[i]<y[i]:
return -1
return 0
#加法运算
def Add(lf,x,y):
result=''
ri=0
maxlen=max(len(x),len(y))
x=x.zfill(maxlen)
y=y.zfill(maxlen)
for i in range(1,maxlen+1):
temp=int(x[-i])+int(y[-i])+ri
if temp>9:
ri=1
temp=temp-10
el:
ri=0
result+=str(temp)
if ri>0:
result+=str(ri)
return result[::-1]
# 减法运算 x>=y, resurn x-y
def Minus(lf,x,y):
result=''
own=0
for i in range(1,len(y)+1):
first=int(x[-i])-own
cond=int(y[-i])
平行光线
if first<cond:
own=1
first+=10
el:
own=0
result+=str(first-cond)
result=result.rstrip('0')
if result=='':
贵族姓氏
result='0'
return result[::-1]
return result[::-1]
# Condition: return k times of x, limit k to stop temp over limit def SigleMultiple(lf,k,x):
result=''
ri=0
k=int(k)
for i in range(1,len(x)+1):
temp=int(x[-i])*k+ri
result+=str(temp%10)
ri=temp//10
result=result[::-1]
if ri>0:
result=str(ri)+result
return result
# return x*y
def Multiple(lf,x,y):
if len(x)<len(y):
x,y=y,x
result='0'
zright=''
for i in range(1,len(y)+1):
temp=lf.SigleMultiple(y[-i],x)+zright
result=lf.Add(result,temp)
zright+='0'
return result
# x=ky+b, return k+' '+b
def Recount(lf,x,y):
m=len(x)
n=len(y)
s=n-1
k=''
b=x[0:s]
for i in range(s,m):
b+=x[i]
temp=lf.SingleRecount(b,y)
k+=temp[0]
b=temp[1]
if len(k)==0:
k='0'
return k,b
# x=ky+b, return k,b by loop. faster for k is smaller白色五行属什么
def SingleRecount(lf,x,y):
如果我是你
k=0
while True:
temp=lf.SigleMultiple(k,y)
i=lf.Compare(x,temp)
if i==1:
k+=1
continue
if i==0:
return str(k),'0'
if i==-1:
break
temp=lf.Minus(x,lf.SigleMultiple(k-1,y))
return str(k-1),temp
s=StringArithmetic()
x='15'
y='15'
result=s.SigleMultiple(x,y)
result1=s.Add(x,y)
print(result)
print(result)
print(result1)
result2=s.SingleRecount(x,y)
print(result2)
result3=s.Minus(x,y)
print(result3)
三、程序结果
盐焗鸡翅的做法
四、PSP表格
由于本⼈刚学Python不久,有参考⼤家的程序代码,在此声明离愁