Guest User

Untitled

a guest
May 26th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. //      calculadoraNumerosRomanos.c
  2. //      
  3. //      Copyright 2011 marcelo moran <marcelo@marcelo-laptop>
  4. //      
  5. //      This program is free software; you can redistribute it and/or modify
  6. //      it under the terms of the GNU General Public License as published by
  7. //      the Free Software Foundation; either version 2 of the License, or
  8. //      (at your option) any later version.
  9. //      
  10. //      This program is distributed in the hope that it will be useful,
  11. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //      GNU General Public License for more details.
  14. //      
  15. //      You should have received a copy of the GNU General Public License
  16. //      along with this program; if not, write to the Free Software
  17. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. //      MA 02110-1301, USA.
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #define MAX 20
  25.  
  26. int romanoAdecimal( char romano[ ] );
  27. int calculadora( int dec1, int dec2, char operador );
  28. void mensaje( void );
  29.  
  30. int main(int argc, char **argv)
  31. {
  32.     char num1[ MAX ];
  33.     char num2[ MAX ];
  34.    
  35.     int dec1, dec2;
  36.     char operador;
  37.    
  38.     mensaje( );
  39.    
  40.     printf( "Ingrese el primer numero romano: " );
  41.     fgets( num1, strlen( num1 ), stdin );
  42.    
  43.     dec1 = romanoAdecimal( num1 );
  44.    
  45.     printf( "Ingrese el operador: " );
  46.     scanf( "%c", &operador );
  47.    
  48.     getchar();
  49.    
  50.     printf( "Ingrese el segundo numero romano: " );
  51.     fgets( num2, strlen( num2 ), stdin );
  52.     dec2 = romanoAdecimal( num2 );
  53.    
  54.     printf( "\nEl resultado es: %d %c %d = %d\n",dec1, operador, dec2, calculadora( dec1, dec2, operador ) );
  55.    
  56.     return 0;
  57. }
  58.  
  59. int romanoAdecimal( char romano[ ] ){
  60.     int i; //contador for loop
  61.     int decimal = 0; //resultado
  62.     int ultimoValor = 0;//ultima suma realizada
  63.    
  64.     if( strlen( romano ) == 0  ){ return 0; }
  65.    
  66.     char caracter;//el caracter que se esta analizando
  67.     int valor;//valor del caracter en decimal
  68.    
  69.     char simbolos[  ] = "MDCLXVI";
  70.     int valores[  ] = { 1000, 500, 100, 50, 10, 5, 1 };
  71.     int indice;// indice del elemento del array valores que queremos sumar
  72.    
  73.     for( i = 0; i < strlen( romano )-1; i++ ){
  74.         caracter = romano[ i ]; //guardamos el caracter actual
  75.        
  76.         int j;//contador for loop
  77.         for( j = 0; j < strlen( simbolos ); j++ ){ // obtenemos el indice del simbolo( caracter )
  78.             if( caracter == simbolos[ j ] ){
  79.                 indice = j;
  80.                 break;
  81.                 }
  82.             }
  83.         valor = valores[ indice ];
  84.        
  85.         if( indice >= 0 ){
  86.             decimal += valor;
  87.            
  88.             if( valores[ indice ] > ultimoValor ){
  89.                 decimal -= 2 * ultimoValor;
  90.                 }
  91.            
  92.             ultimoValor = valores[ indice ];
  93.             }
  94.            
  95.         }
  96.     return decimal;
  97.     }
  98.  
  99. int calculadora( int dec1, int dec2, char operador ){
  100.    
  101.     switch( operador ){
  102.         case '+':
  103.             return dec1 + dec2;
  104.         break;
  105.        
  106.         case '-':
  107.             return dec1 - dec2;
  108.         break;
  109.        
  110.         case '*':
  111.             return dec1 * dec2;
  112.         break;
  113.        
  114.         case '/':
  115.             if( dec2 != 0 ){
  116.                 return dec1 / dec2;
  117.                 }else{
  118.                     printf( "\nERROR. Division entre cero\n" );
  119.                     exit( -1 );
  120.                     }
  121.         break;
  122.        
  123.         case '%':
  124.             if( dec2 != 0 ){
  125.                 return dec1 % dec2;
  126.                 }else{
  127.                     printf( "\nERROR. Division entre cero\n" );
  128.                     exit( -1 );
  129.                     }
  130.         break;
  131.        
  132.         default:
  133.             return 0;
  134.         break;
  135.         }
  136.     }
  137.  
  138. void mensaje( void ){
  139.     printf( "\t\tCALCULADORA DE NUMEROS ROMANOS\nAsegurese de ingresar correctamente los numeros romanos.\n"
  140.             "La calculadora asume que los operandos estan correctamente escritos.\n\n" );
  141.     }
Add Comment
Please, Sign In to add comment