Avrei urgente bisogno di un'algoritmo per la conversione di numeri decimali in numeri romani.
Ho già googlato ma non ho trovato niente.
Grazie in anticipo !![]()
Avrei urgente bisogno di un'algoritmo per la conversione di numeri decimali in numeri romani.
Ho già googlato ma non ho trovato niente.
Grazie in anticipo !![]()
Questo è un esercizio del genere che stampa i n romani da 1 a 100.
Ciaocodice:#include<stdio.h> int main( void ) { int loop, div, mod; printf( " Roman\nNumeral\t\tDecimal\n" ); for ( loop = 1; loop <= 100; loop++ ) { div = loop / 10; mod = loop % 10; /* switch structure for tens digit */ switch ( div ) { case 0: break; case 1: printf( "X" ); break; case 2: printf( "XX" ); break; case 3: printf( "XXX" ); break; case 4: printf( "XL" ); break; case 5: printf( "L" ); break; case 6: printf( "LX" ); break; case 7: printf( "LXX" ); break; case 8: printf( "LXXX" ); break; case 9: printf( "XC" ); break; case 10: printf( "C" ); break; default: break; } /* switch structure for ones digit */ switch( mod ) { case 0: printf( "\t\t%4d\n", div * 10 ); break; case 1: printf( "I\t\t%4d\n", div * 10 + mod ); break; case 2: printf( "II\t\t%4d\n", div * 10 + mod ); break; case 3: printf( "III\t\t%4d\n", div * 10 + mod ); break; case 4: printf( "IV\t\t%4d\n", div * 10 + mod ); break; case 5: printf( "V\t\t%4d\n", div * 10 + mod ); break; case 6: printf( "VI\t\t%4d\n", div * 10 + mod ); break; case 7: printf( "VII\t\t%4d\n", div * 10 + mod ); break; case 8: printf( "VIII\t\t%4d\n", div * 10 + mod ); break; case 9: printf( "IX\t\t%4d\n", div * 10 + mod ); break; case 10: printf( "X\t\t%4d\n", div * 10 + mod ); break; default: break; } } system ("PAUSE"); return 0; }![]()
L'algoritmo funziona ( modificato naturalmente ) l'unico problema e che converte i numeri solo fino a 110 ed espanderlo significherebbe un sacco di codice in più, non c'è ne uno più ottimale ?
Grazie comunque![]()
Perche' non provi a modificarlo ed ampliarlo tu? Il suggerimento mi sembra un buon punto di partenza ...
Scuse moi !![]()
Un piccolo piccolo aiuto : Wiki
01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
All errors are undocumented features waiting to be discovered.
codice:#include <stdio.h> #include <string.h> #include <math.h> int *CreateNumber(int n); int main(){ int num; char *Onesnumerals[10]={"","I","II","III","IV","V","VI","VII","VIII","IX"}; char *Tensnumerals[10]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; char *Hundredsnumerals[10]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; char *Thousandsnumerals[5]={"","M","MM","MMM","MMMM"}; int *Ptr; printf("inserisci un numero intero compreso fra 0 e 4999:\n"); scanf("%d", &num); if ((num>0)&&(num<5000)){ Ptr=CreateNumber(num); printf("%s%s%s%s\n",Thousandsnumerals[Ptr[0]],Hundredsnumerals[Ptr[1]], Tensnumerals[Ptr[2]], Onesnumerals[Ptr[3]]); } else printf("Numero non valido"); return 0; } int *CreateNumber(int n){ int tens,ones,hundreds,thousands,*Vettore; int new_num = n; thousands = floor(new_num / 1000); new_num -= thousands * 1000; hundreds = floor(new_num / 100); new_num -= hundreds * 100; tens = floor(new_num / 10); new_num -= tens * 10; ones = floor(new_num / 1); *Vettore=calloc(4, sizeof(int)); Vettore[0]=thousands; Vettore[1]=hundreds; Vettore[2]=tens; Vettore[3]=ones; return Vettore; }![]()
Raga!!! Ma quanto codice scrivete per convertire in numeri romani??!!!![]()
Ecco:
codice:#define MIN_ROMAN 1 #define MAX_ROMAN 4999 int num2roman (int number, char *buffer) { int i; static struct romantable { int value; char *roman; } romanTable[13] = { { 1000, "M" }, { 900, "CM" }, { 500, "D" }, { 400, "CD" }, { 100 , "C" }, { 90 , "XC" }, { 50 , "L" }, { 40 , "XL" }, { 10 , "X" }, { 9 , "IX" }, { 5 , "V" }, { 4 , "IV" }, { 1 , "I" } }; if (number < MIN_ROMAN || number > MAX_ROMAN) return 0; buffer[0] = '\0'; for (i = 0; i < 13; i++) { while (number >= romanTable[i].value) { strcat (buffer, romanTable[i].roman); number -= romanTable[i].value; } } return 1; }
Andrea, andbin.dev – Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
java.util.function Interfaces Cheat Sheet — Java Versions Cheat Sheet
non mi sembra troppo... considera che ho messo anche gli include, il prototipo e la main che tu hai tolto e vedi che più o meno è la stessa quantità di codice quella che abbiamo scritto.
L'unico problema è che nel mio caso ho dovuto dichiarare altre quattro variabili interne alla funzione perchè non riesco a prelevare il valore del vettore che alloco. Dichiarandolo così: *Vettore[0] non restituisce una mazza... se qualcuno mi desse una dritta in tal senso sarebbe molto apprezzata
Bella comunque la tua tecnica, non ci sarei mai arrivato.![]()