/* 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) == '\n')
#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. */
/* File: driver.c
by: Tep Dobry
date:
*/
/* This file contains a test driver which reads a number in roman
numerals and prints it in decimal. I uses the function
get_roman() in roman.c
*/
#include <stdio.h>
#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\n",number);
/* prompt for next number */
printf("Enter an number in roman numerals(EOF to quit): ");
}
/* clean up screen */
printf("\n");
}
ROMAN = /inst/ee/ee160/ee160/Code.lect/Roman
CHAP4 = /inst/ee/ee160/ee160/Code.text/Chap4
roman: driver.o roman.o romanutil.o
cc driver.o roman.o romanutil.o -o roman
driver.o: roman.h
roman.o: roman.h romanutil.h
romanutil.o: roman.h romanutil.h tfdef.h chrutil.h
links:
ln -s -f $(ROMAN)/roman.c .
ln -s -f $(ROMAN)/roman.h .
ln -s -f $(ROMAN)/romanutil.c .
ln -s -f $(ROMAN)/romanutil.h .
ln -s -f $(ROMAN)/driver.c .
ln -s -f $(CHAP4)/chrutil.h .
ln -s -f $(CHAP4)/tfdef.h .
clean:
rm *.o
real_clean: clean
rm roman
/* File: roman.c
by: Tep Dobry
date:
mod :
*/
/* This file contains the functions used to read and convert a number
in roman numerals.
*/
#include <stdio.h>
#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;
}
/* File: roman.h
by: Tep Dobry
date:
Mod :
*/
/* This file contains the prototypes and macros for converting
roman numerals.
*/
/* 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 */
/* File: romanutil.c
by: Tep Dobry
date:
mod :
*/
/* This file contains the functions used to read and convert a number
in roman numerals.
*/
#include <stdio.h>
#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;
}
/* File: romanutil.h
by: Tep Dobry
date:
Mod :
*/
/* 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 */