/* * * 1998-2024 * Simple reverse polish calculator * */ #include #include /* atof() */ #include /* atof() */ #include "calc.h" static char __ident[] = "@(#)SPDsoft, Tue Oct 22 22:33:29 CEST 2024"; #define VERS_STR ((char*)&__ident[4]) #ifndef M_PI # define M_PI 3.14159265358979323846 #endif #ifndef M_E # define M_E 2.718281828459045235360287 #endif #define MAXOP 256 /* max size of operand or operator */ static int dostr( int type, char *s); void help() { fprintf(stderr,"%s\n", VERS_STR); fprintf(stderr,"\n+,*,-,/,h,;=top to console,q=quit\n"); fprintf(stderr,"H top to console (time format)\n"); fprintf(stderr,"e=power: p =pop to console\n"); fprintf(stderr,"s sin, c cos, t tan\n"); fprintf(stderr,"S asin, C acos, T atan\n"); fprintf(stderr,"N clear stack,V print stack\n"); fprintf(stderr,"E=push exp1, P= push PI\n\n"); fprintf(stderr,"Example: sen(sqrt(3*3+1)*3.5*PI/2) =\n"); fprintf(stderr," 3 3 x 1 + 0.5 e 3.5 P x x 2 / s p\n"); fprintf(stderr,"Example: 07:14:00 + 00:13\n"); fprintf(stderr," 07:14:00 00:13 + H\n"); } /* reverse Polish calculator */ int main( int argc, char **argv) { int i=0; int type=0; char s[MAXOP]; if ( argc==1 ) { while ((type = getop(s)) != EOF) { dostr ( type, s ); } } else { while ( ++i < argc ) { dostr ( sgetop(argv[i]), argv[i] ); } } return 0; } static int dostr( int type, char *s) { double op2; int tt[3]; switch (type) { case TIME: push(fromtime(s)); break; case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': case 'x': push(pop() * pop()); break; case '-': op2 = pop ( ); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else fprintf(stderr, "error: divisor is zero\n"); break; case 'e': op2 = pop(); push(pow(pop() , op2)); break; case 's': push(sin(pop())); break; case 'c': push(cos(pop())); break; case 't': push(tan(pop())); break; case 'T': push(atan(pop())); break; case 'C': push(acos(pop())); break; case 'S': push(asin(pop())); break; case 'E': push(M_E); break; case 'P': push(M_PI); break; case 'v': case ';': op2=pop();push(op2); printf(FORMATO_FL, op2); break; case 'H': op2=pop();push(op2); totime(op2, tt); printf("%0.2d:%0.2d:%0.2d\n", tt[0], tt[1], tt[2]); break; case 'V': PrintStack(); break; case 'p': printf(FORMATO_FL, pop()); break; case 'N': ClearStack(); break; case 'h': help(); break; case '\n': break; case 'q': exit(0); break; default: fprintf(stderr, "error: unknown command \"%s\"\n", s); break; } return 0; }