Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // calculadoraNumerosRomanos.c
- //
- // Copyright 2011 marcelo moran <marcelo@marcelo-laptop>
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 of the License, or
- // (at your option) any later version.
- //
- // This program 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 General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; 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>
- #include <stdlib.h>
- #define MAX 20
- int romanoAdecimal( char romano[ ] );
- int calculadora( int dec1, int dec2, char operador );
- void mensaje( void );
- int main(int argc, char **argv)
- {
- char num1[ MAX ];
- char num2[ MAX ];
- int dec1, dec2;
- char operador;
- mensaje( );
- printf( "Ingrese el primer numero romano: " );
- fgets( num1, strlen( num1 ), stdin );
- dec1 = romanoAdecimal( num1 );
- printf( "Ingrese el operador: " );
- scanf( "%c", &operador );
- getchar();
- printf( "Ingrese el segundo numero romano: " );
- fgets( num2, strlen( num2 ), stdin );
- dec2 = romanoAdecimal( num2 );
- printf( "\nEl resultado es: %d %c %d = %d\n",dec1, operador, dec2, calculadora( dec1, dec2, operador ) );
- return 0;
- }
- int romanoAdecimal( char romano[ ] ){
- int i; //contador for loop
- int decimal = 0; //resultado
- int ultimoValor = 0;//ultima suma realizada
- if( strlen( romano ) == 0 ){ return 0; }
- char caracter;//el caracter que se esta analizando
- int valor;//valor del caracter en decimal
- char simbolos[ ] = "MDCLXVI";
- int valores[ ] = { 1000, 500, 100, 50, 10, 5, 1 };
- int indice;// indice del elemento del array valores que queremos sumar
- for( i = 0; i < strlen( romano )-1; i++ ){
- caracter = romano[ i ]; //guardamos el caracter actual
- int j;//contador for loop
- for( j = 0; j < strlen( simbolos ); j++ ){ // obtenemos el indice del simbolo( caracter )
- if( caracter == simbolos[ j ] ){
- indice = j;
- break;
- }
- }
- valor = valores[ indice ];
- if( indice >= 0 ){
- decimal += valor;
- if( valores[ indice ] > ultimoValor ){
- decimal -= 2 * ultimoValor;
- }
- ultimoValor = valores[ indice ];
- }
- }
- return decimal;
- }
- int calculadora( int dec1, int dec2, char operador ){
- switch( operador ){
- case '+':
- return dec1 + dec2;
- break;
- case '-':
- return dec1 - dec2;
- break;
- case '*':
- return dec1 * dec2;
- break;
- case '/':
- if( dec2 != 0 ){
- return dec1 / dec2;
- }else{
- printf( "\nERROR. Division entre cero\n" );
- exit( -1 );
- }
- break;
- case '%':
- if( dec2 != 0 ){
- return dec1 % dec2;
- }else{
- printf( "\nERROR. Division entre cero\n" );
- exit( -1 );
- }
- break;
- default:
- return 0;
- break;
- }
- }
- void mensaje( void ){
- printf( "\t\tCALCULADORA DE NUMEROS ROMANOS\nAsegurese de ingresar correctamente los numeros romanos.\n"
- "La calculadora asume que los operandos estan correctamente escritos.\n\n" );
- }
Add Comment
Please, Sign In to add comment