Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Programa q pase numeros decimales a numeros romanos

Estas en el tema de Programa q pase numeros decimales a numeros romanos en el foro de C/C++ en Foros del Web. wenas a todos!!! me gustaria q me ayudaseis a crear un programa en C q pasara numeros a numeros romanos del 1 al 3999es un ...
  #1 (permalink)  
Antiguo 13/01/2010, 04:08
 
Fecha de Ingreso: enero-2010
Mensajes: 1
Antigüedad: 14 años, 3 meses
Puntos: 0
Programa q pase numeros decimales a numeros romanos

wenas a todos!!! me gustaria q me ayudaseis a crear un programa en C q pasara numeros a numeros romanos del 1 al 3999es un trabajito y neceseto tenerlo pra dentro de tres dias! si pudierais darme una sloucion...! solo podemos hacerlos con bucles y con if. gracias a todos por vuestra ayuda!! un saludo!
  #2 (permalink)  
Antiguo 14/01/2010, 09:18
Avatar de romulo404  
Fecha de Ingreso: enero-2010
Mensajes: 4
Antigüedad: 14 años, 3 meses
Puntos: 0
Respuesta: Programa q pase numeros decimales a numeros romanos

O_O eso estuvo entretenido.. pero aun no resuelvo mi problema de los numeros grandes alguien por favor ayuda soy muy noob

esta respuesta es la que yo hice.. no sera la mas rapida ni la mejor :D pero como dije antes estuvo entretenido escribirla

Código C++:
Ver original
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7.  
  8. template <class A>
  9. A to(string s){  A o;  istringstream os(s);  os>>o;  return o;}
  10.  
  11. template <class A>
  12. string toString(A s){ ostringstream is;  is<<s;  return is.str();}
  13.  
  14.  
  15. string getLetter(long int n){
  16.     switch(n){
  17.     case 1000000 : return "|M";
  18.     case 500000 : return "|D";
  19.     case 100000 : return "|C";
  20.     case 50000 : return "|L";
  21.     case 10000 : return "|X";
  22.     case 5000 : return "|V";
  23.     case 1000 : return "M";
  24.     case 500 : return "D";
  25.     case 100 : return "C";
  26.     case 50 : return "L";
  27.     case 10 : return "X";
  28.     case 5 : return "V";
  29.     case 1 : return "I";
  30.     }
  31. }
  32.  
  33. string ArabicToRoman( string myArabicNum) {
  34.         long int arabic=to<long int>(myArabicNum);
  35.  
  36.     if( arabic > 3999999 ) { return "el numero es mayor a 3999999... los romanos no sabian de inflacion"; }
  37.     if( arabic <= 0 ) { return "los romanos no conocian los negativos :D.. siempre fuerons muy positivos"; }
  38.     int num_nines,i,i2;
  39.     string subnum,sArabic,myRomanNum;
  40.     for( long int x = 1000000; x >= 1; x /= 10 ) {
  41.         switch( int( arabic / x ) ) {
  42.             case 1 :
  43.                 myRomanNum += getLetter(x);
  44.                 break;
  45.             case 2 :
  46.                 myRomanNum += getLetter(x)+ getLetter(x) ;
  47.                 break;
  48.             case 3 :
  49.                 myRomanNum += getLetter(x)+ getLetter(x) + getLetter(x);
  50.                 break;
  51.             case 4 :
  52.                     num_nines = 1,i2 = 0;
  53.                     subnum = "0.";sArabic=toString(arabic);
  54.                     for( i = 1; sArabic[i] == '9'; i++ ) { num_nines *= 10; i2 = i; }
  55.                     for( i = 1; i <= i2; i++ ) { subnum += '9'; }
  56.                     if( sArabic[ i2+1] == '5' ) {
  57.                         myRomanNum += getLetter(x / ( 2 * num_nines ))+getLetter(5 * x);
  58.                         subnum += '5';
  59.                     } else {
  60.                         myRomanNum += getLetter(x / num_nines)+ getLetter(5 * x);
  61.                     }
  62.                     arabic-= to<double>(subnum) * x;
  63.                 break;
  64.             case 5 :
  65.                 myRomanNum += getLetter(5 * x);
  66.                 break;
  67.             case 6 :
  68.                 myRomanNum += getLetter(5 * x)+getLetter(x);
  69.                 break;
  70.             case 7 :
  71.                 myRomanNum += getLetter(5 * x)+getLetter(x)+getLetter(x);
  72.                 break;
  73.             case 8 :
  74.                 myRomanNum += getLetter(5 * x)+getLetter(x)+getLetter(x)+getLetter(x);
  75.                 break;
  76.             case 9 :
  77.                     num_nines = 1,i2 = 0;
  78.                     subnum = "0.";sArabic=toString(arabic);
  79.                     for( i = 1; sArabic[i] == '9'; i++ ) { num_nines *= 10; i2 = i; }
  80.                     for( i = 1; i <= i2; i++ ) { subnum += '9'; }
  81.                     if( sArabic[ i2+1] == '5' ) {
  82.                         myRomanNum += getLetter(x / ( 2 * num_nines ))+getLetter(10 * x);
  83.                         subnum += '5';
  84.                     } else {
  85.                         myRomanNum += getLetter(x / num_nines)+ getLetter(10 * x);
  86.                     }
  87.                     arabic-= to<double>(subnum) * x;
  88.                 break;
  89.         }
  90.         arabic %= x;
  91.     }
  92.     return myRomanNum;
  93. }
  94.  
  95.  
  96. int main(){
  97.   string numero;
  98.   cout<<"numero arabico:\n";
  99.     cin>>numero;
  100.   cout<<"en romano seria:\n";
  101.     cout<<ArabicToRoman(numero)<<endl;
  102. }

Etiquetas: numeros, pase, programa
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:26.