Question

Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its...

Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its decimal number representation. Write a main program to test the function. Your function should have two arguments - the roman number as a string, and an error processing function. Write a helper function that will return the numeric value of each of the letters used in roman numbers. Then convert the string argument as follows look at the first two characters. If the first has a larger value than the second, then simply convert the first, call the conversation function again for the substring starting with the second character, and add both values. If the first character has a smaller value than the second, subtract the first from the second, and add the result to the conversion of the remaining string. Without validation this will convert also string that are not roman numbers, for example, "IC". Validate the string argument, if there is an error, call the error processing function passed as the second argument. Provide at least two error processing functions and test toDecimal with each of them. For example, one function may ask the user to correct the error, another may try to correct the error in some simple way, a third may just print a message and cause toDecimal to return a default value, etc..

- The symbols "I", "X", "C", and "M" can be repeated up to 3 times in succession, but no more. (They may appear more than three times if they appear non-sequentially, such as XXXIX.) "D", "L", and "V" can never be repeated.

- "I" can be subtracted from "V" and "X" only. "X" can be subtracted from "L" and "C" only. "C" can be subtracted from "D" and "M" only. "V", "L" and "D" can never be subtracted.

-Only one small value symbol may be subtracted from any large-value symbol.


please write this in C++ language
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

// Program to convert Roman Numerals to Numbers

#include<bits/stdc++.h>

using namespace std;

// This function returns value of a Roman symbol

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;

}

// Returns decimal value of roman numaral

int toDecimal(string &str)

{

  // Initialize result

  int res = 0;

  // Traverse given input

  for (int i=0; i<str.length(); i++)

  {

    // Getting value of symbol s[i]

    int s1 = value(str[i]);

    if (i+1 < str.length())

    {

      // Getting value of symbol s[i+1]

      int s2 = value(str[i+1]);

      // Comparing both values

      if (s1 >= s2)

      {

        // Value of current symbol is greater

        // or equal to the next symbol

        res = res + s1;

      }

      else

      {

        res = res + s2 - s1;

        i++; // Value of current symbol is

          // less than the next symbol

      }

    }

    else

    {

      res = res + s1;

      i++;

    }

  }

  return res;

}

// Driver Program

int main()

{

  // Considering inputs given are valid

  string str ="MCMLXXVII";

  cout << "Integer form of Roman Numeral is "

    << toDecimal(str) << endl;

  return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers....

    (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....

  • Write a Java program. In the "modern Roman" system a number is also a sequence of...

    Write a Java program. In the "modern Roman" system a number is also a sequence of M's, D's, C's, L's, X's, V's, and 1's. The symbols have to appear more or less in that order and the value of a number is obtained as before with one important exception: A symbol C, X, or I may precede a symbol of higher value, in which case the value of that symbol C, X, or I is taken to be negative. Write...

  • Write the roman.cpp implementation file that converts a number entered in Roman numerals to a positive...

    Write the roman.cpp implementation file that converts a number entered in Roman numerals to a positive integer. Your program should consist of a class, say, romanType. An object of type romanType should do the following: Step a: Store the number as a Roman numeral. Step b: Convert and store the number as a positive integer. Step c: Print the number as a Roman numeral or positive integer as requested by the user. Step d: Test your program using the following...

  • Create a function in python called “num_roman” that converts integers between 1 and 3999 into Roman...

    Create a function in python called “num_roman” that converts integers between 1 and 3999 into Roman numerals. The input will be a scalar number and it should output a string that is the Roman numeral representation of the input number. The function should give a meaningful error message if the input is less than one. The function should truncate a fractional value to an integer. Different meaningful error messages should occur if you enter a value greater than 3999 or...

  • Write up a detailed solution to the problem: Design a program to convert a Roman numeral...

    Write up a detailed solution to the problem: Design a program to convert a Roman numeral to a decimal number. The program should read a Roman numeral. You may read it as a string or one character at a time. Do the conversion and then output the decimal number. Here are the “letters” you need to know: Symbol =   Value I   = 1 V =   5 X =   10 L =   50 C =   100 D =   500 M   =...

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • C++ Write a program that converts anumber entered in Roman numerals to decimal.

    In C++ Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say,romanType. An object of typeromanTypeshould do the following:a. Store the number as a Romannumeral.b. Convert and store the number into decimalform.c. Print the number as a Roman numeral ordecimal number as requested by the user.The decimal values of the Roman numerals are:M 1000D 500C 100L 50X 10V 5I 1d. Test your program using the followingRoman numerals: MCXIV, CCCLIX,...

  • PYTHON 1- Write a function that receives a string value as a parameter, converts its first...

    PYTHON 1- Write a function that receives a string value as a parameter, converts its first character to uppercase and returns the first character. 2- Test your function by writing a main, which calls your function and prints its return value. 3- Save your program in a file called firstChar.py and use the following link to submit your file.  

  • Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”)...

    Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”) returns 1234. Hint: To convert a character to a number, subtract the ASCII value ‘0’ from the character. For example, if the string s has only one character, then the function can return the value s[0] – ‘0’.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT