Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We would like to write a function that will read the next number in the input in roman numerals, and return it as an integer.

We would like to write a function that will read the next number in the input in roman numerals, and return it as an integer. We will also write a driver program to test this function.

Unfortunately, C does not provide a "%R" conversion specifier, or anything like that, so we will have to write our own. The function will be similar to getchar(), in that it is given nothing and returns the next number in the input as an integer.

-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.

To do this, you will need access to the files for the roman number program. Instead of everyone in the class copying my files to each of your directory, you will share my files using a symbolic link. To do this, first, copy the makefile from ~ee160/Code.lect/Roman to your Lab8 directory. Then run the command:

 make links 

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.

Compile and run the program found in readcmd.c ***( readcmd.c is at the bottom)This program accepts single letter commands as input and simply prints out the command it read. Any blanks in front of the command are ignored, as are any characters after the command. You should type in input lines like the following to see how it behaves:

 a a and this is a long line 

One of the things you will notice about this program is that it is not documented with comments for the algorithm or for the functions. Your first task is to document this code. Copy the program into myreadcmd.c, and extend this file to ignores leading tabs as well as leading blanks (or any combinations of blanks and tabs). Now extend the program to have either a semi-colon or a newline character indicate the end of a command. Here's a sample run:

 a ; b The command is: a The command is: b a The command is: a 

Now extend the program to verify that the command is an upper or lower case letter. It should print an error message if it isn't. Also, verify that the line has a command: if it hits a blank or ; without an intervening command, you should print an error. Here's a sample run:

 a ; b The command is: a The command is: b + Error: + is not a letter. / Error: / is not a letter. Error: missing command. ; b Error: missing command. The command is: b 

/* File : readcmd.c * * By : * * login: * * team : * * Date : */ /* * Read a command, skipping over leading blanks and any trailing * characters. */ #include  int skipBlanks(void); int skipOverRestOfCommand(void); int main() { int cmd; cmd = skipBlanks(); while (cmd != EOF) { printf("The command is: %c ", cmd); skipOverRestOfCommand(); cmd = skipBlanks(); } } int skipBlanks(void) { int c; c = getchar(); while (c == ' ') c = getchar(); return c; } int skipOverRestOfCommand(void) { int c; c = getchar(); while (c != ' ') c = getchar(); return c; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions