Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//** There are error handling conditions. Can you write a program that handles t

ID: 3606188 • Letter: #

Question

//** There are error handling conditions. Can you write a program that handles the errors and do not give output if the Roman number is wrongly entered like 950=CML but not LM

There are conditions of auxiliary and basic .Can you check this and write code**//

Write a program to convert roman numerals into their arabic equivalent.

INPUT REQUIREMENTS

Read one or more roman numerals from standard input. Process one line at a time. Each input line contains only one roman numeral, starting in column one. Assume the characters are all upper case with no embedded blanks.

OUTPUT REQUIREMENTS

The arabic equivalent of each input roman numeral is displayed on standard output, starting in column one.

FUNCTIONAL REQUIREMENTS

Here are the arabic equivalents for roman symbols:

The "basic" roman symbols                                 The "auxiliary" roman symbols

I         X      C       M                                           V     L      D

1        10     100    1000                                      5     50     500


Convert the roman numeral to arabic processing the symbols from left to right according to the following rules:

1. A symbol following one of greater or equal value adds to its value. (E.g., XII = 12)
2. A symbol preceding one of greater value subtracts its value.(E.g., IV = 4; XL = 40)


ERROR HANDLING REQUIREMENTS

In each of the error conditions below, display the given message and skip the numeral and continue processing the next line.

"Invalid character in input. Valid characters are I,V,X,L,C,D,M."

Only the listed characters are valid.

"Invalid numeral: can't subtract auxiliary symbol."

It is not permitted to subtract an "auxiliary" symbol. (CML, not LM = 950; XLV not VL, = 45).

"Invalid numeral: two consecutive subtractions."

Can't do two subtractions in a row, thus LIVX is illegal.

"Invalid numeral: additions don't decrease."

Additions must decrease, as you go from left to right. Thus, each symbol added must have a value equal or less than the last symbol which was added. Thus, LIIX is wrong, cause we added L, added I, subtracted I, then try to add X.

Explanation / Answer

EDIT : I have also added the C program.

I have created the java program considering all the requirements with handling the all types of error. I have also added the INPUT format and the OUPUT format for your reference. I have also added the summary to every functions that I have created in the following program. Thank you for the question. Have a great day ahead.

/*
   C Program to convert Roman Numerals to Arabic Numbers with Error Handling Requirements.
*/
#include <stdio.h>
int result;
int length;

// to check auxiliary symbols
int isAuxiliary(char c){
   if(c == 'V' || c == 'L' || c == 'D'){
       return 1;
   }
   return 0;
}

// to find the corresponding Arabic value of roman numeral
int values(char c){
   if(c == 'I'){
       return 1;
   }
   if(c == 'V'){
       return 5;
   }
   if(c == 'X'){
       return 10;
   }
   if(c == 'L'){
       return 50;
   }
   if(c == 'C'){
       return 100;
   }
   if(c == 'D'){
       return 500;
   }
   if(c == 'M'){
       return 1000;
   }
   return 0;
}

// to check valid Character
int isValidCharacter(char romanNumeral[]){
   int i;
   for(i=0;i<length;i++){
       if(romanNumeral[i] == 'I' || romanNumeral[i] == 'V' || romanNumeral[i] == 'X'
       || romanNumeral[i] == 'L' || romanNumeral[i] == 'C' || romanNumeral[i] == 'D' || romanNumeral[i] == 'M'){

       }else{
           return 0;
       }
   }
   return 1;
}

// to check subtraction of auxiliary numerals
int isValidNumeral(char romanNumeral[]){
   int i;
   for(i=0;i<length-1;i++){
       int val1 = values(romanNumeral[i]);
       int val2 = values(romanNumeral[i+1]);
       if(val1 < val2){
           if(isAuxiliary(romanNumeral[i])){
               return 0;
           }
       }
   }
   return 1;  
}

// to check consecutive subtractions
int isConsecutiveSubtractions(char romanNumeral[]){
   int i;
   for(i=0;i<length-2;i++){
       int val1 = values(romanNumeral[i]);
       int val2 = values(romanNumeral[i+1]);
       int val3 = values(romanNumeral[i+2]);
       if(val1 < val2 && val2 < val3){
           return 1;
       }
   }
   return 0;  
}

// to check addition decrease and also to calculate the result
int isAdditionDecrease(char romanNumeral[]){
   int lastAddedValue = 0;
   int i;
   for(i=0;i<length;i++){
       int val1 = values(romanNumeral[i]);
       if(i+1 < length){
           int val2 = values(romanNumeral[i+1]);
           if(val1 >= val2){
               result = result + val1;
               if(lastAddedValue != 0 && lastAddedValue < val1){
                   return 1;
               }
               lastAddedValue = val1;
           }else{
               result = result + val2 - val1;
               if(lastAddedValue != 0 && lastAddedValue < (val2 - val1)){
                   return 1;
               }
               lastAddedValue = val2 - val1;
               i ++;
           }
       }else{
           result = result + val1;
           i++;
       }
   }
   return 0;  
}

int main(){
   int testCase;
   scanf("%d",&testCase);
   while(testCase--){
       result = 0;
       char romanNumeral[100];
       scanf("%s",romanNumeral);
       int i;
       length = 0;
       for(i=0;romanNumeral[i] != 0;i++){
           length++;
       }
       if(!isValidCharacter(romanNumeral)){
           printf("Invalid character in input. Valid characters are I,V,X,L,C,D,M.");
       }else if(!isValidNumeral(romanNumeral)){
           printf("Invalid numeral: can't subtract auxiliary symbol.");
       }else if(isConsecutiveSubtractions(romanNumeral)){
           printf("Invalid numeral: two consecutive subtractions.");
       }else if(isAdditionDecrease(romanNumeral)){
           printf("Invalid numeral: additions don't decrease.");
       }else{
           printf("%d",result);
       }
       printf(" ");
   }
}

/*
   Java Program to convert Roman Numerals to Arabic Numbers with Error Handling Requirements.
  
   INPUT :
   10
   ABC
   ADS
   CML
   LM
   XLV
   VL
   LIXC
   LIIX
   LIX
   LXXX
-----------------------------------
   OUTPUT :
   Invalid character in input. Valid characters are I,V,X,L,C,D,M.
   Invalid character in input. Valid characters are I,V,X,L,C,D,M.
   950
   Invalid numeral: can't subtract auxiliary symbol.
   45
   Invalid numeral: can't subtract auxiliary symbol.
   Invalid numeral: two consecutive subtractions.
   Invalid numeral: additions don't decrease.
   59
   80
*/
import java.util.Scanner;
class RomanToArabic{
   static int result;
   public static void main(String []arg){
       Scanner sc = new Scanner(System.in);
       int testCase = sc.nextInt();
       while(testCase-- > 0){
           result = 0;
           String romanNumeral = sc.next();
           boolean flag = true;
           if(!isValidCharacter(romanNumeral)){
               System.out.println("Invalid character in input. Valid characters are I,V,X,L,C,D,M.");
           }else if(!isValidNumeral(romanNumeral)){
               System.out.println("Invalid numeral: can't subtract auxiliary symbol.");
           }else if(isConsecutiveSubtractions(romanNumeral)){
               System.out.println("Invalid numeral: two consecutive subtractions.");
           }else if(isAdditionDecrease(romanNumeral)){
               System.out.println("Invalid numeral: additions don't decrease.");
           }else{
               System.out.println(result);
           }
       }
   }

   // to check valid Character
   public static boolean isValidCharacter(String romanNumeral){
       for(int i=0;i<romanNumeral.length();i++){
           if(romanNumeral.charAt(i) == 'I' || romanNumeral.charAt(i) == 'V' || romanNumeral.charAt(i) == 'X'
           || romanNumeral.charAt(i) == 'L' || romanNumeral.charAt(i) == 'C' || romanNumeral.charAt(i) == 'D' || romanNumeral.charAt(i) == 'M'){

           }else{
               return false;
           }
       }
       return true;
   }

   // to check subtraction of auxiliary numerals
   public static boolean isValidNumeral(String romanNumeral){
       for(int i=0;i<romanNumeral.length()-1;i++){
           int val1 = values(romanNumeral.charAt(i));
           int val2 = values(romanNumeral.charAt(i+1));
           if(val1 < val2){
               if(isAuxiliary(romanNumeral.charAt(i))){
                   return false;
               }
           }
       }
       return true;  
   }

   // to check consecutive subtractions
   public static boolean isConsecutiveSubtractions(String romanNumeral){
       for(int i=0;i<romanNumeral.length()-2;i++){
           int val1 = values(romanNumeral.charAt(i));
           int val2 = values(romanNumeral.charAt(i+1));
           int val3 = values(romanNumeral.charAt(i+2));
           if(val1 < val2 && val2 < val3){
               return true;
           }
       }
       return false;  
   }

   // to check addition decrease and also to calculate the result
   public static boolean isAdditionDecrease(String romanNumeral){
       int lastAddedValue = 0;
       for(int i=0;i<romanNumeral.length();i++){
           int val1 = values(romanNumeral.charAt(i));
           if(i+1 < romanNumeral.length()){
               int val2 = values(romanNumeral.charAt(i+1));
               if(val1 >= val2){
                   result = result + val1;
                   if(lastAddedValue != 0 && lastAddedValue < val1){
                       return true;
                   }
                   lastAddedValue = val1;
               }else{
                   result = result + val2 - val1;
                   if(lastAddedValue != 0 && lastAddedValue < (val2 - val1)){
                       return true;
                   }
                   lastAddedValue = val2 - val1;
                   i ++;
               }
           }else{
               result = result + val1;
               i++;
           }
       }
       return false;  
   }

   // to check auxiliary symbols
   public static boolean isAuxiliary(char c){
       if(c == 'V' || c == 'L' || c == 'D'){
           return true;
       }
       return false;
   }

   // to find the corresponding Arabic value of roman numeral
   public static int values(char c){
       if(c == 'I'){
           return 1;
       }
       if(c == 'V'){
           return 5;
       }
       if(c == 'X'){
           return 10;
       }
       if(c == 'L'){
           return 50;
       }
       if(c == 'C'){
           return 100;
       }
       if(c == 'D'){
           return 500;
       }
       if(c == 'M'){
           return 1000;
       }
       return 0;
   }
}