XmX

Calcolatrice con numeri romani, tipo XX - IX = XI

« Older   Newer »
  Share  
gigio^ne
view post Posted on 16/12/2010, 21:30 by: gigio^ne
Avatar

Advanced Member

Group:
Member
Posts:
1,184
Location:
Sedna

Status:


RomanCalc Ver 1.01 - Versione in Python finale QUESTA L'HO PROVATA CON PYTHON 2.7 SU IPAD AIR 2 E IPHONE 5S FUNZA BENE.

1) - Semplificazione del listato sorgente
2) - Possibilitą di eseguire un'operazione Romana in un'unico input
3) - BUG FIX che richiamava in loop la funzione di conversione Romano Decimale

CODICE
#************* RomanCalc.py ver 1.01 *************
#************ Scritto in Python ver 2.7  *********
#*** Calcolatrice e convertitore numeri decimali in numeri romani e viceversa ***

#*** 17/12/2010 RomanCalc.py is licenced under LGPL. ***
#*** http://www.gnu.org/licenses/lgpl.html ***

#*** I'd be happy to hear if you use this in your software. ***
# Copyright (C) 2010 xyzangelo@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Le funzioni del programma sono illustrate nel primo listato
#
#**************************** MODIFICHE DELLA VER. 1.01 ********************************
# 1) Semplificazione del listato sorgente
# 2) Possibilitą di eseguire un'operazione Romana in un'unico input
# 3) Coretto il BUG che richiamava a loop la funzione di conversione Romano Decimale
#****************************************************************************************
#*** ATTENZIONE: oltre il 3999 il programma da i numeri nel vero senso
#*** della parola, es. digitate 5656 e vi esce XX.
import sys    
def SelezUno():
   Num = input("Inserire un numero decimale inferiore a 4000: ")
   print "numero romano = ",ConvertNum(Num)  
def SelezDue():
   print "Premere e lasciare premuto (Caps Lock) per comodita."
   print "ATTENZIONE: non sbagliare ad immettere i numeri romani. Pena errore nella conversione."
   print
   Rom = raw_input("Inserire un numero romano non superiore a MMMCMXCIX (3999): ")
   print "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 "Risultato = XXX"
   N1 = raw_input("Immettere l'operazione Romana da eseguire -> ")
   #*** 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 "Risultato 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 "Calcolatrice romana... ver 1.01"
   print "-------------------------------"
   print "1) = Convertitore Decimale Romano..."
   print "2) = Convertitore Romano Decimale..."
   print "3) = Calcolatrice Romana............"
   print "4) = Uscita dal programma..........."
   print
   Qst = input("Sciegliere un numero tra 1 e 4: ")
   if Qst == 1:
       SelezUno()
   elif Qst == 2:
       SelezDue()
   elif Qst == 3:
       SelezTre()
   elif Qst == 4:
       print "FINE. Grazie per aver usato CalcRoman.py"
       break


Ora funza benissimo... ^_^

Edited by gigio^ne - 13/5/2015, 23:16
 
Top
20 replies since 6/12/2010, 21:08   8216 views
  Share