/* 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; }
void input (void) { int c; u64 x; u64 new_block = 0; NewNumber = TRUE; Radix = HEX; output(); for (;;) { if (new_block != BlockNum) { if (readDisk(new_block) != 0) { new_block = BlockNum; } } output(); c = getch(); switch (c) { /* * Don't use these: CNTRL(C), CNTRL(Z), CNTRL(S), * CNTLR(Q), CNTRL(Y) */ case '?': help(); break; case '_': lastx(); neg(); break; case '+': lastx(); add(); break; case '-': lastx(); sub(); break; case '*': lastx(); mul(); break; case '/': lastx(); divide(); break; case '%': lastx(); mod(); break; case '~': lastx(); not(); break; case '&': lastx(); and(); break; case '|': lastx(); or(); break; case '^': lastx(); xor(); break; case '<': lastx(); leftShift(); break; case '>': lastx(); rightShift(); break; case '.': lastx(); swap(); break; case '!': lastx(); store(); break; case '=': lastx(); retrieve(); break; case '#': lastx(); qrand(); break; case KEY_RESIZE: /* Screen resize event - don't do anything */ break; case KEY_LEFT: case 'h': Current->left(); /* left one column */ break; case KEY_DOWN: case 'j': Current->down(); /* down one row */ break; case KEY_UP: case 'k': Current->up(); /* up one row */ break; case KEY_RIGHT: case 'l': Current->right(); /* right one column */ break; case CNTRL('A'):clear(); /* switch to next format */ next_format(); break; case CNTRL('W'):clear(); /* switch to previous format */ prev_format(); break; case CNTRL('B'):/* Backward one block */ if (BlockNum > 0) { new_block = BlockNum - 1; } break; case EOF: /* Ignore EOF - send on resize */ break; case 'q': case CNTRL('D'):finish(0); /* Exit */ break; case CNTRL('F'):/* Forward a block */ new_block = BlockNum + 1; break; case 'g': /* Go to specified Block */ lastx(); x = pop(); NewNumber = FALSE; new_block = x; break; case KEY_BACKSPACE: case KEY_DC: case CNTRL('H'):/* Delete last digit entered */ push(pop() / RadixTable[Radix]); break; case CNTRL('I'):/* Next field */ Current->next(); break; case KEY_CLEAR: case CNTRL('L'):/* Refresh the display */ wrefresh(curscr); break; case KEY_ENTER: case CNTRL('M'):/* Finish entry of number or duplicate top */ if (!NewNumber) { duptop(); } NewNumber = FALSE; break; case CNTRL('P'):/* Push copy of current field */ Current->copy(); NewNumber = FALSE; break; case CNTRL('R'):/* Rotate from bottom of stack to top */ rotate(); break; case 'r': /* Change radix */ ++Radix; if (Radix > UNSIGNED) Radix = HEX; break; case CNTRL('T'):/* Reinitialize stack */ initStack(); NewNumber = TRUE; break; case CNTRL('U'):/* Push last x */ push(Lastx); NewNumber = FALSE; break; case CNTRL('X'):/* Delete top of stack */ (void)pop(); push(0); NewNumber = TRUE; case 'Z': /* Previous field */ Current->prev(); break; default: /* Is it a digit? */ if ((Radix == HEX) && isxdigit(c)) { if (isdigit(c)) { digit(c, '0'); } else if (isupper(c)) { digit(c, 'A' - 10); } else { digit(c, 'a' - 10); } } else if ((Radix == UNSIGNED) && isdigit(c)) { digit(c, '0'); } else if ((Radix == SIGNED) && isdigit(c)) { digit(c, '0'); } else if (Radix == CHARACTER) { digit(c, 0); } else { flash(); sprintf(Error, "bad char=%d", c); } break; } } }
/* 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; }