Good day to you.... I have this problem with my postfix calc.. I suspect it doesnt calculate at all ! .. it only writes input...maybe theres error in string conversion.. please help if you can
Code:#include <iostream> #include <math.h> #include <string.h> bool err = false; //stack : const int SIZE = 100; double stack[SIZE]; int nOperandu = 0; bool push(double x) { if (nOperandu<SIZE) { stack[nOperandu++] = x; return true; } return false; } bool pop(double &x) { if (nOperandu>0) { x = stack[--nOperandu]; return true; } return false; } int getCount() { return nOperandu; } bool isEmpty() { return nOperandu==0; } //end of stack // functions: void add() { double a; double b; pop(b); pop(a); push(a+b); } void minus() { double a; double b; pop(b); pop(a); push(a-b); } void multiply() { double a; double b; pop(b); pop(a); push(a*b); } void divi() { double a; double b; pop(b); pop(a); if (b == 0) { printf("Cant div. by 0.\n"); err = true; } else push(a/b); } void mod() { double a; double b; pop(b); pop(a); if (b == 0) { printf("Cant div. by 0.\n"); err = true; } else push(a / b); } void powa() { double a; double b; pop(b); pop(a); if ((a == 0) && (b < 0)) { printf("Cant pow. with negative number.\n"); err = true; } else push(pow(a,b)); } void full() { double a; pop(a); if (a < 0) push(ceil(a)); else push(floor(a)); } // end of functions // main alg: void alg() { char line[256]; char *cast = NULL; char *nextCast = NULL; double result; printf("Add numbers in postfix:\n "); gets_s(line,255); // loads line cast = strtok_s(line," \n", &nextCast); while ((cast != NULL) && !(err)) // loop till error { if ((cast[0]!='+') || (cast[0]!='-') || (cast[0]!='*') || (cast[0]!='/') || (cast[0]!='%') || (cast[0]!='^') || (cast[0]!='f')) // if first char isnt operator then convert to number and add to stack { push(atof(cast)); } else { switch (cast[0]) // chceks if there are enough operands in cases { case '+': if (getCount() >= 2) add(); else { printf("Not enough operators.\n"); err = true; } break; case '-': if (getCount() >= 2) minus(); else { printf("Not enough operators.\n"); err = true; } break; case '*': if (getCount() >= 2) multiply(); else { printf("Not enough operators.\n"); err = true; } break; case '/': if (getCount() >= 2) divi(); else { printf("Not enough operators.\n"); err = true; } break; case '%': if (getCount() >= 2) mod(); else { printf("Not enough operators.\n"); err = true; } break; case '^': if (getCount() >= 2) powa(); else { printf("Not enough operators.\n"); err = true; } break; case 'f': if (getCount() >= 1) full(); else { printf("Not enough operators.\n"); err = true; } break; } } cast = strtok_s( NULL, " \n", &nextCast); // jumps to next part of the line } /* if (isEmpty()) printf("Lack of operators\n"); else if (getCount() > 1) printf("Too many operators\n"); else*/ if (!err) { pop(result); printf("Result is: %0.5lf.\n",result); // writes result with 5 decimal } } //and of alg int main() { alg(); //return 0; system("PAUSE"); }



LinkBack URL
About LinkBacks
...maybe theres error in string conversion.. please help if you can


