#include #include #include #include struct {int oper,span; double val;} * expr; char *pb; int isz,ssz; #define OI(i) (expr[i-1].span) int compile(int c) { int n,s,k,i; double x; char *p,*q,*qq, op[] = "+-*/"; for ( p = pb, n = s = k = 0 ; c!=EOF && c!='\n' && c!='\r' ; *p++ = c, ++n, c=getchar() ) { if (c==' ' || c=='\t') s = 0; else if (s==0) {s = 1; ++k;} if (n+1==isz) { pb = realloc(pb,isz*=2); p = pb+n; } } *p = 0; // a line just read, now parse & compile it if (ssz1) return -1; return k; } cilk double eval(int n) { double x,y; if (expr[n].oper==0) return expr[n].val; y = spawn eval(n-1); x = spawn eval(OI(n)-1); sync; switch (expr[n].oper) { case 1: return x+y; case 2: return x-y; case 3: return x*y; case 4: return x/y; } } cilk int main() { int c,k; double x; pb = malloc(isz=2); expr = malloc((ssz=1)*sizeof(*expr)); while ((c=getchar())!=EOF) { k = compile(c); if (k>0) { x = spawn eval(k-1); sync; printf("%f\n",x); } else if (k<0) printf("error\n"); } return 0; }