python对字符串实现四则运算

华电北风吹
天津大学认知计算与应用重点实验室
日期:2015/9/12

根据小学学的四则元算法则,对输入的字符串实现比较大小,加法,减法,乘法,除法和求余等运算。

__author__ = 'zhengyi'
class StringArithmetic:
    # Class Condition: x,y are both non-negative

    # Compare x and y, if x>y retuen 1,x==y return 0, x<y resurn -1
    def Compare(self,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(self,x,y):
        result=''
        rise=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])+rise
            if temp>9:
                rise=1
                temp=temp-10
            else:
                rise=0
            result+=str(temp)
        if rise>0:
            result+=str(rise)
        return result[::-1]

    # Condition x>=y, resurn x-y
    def Minus(self,x,y):
        result=''
        own=0
        for i in range(1,len(y)+1):
            first=int(x[-i])-own
            second=int(y[-i])
            if first<second:
                own=1
                first+=10
            else:
                own=0
            result+=str(first-second)
        result=result.rstrip('0')
        if result=='':
            result='0'
        return result[::-1]

    # Condition: return k times of x, limit k to stop temp over limit
    def SigleMultiple(self,k,x):
        result=''
        rise=0
        k=int(k)
        for i in range(1,len(x)+1):
            temp=int(x[-i])*k+rise
            result+=str(temp%10)
            rise=temp//10
        result=result[::-1]
        if rise>0:
            result=str(rise)+result
        return result

    # return x*y
    def Multiple(self,x,y):
        if len(x)<len(y):
            x,y=y,x
        result='0'
        zright=''
        for i in range(1,len(y)+1):
            temp=self.SigleMultiple(y[-i],x)+zright
            result=self.Add(result,temp)
            zright+='0'
        return result

    # x=ky+b, return k+' '+b
    def Recount(self,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=self.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(self,x,y):
        k=0
        while True:
            temp=self.SigleMultiple(k,y)
            i=self.Compare(x,temp)
            if i==1:
                k+=1
                continue
            if i==0:
                return str(k),'0'
            if i==-1:
                break
        temp=self.Minus(x,self.SigleMultiple(k-1,y))
        return str(k-1),temp

s=StringArithmetic()
x='123'
y='321'
result=s.Multiple(x,y)
print(result)

你可能感兴趣的:(python对字符串实现四则运算)