Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programming Challenge: Roman Numbers

Status
Not open for further replies.

vlitzer

Programmer
Apr 30, 2002
31
AR
Are you capable to design an algorithm, that converts a decimal number to a Roman Number, only using no more than 7 bifurcative structures (like if, or case). Ej: you input 10 -> the output is X. hehe im already design it, so im testing your programming capability ;)
 
Hello Mr vlitzer,

Here i have written to convert decimal numbers into roman numbers using 5 bifurcative structures. I have used fortec++ compiler on solaris.

#include <iostream.h>
#include <string.h>
#include <math.h>


char * fnConvertRoman(int num, int len)
{
char array[3][3] = {{'I','V','X'},
{'X','L','C'},
{'C','D','M'}};

char * temp = new char[128];
strcpy(temp, &quot;\0&quot;);

if (len >2)
{
int limit;
limit = num * pow(10, len-3);

for(int index=0; index < limit; index++)
strcat(temp, &quot;M&quot;);

return temp;
}
else if ((num >= 1) && (num<=3))
{
for (int index=0; index< num; index++)
temp[index] = array[len][0];
return temp;
}
else if (num == 4)
{
temp[0] = array[len][0];
temp[1] = array[len][1];
return temp;
}
else if ((num >= 5) && (num <= 8))
{
temp[0] = array[len][1];
for(int index=1; index <= (num-5); index++)
temp[index] = array[len][0];
return temp;
}
else if (num == 9)
{
temp[0] = array[len][0];
temp[1] = array[len][2];
return temp;
}
else
return temp;
}//End fnConvertRoman

int toIntArray(int number, int * numValue)
{
char numValueStr[64];
int numValueLen;

sprintf(numValueStr, &quot;%d&quot;, number);
numValueLen = strlen(numValueStr);

for(int index = numValueLen-1; number >0; index--)
{
numValue[index] = number%10;
number = number/10;
}

return numValueLen;
}//End toIntArray

int main()
{
char romanValue[128]=&quot;\0&quot;;
int number;
int numberLength=0;
int * numValue;

cout << &quot;Enter a number: &quot;;
cin >> number;

numValue = new int[64];
numberLength = toIntArray(number, numValue);

for (int index = 0; index < numberLength; index ++)
strcat(romanValue, fnConvertRoman((numValue[index]), numberLength - index - 1));

cout << number <<&quot;\t\t - &quot; << romanValue << endl;
strcpy(romanValue, &quot;\0&quot;);

return 1;
}//End main

 
another implementation.
#include <iostream.h>
#define COUNT 13
int main()
{

int n;
int nRoman[COUNT] = {1000,900,500,400,100, 90, 50,40, 10, 9, 5, 4, 1};
char strRoman[COUNT][3] = {&quot;M&quot;,&quot;CM&quot;,&quot;D&quot;,&quot;CD&quot;,&quot;C&quot;, &quot;XC&quot;, &quot;L&quot;,&quot;XL&quot;, &quot;X&quot;, &quot;IX&quot;, &quot;V&quot;, &quot;IV&quot;, &quot;I&quot;};

cout << &quot;Enter number: &quot;;
cin >> n;
cout << endl<< &quot;Roman numeral is: &quot;;

while ( n > 0 )
{
for ( int i = 0; i < COUNT; i++)
{
if ( n >= nRoman )
{
cout << strRoman;
n -= nRoman;
break;
}
}
}

cout << endl;
return 0;
}
 
hehe, it seems that you are the only one!. Well i dont know why but i cannot run you prog... Well this is my version.. ill used the MSVC++ compiler..



#include <stdio.h>

void main()
{
int control, numero, factor = 1000;
int roman[13], counter = 0, cont;

/* 0 - M - 1000

1 - CM - 900
2 - D - 500
3 - CD - 400
4 - C - 100

5 - XC - 90
6 - L - 50
7 - XL - 40
8 - X - 10

9 - IX - 9
10- V - 5
11- IV - 4
12- I - 1
*/
for(int a = 0; a != 13; a++) //inicializa a 0 el vector
roman[a] = 0;

scanf(&quot;%d&quot;, &numero);

control = numero / factor;
if(control < 4 && control > 0)
{
numero = numero - (factor * control);
roman[0] = control;

}

for(int c = 1; c != 4; c++)
{
counter++;
factor = factor * 9 / 10; // IX
control = numero / factor;
if(control == 1)
{
numero = numero - (factor * control);
roman[counter]++;
}

counter++;
factor = factor * 5 / 9;// V
control = numero / factor;
if(control == 1)
{
numero = numero - (factor * control);
roman[counter]++;
}

counter++;
factor = factor * 4 / 5; // IV
control = numero / factor;
if(control == 1)
{
numero = numero - (factor * control);
roman[counter]++;
}

counter++;
factor = factor / 4; // I
control = numero / factor;
if(control < 4 && control > 0)
{
numero = numero - (factor * control);
roman[counter] = control ;
}

}

for(cont = 0; cont != roman[0]; cont++)
printf(&quot;M&quot;);
for(cont = 0; cont != roman[1]; cont++)
printf(&quot;CM&quot;);
for(cont = 0; cont != roman[2]; cont++)
printf(&quot;D&quot;);
for(cont = 0; cont != roman[3]; cont++)
printf(&quot;CD&quot;);
for(cont = 0; cont != roman[4]; cont++)
printf(&quot;C&quot;);
for(cont = 0; cont != roman[5]; cont++)
printf(&quot;XC&quot;);
for(cont = 0; cont != roman[6]; cont++)
printf(&quot;L&quot;);
for(cont = 0; cont != roman[7]; cont++)
printf(&quot;XL&quot;);
for(cont = 0; cont != roman[8]; cont++)
printf(&quot;X&quot;);
for(cont = 0; cont != roman[9]; cont++)
printf(&quot;IX&quot;);
for(cont = 0; cont != roman[10]; cont++)
printf(&quot;V&quot;);
for(cont = 0; cont != roman[11]; cont++)
printf(&quot;IV&quot;);
for(cont = 0; cont != roman[12]; cont++)
printf(&quot;I&quot;);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top