Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a C++ program that will read a series of Roman numerals from a file (via

ID: 3565638 • Letter: D

Question

Design a C++ program that will read a series of Roman numerals from a file (via Linux redirection). Each Roman numeral must be translated to its Arabic equivalent. Both roman and Arabic values must be displayed. Additionally, the program must count how many numerals were converted and sum the Arabic values. No arrays, stings, or other structured data types can be used in this program. Declare only char and int type variables.

An example of the terminal session is:

[kim@bobby keys]$ more sampledata.txt
XXII
MMMDCCLXXVIII
CCCLXXXIII
XIX
CDLXIV
MIX
[kim@bobby keys]$ g++ prog08.cpp
[kim@bobby keys]$ ./a.out < sampledata.txt

XXII = 22
MMMDCCLXXVIII = 3778
CCCLXXXIII = 383
XIX = 19
CDLXIV = 464
MIX = 1009

6 Roman numerals converted to Arabic
Sum of all numbers converted is 5675

Explanation / Answer

#include #include #include using namespace std; class RomanNumeral { public: const static int M = 1000; const static int D = 500; const static int C = 100; const static int L = 50; const static int X = 10; const static int V = 5; const static int I = 1; RomanNumeral( const int arabic ) : m_roman( "" ), m_arabic( ((arabic > 0) ? arabic : 0) ) { if( arabic > 0 ) { int i = arabic; while( i > 0 ) { if( ( i - M ) >= 0 ) { m_roman += "M"; i -= M; continue; } if( ( i - D ) >= 0 ) { m_roman += "D"; i -= D; continue; } if( ( i - C ) >= 0 ) { m_roman += "C"; i -= C; continue; } if( ( i - L ) >= 0 ) { m_roman += "L"; i -= L; continue; } if( ( i - X ) >= 0 ) { m_roman += "X"; i -= X; continue; } if( ( i - V ) >= 0 ) { m_roman += "V"; i -= V; continue; } if( ( i - I ) >= 0 ) { m_roman += "I"; i -= I; continue; } } } else { m_roman = "0"; } } RomanNumeral( const std::string& string ) : m_roman( ((string.size() > 0 ) ? string : "0" ) ), m_arabic( 0 ) { int i = 0; while( i < (int)string.size() ) { char c = string[i++]; switch( c ) { case 'M': case 'm': m_arabic += M; break; case 'D': case 'd': m_arabic += D; break; case 'C': case 'c': m_arabic += C; break; case 'L': case 'l': m_arabic += L; break; case 'X': case 'x': m_arabic += X; break; case 'V': case 'v': m_arabic += V; break; case 'I': case 'i': m_arabic += I; break; default: throw new std::out_of_range( "Not a valid Roman numeral!" ); break; } } } int getArabic() { return m_arabic; } void setArabic( const int arabic ); const std::string& getRoman() { return m_roman; } protected: std::string m_roman; int m_arabic; }; int main() { cout > roman; try { RomanNumeral rn( roman ); cout