In C Language, write get_opnd() whose prototype is in opnd.h. // File: opnd.h /*
ID: 3705094 • Letter: I
Question
In C Language, write get_opnd() whose prototype is in opnd.h.
// File: opnd.h
/* This file contains the prototypes for functions in opnd.c */
int get_opnd(char ch);
/* This function is given the first character of an operand. It
reads the remaining characters of the operand and converts
it to internal integer form. Operands may be a sequence of
digit characters or a sequence of digits (the base) followed
by a sequence of exponent characters, in which case get_opnd()
computes the base to the exponent as its result. All characters
are echoed to the display using Display Module routines.
*/
Explanation / Answer
Please follow the code and comments for description:
CODE:
int get_opnd(char ch)
/* This function is given the first character of an operand. It
reads the remaining characters of the operand and converts
it to internal integer form. Operands may be a sequence of
digit characters or a sequence of digits (the base) followed
by a sequence of exponent characters, in which case get_opnd()
computes the base to the exponent as its result. All characters
are echoed to the display using Display Module routines.
*/
{ int baseVal = 0, // the base value
exponentVal = 0 ; // the exponent value
int got_dig = FALSE; // for error checking
char exponChar = '1'; // to hold the converted expon char
// skip any leading whitespace - no echoing
while(IS_WHITE_SPACE(ch)) ch = getchar();
// while there are digit characters
while(IS_DIGIT(ch))
{ // echo it
write_char(ch);
// we have a base
got_dig = TRUE;
// add it in baseVal
baseVal = baseVal * 10 + dig_to_int(ch);
// get the next character
ch = getchar();
}
// if the char is an exponent character
if(is_exp(ch))
// while we get more exponent chars
while(is_exp(ch))
{ // convert to a digit character
exponChar = exp_value(ch);
// echo it
write_exp(exponChar);
// add it in
exponentVal = exponentVal * 10 + dig_to_int(exponChar);
// get next character
ch = getchar();
}
// else the exponent is 1
else exponentVal = 1;
// echo the operand terminating character
write_char(ch);
// if not a valid operand
if((ch != ' ') || !got_dig)
{ // write an error and return 0
write_error("Illegal Operand, 0 Assumed");
return 0;
}
// else return the operand
return (int)pos_power((float)n, exponentVal);
}
Hope this is helpful.