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
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...
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...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher. A Caesar cipher shifts each letter a certain number of positions in the alphabet. The number of positions to shift will be given to your program as a command line argument. Characters other than letters should not be encrypted. Your program should stop only at the end of input. Your program...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
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.
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT