11 char opstack[BUFSIZE];
14 static char lex(int *ip)
19 while (isspace(*arg)) arg++;
21 case 0: case '+': case '-': case '*': case '/': case '(': case ')':
26 l=strtol(arg,&arg,10);
30 fprintf(stderr,"Invalid characted in string: %s\n",arg);
35 static int oppri(char op)
39 case '+': case '-': return (10);
40 case '*': case '/': return (20);
47 static void evalit(void)
51 fprintf(stderr,"2 arguments for operator required!\n");
55 switch (opstack[--opstackp]) {
56 case '+': istack[istackp-1]+=istack[istackp]; break;
57 case '-': istack[istackp-1]-=istack[istackp]; break;
58 case '*': istack[istackp-1]*=istack[istackp]; break;
60 if (!istack[istackp]) {
61 fprintf(stderr,"Division of %d by zero not supported!\n",istack[istackp-1]);
64 istack[istackp-1]/=istack[istackp];
69 static void pushop(char op)
72 while (opstackp && op!='(' && oppri(opstack[opstackp-1])>=mypri)
76 if (!opstackp || opstack[opstackp-1]!='(') {
77 fprintf(stderr,"Not enough content for brackets-inside space!\n");
83 assert(opstackp<BUFSIZE);
84 opstack[opstackp++]=op;
87 static void process(void)
93 for (;;) switch ((op=lex(&curi))) {
96 fprintf(stderr,"Two successive numbers not permitted!\n");
99 assert(istackp<BUFSIZE);
100 istack[istackp++]=curi;
104 fprintf(stderr,"Open-bracket not permitted after number!\n");
109 case '+': case '-': case '*': case '/': case ')': pushop(op); break;
112 if (istackp!=1||opstackp) {
113 fprintf(stderr,"Machine not in sane state on the end of expression, ints=%d,ops=%d!\n",istackp,opstackp);
116 printf("%d\n",istack[0]);
122 int main(int argc,char **argv)
125 for (argn=1;argn<argc;argn++) {
129 return(EXIT_SUCCESS);