1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
1.5 |
static const char RCSid[] = "$Id: calc.c,v 1.4 2003/07/17 09:21:29 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* calc.c - simple algebraic desk calculator program. |
6 |
|
|
* |
7 |
|
|
* 4/1/86 |
8 |
|
|
*/ |
9 |
|
|
|
10 |
schorsch |
1.2 |
#include <stdlib.h> |
11 |
greg |
1.1 |
#include <stdio.h> |
12 |
schorsch |
1.2 |
#include <string.h> |
13 |
greg |
1.1 |
#include <setjmp.h> |
14 |
|
|
#include <ctype.h> |
15 |
|
|
|
16 |
schorsch |
1.4 |
#include "rterror.h" |
17 |
greg |
1.1 |
#include "calcomp.h" |
18 |
|
|
|
19 |
|
|
#define MAXRES 100 |
20 |
|
|
|
21 |
|
|
double result[MAXRES]; |
22 |
|
|
int nres = 0; |
23 |
|
|
|
24 |
|
|
jmp_buf env; |
25 |
|
|
int recover = 0; |
26 |
|
|
|
27 |
|
|
|
28 |
schorsch |
1.5 |
int |
29 |
greg |
1.1 |
main(argc, argv) |
30 |
|
|
int argc; |
31 |
|
|
char *argv[]; |
32 |
|
|
{ |
33 |
|
|
char expr[512]; |
34 |
|
|
FILE *fp; |
35 |
|
|
int i; |
36 |
|
|
register char *cp; |
37 |
|
|
|
38 |
|
|
esupport |= E_VARIABLE|E_INCHAN|E_FUNCTION; |
39 |
|
|
esupport &= ~(E_REDEFW|E_RCONST|E_OUTCHAN); |
40 |
|
|
#ifdef BIGGERLIB |
41 |
|
|
biggerlib(); |
42 |
|
|
#endif |
43 |
|
|
varset("PI", ':', 3.14159265358979323846); |
44 |
|
|
|
45 |
|
|
for (i = 1; i < argc; i++) |
46 |
|
|
fcompile(argv[i]); |
47 |
|
|
|
48 |
|
|
setjmp(env); |
49 |
|
|
recover = 1; |
50 |
|
|
eclock++; |
51 |
|
|
|
52 |
|
|
while (fgets(expr, sizeof(expr), stdin) != NULL) { |
53 |
|
|
for (cp = expr; *cp && *cp != '\n'; cp++) |
54 |
|
|
; |
55 |
|
|
*cp = '\0'; |
56 |
|
|
switch (expr[0]) { |
57 |
|
|
case '\0': |
58 |
|
|
continue; |
59 |
|
|
case '?': |
60 |
|
|
for (cp = expr+1; isspace(*cp); cp++) |
61 |
|
|
; |
62 |
|
|
if (*cp) |
63 |
|
|
dprint(cp, stdout); |
64 |
|
|
else |
65 |
|
|
dprint(NULL, stdout); |
66 |
|
|
continue; |
67 |
|
|
case '>': |
68 |
|
|
for (cp = expr+1; isspace(*cp); cp++) |
69 |
|
|
; |
70 |
|
|
if (!*cp) { |
71 |
|
|
eputs("file name required\n"); |
72 |
|
|
continue; |
73 |
|
|
} |
74 |
|
|
if ((fp = fopen(cp, "w")) == NULL) { |
75 |
|
|
eputs(cp); |
76 |
|
|
eputs(": cannot open\n"); |
77 |
|
|
continue; |
78 |
|
|
} |
79 |
|
|
dprint(NULL, fp); |
80 |
|
|
fclose(fp); |
81 |
|
|
continue; |
82 |
|
|
case '<': |
83 |
|
|
for (cp = expr+1; isspace(*cp); cp++) |
84 |
|
|
; |
85 |
|
|
if (!*cp) { |
86 |
|
|
eputs("file name required\n"); |
87 |
|
|
continue; |
88 |
|
|
} |
89 |
|
|
fcompile(cp); |
90 |
|
|
eclock++; |
91 |
|
|
continue; |
92 |
|
|
} |
93 |
schorsch |
1.3 |
if ((cp = strchr(expr, '=')) != NULL || |
94 |
|
|
(cp = strchr(expr, ':')) != NULL) { |
95 |
greg |
1.1 |
if (cp[1]) |
96 |
|
|
scompile(expr, NULL, 0); |
97 |
|
|
else if (*cp == '=') { |
98 |
|
|
*cp = '\0'; |
99 |
|
|
if (!strcmp(expr, "*")) |
100 |
|
|
dcleanup(1); |
101 |
|
|
else |
102 |
|
|
dclear(expr); |
103 |
|
|
} else { |
104 |
|
|
*cp = '\0'; |
105 |
|
|
if (!strcmp(expr, "*")) |
106 |
|
|
dcleanup(2); |
107 |
|
|
else |
108 |
|
|
dremove(expr); |
109 |
|
|
} |
110 |
|
|
eclock++; |
111 |
|
|
} else { |
112 |
|
|
printf("$%d=%.9g\n", nres+1, |
113 |
|
|
result[nres%MAXRES] = eval(expr)); |
114 |
|
|
nres++; |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
recover = 0; |
119 |
|
|
quit(0); |
120 |
schorsch |
1.5 |
return 0; /* pro forma exit */ |
121 |
greg |
1.1 |
} |
122 |
|
|
|
123 |
|
|
|
124 |
|
|
double |
125 |
|
|
chanvalue(n) /* return channel value */ |
126 |
|
|
int n; |
127 |
|
|
{ |
128 |
|
|
if (n == 0) |
129 |
|
|
n = nres; |
130 |
|
|
else if (n > nres || nres-n >= MAXRES) { |
131 |
|
|
fprintf(stderr, "$%d: illegal result\n", n); |
132 |
|
|
return(0.0); |
133 |
|
|
} |
134 |
|
|
return(result[(n-1)%MAXRES]); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
|
138 |
|
|
void |
139 |
|
|
eputs(msg) |
140 |
|
|
char *msg; |
141 |
|
|
{ |
142 |
|
|
fputs(msg, stderr); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
|
146 |
|
|
void |
147 |
|
|
wputs(msg) |
148 |
|
|
char *msg; |
149 |
|
|
{ |
150 |
|
|
eputs(msg); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
|
154 |
|
|
void |
155 |
|
|
quit(code) |
156 |
|
|
int code; |
157 |
|
|
{ |
158 |
|
|
if (recover) /* a cavalier approach */ |
159 |
|
|
longjmp(env, 1); |
160 |
|
|
exit(code); |
161 |
|
|
} |