C++ program to convert the number from roman to integer using class

Given a roman number, we have to convert it into integer using the class and object approach.
Submitted by Shubh Pachori, on October 09, 2022

Example:

Input:
Enter Roman Number : VI

Output:
Integer : 6

C++ code to convert the number from roman to integer using the class and object approach

#include <iostream>
using namespace std;

// create a class
class RomanToInteger {
  // private data members
 private:
  string roman;

  // public member functions
 public:
  // getRoman() function to insert the
  // number in roman
  void getRoman() {
    cout << "Enter Roman Number : ";
    cin >> roman;
  }

  // RToI() function to convert number 
  // from roman to integer
  void RToI() {
    // initialising int type variables to 
    // perform operations
    int index, result = 0, length = int(roman.length());

    // for loop to traverse the roman number 
    // stored in the string
    for (index = 0; index < length; index++) {
      // switch and case to convert the number in
      // roman to integer
      switch (roman[index]) {
        // case 'I' for 1
        case 'I':
          result = result + 1;
          break;

        // case 'V' for 5
        case 'V':
          result = result + 5;
          break;

        // case 'X' for 10
        case 'X':
          result = result + 10;
          break;

        // case 'L' for 50
        case 'L':
          result = result + 50;
          break;

        // case 'C' for 100
        case 'C':
          result = result + 100;
          break;

        // case 'D' for 500
        case 'D':
          result = result + 500;
          break;

        // case 'M' for 1000
        case 'M':
          result = result + 1000;
          break;
      }
    }

    // for loop to convert the number
    for (index = 1; index < length; index++) {
      // multiple if condition for manipulation in the number in roman
      if ((roman[index] == 'V' || roman[index] == 'X') &&
          roman[index - 1] == 'I') {
        result -= 1 + 1;
      }

      if ((roman[index] == 'L' || roman[index] == 'C') &&
          roman[index - 1] == 'X') {
        result -= 10 + 10;
      }

      if ((roman[index] == 'D' || roman[index] == 'M') &&
          roman[index - 1] == 'C') {
        result -= 100 + 100;
      }
    }

    cout << "Integer : " << result;
  }
};

int main() {
  // create an object
  RomanToInteger R;

  // calling getRoman() function to
  // insert number in roman
  R.getRoman();

  // calling RToI() function to convert
  // number from roman to Integer
  R.RToI();

  return 0;
}

Output:

Enter Roman Number : MC
Integer : 1100

Explanation:

In the above code, we have created a class RomanToInteger, one string type data member roman to store the numbers in roman, and public member functions getRoman() and RToI() to store and convert the given numbers.

In the main() function, we are creating an object R of class RomanToInteger, reading the numbers by the user using the function getRoman(), and finally calling the RToI() member function to convert the given numbers from roman to intger. The RToI() function contains the logic to convert the given numbers from roman to integer and printing the result.

C++ Class and Object Programs (Set 2) »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.