-
Very confused
I'm looking at K&R right now, and I just realized I've been wondering something for quite a while now.
In the functions chapter, they outline a reverse Polish calculator which uses the following code to determine if a number is a digit or not:
Code:
/*main.c*/
#include <stdio.h>
#include <math.h>
#include "calc.h"
#define MAXOP 100
#define NUMBER '0'
int main(void)
{
int type;
double op2;
char s[MAXOP];
while((type = getop(s)) != EOF){
switch(type){
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if(op2 != 0.0)
push(pop() / op2);
else
printf("Error: zero divisor!\n");
break;
case '\n':
printf("\t%.8g\n", pop());
break;
case 'q':
printf("Exiting...\n");
return 0;
default:
printf("Error: unknown command %s.\n", s);
break;
}
}
This isn't all the code, but it's enough to see what I want to know: basically, how does signal that a number has been found? I am not sure how comparing type to NUMBER would ever return true unless type was equal to 0 anyway.
-
If you assume that getop() reads in a string, and returns either an operator (eg '+') or the value which is in NUMBER, depending on what was typed in.
In short, look at the code for getop(), or read about it if it's some predefined function.
-
Oh, right... I totally missed that somehow. Prolly because I've been trying to read this book straight through without writing any code to practice, because I can't make Linux work properly at the moment.
On that subject, anyone know where I can get shell/compiler access, without HUGE(>1 second) command delays?
-
>>anyone know where I can get shell/compiler access, without
>>HUGE(>1 second) command delays?
You mean from a C program? Calling a system command processor will always be slower than you'd like because it has to make external requests to the environment. However, you may find that nonportable functions like exec* or spawn* are faster at it than the standard system function.