I'm trying to solve a K&R exercise(5.10) that uses the argc and argv arguments.It's supposed to be a polish notation calculator that uses command line arguments as its' input.Well,the program runs fine for the '+' and '-' operators but I get errors for '*'.I figured out that it has something to do specifically with the '*' symbol,because if I replace it with something else the program works.Any ideas?Thank you.
Note:I didn't include the functions stack_in,stack_out and print_stack.I think they're good.
#include <stdio.h>
#define MAXST 10
#define NUMBER '1'
void stack_in(int n);
int stack_out(void);
void print_stack(void);
int main(int argc,char **argv)
{
char c,k;
while(--argc>0 && ++argv)
while(c=*(*argv)++){
if(c>='0' && c<='9')
k=NUMBER;
else
k=c;
switch (k){
int g1,g2;
case NUMBER:stack_in(c-'0');
break;
case '+':g1=stack_out();
g2=stack_out();
stack_in(g1+g2);
break;
case '-':g1=stack_out();
g2=stack_out();
stack_in(g2-g1);
break;
case '*':g1=stack_out();
g2=stack_out();
stack_in(g1*g2);
break;
default:printf("error on switch \n");
break;
}
}
print_stack();
return 0;
}
I think I've done that very exercise!
Anyway, it's likely that the * operator on the command line is turning into "all the files in the directory" which won't work. So you need to quote it when you call the program:
# rpmprog 2 3 '*'
or you can use back-quotes for most shells:
# rpmprog 2 3 \*
On Linux/UNIX shells, you should enclose the *
between single quotes '*'
if you don't want it to be expanded by your shell.
Your operating system uses "*" for a wildcard on the command line; no changes you make to your program will affect this. You may be able to "escape" the symbol on the command line -- i.e.,
calculator 2 2 \*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With