int main( void ){ int type; double operand; char expression[MAX_POLISH_SIZE]; // instruction printf("use ~ to represent sin operation.\n"); printf("use # to represent exp operation.\n"); printf("use @ to represent power operation.\n"); while((type=gettop(expression)) != EOF){ switch(type){ case NUMBER: { push(atof(expression)); break;} case '+': { push(pop() + pop()); break;} case '*': { push(pop() * pop()); break;} case '-': { swaptop2(); push(pop() - pop()); break;} case '/': { operand = peek(); swaptop2(); assert(operand != 0); push(pop() / pop()); break;} case '%': { operand = peek(); swaptop2(); assert(operand != 0); push(mod(pop(), operand)); break;} case '~': // sin { push(sin(pop())); break;} case '#': // exp { push(exp(pop())); break;} case '@': // power { operand = peek(); swaptop2(); push(pow(pop(), operand)); break;} case '\n': { printf("\t%.8g\n", pop()); break;} default: { printf("error happens.\n"); break;} } } return 0; }
/* reverse Polish calculator */ main() { int type; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '"': duptop(); break; case '\'': printtop(); break; case '!': swaptop2(); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) { push(pop() / op2); } else { printf("error: zero divisor\n"); } break; case '%': op2 = pop(); if (op2 != 0.0) { push(((int) pop()) % (int) op2); } else { printf("error: zero divisor\n"); } break; case '^': op2 = pop(); push(pow(pop(), op2)); break; case '#': push(exp(pop())); break; case '$': push(sin(pop())); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0; }
// But it's a polish calculator int main() { int type; double op2; // For div and sub, we need to do the operation backwards char s[MAXOP]; while ( (type=getop(s)) != EOF ) { switch (type) { case NUMBER: push(atof(s)); break; case VARIABLE: push(variables[s[0] - 'A']); break; case ASSIGN: if (s[0] < 'A' || s[0] > 'Z') printf("Error: Not a proper variable name\n"); else variables[s[0] - 'A'] = pop(); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("Error: Zero division\n"); break; case '%': op2 = pop(); if (op2 != 0.0) push((int)pop() % (int)op2); else printf("Error: Zero division with modulus\n"); break; case SIN: push(sin(pop())); break; case COS: push(cos(pop())); break; case TAN: // Not bothering to worry about multiples of pi/2 push(tan(pop())); break; case EXP: push(exp(pop())); break; case POW: op2 = pop(); push(pow(pop(), op2)); break; case 'd': // d for duplicate duptop(); break; case 's': // s for swap swaptop2(); break; case 'c': // c for clear clear(); break; case '\n': printf("\t%.8g\n", peek()); break; default: printf("Error: unknown command %s\n", s); break; } } return 0; }
/* reverse Polish calculator */ main() { int type; double op2, last, vars[26]; char s[MAXOP], varname; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '"': duptop(); break; case '\'': printtop(); break; case '!': swaptop2(); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) { push(pop() / op2); } else { printf("error: zero divisor\n"); } break; case '%': op2 = pop(); if (op2 != 0.0) { push(((int) pop()) % (int) op2); } else { printf("error: zero divisor\n"); } break; case '^': op2 = pop(); push(pow(pop(), op2)); break; case '#': push(exp(pop())); break; case '$': push(sin(pop())); break; case ASSIGNVAR: varname = s[0]; duptop(); vars[varname - 'a'] = pop(); break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': push(vars[type - 'a']); break; case '?': push(last); break; case '\n': last = pop(); printf("\t%.8g\n", last); break; default: printf("error: unknown command %s\n", s); break; } } return 0; }