Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Numero():
- def __init__(self,stringaS,B):
- assert(type(stringaS)==type(''))
- assert(type(float(stringaS))==type(float('1'))),"la stringaS non e' composta da numeri"
- assert(type(B)==type(int(1)))
- self.num=stringaS #stringa di numeri interi e non che siano positivi o negativi
- self.B=B #numero intero
- def getStringa(self):
- return self.num
- def max(self):
- Max= self.num[0]
- for i in self.num:
- Max=max(Max,i)
- if i=='.':
- Max=max(Max,Max)
- return Max
- def negativo(self):
- if self.num[0]=='-':
- numeroNegativo='-'
- else:
- numeroNegativo='+'
- return numeroNegativo
- def modulo(self):
- if self.num[0]=='-':
- modulo=self.num[1:]
- else:
- modulo=self.num
- return modulo
- def valoreCambioBase(self,s=0):
- A=Numero(self.num,self.B)
- modulo=A.modulo()
- if self.B==10:
- return self.num
- d=int(A.max())
- assert(int(d)<int(self.B)),"non e' possibile eseguire la conversione"
- c=modulo.find('.')
- if c==-1:
- for i in range(0,len(modulo)):
- if modulo[i]==0:
- s=s
- else:
- x=len(modulo)-(i+1)
- n=int(modulo[i])*(self.B**x)
- s=s+n
- if c!=-1:
- for i in range(0,c):
- if modulo[i]==0:
- s=s
- else:
- x=len(modulo[:c])-(i+1)
- n=int(modulo[i])*(self.B**x)
- s=s+n
- for j in range(c+1,len(modulo)):
- if modulo[j]==0:
- s=s
- else:
- x=j-c
- n=int(modulo[j])*(self.B**-x)
- s=s+n
- if len(modulo)<len(self.num):
- print "il numero convertito e' :"
- return '-'+str(s)
- else:
- print "il numero convertito e' :"
- return '+'+str(s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement