Advertisement
Guest User

Classe Numero

a guest
Feb 19th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. class Numero():
  2.   def __init__(self,stringaS,B):
  3.    assert(type(stringaS)==type(''))
  4.    assert(type(float(stringaS))==type(float('1'))),"la stringaS non e' composta da numeri"
  5.    assert(type(B)==type(int(1)))
  6.    self.num=stringaS #stringa di numeri interi e non che siano positivi o negativi
  7.    self.B=B #numero intero  
  8.  
  9.   def getStringa(self):
  10.    return self.num
  11.    
  12.   def max(self):    
  13.     Max= self.num[0]
  14.     for i in self.num:
  15.      Max=max(Max,i)
  16.      if i=='.':
  17.       Max=max(Max,Max)
  18.     return Max
  19.  
  20.   def negativo(self):
  21.     if self.num[0]=='-':
  22.      numeroNegativo='-'
  23.     else:
  24.      numeroNegativo='+'
  25.     return numeroNegativo
  26.  
  27.   def modulo(self):
  28.    if self.num[0]=='-':
  29.      modulo=self.num[1:]
  30.    else:
  31.      modulo=self.num
  32.    return modulo
  33.      
  34.  
  35.   def valoreCambioBase(self,s=0):    
  36.     A=Numero(self.num,self.B)
  37.     modulo=A.modulo()
  38.     if self.B==10:
  39.      return self.num
  40.     d=int(A.max())
  41.     assert(int(d)<int(self.B)),"non e' possibile eseguire la conversione"
  42.     c=modulo.find('.')
  43.     if c==-1:
  44.       for i in range(0,len(modulo)):
  45.        if modulo[i]==0:
  46.         s=s
  47.        else:
  48.         x=len(modulo)-(i+1)
  49.         n=int(modulo[i])*(self.B**x)
  50.         s=s+n
  51.     if c!=-1:
  52.      for i in range(0,c):
  53.        if modulo[i]==0:
  54.         s=s
  55.        else:
  56.         x=len(modulo[:c])-(i+1)
  57.         n=int(modulo[i])*(self.B**x)
  58.         s=s+n
  59.      for j in range(c+1,len(modulo)):
  60.        if modulo[j]==0:
  61.         s=s
  62.        else:
  63.         x=j-c
  64.         n=int(modulo[j])*(self.B**-x)
  65.         s=s+n
  66.     if len(modulo)<len(self.num):
  67.       print "il numero convertito e' :"
  68.       return '-'+str(s)
  69.     else:
  70.       print "il numero convertito e' :"
  71.       return '+'+str(s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement