Question

In: Computer Science

Write a C++ program which reads a string, less than 10 characters long. This string represents...

Write a C++ program which reads a string, less than 10 characters long. This string represents
an integer expressed in roman numbers. Let a function convert the number from roman to
arabic form (i.e., our standard digits). Let then the main program writes out both forms. The
roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1.
Examples:
LXXXVII = 87
CCXIX = 219
MCCCLIV = 1354
MMDCLXXIII = 2673

Solutions

Expert Solution

Code:

#include <bits/stdc++.h>
using namespace std;

int count(char n)
{
   if (n == 'I')
       return 1;
   if (n == 'V')
       return 5;
   if (n == 'X')
       return 10;
   if (n == 'L')
       return 50;
   if (n == 'C')
       return 100;
   if (n == 'D')
       return 500;
   if (n == 'M')
       return 1000;

   return -1;
}

//function to convert from Roman to Arabic
int convert(string& x)
{
   //initializing m=0 to store the result
   int m = 0;

   for (int j = 0; j < x.length(); j++) {
       //a[j]'s value
       int a = count(x[j]);

       if (j + 1 < x.length()) {
           // a[j+1]'s value
           int b = count(x[j + 1]);

           //evaluating a and b
           if (a >= b) {
               m = m + a;
           }
           else {
               m = m + b - a;
               j++;
           }
       }
       else {
           m = m + a;
       }
   }
   return m;
}

//Main
int main()
{
   //reading the string
   string x = "MMDCLXXIII";
   //if condtion to check the length of the string
   if (x.length()>10){
        //if length of the string is greater than 10. program will print
        //invalid input
        cout<<"Invalid Input. Input must be less than 10 characters"<<endl;
   }
   else{
        //printing the output in both forms
   cout <<"Roman Number    Arabic Number"<<endl;
   cout << x<< "       =        "<< convert(x) << endl;
   }

   return 0;
}

Explanation:

The above C++ program reads a string less than 10 characters which represents a Roman number

and convert it to the Arabic form

This program consists of a Function named "convert" and main function

and prints the output in both forms i.e Roman and Arabic form in the main function.

if the string is longer than 10 characters , the program will print the output as "Invalid input"

Screenshots of the Code:

Screenshots of the Output:

If the String is longer than 10 characters. Then output is:


Related Solutions

In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run output Please input the number of nodes: 6 Please input the adjacency relation: {(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)}...
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run (to make program output more clear, I have put it in boldface): Please input...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT