XmX

Calcolatrice con numeri romani, tipo XX - IX = XI

« Older   Newer »
  Share  
gigio^ne
view post Posted on 19/5/2015, 10:51 by: gigio^ne
Avatar

Advanced Member

Group:
Member
Posts:
1,184
Location:
Sedna

Status:


^_^ della serie come ammazzare il tempo a casa
una versione della calcolatrice romana che non fa caso a lettere minuscole o maiuscole.

CODICE
# *** Convertitore da numeri romani a decimali e viceversa
# *** e calcolatrice con numeri romani
# *** in questa versione unicamente per python 2.7
# *** č possibile utilizzare sia lettere minuscole che maiuscole
# *** per digitare i numeri romani.
import sys
def SelezUno():
   Num = input("Inserire un numero decimale inferiore a 4000: ")
   print "Il numero romano č = ",ConvertNum(Num)
def SelezDue():
   print
   print "ATTENZIONE: non sbagliare ad immettere i numeri romani. Pena errore nella conversione."
   print
   Nz = raw_input("Inserire un numero romano non superiore a MMMCMXCIX (3999): ")
   Rom = Nz.upper()
   print "Il numero decimale č = ",ConvRomToDec(Rom)  
def SelezTre():
   print
   print "immettere i numeri romani da sommare, sotrarre, moltiplicare,o dividere max(MMMCMXCIX 3999)."
   print "Immettere l'espressione senza spazi."
   print "(Es: XXV+V e premere invio)"
   print
   Nz = raw_input("Immettere l'operazione Romana da eseguire -> ")
   N1 = Nz.upper()
   #*** Estrapola dalla stringa  primo secondo numero e l'operatore
   z = 0
   Nx = 0
   Ny = 0
   Risult = 0
   for z in range(len(N1)):
       if N1[z:z+1] == '+' or N1[z:z+1] == '-' or N1[z:z+1] == '*' or N1[z:z+1] == '/':
           p1 = N1[:z]     #* Estrae il primo numero
           op = N1[z:z+1]  #* Estrae l'operatore
           N2 = N1[z+1:]   #Estrae il secondo numero
   Nx = ConvRomToDec(p1)
   Ny = ConvRomToDec(N2)
   if op == '+':
       Risult = Nx + Ny
   elif op == '-':
       Risult = Nx - Ny
   elif op == '*':
       Risult = Nx * Ny
   elif op == '/':
       Risult = int(Nx / Ny)
   print
   print "Il risultato dell'operazione č = ",ConvertNum(Risult)    
def ConvRomToDec(Rom):
   x = 0
   x1 = 1
   t = ""
   dc = 0
   dc1 = 0
   dec = 0
   z = 0
   for z in range(len(Rom)):
       t = Rom[x:x1]
       dc = RomToDec(t)
       t = Rom[x1:x1 + 1]
       dc1 = RomToDec(t)
       if dc >= dc1:      
           dec += (dc + dc1) - dc1
       elif dc < dc1:    
           dec +=  (-dc * 2 + dc1) + dc - dc1
       x += 1
       x1 += 1
   return dec
def ConvertNum(Num):
   dgt = "IVXLCDM"      
   x = 0
   t = ""
   while Num > 0:      
       dg = Num % 10  
       Num = int(Num / 10)
       if dg == 1:
           t = dgt[x:x+1] + t
       elif dg == 2:
           t = dgt[x:x+1] + dgt[x:x+1] + t
       elif dg == 3:
           t = dgt[x:x+1] + dgt[x:x+1] + dgt[x:x+1] + t
       elif dg == 4:
           t = dgt[x:x+2] + t
       elif dg == 5:
           t = dgt[x + 1:x+2] + t
       elif dg == 6:
           t = dgt[x + 1:x+2] + dgt[x:x+1] + t
       elif dg == 7:
           t = dgt[x + 1:x+2] + dgt[x:x+1] + dgt[x:x+1] + t
       elif dg == 8:
           t = dgt[x + 1:x+2] + dgt[x:x+1] + dgt[x:x+1] + dgt[x:x+1] + t
       elif dg == 9:
           t = dgt[x:x+1] + dgt[x + 2:x+3] + t
       x += 2
   return t
def RomToDec(t):
   Y = 0
   I = 1
   V = 5
   X = 10
   L = 50
   C = 100
   D = 500
   M = 1000
   if t == "I":
       Y = I
   elif t == "V":
       Y = V
   elif t == "X":
       Y = X
   elif t == "L":
       Y = L
   elif t == "C":
       Y = C
   elif t == "D":
       Y = D
   elif t == "M":
       Y = M
   return Y
while 1:
   print
   print "BENVENUTI NELLA CALCOLATRICE ROMANA.     ver 1.04"
   print "--------------------------------------------------"
   print "1) = Convertitore da decimale a numero romano..."
   print "2) = Convertitore da romano  a numero decimale.."
   print "3) = Utilizza la calcolatrice romana............"
   print "4) = Uscita dal programma......................."
   print
   Qst = raw_input("Sciegliere un'opzione tra 1 e 4: ")
   try:
       if Qst == "1":
           SelezUno()
       elif Qst == "2":
           SelezDue()
       elif Qst == "3":
           SelezTre()
       elif Qst == "4":
           print "FINE. Grazie per aver usato CalcRoman.py"
           break
   except IOError as err:
       # Uso di IOError as an instance.come istanza
       print("Error:", err)
       print("Number:", err.errno)
 
Top
20 replies since 6/12/2010, 21:08   8215 views
  Share