#include #include #import #import #import int main() { int c,err; double x,y; char *p,*q; NSMutableData *line = [NSMutableData new]; NSMutableArray *stk = [NSMutableArray new]; #define POP(z) z = [[stk lastObject] doubleValue]; \ [stk removeLastObject] while ((c=getchar())!=EOF) { if (c=='\n' || c=='\r') c = 0; [line appendBytes:(void*)&c length:1]; if (c) continue; [stk removeAllObjects]; p = [line mutableBytes]; err = 0; while (NULL!=(q=strtok(p," \t"))) { x = strtod(q,&p); err = p!=q+strlen(q); p = NULL; if (err) { err = q[1] || NULL==strchr("+-*/",*q) || 2>[stk count]; if (err) break; POP(y); POP(x); switch (*q) { case '+': x += y; break; case '-': x -= y; break; case '*': x *= y; break; case '/': x /= y; } } [stk addObject:[NSNumber numberWithDouble:x]]; } if (err || 1<[stk count]) puts("error"); else if ([stk count]) {POP(x); printf("%f\n",x);} [line setLength:0]; } return 0; }