# DECIMAL NUMERALS to ROMAN NUMERALS

Page 1 of 1

## 1 Replies - 540 Views - Last Post: 13 September 2010 - 11:47 PM

### #1 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

# DECIMAL NUMERALS to ROMAN NUMERALS

Posted 14 October 2008 - 06:58 AM

Description: Here is the trick, (from Randy of HLA), that makes this work: 'Multiply' the Roman Number by Ten ...

This is achieved by swapping the characters in the char string 's1' below with their corresponding character in char string 's2' below ...
char s1[] = "IVXLCDM";
char s2[] = "XLCDM**";Uses a neat trick supplied by Randall Hyde Of HLA fame.
```/*
Demo of converting DECIMAL NUMERALS to ROMAN NUMERALS ...
Modified for strings, and to use the MSD, (Most Significant Digit)
rather than the LSD (Least Significant Digit) method used by Randy.
Randall Hyde is the author of HLA (High Level Assembly) ... See ...
http://webster.cs.ucr.edu/AoA/index.html
http://developers-heaven.net/forum/index.php/topic,46.0.html
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

#define debugging true

using namespace std;

string convert( int n )
{
if( n > 3999 ) return "Largest number handled is 3999";
if( n < 1 ) return "Smallest number handled is 1";

string romanNum[] =
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
char buffer[20];
itoa( n, buffer, 10 ); // convert integer to C string char's
string nStr = string(buffer); // convert C string to C++ string
string rm =""; // to hold the Roman Number
int msd;
for( int i=0; i<nStr.length(); ++i )
{
// Extract the NEXT Most Significant Digit (MSD)
//  from the number 'n' ... as a string now, i.e. 'nStr'

//  msd =  ???? ...................................
msd = atoi( nStr.substr(i,1).c_str() );

// Here is the trick, (from Randy of HLA), that makes this work ...
// 'Multiply' the Roman Number by Ten.  This is achieved by
// swapping the characters in the char string 's1' below
// with their corresponding character in char string 's2' below ...
char s1[] = "IVXLCDM";
char s2[] = "XLCDM**";
int j, k;
for( j=0; j< rm.length(); j++ )
{
for( k=0; k<7; k++ )
{
if( rm[j] == s1[k] )
{
rm[j] = s2[k];
break;
}
}
}
// Convert the current MSD to a string
// and append to the end of "rm".
rm = rm + romanNum[msd];
}
return rm;
}

void convertAndShow( int n )
{
cout << "The number " << n << " converted is " << convert(n) << endl;
}

int main()
{
// get file name and open that file to read numbers
// and validate that it was opened ok ...
ifstream myfile;
string filename;

// or for testing via keyboard input ...
#if debugging
goto testing;
#endif

do
{
cout << "Please enter filename: ";
myfile.clear();
cin >> filename;
cin.sync(); //eat any char's that cin may have missed in in-stream
myfile.open( filename.c_str() );
if( myfile.fail() )
cout << "File cannot be found" << endl;
}while( myfile.fail() );

int a;
while( myfile >> a )
convertAndShow( a );

myfile.close();

#if debugging
testing:
string message = "Enter number from the keyboard (q to quit) : ";
cout << message;
while( cin >> a ) { convertAndShow( a ); cout << message; }
cin.clear();
cin.sync();
#endif

cout << "nPress 'Enter' to continue ... ";
cin.get();
}

```

Is This A Good Question/Topic? 0

## Replies To: DECIMAL NUMERALS to ROMAN NUMERALS

### #2 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: DECIMAL NUMERALS to ROMAN NUMERALS

Posted 14 October 2008 - 06:58 AM

Description: Here is the trick, (from Randy of HLA), that makes this work: 'Multiply' the Roman Number by Ten ...

This is achieved by swapping the characters in the char string 's1' below with their corresponding character in char string 's2' below ...
char s1[] = "IVXLCDM";
char s2[] = "XLCDM**";Uses a neat trick supplied by Randall Hyde Of HLA fame.
```/*
Demo of converting DECIMAL NUMERALS to ROMAN NUMERALS ...
Modified for strings, and to use the MSD, (Most Significant Digit)
rather than the LSD (Least Significant Digit) method used by Randy.
Randall Hyde is the author of HLA (High Level Assembly) ... See ...
http://webster.cs.ucr.edu/AoA/index.html
http://developers-heaven.net/forum/index.php/topic,46.0.html
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

#define debugging true

using namespace std;

string convert( int n )
{
if( n > 3999 ) return "Largest number handled is 3999";
if( n < 1 ) return "Smallest number handled is 1";

string romanNum[] =
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
char buffer[20];
itoa( n, buffer, 10 ); // convert integer to C string char's
string nStr = string(buffer); // convert C string to C++ string
string rm =""; // to hold the Roman Number
int msd;
for( int i=0; i<nStr.length(); ++i )
{
// Extract the NEXT Most Significant Digit (MSD)
//  from the number 'n' ... as a string now, i.e. 'nStr'

//  msd =  ???? ...................................
msd = atoi( nStr.substr(i,1).c_str() );

// Here is the trick, (from Randy of HLA), that makes this work ...
// 'Multiply' the Roman Number by Ten.  This is achieved by
// swapping the characters in the char string 's1' below
// with their corresponding character in char string 's2' below ...
char s1[] = "IVXLCDM";
char s2[] = "XLCDM**";
int j, k;
for( j=0; j< rm.length(); j++ )
{
for( k=0; k<7; k++ )
{
if( rm[j] == s1[k] )
{
rm[j] = s2[k];
break;
}
}
}
// Convert the current MSD to a string
// and append to the end of "rm".
rm = rm + romanNum[msd];
}
return rm;
}

void convertAndShow( int n )
{
cout << "The number " << n << " converted is " << convert(n) << endl;
}

int main()
{

// for testing via keyboard input ...
#if debugging
goto testing;
#endif

// get file name and open that file to read numbers
// and validate that it was opened ok ...
ifstream myfile;
string filename;

do
{
cout << "Please enter filename: ";
myfile.clear();
cin >> filename;
cin.sync(); //eat any char's that cin may have missed in in-stream
myfile.open( filename.c_str() );
if( myfile.fail() )
cout << "File cannot be found" << endl;
}while( myfile.fail() );

int a;
while( myfile >> a )
convertAndShow( a );

myfile.close();

#if debugging
testing:
string message = "Enter number from the keyboard (q to quit) : ";
cout << message;
while( cin >> a ) { convertAndShow( a ); cout << message; }
cin.clear();
cin.sync();
#endif

cout << "nPress 'Enter' to continue ... ";
cin.get();
}

```

### #3 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: DECIMAL NUMERALS to ROMAN NUMERALS

Posted 14 October 2008 - 06:58 AM

Description: Here is the trick, (from Randy of HLA), that makes this work: 'Multiply' the Roman Number by Ten ...

This is achieved by swapping the characters in the char string 's1' below with their corresponding character in char string 's2' below ...
char s1[] = "IVXLCDM";
char s2[] = "XLCDM**";Uses a neat trick supplied by Randall Hyde Of HLA fame.
```// intToRomanNumerals.cpp // this revision 2010-09-14 //

/*
Demo of converting DECIMAL NUMERALS to ROMAN NUMERALS ...
Modified for strings, and to use the MSD, (Most Significant Digit)
rather than the LSD (Least Significant Digit) method used by Randy.
Randall Hyde is the author of HLA (High Level Assembly) ... See ...
http://webster.cs.ucr.edu/AoA/index.html
http://developers-heaven.net/forum/index.php/topic,46.0.html
*/

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

string iToString( int i ) // convert integer 'i' to a string
{
ostringstream oss;
oss << i;
return oss.str(); // return as a string ...
}

string convert( int n )
{
if( n > 3999 ) return "Largest number handled is 3999";
if( n < 1 ) return "Smallest number handled is 1";

string nStr = iToString( n ); // now n is in nStr ...

string rm; // construct an empty string to hold the growing Roman Num...

for( size_t i = 0; i < nStr.length(); ++i ) // traverse nStr ...
{
// on each pass ... extract the NEXT Most Significant Digit (MSD)
// from the number 'n' ... i.e. from 'nStr'
int msd = nStr[i] - '0'; // ok ... now have next Most Significant Digit

// Here is the trick, (from Randy of HLA), that makes this work ...
// on each pass ... 'multiply' the previous Roman Num by 10
// This is achieved by swapping the characters in the char string 's1'
// with their corresponding character in char string 's2' below ...
char s1[] = "IVXLCDM";
char s2[] = "XLCDM**"; // each valid 'char' here is 10 times the above

// Note: next loops are skipped on first pass when rm.size() is still 0
for( size_t j = 0; j < rm.size(); ++j ) // for each digit in nStr, get rm
{
for( size_t k = 0; k < 7; ++k ) // for each Roman digit above, traverse s1
if( rm[j] == s1[k] ) // find j... so can ... 'multiply by 10'
{
rm[j] = s2[k];
break; // since j was found ... and x10 is done
}
}
// ok ... all previous Roman Numerals have been multiplied by 10 ... so

// now ... convert the current MSD int ... to a Roman Num string
// and add in ... at the end of the growing 'rm' string ...
string romanNum[] =
{//  0,  1,    2,     3,    4,   5,    6,     7,     8,     9
"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"
};

rm = rm + romanNum[msd];

cout << rm << endl; // shows steps ... comment out if desired //
}
return rm; // return the string holding the Roman Numerals ...
}

int main()
{
int n;
string prompt = "Enter a number from the keyboard (q to quit) : ";

// the while loop will exit if the 'n' that was input was NOT a valid int
while( cout << prompt << flush  &&  cin >> n )
cout << "The number " << n << " converted is " << convert(n) << endl;
}

```

### #4 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: DECIMAL NUMERALS to ROMAN NUMERALS

Posted 13 September 2010 - 11:47 PM

See latest updates maintained at ... http://developers-he...index.php/topic,134.msg709.html#msg709