From b45c02e56dfaa32a17c1c2a8ba194f731307d12d Mon Sep 17 00:00:00 2001 From: Akhil Achary <0018akhil@gmail.com> Date: Thu, 25 May 2023 12:54:34 +0530 Subject: [PATCH] Added Roman to Integer converter program in cpp --- algorithms/CPlusPlus/Maths/roman-integer.cpp | 76 ++++++++++++++++++++ algorithms/CPlusPlus/README.md | 4 +- 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 algorithms/CPlusPlus/Maths/roman-integer.cpp diff --git a/algorithms/CPlusPlus/Maths/roman-integer.cpp b/algorithms/CPlusPlus/Maths/roman-integer.cpp new file mode 100644 index 00000000..de67fbaa --- /dev/null +++ b/algorithms/CPlusPlus/Maths/roman-integer.cpp @@ -0,0 +1,76 @@ +#include +#include + +using namespace std; + +string intToRoman(int num) +{ + // Define the symbols and their corresponding values + unordered_map symbol_map = { + {1000, "M"}, + {900, "CM"}, + {500, "D"}, + {400, "CD"}, + {100, "C"}, + {90, "XC"}, + {50, "L"}, + {40, "XL"}, + {10, "X"}, + {9, "IX"}, + {5, "V"}, + {4, "IV"}, + {1, "I"}}; + + // Build the Roman numeral string + string roman = ""; + for (auto &[value, symbol] : symbol_map) + { + while (num >= value) + { + roman += symbol; + num -= value; + } + } + return roman; +} + +int romanToInt(string s) +{ + // Define the symbols and their corresponding values + unordered_map symbol_map = { + {'M', 1000}, + {'D', 500}, + {'C', 100}, + {'L', 50}, + {'X', 10}, + {'V', 5}, + {'I', 1}}; + + // Calculate the integer value + int value = 0; + for (int i = 0; i < s.length(); i++) + { + if (i > 0 && symbol_map[s[i]] > symbol_map[s[i - 1]]) + { + value += symbol_map[s[i]] - 2 * symbol_map[s[i - 1]]; + } + else + { + value += symbol_map[s[i]]; + } + } + return value; +} + +int main() +{ + int num = 1234; + string roman = intToRoman(num); + cout << num << " in Roman numerals is " << roman << endl; + + string s = "MCCXXXIV"; + int value = romanToInt(s); + cout << s << " in integer value is " << value << endl; + + return 0; +} \ No newline at end of file diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 064a599b..817e3e1a 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -60,8 +60,6 @@ - [Floyd Warshall](Graphs/floyd-warshall.cpp) - [Detecting Cycle in Directed graph using three colors](Graphs/detecting-cycle-in-a-graph-using-three-color-mechanism.cpp) - - ## Multiplication - [Karatsuba](Multiplication/karatsuba.cpp) @@ -82,6 +80,7 @@ - [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp) - [Reverse the linked list using stack](Linked-Lists/reverse-the-list-using-stack.cpp) - [Reverse the linked list in groups of K](Linked-Lists/reverse-the-list-in-groups-of-k.cpp) + ## Searching - [Linear Search](Searching/linear-search.cpp) @@ -183,6 +182,7 @@ - [Small numbers](Maths/small-numbers.cpp) - [Segmented Sieve](Maths/segmented-sieve-range.cpp) - [Binary Power](Maths/binary-power.cpp) +- [Roman To Integer](Maths/roman-integer.cpp) # Recursion