this is my roman.c. includes get_roman function.
Code:
#include <stdio.h>
#include "roman.h"
#include "romanutil.h"
#include "chrutil.h"
#include "tfdef.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();
/* if it is a white space skip it */
while(IS_WHITE_SPACE(rdigit))
rdigit = getchar();
while(!is_roman(rdigit) && !IS_WHITE_SPACE(rdigit) && rdigit != EOF) return 0;
/* 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;
}
this is function that examine if it's a roman numeral.
Code:
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 = to_upper(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 = to_upper(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;
Thank you so much!!!!!!
Sorry I didn't post it clear.