#include <cstdlib>
#include <iostream>
#include <conio2.h>
#include <string>
#include <limits>
#include <sstream>

////// definiciones personales \\\\\\
#define amarillo textcolor(YELLOW);
#define verde textcolor(LIGHTGREEN);
#define blanco textcolor(WHITE);
#define rojo textcolor(LIGHTRED);
/////////////////\\\\\\\\\\\\\\\\\\\\

using namespace std;

////// Inclusion de archivo de cabecera \\\\\\
#include "gui.h"
// Prototipo de funcion para conversion a romanos
int dec_romana(int num);
int romano_dec();

// funcion principal
int main(int argc, char *argv[])
{
    int i;
    int numerito,opcion;
    //gui();
    do
    { 
		system("cls");
		textcolor(YELLOW);
		system("TITLE Conversion Romano - Decimal, Decimal - Romano - Desarrollado por José Chafardet, Angel Uribe, Oswaldo Coronel.");
		system("COLOR 1F");
		textbackground(BLUE);
		// dibuja la primera linea del GUI, la parte superior con sus esquinas
		gotoxy(1,1);
		printf("%c", 201);
		for(i=0;i<78;i++) 
			{
				printf("%c",205);
			}
		printf("%c", 187);
		
		// Dibuja las lineas laterales del GUI
		for(i=2;i<=39;i++)
			{
				 gotoxy(1,i);
				 printf("%c", 186);
				 gotoxy(80,i);
				 printf("%c", 186);
			}
		// dibuja la linea inferior del GUI
		gotoxy(1,39);
		printf("%c", 200);
		for(i=0;i<78;i++) 
		  {
			  printf("%c",205);
		  }
		printf("%c", 188);
		
		// Imprime el titulo del programa
		gotoxy(30,3);
		verde
		cout << "CONVERSION DE NUMEROS" << endl;
		textcolor(YELLOW);
		// imprime la linea divisora entre el titulo y el programa
		gotoxy(1,5);
		printf("%c", 199);
		for(i=0;i<78;i++)
			{
				 printf("%c",196);
			}
		printf("%c", 182);
			blanco
			gotoxy(2,6);   
			cout << " 1 - Arabigo a Romano. " << endl;
			gotoxy(2,7);
			cout << " 2 - Romano a Arabigo. " << endl;
			gotoxy(2,8);
			cout << " 3 - Salir. " << endl;
			gotoxy(3,10);
			cout << "Por favor seleccione el tipo de conversion: ";
			rojo
			cin >> opcion;
			blanco
			
			// Comienzan las opciones del menu
			switch (opcion)
			{   
				// caso 1, convertir de arabigo a romano 
				case 1:
				blanco
				gotoxy(3,11);
				cout  << "Por favor introduzca un numero entre 1 y 3999: ";
				rojo
				cin >> numerito;
				blanco
				cin.ignore(numeric_limits<streamsize>::max(), '\n');
			
						  if (!cin || cin.gcount() != 1)
							  {
								  gotoxy(15,17);
								  verde
								  printf("%c", 175);
								  rojo
								  cout << " No es un valor numerico." << endl << flush;
								  cin.clear();
								  gotoxy(15,18);
								  verde
								  printf("%c ", 175);
								  rojo
								  cin.ignore(numeric_limits<streamsize>::max(), '\n');
								  system("PAUSE");
								  break;
								  blanco
							  }
						  else
							  {
								  blanco
								  gotoxy(3,13);
								  cout << "El resultado es: ";
								  rojo
								  dec_romana( numerito );
								  cout << endl;
								  blanco
								  gotoxy(3,16);
								  system("PAUSE");
								  break;
							  }
				break;
				case 2:              
					 blanco
					 romano_dec();
					 cout << endl;
					 blanco
					 gotoxy(3,16);
					 system("PAUSE");                 
				break;
				
				case 3:
					 blanco
					 gotoxy(14,15);
					 cout<<"Gracias por usar el software de conversion de numeros."<<endl;
					 gotoxy(30,16);
					 cout<<"Tenga un buen dia."<<endl;
					 getch();
					 break;
								  
				
			 }
    } while ( opcion != 3);
    return EXIT_SUCCESS;
}

int dec_romana (int num)
{ 

int numeros[8] = {1,5,10,50,100,500,1000,5000},i,p;
string letras[8] = {"I","V","X","L","C","D","M"},romano;

     if(num<=0)
		 {
			  rojo
			  gotoxy(3,14);
			  cout << " Somos buenos, pero no magos." << endl;
			  blanco      
		 }
     else if(num<=3999)
         {
             while(num > 0)
                 {
                      i = 0;
                      while(i < 7)
                          {
                              while(num >= numeros[i] && num < numeros[i+1])
                                  {
                                       p = i%2;
                                       if(num >= numeros[i+1] - numeros[i-p])
                                           {
                                                 romano = romano + letras[i-p] + letras[i+1];
                                                 num = num-(numeros[i+1]-numeros[i-p]);
                                                 //cout << romano;
                                           }
                                       else
                                           {
                                                 romano = romano + letras[i];
                                                 num = num-numeros[i];
                                                 //cout << romano;
                                           }
                                  }
                              i++;
                          }
                 }
                 cout << romano << endl;
         }
     else
         {
			 gotoxy(3,14);
			 romano = "Fuera de rango.";
			 rojo
			 cout << romano << endl;
			 blanco
         }
}

// romanos a enteros
int romano_dec()
{
     char roman_num[10];
     gotoxy(3,11);
	 cout << "Por favor ingrese un numero romano: ";
	 cin >> roman_num;
	 int len = strlen(roman_num);
	 int number = 0;
	 int counter = 0;
	 int b4sum = 0;
	 int sum = 0;

	 for (counter = len; counter >= 0; counter--)
		 {
			 if (roman_num[counter] == 'M' || roman_num[counter] == 'm')
				 number = 1000;
			 else if (roman_num[counter] == 'D' || roman_num[counter] == 'd')
				 number = 500;
			 else if (roman_num[counter] == 'C' || roman_num[counter] == 'c')
				 number = 100;
			 else if (roman_num[counter] == 'L' || roman_num[counter] == 'l')
				 number = 50;
			 else if (roman_num[counter] == 'X' || roman_num[counter] == 'x')
				 number = 10;
			 else if (roman_num[counter] == 'V' || roman_num[counter] == 'v')
				 number = 5;
			 else if (roman_num[counter] == 'I' || roman_num[counter] == 'i')
				 number = 1;
			 else
				 number = 0;

			 if (b4sum > number)
				 sum = b4sum - number;
			 else
				 sum = sum + number;
			 b4sum = number;
		}
    blanco
    gotoxy(3,13);
	cout << "El resultado es: " ;
    rojo
    cout << sum << endl;
    blanco
}
