gigio^ne |
|
| Wowwwww!!!! Fine Calcolatrite romana in C. ed ora Beorn che ne pensi del linguaggio ruby? o crearci intorno un'interfaccia grafica in c? La vedo dura!!! CODICE // ************* CalcRoman.c ver 1.00 ************* // ************** Compilato con g++ *************** //*** Calcolatrice e convertitore numeri decimali in numeri romani e viceversa *** // //*** 09/01/2011 CalcRoman.c 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 // #include <stdio.h> #include <string.h> //Costanti #define I 1 #define V 5 #define X 10 #define L 50 #define C 100 #define D 500 #define M 1000 #define MAX 30 #define TRUE 1 #define FALSE 0 //Prototipi di funzioni int ContConvDec(); int ConvDecToRom(); int SumRomanNumber(); char convert_dec(); int convert_rom(); int StrReverse(); int Unisci(); //Dichiarazione variabili globali char dgt[] = "IVXLCDM"; char g[MAX],t[MAX],Rom[MAX],c; int num = 0,dec = 0,s = 0,z = 0,x = 0,Y = 0; int selezione = TRUE; int choice; //Corpo del programma int main() { while (selezione == TRUE) { printf("\nCALCOLATRICE E CONVERTITORE IN NUMERI ROMANI E VICEVERSA."); printf("\n"); printf("\nMenu principale.\n"); printf("----------------\n"); printf("\n1) - Convertitore Decimale Romano."); printf("\n2) - Convertitore Romano Decimale."); printf("\n3) - Calcolatrice Romana."); printf("\n4) - Uscita dal programma.\n"); printf("\n"); printf("\nScegli un'opzione da tra (1 e 4) -> "); scanf("%d",&choice); if (choice == 1) { ContConvDec(); //Chiama la funzione di conversione decimale romano. } if (choice == 2) { printf("\nInserisci un numero Romano da I a MMMCMXCIX -> "); scanf("%s", Rom); //chiede un numero romano (Stringa). ConvDecToRom(); //Chiama la funzione di conversione romano decimale. printf("\nNUMERO DECIMALE = %d\n",dec); printf("----------------------\n\n"); } if (choice == 3) { SumRomanNumber(); //Chiamata della calcolatrice romana. } if (choice == 4) { printf("\n\nGrazie per aver usato la Calcolatrice Romana!!\n"); break; } if (choice <= 0 || choice >= 5) { printf("\n\nScelta non valida. Programma terminato!\n"); selezione = FALSE; } } return 0; } int ContConvDec() { s = 0; z = 0; x = 0; printf("\nInserisci un numero decimale da 1 a 3999: "); scanf("%d", &num); //chiede un numero decimale. convert_dec(); //Converte il decimale in numero romano StrReverse(); //Inverte il numero romano (o meglio lo radrizza) printf("\n\nNUMERO ROMANO = %s\n",g); printf("-----------------------------\n"); } char convert_dec() //Funzione di conversione decimale romano. { int dg = 0; do { dg = num % 10; num = num / 10; if (dg == 1) { Unisci(); } if (dg == 2) { Unisci(); Unisci(); } if (dg == 3) { Unisci(); Unisci(); Unisci(); } if (dg == 4) { t[s] = dgt[x+1]; s++; Unisci(); } if (dg == 5) { t[s] += dgt[x+1]; s++; } if (dg == 6) { Unisci(); t[s] = dgt[x+1]; s++; } if (dg == 7) { Unisci(); Unisci(); t[s] = dgt[x+1]; s++; } if (dg == 8) { Unisci(); Unisci(); Unisci(); t[s]= dgt[x+1]; s++; } if (dg == 9) { t[s] = dgt[x+2]; s++; Unisci(); } x += 2; }while (num != 0); s++; t[s] = '\0'; } int StrReverse() //Funzione di inversione stringa (numero romano) { int r; for (r = s-2;r >= 0;r--) { g[z] = t[r]; z++; } g[z] = '\0'; } int Unisci() //Funzione per assegnare il numero romano { t[s] = dgt[x]; s++; } int ConvDecToRom() //Converte il romano in numero decimale { Y = 0; dec = 0; int dc = 0; int dc1 = 0; int xx = 0; int x1 = 1; int r; for (r = 0;r < strlen(Rom);r++) { c = Rom[xx]; dc = convert_rom(); c = Rom[x1]; dc1 = convert_rom(); if (dc >= dc1) dec += (dc + dc1) - dc1; if (dc < dc1) dec += (-dc * 2 + dc1) + dc - dc1; xx++; x1++; } return dec; } int convert_rom() { if (c == 'I') Y = I; if (c == 'V') Y = V; if (c == 'X') Y = X; if (c == 'L') Y = L; if (c == 'C') Y = C; if (c == 'D') Y = D; if (c == 'M') Y = M; return Y; } int SumRomanNumber() { char N1[MAX],N2[MAX],N3[MAX],OP; int f = 0; printf("\n\nImmettere i numeri romani da sommare, sotrarre, moltiplicare,o dividere max(MMMCMXCIX 3999)."); printf("\nImmettere l'espressione senza spazi."); printf("\n(Es: XXV+V e premere invio)"); printf("\nRisultato esempio = XXX\n"); printf("\nImmettere l'operazione Romana da eseguire -> "); scanf("%s",N1); //Estrae l'operatore. // Estrapola dalla stringa primo secondo numero e l'operatore z = 0; x = 0; int Nx = 0; int Ny = 0; int Risult = 0; for (z = 0; z < strlen(N1); z++) { if (N1[z] == '+' || N1[z] == '-' || N1[z] == '*' || N1[z] == '/') { OP = N1[z]; // Estrae l'operatore for(f = 0;f < z;f++) Rom[f] = N1[f]; // Estrae il primo numero Rom[f] = '\0'; ConvDecToRom(); Nx = dec; for(f = z+1;f < strlen(N1);f++) { Rom[x] = N1[f]; // Estrae il secondo numero x++; } Rom[x] = '\0'; ConvDecToRom(); Ny = dec; } } if (OP == '+') Risult = Nx + Ny; if (OP == '-') Risult = Nx - Ny; if (OP == '*') Risult = Nx * Ny; if (OP == '/') Risult = Nx / Ny; num = Risult; s = 0; z = 0; x = 0; convert_dec(); //Converte il decimale in numero romano StrReverse(); //Inverte il numero romano. printf("\n\nRisultato operazione = %s\n",g); printf("------------------------------\n"); } Funza bene se non sbagliate a digitare i numeri romani....
|
| |