## C++ || Roman Numeral Conversion – How To Convert Roman Numeral To Integer & Integer To Roman Numeral Using C++

The following is a program with functions which demonstrates how to convert roman numerals to integer, and integers to roman numerals.

The sample program implemented on this page is an updated version of a homework assignment which was presented in a C++ Data Structures course. This program was assigned in order to practice the use of the class data structure, which is very similar to the struct data structure.

1. Roman Numeral Conversion

The example below demonstrates how to convert integers to roman numerals and roman numerals to integers.

``` 1. Roman Numeral Conversion C++ // ============================================================================ // Author: Kenneth Perkins // Taken From: http://programmingnotes.org/ // Date: Nov 2, 2020 // File: romanNumeralConversion.cpp // Description: The following demonstrates how to convert roman numerals. // ============================================================================ #include <iostream> #include <vector> #include <string> #include <cctype> #include <algorithm> /** * USE: Converts values from decimal to roman numeral and back */ class RomanNumeral { private: double m_decimal; std::string m_roman; bool m_isEmpty; public: RomanNumeral() { this->m_isEmpty = true; } RomanNumeral(double decimal, std::string roman) { this->m_decimal = decimal; this->m_roman = roman; this->m_isEmpty = false; } double decimal() const { return this->m_decimal; } std::string roman() const { return this->m_roman; } bool isEmpty() const { return this->m_isEmpty; } /** * FUNCTION: convertToRoman * USE: Converts a decimal number to a roman numeral * @param decimal: The number to be converted to a roman numeral. * @return: The converted roman numeral value. */ static std::string convertToRoman(double decimal) { std::string roman = ""; if (decimal > 0) { auto conversionValues = getValues(); for (const auto& conversionValue : conversionValues) { while (decimal > 0 && decimal >= conversionValue.decimal()) { roman += conversionValue.roman(); decimal -= conversionValue.decimal(); } if (decimal <= 0) { break; } } } return roman; } /** * FUNCTION: convertToDecimal * USE: Converts a roman numeral to a decimal value * @param roman: The number to be converted to a decimal number. * @return: The converted decimal value. */ static double convertToDecimal(std::string roman) { double decimal = 0; if (!isEmpty(roman)) { double previousNumber = 0; auto conversionValues = getValues(); // Iterate the std::string starting from the end for (int index = roman.length() - 1; index >= 0; --index) { // Get the current letter std::string currentLetter = std::string(1, roman[index]); // Skip whitepsace characters if (isEmpty(currentLetter)) { continue; } // Find a conversion value that matches the current letter auto conversionValue = find(conversionValues, currentLetter); // If a valid conversion was found, get the value if (conversionValue.isEmpty()) { continue; } auto currentNumber = conversionValue.decimal(); // Calculate the result if (previousNumber > currentNumber) { decimal -= currentNumber; } else { decimal += currentNumber; } // Save the current number in order to process the next letter previousNumber = currentNumber; } } return decimal; } // Returns roman Numeral Conversion Values static std::vector<RomanNumeral> getValues() { std::vector<RomanNumeral> conversionValues; conversionValues.push_back(RomanNumeral(1000, "M")); conversionValues.push_back(RomanNumeral(900, "CM")); conversionValues.push_back(RomanNumeral(500, "D")); conversionValues.push_back(RomanNumeral(400, "CD")); conversionValues.push_back(RomanNumeral(100, "C")); conversionValues.push_back(RomanNumeral(90, "XC")); conversionValues.push_back(RomanNumeral(50, "L")); conversionValues.push_back(RomanNumeral(40, "XL")); conversionValues.push_back(RomanNumeral(10, "X")); conversionValues.push_back(RomanNumeral(9, "IX")); conversionValues.push_back(RomanNumeral(5, "V")); conversionValues.push_back(RomanNumeral(4, "IV")); conversionValues.push_back(RomanNumeral(1, "I")); // Sort the list in descending roman numeral order std::sort(conversionValues.begin(), conversionValues.end(), [](const auto& lhs, const auto& rhs) { return lhs.decimal() > rhs.decimal(); }); return conversionValues; } // Finds a conversion that matches the given roman numeral search value static RomanNumeral find(std::vector<RomanNumeral> conversionValues, std::string searchValue) { RomanNumeral result; searchValue = toLower(searchValue); auto predicate = [searchValue](auto x) { return (toLower(x.roman()) == searchValue); }; auto iter = std::find_if(conversionValues.begin(), conversionValues.end(), predicate); if (iter != conversionValues.end()) { result = *iter; } return result; } // Checks if a string is empty or only contains whitespace static bool isEmpty(std::string str) { return str.empty() || std::all_of(str.begin(), str.end(), [](char c) { return std::isspace(static_cast<unsigned char>(c)); }); } // Converts a string to lowercase static std::string toLower(std::string str) { std::transform(str.begin(), str.end(), str.begin(), [](char c) { return std::tolower(static_cast<unsigned char>(c)); }); return str; } }; void display(std::string message) { message += "\n"; std::cout << message; } void romanTest(std::string roman) { auto decimal = RomanNumeral::convertToDecimal(roman); auto convertedBack = RomanNumeral::convertToRoman(decimal); display("======= Roman Test Start ======="); display("Original = " + roman + "\n Converted = " + std::to_string(decimal) + "\n Converted Back To Original = " + convertedBack); display("======= Roman Test End ======="); } void decimalTest(double decimal) { auto roman = RomanNumeral::convertToRoman(decimal); auto convertedBack = RomanNumeral::convertToDecimal(roman); display("======= Decimal Test Start ======="); display("Original = " + std::to_string(decimal) + "\n Converted = " + roman + "\n Converted Back To Original = " + std::to_string(convertedBack)); display("======= Decimal Test End ======="); } int main() { auto decimal = 1987; decimalTest(decimal); display(""); auto roman = "McMxcI"; romanTest(roman); std::cin.get(); return 0; }// http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 // ============================================================================//   Author:  Kenneth Perkins//   Taken From: http://programmingnotes.org///   Date:  Nov 2, 2020//   File: romanNumeralConversion.cpp//   Description: The following demonstrates how to convert roman numerals.// ============================================================================#include <iostream>#include <vector>#include <string>#include <cctype>#include <algorithm> /*** USE: Converts values from decimal to roman numeral and back*/class RomanNumeral { private:    double m_decimal;    std::string m_roman;    bool m_isEmpty; public:    RomanNumeral() {        this->m_isEmpty = true;    }     RomanNumeral(double decimal, std::string roman) {        this->m_decimal = decimal;        this->m_roman = roman;        this->m_isEmpty = false;    }     double decimal() const {        return this->m_decimal;    }     std::string roman() const {         return this->m_roman;    }     bool isEmpty() const {        return this->m_isEmpty;    }     /**    * FUNCTION: convertToRoman    * USE: Converts a decimal number to a roman numeral    * @param decimal: The number to be converted to a roman numeral.    * @return: The converted roman numeral value.    */    static std::string convertToRoman(double decimal) {        std::string roman = "";         if (decimal > 0) {            auto conversionValues = getValues();            for (const auto& conversionValue : conversionValues) {                while (decimal > 0                    && decimal >= conversionValue.decimal()) {                    roman += conversionValue.roman();                    decimal -= conversionValue.decimal();                }                if (decimal <= 0) {                    break;                }            }        }        return roman;    }     /**    * FUNCTION: convertToDecimal    * USE: Converts a roman numeral to a decimal value    * @param roman: The number to be converted to a decimal number.    * @return: The converted decimal value.    */    static double convertToDecimal(std::string roman) {        double decimal = 0;         if (!isEmpty(roman)) {            double previousNumber = 0;            auto conversionValues = getValues();             // Iterate the std::string starting from the end            for (int index = roman.length() - 1; index >= 0; --index) {                // Get the current letter                std::string currentLetter = std::string(1, roman[index]);                 // Skip whitepsace characters                if (isEmpty(currentLetter)) {                    continue;                }                 // Find a conversion value that matches the current letter                auto conversionValue = find(conversionValues, currentLetter);                 // If a valid conversion was found, get the value                if (conversionValue.isEmpty()) {                    continue;                }                 auto currentNumber = conversionValue.decimal();                 // Calculate the result                if (previousNumber > currentNumber) {                    decimal -= currentNumber;                } else {                    decimal += currentNumber;                }                 // Save the current number in order to process the next letter                previousNumber = currentNumber;            }        }         return decimal;    }     // Returns roman Numeral Conversion Values    static std::vector<RomanNumeral> getValues() {        std::vector<RomanNumeral> conversionValues;         conversionValues.push_back(RomanNumeral(1000, "M"));        conversionValues.push_back(RomanNumeral(900, "CM"));        conversionValues.push_back(RomanNumeral(500, "D"));        conversionValues.push_back(RomanNumeral(400, "CD"));        conversionValues.push_back(RomanNumeral(100, "C"));        conversionValues.push_back(RomanNumeral(90, "XC"));        conversionValues.push_back(RomanNumeral(50, "L"));        conversionValues.push_back(RomanNumeral(40, "XL"));        conversionValues.push_back(RomanNumeral(10, "X"));        conversionValues.push_back(RomanNumeral(9, "IX"));        conversionValues.push_back(RomanNumeral(5, "V"));        conversionValues.push_back(RomanNumeral(4, "IV"));        conversionValues.push_back(RomanNumeral(1, "I"));         // Sort the list in descending roman numeral order        std::sort(conversionValues.begin(), conversionValues.end(), [](const auto& lhs, const auto& rhs) {            return lhs.decimal() > rhs.decimal();        });         return conversionValues;    }     // Finds a conversion that matches the given roman numeral search value    static RomanNumeral find(std::vector<RomanNumeral> conversionValues, std::string searchValue) {        RomanNumeral result;         searchValue = toLower(searchValue);        auto predicate = [searchValue](auto x) { return (toLower(x.roman()) == searchValue); };        auto iter = std::find_if(conversionValues.begin(), conversionValues.end(), predicate);        if (iter != conversionValues.end()) {            result = *iter;        }        return result;    }     // Checks if a string is empty or only contains whitespace    static bool isEmpty(std::string str) {        return str.empty()            || std::all_of(str.begin(), str.end(), [](char c) {                return std::isspace(static_cast<unsigned char>(c));            });    }     // Converts a string to lowercase    static std::string toLower(std::string str) {        std::transform(str.begin(), str.end(), str.begin(),            [](char c) { return std::tolower(static_cast<unsigned char>(c)); });        return str;    }}; void display(std::string message) {    message += "\n";    std::cout << message;} void romanTest(std::string roman) {    auto decimal = RomanNumeral::convertToDecimal(roman);    auto convertedBack = RomanNumeral::convertToRoman(decimal);     display("======= Roman Test Start =======");     display("Original = " + roman        + "\n Converted = " + std::to_string(decimal)        + "\n Converted Back To Original = " + convertedBack);     display("======= Roman Test End =======");} void decimalTest(double decimal) {    auto roman = RomanNumeral::convertToRoman(decimal);    auto convertedBack = RomanNumeral::convertToDecimal(roman);     display("======= Decimal Test Start =======");     display("Original = " + std::to_string(decimal)        + "\n Converted = " + roman         + "\n Converted Back To Original = " + std::to_string(convertedBack));     display("======= Decimal Test End =======");} int main() {    auto decimal = 1987;    decimalTest(decimal);     display("");     auto roman = "McMxcI";    romanTest(roman);     std::cin.get();    return 0;}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

```======= Decimal Test Start ======= Original = 1987.000000 Converted = MCMLXXXVII Converted Back To Original = 1987.000000 ======= Decimal Test End =======```

``` ======= Roman Test Start ======= Original = McMxcI Converted = 1991.000000 Converted Back To Original = MCMXCI ======= Roman Test End ======= ```