Answer to Exercise 4-3, page 79
Solution by Bob Wightman
Given the basic framework, it's straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.
In Bob's words: "Here's my attempt Adding the modulus is easily done by another case in main and using the fmod function. The standard library has been mentioned at this point so it should be valid to use this for type 0 compliance. math.h should be added to the list of #includes for fmod."
int main(void)
{
int type;
double op2;
char s[MAXOP];
int flag = TRUE;
while((type = Getop(s)) != EOF)
{
switch(type)
{
/* other cases snipped for brevity */
case '%':
op2 = pop();
if(op2)
push(fmod(pop(), op2));
else
printf("\nError: Division by zero!");
break;
}
}
return EXIT_SUCCESS;
}
Bob goes on to say: "Deal with unary minus when retrieving tokens. This is based on the fact that a unary minus will have no intervening space between it and its operand."
/* Getop: get next operator or numeric operand. */
int Getop(char s[])
{
#define PERIOD '.'
int i = 0;
int c;
int next;
/* Skip whitespace */
while((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
/* Not a number but may contain a unary minus. */
if(!isdigit(c) && c != PERIOD && c != '-')
return c;
if(c == '-')
{
next = getch();
if(!isdigit(next) && next != PERIOD)
{
return c;
}
c = next;
}
else
{
c = getch();
}
while(isdigit(s[++i] = c))
c = getch();
if(c == PERIOD) /* Collect fraction part. */
while(isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if(c != EOF)
unGetch(c);
return NUMBER;
}
'The C Programming Language' 카테고리의 다른 글
Chapter 4 - Functions and Program Structure 5 (0) | 2009.03.27 |
---|---|
Chapter 4 - Functions and Program Structure 4 (0) | 2009.03.27 |
Chapter 4 - Functions and Program Structure 2 (0) | 2009.03.27 |
Chapter 4 - Functions and Program Structure 1 (0) | 2009.03.25 |
Chapter 3 - Control Flow 6 (0) | 2009.03.25 |