The input will be a string of Roman numerals that have to be sorted by their value. Also this task has to be completed using classes in c++
So far I've created my class
#include<iostream>
#include<string>
using namespace std;
class RomanNumbers
{
public:
RomanNumbers(string = "");
void setRoman(string);
int convertToDecimal();
void printDecimal();
void printRoman();
private:
string roman;
int decimal;
};
And the functions to convert a number from Roman numeral to integer form but my question is : How shall I sort them because I can't create a new string that will contain the converted Roman numerals and sort the string. Any help will be appreciated.
#include<iostream>
#include<string>
#include "RomanNumbers.h"
using namespace std;
RomanNumbers::RomanNumbers(string myRoman)
{
roman = myRoman;
decimal = 0;
}
void RomanNumbers::setRoman(string myRoman)
{
roman = myRoman;
decimal = 0;
}
int RomanNumbers::convertToDecimal()
{
enum romans { I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 };
for (int i = 0; i < roman.size(); i++)
{
switch (roman[i])
{
case 'M': decimal += M; break;
case 'D': decimal += D; break;
case 'C': decimal += C; break;
case 'L': decimal += L; break;
case 'X': decimal += X; break;
case 'V': decimal += V; break;
case 'I':
if (roman[i + 1] != 'I' && i + 1 != roman.size())
{
decimal -= 1;
}
else
{
decimal += 1;
}
break;
}
}
return decimal;
}
void RomanNumbers::printRoman()
{
cout << "Number in Roman form : " << roman;
cout << endl;
}
void RomanNumbers::printDecimal()
{
cout << "Number converted in integer form : " << decimal;
cout << endl;
}