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 ___ roman.c
#include ___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 _____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
Get step-by-step solutions from verified subject matter experts
