Introduction - \"The numeric system represented by Roman numerals originated in
ID: 3588694 • Letter: I
Question
Explanation / Answer
NOTE : I am using C++ for this Code.
#include<iostream>
#include<string>
using namespace std;
class romanType //making required class
{
public:
string s1,s5;
int sum=0;
int choice;
void StoreNumber(string s4)
{
s1=s4; //storing the number as roman numeral
}
int getvalue(char c)
{
if (c == 'I')
return 1;
if (c == 'V')
return 5;
if (c == 'X')
return 10;
if (c == 'L')
return 50;
if (c == 'C')
return 100;
if (c == 'D')
return 500;
if (c == 'M')
return 1000;
return 0;
}
void ConvertNumber() //converting roman numeral to decimal number
{
for(int i=0;i<s1.length();i++)
{
int str1 = getvalue(s1[i]);
if (i+1 < s1.length())
{
int str2 = getvalue(s1[i+1]);
if (str1 >= str2)
{
sum = sum+ str1;
}
else
{
sum = sum + str2 - str1;
i++;
}
}
else
{
sum = sum + str1;
i++;
}
}
}
void AddTwoRomanNumbers(string s2,string s3) //adding two roman numbers
{
s5=s2+s3;
}
void PrintNumber() //printing the number choiced by user
{
cout<<"press 1 to see result in Decimal Number.. ";
cout<<"press 2 to see result in Roman Numerals.. ";
cout<<" Enter Your choice :";
cin>>choice;
switch(choice)
{
case 1:
cout<<" Decimal Number = "<<sum;
break;
case 2:
cout<<" Roman Numerals = "<<s5;
break;
default:
cout<<"Entered Incorrect Choice..";
}
}
};
int main()
{
romanType r1;
r1.StoreNumber("MCXIV"); // you can check for other roman numerals also by passing here
r1.AddTwoRomanNumbers("CCCLXIX","DCCCXLV");
r1.ConvertNumber();
r1.PrintNumber();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.