📄 calc.c
字号:
CSP++; } else { if (isdot || base == 10) calcdata[CSP] = atof(CS[CSP]); else calcdata[CSP] = (double) strtol(CS[CSP], 0, 16); enablepush = 1; inputptr = 0; } formatentry(CSP); muiSetTLTopInt(tl, CSP - Displaylines + 1); glutPostRedisplay();}void binop(char c){ int i, j; if (enablepush == 0) doenter(); if (CSP < Displaylines) return; switch (c) { case '+': calcdata[CSP-1] = calcdata[CSP-1] + calcdata[CSP]; break; case '-': calcdata[CSP-1] = calcdata[CSP-1] - calcdata[CSP]; break; case '*': calcdata[CSP-1] = calcdata[CSP-1] * calcdata[CSP]; break; case '/': calcdata[CSP-1] = calcdata[CSP-1] / calcdata[CSP]; break; case '^': if (invmode == 0) calcdata[CSP-1] = pow(calcdata[CSP-1], calcdata[CSP]); else calcdata[CSP-1] = pow(calcdata[CSP-1], 1.0/calcdata[CSP]); break; case '&': if (calcdata[CSP-1] - trunc(calcdata[CSP-1]) != 0) return; if (calcdata[CSP] - trunc(calcdata[CSP]) != 0) return; i = calcdata[CSP-1]; j = calcdata[CSP]; calcdata[CSP-1] = (int)(i&j); break; case '|': if (calcdata[CSP-1] - trunc(calcdata[CSP-1]) != 0) return; if (calcdata[CSP] - trunc(calcdata[CSP]) != 0) return; i = calcdata[CSP-1]; j = calcdata[CSP]; calcdata[CSP-1] = (int)(i|j); break; } CS[CSP][0] = 0; CSP--; formatentry(CSP); muiSetTLTopInt(tl, CSP - Displaylines + 1); glutPostRedisplay(); enablepush = 1;}void unop(int c){ int i; if (enablepush == 0) doenter(); switch (c) { case Storekey: memory = calcdata[CSP]; break; case Recallkey: doenter(); calcdata[CSP] = memory; break; case Flipsignkey: calcdata[CSP] = -calcdata[CSP]; break; case Oneoverkey: calcdata[CSP] = 1.0/calcdata[CSP]; break; case Clearkey: calcdata[CSP] = 0.0; enablepush = 0; formatentry(CSP); glutPostRedisplay(); return; case Sinkey: if (invmode) { calcdata[CSP] = asin(calcdata[CSP]); if (degreemode) calcdata[CSP] *= 180.0/M_PI; } else { if (degreemode) calcdata[CSP] *= M_PI/180.0; calcdata[CSP] = sin(calcdata[CSP]); } break; case Coskey: if (invmode) { calcdata[CSP] = acos(calcdata[CSP]); if (degreemode) calcdata[CSP] *= 180.0/M_PI; } else { if (degreemode) calcdata[CSP] *= M_PI/180.0; calcdata[CSP] = cos(calcdata[CSP]); } break; case Tankey: if (invmode) { calcdata[CSP] = atan(calcdata[CSP]); if (degreemode) calcdata[CSP] *= 180.0/M_PI; } else { if (degreemode) calcdata[CSP] *= M_PI/180.0; calcdata[CSP] = tan(calcdata[CSP]); } break; case Tentoxkey: if (invmode) calcdata[CSP] = log10(calcdata[CSP]); else calcdata[CSP] = pow(10, calcdata[CSP]); break; case Expkey: if (invmode) calcdata[CSP] = log(calcdata[CSP]); else calcdata[CSP] = exp(calcdata[CSP]); break; case Intkey: if (invmode) calcdata[CSP] = calcdata[CSP] - trunc(calcdata[CSP]); else calcdata[CSP] = trunc(calcdata[CSP]); break; case Sqrtkey: if (invmode==0) calcdata[CSP] = sqrt(calcdata[CSP]); else calcdata[CSP] = calcdata[CSP]*calcdata[CSP]; break; case Notkey: if (calcdata[CSP] - trunc(calcdata[CSP]) != 0) return; i = calcdata[CSP]; calcdata[CSP] = (int)(~i); break; } formatentry(CSP); muiSetTLTopInt(tl, CSP - Displaylines + 1); glutPostRedisplay(); enablepush = 1;}void interpclick(int x){ int i, rcount; float f; char *c; if (x == 0) return; if (savingprog && x != Progkey && x != Runkey) program[proglen++] = x; if ((Zerokey <= x && x <= Ninekey) || x == Dotkey || ((base == 16) && (Akey <= x && x <= Fkey))) { if (enablepush) push(); enablepush = 0; if (x == Dotkey) CS[CSP][inputptr++] = '.'; else if (Akey <= x && x <= Fkey) CS[CSP][inputptr++] = 'a' + x - Akey; else CS[CSP][inputptr++] = '0'+x-Zerokey; CS[CSP][inputptr] = 0; muiSetTLTopInt(tl, CSP - Displaylines + 1); glutPostRedisplay(); } else switch (x) { case Pluskey: binop('+'); break; case Minuskey: binop('-'); break; case Timeskey: binop('*'); break; case Dividekey: binop('/'); break; case Xtoykey: binop('^'); break; case Helpkey: showhelp(); break; case Clearkey: unop(Clearkey); break; case Flipsignkey: unop(Flipsignkey); break; case Enterkey: doenter(); break; case Exchkey: if (CSP < Displaylines) break; if (enablepush == 0) doenter(); f = calcdata[CSP]; calcdata[CSP] = calcdata[CSP-1]; calcdata[CSP-1] = f; c = CS[CSP]; CS[CSP] = CS[CSP-1]; CS[CSP-1] = c; glutPostRedisplay(); break; case Rollkey: if (CSP < Displaylines) break; rcount = CSP - Displaylines+1; if (enablepush == 0) doenter(); f = calcdata[CSP]; c = CS[CSP]; for (i = 0; i < rcount; i++) { calcdata[CSP-i] = calcdata[CSP-i-1]; CS[CSP-i] = CS[CSP-i-1]; } calcdata[CSP-rcount] = f; CS[CSP-rcount] = c; glutPostRedisplay(); break; case Dup2key: if (CSP < Displaylines) break; if (enablepush == 0) doenter(); strcpy(CS[CSP+2], CS[CSP]); calcdata[CSP+2] = calcdata[CSP]; strcpy(CS[CSP+1], CS[CSP-1]); calcdata[CSP+1] = calcdata[CSP-1]; CSP += 2; muiSetTLTopInt(tl, CSP - Displaylines + 1); glutPostRedisplay(); break; case Radkey: if (degreemode) { keypad[0][5].label = "Rad"; keypad[0][5].invlabel = "Rad"; } else { keypad[0][5].label = "Deg"; keypad[0][5].invlabel = "Deg"; } muiLoadButton(keypad[0][5].b, keypad[0][5].label); degreemode = 1 - degreemode; loadbuttons(invmode); break; case Invkey: if (invmode) { invmode = 0; loadbuttons(0); } else { invmode = 2; loadbuttons(1); } break; case Progkey: if (savingprog == 0) { proglen = 0; } savingprog = 1 - savingprog; break; case Runkey: if (savingprog) break; for (i = 0; i < proglen; i++) interpclick(program[i]); break; case Storekey: unop(Storekey); break; case Recallkey: unop(Recallkey); break; case Oneoverkey: unop(Oneoverkey); break; case Sinkey: unop(Sinkey); break; case Coskey: unop(Coskey); break; case Tankey: unop(Tankey); break; case Expkey: unop(Expkey); break; case Tentoxkey: unop(Tentoxkey); break; case Intkey: unop(Intkey); break; case Andkey: binop('&'); break; case Orkey: binop('|'); break; case Notkey: unop(Notkey); break; case Base10key: if (enablepush == 0) doenter(); base = 10; for (i = Displaylines-1; i <= CSP; i++) formatentry(i); glutPostRedisplay(); break; case Base16key: if (enablepush == 0) doenter(); base = 16; for (i = Displaylines-1; i <= CSP; i++) formatentry(i); glutPostRedisplay(); break; case Sqrtkey: unop(Sqrtkey); break; } if (invmode == 1) { loadbuttons(0); } if (invmode > 0) invmode--;}void showhelp(void){printf("\n\n------------------------------------------\n\n");printf("Calc is a Reverse-Polish-Notation (RPN) calculator. You\n");printf("must enter the operands first, then the operation. For\n");printf("example, to add 3 and 4, press [3] [Enter] [4] [+]. If\n");printf("the operation is unary, like sine, it operates on the bottom\n");printf("element of the display. To take the sine of .54, do:\n");printf("\n");printf("[.] [5] [4] [Sin].\n");printf("\n");printf("The last 6 entries of the stack are visible, and you can\n");printf("scroll to see the rest. All operations are performed on\n");printf("the element(s) at the bottom of the stack. The bottom\n");printf("element is called 'x' and the next element up is called 'y'.\n");printf("\n");printf("The [+/-] key changes the sign of x. To find the cosine of\n");printf("-.22, do:\n");printf("\n");printf("[.] [2] [2] [+/-] [Cos].\n");printf("\n");printf("The [Inv] key changes the operation of some of the other keys\n");printf("so they perform the inverse operation. It is only active for\n");printf("one keystroke. Press [Inv] again to cancel the operation.\n");printf("\n");printf("[Sto] and [Rcl] stores and recalls a single value.\n");printf("\n");printf("[Dup2] duplicates the bottom 2 items on the stack.\n");printf("\n");printf("[Roll] rolls all the stack elements down one, and puts the\n");printf("bottom element on the top.\n");printf("\n");printf("[Exch] swaps the bottom two elements.\n");printf("\n");printf("[Int] gives the integer part.\n");printf("\n");printf("[Inv] [Frac] gives the fractional part.\n");printf("\n");printf("[Clr] clears the bottom element to zero. Use this when you\n");printf("get some kind of error\n");printf("\n");printf("[B10] and [B16] put you in base 10 or base 16 mode. Numbers\n");printf("with fractional parts are always displayed in base 10. In\n");printf("base 16 mode, the keys [a] through [f] are used for numeric\n");printf("entry. They do nothing, otherwise.\n");printf("\n");printf("[And], [Or] and [Not] are logical operations on 32 bit\n");printf("integers. If there's a fractional part, they don't do\n");printf("anything.\n");printf("\n");printf("To remember a sequence of keystrokes, press [Prog], then the\n");printf("sequence of keystrokes, and then [Prog] again. For example,\n");printf("if you want to calculate x^2 + y^2 repeatedly, where x and y\n");printf("are the two bottom entries of the stack, do this:\n");printf("\n");printf("[Prog] [Inv] [x^2] [Exch] [Inv] [x^2] [+] [Prog].\n");printf("\n");printf("Then, to calculate 5^2+7^2, do this:\n");printf("\n");printf("[5] [Enter] [7] [Run].\n");printf("\n");printf("The following keys from the computer keyboard are understood\n");printf("by calc:\n");printf("\n");printf("[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [.],\n");printf("[Enter], [+], [-], [*], [/], [a], [b], [c], [d], [e], [f].\n");printf("\n");printf("The [Deg]/[Rad] key shows the current angle mode. Press\n");printf("it to get the other angle mode.\n");printf("\n------------------------------------------\n\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -