Question: C language only : The roman number program above works correctly, provided the user enters valid roman digits followed by a newline at the prompt.

C language only: The roman number program above works correctly, provided the user enters valid roman digits followed by a newline at the prompt. If the user enters an invalid roman digit (even spaces), the programs behaves somewhat reasonably (What does it do?), but we would like to make it better.

driver.c is the driver and everything else is a function/ function declaration.

We would like the program to be tolerent of white space (spaces and tabs, and still provide the result if all of the other characters are valid roman digits.

If the user enters invalid characters, we would like the function to return 0 as a special value indicating an error instead of a partial result so far. We can use 0 as a special value because there is no Roman number representation for 0 (the Romans didn't understand 0) so 0 would not be returned for normal input. The driver should print an error message when this occurs and let the user try entering another number. You may modify any of the files that you need to, but you will need to break the link and copy the file for those you want to change.

code:

___chrutil.h

/* File: chrutil.h */ /* This file contains various macros and prototypes for character processing */ #define ERROR -2 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9') #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z') #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z') #define IS_WHITE_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == ' ') #define IS_PRINT(c) ((c) >= 32 && (c) < 127) #define LOWER 0 #define UPPER 1 #define DIGIT 2 #define PUNCT 3 #define SPACE 4 #define CONTROL 5 #define SPECIAL 6 int dig_to_int(char ch); char int_to_dig(int n); char uppercase(char ch); int getint(); int delimitp(char c); int whitep(char c); int punctp(char c); int vowelp(char c); int letterp(char c); int illegal(char c); /* Tests if c is legal. */

____driver.c

#include  #include "roman.h" main() { int number; /* prompt for first roman number entry */ printf("Enter an number in roman numerals(EOF to quit): "); /* while there are more numbers */ while((number = get_roman()) != EOF) { printf("The number is %d ",number); /* prompt for next number */ printf("Enter an number in roman numerals(EOF to quit): "); } /* clean up screen */ printf(" "); }

___ roman.c

#include  #include "roman.h" #include "romanutil.h" int get_roman(void) /* This function reads the next number in roman numerals from the input and returns it as an integer */ { char rdigit; int num = 0; int dig_value, last_dig_value = M; /* get the first digit */ rdigit = getchar(); /* while it is a roman digit */ while( is_roman(rdigit)) { /* convert roman digit to its value */ dig_value = convert_roman(rdigit); /* if previous digit was a prefix digit */ if(dig_value > last_dig_value) /* adjust total */ num = num - 2 * last_dig_value + dig_value; /* otherwise accumulate the total */ else num = num + dig_value; /* save this digit as previous */ last_dig_value = dig_value; /* get next digit */ rdigit = getchar(); } /* return EOF if detected */ if(rdigit == EOF) return EOF; /* return the number */ return num; }

___roman. h

/* These macros represent valid roman numerals and their values */ #define M 1000 #define D 500 #define C 100 #define L 50 #define X 10 #define V 5 #define I 1 int get_roman(void); /* returns the next roman number in the input or EOF */

}

____romanutil.c _____

/* This file contains the functions used to read and convert a number in roman numerals. */ #include  #include "roman.h" #include "romanutil.h" #include "tfdef.h" #include "chrutil.h" int is_roman(char c) /* This function is given a character and returns true if it is a valid roman numeral, flase otherwise. */ { /* convert digit to upper */ c = toupper(c); /* test the digit */ switch(c) { case 'M': case 'D': case 'C': case 'L': case 'X': case 'V': case 'I': return TRUE; default : return FALSE; } } int convert_roman(char c) /* This function is given a roman numeral and returns its value. NULL is returned if the character is not valid */ { int digit; /* convert digit to upper */ c = toupper(c); /* convert the digit */ switch(c) { case 'M': digit = M; break; case 'D': digit = D; break; case 'C': digit = C; break; case 'L': digit = L; break; case 'X': digit = X; break; case 'V': digit = V; break; case 'I': digit = I; break; default : digit = NULL; } /* and return its value */ return digit; } char toupper(char c) { if(IS_LOWER(c)) return c - 'a' + 'A'; return c; }

_____romanutil.h______

/* This file contains the prototypes for utilities used in converting roman numerals. */ int is_roman(char c); /* tests if c is a valid roman numeral, returns true or false */ int convert_roman(char c); /* converts roman numeral c to its value, NULL if invalid */ char toupper(char); /* converts a character to upper case */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!