You must design 4 functions to convert a Roman numeral (stored as a string) to i
ID: 3885140 • Letter: Y
Question
You must design 4 functions to convert a Roman numeral (stored as a string) to its decimal equivalent and viceversa. Note roman numbers are represented using strings. To help you work in stages we will consider two possible cases that you might design your program for: a simple conversion to old roman style, followed by the complete conversion using the roman standard. Note: the name of each function has to be exactly as described in this file. Otherwise, the marking script won't be able to call that function. Check your functions with a range of inputs so you are sure they work for all cases. Your final mark will reflect how well your c ode work with a large test script. Simple conversion (20% marks) In the old style roman, the order of the symbols does not matter. In this case, IX and XI both mean 10 +1 or 11 function n oldRoman2Dec(romanNumber) % it accepts a roman numeral and returns its decimal value function roman- oldDec2Roman(n) % it accepts a positive integer number and returns its equivalent roman numeral You should be able to handle the following conversion table: Roman Decimal 10 50 100 500 1000Explanation / Answer
Hello,In the above problem statement of conversion of Decimal to Roman and vicevera programs are given below.
//Decimal to Roman
#include <iostream>
using namespace std;
void newDec2Roman( double &num)
{
int intnum, m, d, c, l, x, v, i, n;
cout << "Enter a number:";
cin >> num;
intnum = (int)num;
m = intnum / 1000;
d = ((intnum%1000)/500);
c = ((intnum%500)/100);
l = ((intnum%100)/50);
x = ((intnum%50)/10);
v = ((intnum%10)/5);
i = (intnum%5);
n = m+d+c+l+x+v+i;
while (n > 0)
{
cout << "";// to show the loop ran everytime
if (m > 0)
{
cout << "M";
m--;
n--;
}
else if (d > 0)
{
cout << "D";
d--;
n--;
}
else if (c > 0)
{
cout << "C";
c--;
n--;
}
else if (l > 0)
{
cout << "L";
l--;
n--;
}
else if (x > 0)
{
cout << "X";
x--;
n--;
}
else if (v > 0)
{
cout << "V";
v--;
n--;
}
else if (i > 0)
{
cout << "I";
i--;
n--;
}
}
}
int main()
{
double num;
newDec2Roman(num);
system("pause");
return 0;
}
// Roman to Decimal
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
// This function returns value of a Roman symbol
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
// Returns decimal value of roman numaral
int newRoman2Dec(string &str)
{
// Initialize result
int res = 0;
// Traverse given input
for (int i=0; i<str.length(); i++)
{
// Getting value of symbol s[i]
int s1 = value(str[i]);
if (i+1 < str.length())
{
// Getting value of symbol s[i+1]
int s2 = value(str[i+1]);
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equal to the next symbol
res = res + s1;
}
else
{
res = res + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
res = res + s1;
i++;
}
}
return res;
}
// Driver Program
int main()
{
// considering inputs given are valid
string str ;
cout<<"Enter a string";
cin>>str;
cout << "Integer form of Roman Numeral is "
<< newRoman2Dec(str) << endl;
return 0;
}
Please let me know if any other clarification is required and drop your feedback.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.