Home C++ How to convert roman numerals to decimals in C++

How to convert roman numerals to decimals in C++

To have an in-depth understanding of converting roman numerals into decimals, we must first have an idea about the roman numeral system. It is a number system where letters are used to denote numbers. Modern roman numerals consist of 7 symbols, and each of them has a fixed decimal value.

Now, the function of our program is to find the decimal values of the given roman numerals. For this, we can use a stack or an array. In this tutorial, we will use the stack data structure. Stack is LIFO(first in, first out), and the insertion and deletion of data on the stack are done at only one end. Let’s first write the code for our stack that will be created using a pointer to data.

#include<iostream>
using namespace std;
//template/generic class
template<class T>
class ArrayStack
{
	private:
  	    //templatized array pointer that can be of any type
		T* Array;
		int top;
		int size;
	public:
		ArrayStack(int s)
		{
          //set size
			size=s;
          // create a dynamic array of the size specified above
			Array=new T[size];
			top=-1;
		}
  	    void SetTop(int top) 
        {
          this->top = top;
        }
        int GetTop() 
        {
          return top;
        }
		bool IsEmpty()
		{
          //condition for if the stack is empty
			if(top==-1)
			return true;
			else
			return false;
		}
		bool IsFull()
		{
			if(top==size-1)
			return true;
			else return false;
		}
        // push function to push an element to the stack
		void Push(T d)
		{
			if(!IsFull())
			{
				Array[top]=d;
				top++;
			}
		}
       // pop function to extract the element out of the class
		T Pop()
		{
			T element;
			if(!IsEmpty())
			{
				top--;
				element=Array[top];
			}
			return element;
		}
	
};

Now that we have created the stack, our next step will be to insert values in it. Let’s first write the program to convert roman numerals to decimals. These roman numeral values are already defined; based on the symbol, there is a corresponding number.

#include<iostream>
using namespace std;
// takes roman numerals and returns corresponding decimal value
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;
    }

The ArrayStack class and the value function are used to create the function called conversion, which contains the original logic for conversion.

int conversion(string roman)
{
  ArrayStack <char> array;
  int i;
  int romanLength;
  int num=0;
  romanLength=roman.length();
  for(i=0;i<romanLength;i++)
  {
    //push the input string in the stack
    array.push(roman[i]);
  }
  while(!array.IsEmpty())
  {
    if(array.SetTop(0))                               //only one character
    {
        num += value(array.GetTop());
        array.pop();
        break;
    }
                //comparing rightmost two characters in case of more than one character
    else if(value(array.GetTop()-1) < value(array.GetTop()))  
    {
      num = num + (value(array.GetTop())-value(a[array.GetTop()-1]));
      array.pop();
      array.pop();
    }
    else
    {
       num += valueof(array.GetTop());
       array.pop();
    }
  }
  return num;
}

The above function will return the number for the value of the roman numeral. Now Let’s create the main function to test all of the above code.

int main() {
   string roman;
   cout<<"Input a roman numeral(I,V,X,M,L,C,D): ";
   cin>>roman;
   int romanConversion;
   romanConversion = conversion(roman);
   cout<<"Decimal value after roman conversion is: "<< romanConversion;
}

The driver program above is used to perform the basic execution of the above two function.

Conclusion

In this tutorial, we learned the use of user created stack and how it can be implemented and used. The basic logic can be used to create any C++ program, and we finally converted a string of roman numerals to decimal. Stay connected and keep on reading to learn more about new topics.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More