int Judge( NS * n,OS * o , char a[]) { float x,y,z; int i = 0; char sh; if(o->optop == Stack_Size - 1 ) { return FALSE; } sh = '#'; PushOperation(o,sh); while(a[i] != '#') {//全部进栈并且将所有操作全部转化,仅剩+ - if(a[i] >= 48 && a[i]<=57 ) { x = a[i] - 48; PushNum(n,x); } else if(a[i] == '+' && ( o->elem[o->optop - 1] == '*' || o->elem[o->optop - 1] == '/')) { PopNum(n,&x); PopNum(n,&y); PopOperation(o , &sh); z = Calculation( x, y , sh); PushNum(n , z); } else if(a[i] == '-' && (o->elem[o->optop - 1] == '*' || o->elem[o->optop - 1] == '/')) { PopNum(n,&x); PopNum(n,&y); PopOperation(o , &sh); z = Calculation( x, y , sh); PushNum(n , z); } else if( a[i] == '*' || a[i] == '/') { sh = a[i]; PopNum( n , &x); y = a[i+1] - 48; z = Calculation( x, y ,sh); PushNum(n,z); i++; } else { sh = a[i]; PushOperation(o , sh); } i++; } Result(n , o , a); return TRUE; }
int Result( NS * n , OS * o ,char a[]) { int flag = 1; float x = 0 , y = 0 , sum = 0; char sh; printf("%s = ",a); PopNum(n , &sum); while( n->numtop != -1 ) { PushOperation(o ,sh); PopNum(n , &x); sum = Calculation(sum,x,sh); } printf("%0.2f",sum); }
/* Two operand functions for infix calc */ void twoop(int keynum) { if (flagINV) { flagINV=0; DrawDisplay(); } if (!entered) { /* something like "5+*" */ if (!isopempty()) (void) PopOp(); /* replace the prev op */ PushOp(keynum); /* with the new one */ return; } if (entered==1) parse_double(&dnum); clrdisp=CLR=1; entered=Dpoint=exponent=0; if (!isopempty()) { /* there was a previous op */ lastop=PopOp(); /* get it */ if (lastop==kLPAR) { /* put it back */ PushOp(kLPAR); PushOp(keynum); PushNum(dnum); return; } /* now, if the current op (keynum) is of higher priority than the lastop, the current op and number are just pushed on top Priorities: (Y^X) > *,/ > +,- > >>,<< > & > ^ > ~ */ if (priority(keynum) > priority(lastop)) { PushNum(dnum); PushOp(lastop); PushOp(keynum); } else { /* execute lastop on lastnum and dnum, push result and current op on stack */ acc=PopNum(); switch (lastop) { /* perform the operation */ case kADD: acc += dnum; break; case kSUB: acc -= dnum; break; case kMUL: acc *= dnum; break; case kDIV: acc /= dnum; break; case kPOW: acc = pow(acc,dnum); break; case kMOD: acc = (long)acc % (long)dnum; break; case kAND: acc = (long)acc & (long)dnum; break; case kOR: acc = (long)acc | (long)dnum; break; case kXOR: acc = (long)acc ^ (long)dnum; break; case kSHL: acc = (long)acc << (long)dnum; break; case kSHR: acc = (long)acc >> (long)dnum; break; } PushNum(acc); PushOp(keynum); format_double(acc); DrawDisplay(); dnum=acc; } }