Conversion of roman numerals to decimal in C++

Hi, everyone in this tutorial we are going to learn the conversion of roman numerals decimal in C++.  In this tutorial let us complete the task using C++ programming language. Before going through this let us learn what roman numerals mean

Roman Numerals:

Roman numerals are numeral systems that originated in ancient days actually in Rome hence the name Roman numerals. And they remained to be the usual way of writing numbers in the Late Middle Ages. Numbers in this system are represented by combinations of letters that are derived from Latin Alphabets. Modern roman numerals include seven symbols, each with a fixed integer value.

Now let’s get into our business of converting them into decimal numbers. Decimal numbers are what we use in our daily life for counting and calculating processes.

Convert Roman numerals to decimal numbers in C++

For, this task, it is always preferable to use a data structure called stack. In a stack deletions and insertions are done at only one end. Inserting a element into a stack is called push and deleting is called pop operation. We may be using these operations in our logic. So, it will be better if we write functions that perform these operations and then we call them when required. So, first, we have to initialize an array that can be used as a stack and then we will be defining functions for operations on the stack. If you want to learn what a stack is and its operations go through this link  Stack 

Once this job is done we can read a roman number and then we can feed it into the stack. You may be wondering how to do that but, we can simply do that by adding each character of the roman numeral into each index of array.  And then we will be creating a new function that does our job of conversion into decimal numbers.

Functions for push and pop:

As we have learnt we need to have functions performing push and pop operations lets see how we can do that. But, the foremost thing is that we have to first initialize an array so that we can use it as a stack.

char a[10];                   //initializing stack
int top=-1;
void push(char c)            //function for push operation on stack
{
  top++;
  a[top]=c;
}
void pop()                   //function for pop operation on stack
{
  a[top]=NULL;
  top--;
}

Value of characters used in roman numerals:

Before the conversion of roman numerals to decimals, our program must know the decimal value of each character that is being used in roman numerals. As we know the benefits of C++ is that it is object-oriented. We can declare a class with as many functions as we require and then we use them by creating an object. So, let’s also create a function to figure out the value of characters

int valueof(char c)       
{
  int value;
  switch(c)                                //using switch statement to check various cases
  {
    case 'i': value=1;
    break;
    case 'v': value=5;
    break;
    case 'x': value=10;
    break;
    case 'l': value=50;
    break;
    case 'c': value=100;
    break;
    case 'd': value=500;
    break;
    case 'm': value=1000;
    break;
        default: cout<<"enter either m or c or d or x or v or i";           
//if user has entered a invalid character
        break;
  }
  return value;                                                       
//return the value of the character
}

conversion:

So far we have declared whatever the functions we want. Now, let’s do our task by using these functions.

Initially, we will be feeding the roman numeral read as a string into the stack. We find out the total value by popping out each character and figuring it out.  If we have only one character we simply take its value that comes by calling the function by passing it as a parameter. In other cases where we have more than one character we compare the value of the rightmost two characters. The same as we do on paper. If rightmost is largest that will be at the top of the stack we simply add its value to the total value. Else we will be adding the difference of the rightmost characters equivalent decimal value and then we add them. Now lets put this as a code

Conversion of roman numerals to decimal in C++

long int convertion(string r)
{
  int i;
  int x;
  long int num=0;
  x=r.length();
  for(i=0;i<x;i++)
  {
    push(r[i]);
  }
  while(top!=-1)
  {
    if(top==0)                               //only one character
    {
    	            num=num+valueof(a[top]);
    	            pop();
    	            break;
    }
                //comparing rightmost two characters in case of more than one character
    else if(valueof(a[top-1])<valueof(a[top]))  
    {
      num=num+(valueof(a[top])-valueof(a[top-1]));
      pop();
      pop();
    }
    else
    {
    	                num=num+valueof(a[top]);
    	                pop();
    }
  }
  return num;
}

So now we have c0mpleted the functions part lets make them operate by calling them from main

main()
{
  string roman;
  long int r;
  cout<<"enter a valid roman numeral"<<endl;
  cin>>roman;
  r1=convertion(roman);
  cout<<"the numerical value is "<<r<<endl;
}

  Output:

The output looks like

Enter a valid roman numeral: cmxx
The numerical value is:920

 

That’s all guys meet you in the next session.

Leave a Reply

Your email address will not be published. Required fields are marked *