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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
// ============================================================================ // 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 =======
Leave a Reply