63 137
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #45895 in C++ for Judiel Agapito Lapero

Question #45895
Write a program that accepts an ordinary number and outputs its equivalent Roman numerals. The ordinary numbers and their equivalent Roman numbers are given below: Ordinary numbers Roman numerals 1=I 5 = V 10= X 50= L 100= C 500= D 1000= M Enter a number: 234 Output: CCXXXIV Use switch-case statement. Limit the number up to 3000
Expert's answer
#include <iostream>
using namespace std;
// Definition of function
const char *Number_AsRomanString( int iNumber );
int main (void)
{
int arabicalNumeral = 1;
int result;
cout<<"Please enter the number: "<<endl;  
 
// Validation
if (cin>>result && result < 3000)
{
  // Display of result
  cout<<endl<<Number_AsRomanString( result )<<endl;
}
else
{
  cout<<"Limit the number up to 3000 or invalid input!"<<endl;
}
 
// Stop of process
system("pause");
return 0;
}
// Function
const char *Number_AsRomanString( int iNumber )
{
// Definition of struct
struct RomanDigit_t
{
  char *m_psString;
  int m_iValue;
};
// Initialization of object struct
static const RomanDigit_t RomanDigits[]=
{
  {"M",  1000},
  {"D", 500},
  {"C", 100},
  {"L",  50},
  {"X",  10},
  {"V",   5},
  {"I",   1},
};
static char sRomanString[20];
sRomanString[0] = '\0';
for (int i=0; iNumber && i<sizeof(RomanDigits)/sizeof(RomanDigits[0]); i++)
{
  switch(i)
  {
case 0:
{
while(RomanDigits[0].m_iValue <= iNumber )
{
  strcat( sRomanString, RomanDigits[0].m_psString );
  iNumber -= RomanDigits[0].m_iValue;
}
while((RomanDigits[0].m_iValue-RomanDigits[2].m_iValue) <= iNumber )
{
  strcat( sRomanString, RomanDigits[2].m_psString );
  strcat( sRomanString, RomanDigits[0].m_psString );
  iNumber -= RomanDigits[0].m_iValue-RomanDigits[2].m_iValue;
}
break;
}

case 1:
{
while(RomanDigits[1].m_iValue <= iNumber )
{
  strcat( sRomanString, RomanDigits[1].m_psString );
  iNumber -= RomanDigits[1].m_iValue;
}
while((RomanDigits[1].m_iValue-RomanDigits[2].m_iValue) <= iNumber )
{
  strcat( sRomanString, RomanDigits[2].m_psString );
  strcat( sRomanString, RomanDigits[1].m_psString );
  iNumber -= RomanDigits[1].m_iValue-RomanDigits[2].m_iValue;
}
break;
}
case 2:
{
while(RomanDigits[2].m_iValue <= iNumber )
{
  strcat( sRomanString, RomanDigits[2].m_psString );
  iNumber -= RomanDigits[2].m_iValue;
}
while((RomanDigits[2].m_iValue-RomanDigits[4].m_iValue) <= iNumber )
{
  strcat( sRomanString, RomanDigits[4].m_psString );
  strcat( sRomanString, RomanDigits[2].m_psString );
  iNumber -= RomanDigits[2].m_iValue-RomanDigits[4].m_iValue;
}
break;
}
case 3:
{
while(RomanDigits[3].m_iValue <= iNumber )
{
  strcat( sRomanString, RomanDigits[3].m_psString );
  iNumber -= RomanDigits[3].m_iValue;
}
while((RomanDigits[3].m_iValue-RomanDigits[4].m_iValue) <= iNumber )
{
  strcat( sRomanString, RomanDigits[4].m_psString );
  strcat( sRomanString, RomanDigits[3].m_psString );
  iNumber -= RomanDigits[3].m_iValue-RomanDigits[4].m_iValue;
}
break;
}
case 4:
{
while(RomanDigits[4].m_iValue <= iNumber )
{
  strcat( sRomanString, RomanDigits[4].m_psString );
  iNumber -= RomanDigits[4].m_iValue;
}
while((RomanDigits[4].m_iValue-RomanDigits[6].m_iValue) <= iNumber )
{
  strcat( sRomanString, RomanDigits[6].m_psString );
  strcat( sRomanString, RomanDigits[4].m_psString );
  iNumber -= RomanDigits[4].m_iValue-RomanDigits[6].m_iValue;
}
break;
}
case 5:
{
while(RomanDigits[5].m_iValue <= iNumber )
{
  strcat( sRomanString, RomanDigits[5].m_psString );
  iNumber -= RomanDigits[5].m_iValue;
}
while((RomanDigits[5].m_iValue-RomanDigits[6].m_iValue) <= iNumber )
{
  strcat( sRomanString, RomanDigits[6].m_psString );
  strcat( sRomanString, RomanDigits[5].m_psString );
  iNumber -= RomanDigits[5].m_iValue-RomanDigits[6].m_iValue;
}
break;
}

case 6:
{
while(RomanDigits[6].m_iValue <= iNumber )
{
  strcat( sRomanString, RomanDigits[6].m_psString );
  iNumber -= RomanDigits[6].m_iValue;
}
break;
}
default:
{
break;
}
  }
  
}
return sRomanString;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions