Added Roman to Integer converter program in cpp

pull/1189/head
Akhil Achary 2023-05-25 12:54:34 +05:30
parent d4e46400d1
commit b45c02e56d
2 changed files with 78 additions and 2 deletions

View File

@ -0,0 +1,76 @@
#include <iostream>
#include <unordered_map>
using namespace std;
string intToRoman(int num)
{
// Define the symbols and their corresponding values
unordered_map<int, string> 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<char, int> 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;
}

View File

@ -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