wmbiff: EXAMINE before STATUS
[dockapps.git] / wmcalc / wmcalcfunc.c
blob8b98338d4dd1de443f20c96ca6dbc41a4b5c2eba
1 /****************************************************************
2 * File: wmcalcfunc.c
3 * Version: 0.21
4 * Author: Edward H. Flora <ehflora@access1.net>
6 * This file is a part of the wmcalc application. As such, this
7 * file is licensed under the GNU General Public License, version 2.
8 * A copy of this license may be found in the file COPYING that should
9 * have been distributed with this file. If not, please refer to
10 * http://www.gnu.org/copyleft/gpl.html for details.
12 ****************************************************************
13 Description:
14 This file contains the code for the actual calculator functions,
15 such as a add, subt, clear, etc.
17 Change History:
18 Date Modification
19 11/03/00 File Header added
20 27/12/06 Increased significant digits (Antony Gelberg, antony@wayforth.co.uk)
22 ****************************************************************/
24 /***** Includes *************************************************/
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <ctype.h>
29 #include "wmcalc_c.h"
30 #include "wmcalc_err.h"
31 #include "wmcalc_f.h"
34 /****************************************************************
35 * Function: clearcalc
36 ****************************************************************
37 Description:
38 This function will clear the calculator display, and internal
39 flags.
41 Change History:
42 Date Modification
43 11/03/00 Function header added
44 11/04/00 Replaced magic numbers with DISPSIZE
45 ****************************************************************/
46 void clearcalc(void) {
47 extern int StrCnt;
48 extern char PlusMinusFlag;
49 extern int ExpFlag;
50 extern int DecFlag;
51 extern double RegisterA;
52 extern double RegisterB;
53 extern char DispString[];
54 extern char OpFlag;
55 int i;
57 RegisterA = 0.0;
58 RegisterB = 0.0;
59 ExpFlag = 0;
60 DecFlag = 0;
61 PlusMinusFlag = '+';
62 StrCnt = 0;
63 for (i=0; i < DISPSIZE; i++) {
64 DispString[i] = ' ';
66 OpFlag = ' ';
67 } /***** End of function clearcalc() *****************************/
69 /****************************************************************
70 * Function: clearnum
71 ****************************************************************
72 Description:
73 Clears the current number being entered.
75 Change History:
76 Date Modification
77 11/03/00 Updated function header
78 11/04/00 Replaced magic numbers with DISPSIZE
79 ****************************************************************/
80 void clearnum(void) {
81 extern int StrCnt;
82 extern char PlusMinusFlag;
83 extern int ExpFlag;
84 extern int DecFlag;
85 extern double RegisterA;
86 extern char DispString[];
87 int i;
89 RegisterA = 0.0;
90 ExpFlag = 0;
91 DecFlag = 0;
92 PlusMinusFlag = '+';
93 StrCnt = 0;
94 for (i=0; i < DISPSIZE; i++) {
95 DispString[i] = ' ';
98 } /***** End of function clearnum() ******************************/
100 /****************************************************************
101 * Function: charkey
102 ****************************************************************
103 Description:
104 Add characters to the number being entered.
106 Change History:
107 Date Modification
108 11/03/00 Updated function header
109 11/04/00 Replaced magic numbers with DISPSIZE
110 ****************************************************************/
111 void charkey(char ch) {
112 extern int Verbose;
113 extern int StrCnt;
114 extern int DecFlag;
115 extern double RegisterA, RegisterB;
116 extern char DispString[];
117 int i;
119 if (Verbose) printf("In function charkey\n");
121 if (StrCnt < DISPSIZE) {
122 if (ch == '.') {
123 if (DecFlag == 0) {
124 for (i = 1; i < DISPSIZE; i++)
125 DispString[i-1] = DispString[i];
126 DecFlag = 1;
127 StrCnt++;
128 DispString[DISPSIZE - 1] = ch;
131 else {
132 for (i = 1; i < DISPSIZE; i++)
133 DispString[i-1] = DispString[i];
134 DispString[9] = ch;
135 StrCnt++;
137 } /* endif (StrCnt < DISPSIZE) */
138 else if (StrCnt == CALCDONE) {
139 RegisterB = RegisterA;
140 clearnum();
141 if (ch == '.') {
142 for (i = 1; i < DISPSIZE; i++)
143 DispString[i-1] = DispString[i];
144 DecFlag = 1;
145 StrCnt++;
146 DispString[DISPSIZE -1] = ch;
148 else {
149 for (i = 1; i < DISPSIZE; i++)
150 DispString[i-1] = DispString[i];
151 DispString[DISPSIZE - 1] = ch;
152 StrCnt++;
154 } /* endif (StrCnt == CALCDONE) */
155 RegisterA = atof(DispString);
156 } /***** End of Function charkey() *******************************/
159 /****************************************************************
160 * Function: chgsignnum
161 ****************************************************************
162 Description:
163 Change the sign of the number currently being entered
165 Change History:
166 Date Modification
167 11/03/00 Updated Function header
168 ****************************************************************/
169 void chgsignnum(void) {
170 extern int Verbose;
171 extern double RegisterA;
172 extern char DispString[];
174 if (Verbose) printf("In function chgsignnum\n");
176 RegisterA = -RegisterA;
177 sprintf(DispString, "%10.5g", RegisterA);
179 } /***** End of function chgsignnum() *****************************/
182 /****************************************************************
183 * Function: sqrnum
184 ****************************************************************
185 Description:
186 Square the number in RegisterA
187 Change History:
188 Date Modification
189 11/03/00 Updated Function header
190 ****************************************************************/
191 void sqrnum(void) {
192 extern int Verbose;
193 extern int StrCnt;
194 extern double RegisterA;
195 extern char DispString[];
196 extern int ImgFlag;
198 if (Verbose) printf("In function sqrnum\n");
199 RegisterA = atof(DispString);
200 RegisterA = pow(RegisterA, 2.0);
201 if (ImgFlag) {
202 RegisterA = -RegisterA;
203 ImgFlag = 0;
205 sprintf(DispString, "%10.5g", RegisterA);
206 StrCnt = CALCDONE;
208 } /***** End of Function sqrnum() *******************************/
210 /****************************************************************
211 * Function: sqrtnum
212 ****************************************************************
213 Description:
214 Take the square root of the number in RegisterA
215 Change History:
216 Date Modification
217 11/03/00 Updated function header
218 11/04/00 Replaced magic numbers with DISPSIZE
219 ****************************************************************/
220 void sqrtnum(void) {
221 extern int Verbose;
222 extern int StrCnt;
223 extern double RegisterA;
224 extern int ImgFlag;
225 extern char ImagChar;
226 extern char DispString[];
227 int i;
229 if (Verbose) printf("In function sqrtnum\n");
230 RegisterA = atof(DispString);
231 if (RegisterA >= 0) {
232 RegisterA = pow(RegisterA, 0.5);
233 sprintf(DispString, "%10.5g", RegisterA);
235 else {
236 RegisterA = pow(-RegisterA, 0.5);
237 ImgFlag = 1;
238 sprintf(DispString, "%10.4g", RegisterA);
239 for(i=1; i < DISPSIZE - 1; i++)
240 DispString[i] = DispString[i+1];
241 DispString[DISPSIZE - 1] = ImagChar;
243 StrCnt = CALCDONE;
245 } /***** End of function sqrtnum() ********************************/
247 /****************************************************************
248 * Function: addnums
249 ****************************************************************
250 Description:
251 Add the number in Registers A to Register B.
252 Change History:
253 Date Modification
254 11/03/00 Updated Function header
255 ****************************************************************/
256 void addnums(void) {
257 extern int Verbose;
258 extern int StrCnt;
259 extern double RegisterA, RegisterB;
260 extern char OpFlag;
262 if(Verbose) printf("In function addnums: ");
264 if(OpFlag != ' ') {
265 equalfunc();
267 else {
268 if(Verbose) printf("%g + ?? = ??\n", RegisterB);
269 RegisterB = RegisterA;
272 StrCnt = CALCDONE;
273 OpFlag = '+';
274 } /***** End of function addnums() *********************************/
277 /****************************************************************
278 * Function: subtnums
279 ****************************************************************
280 Description:
281 Subtract current number (in RegisterA) from accumulated total.
282 Change History:
283 Date Modification
284 11/03/00 Updated Function header
285 ****************************************************************/
286 void subtnums(void) {
287 extern int Verbose;
288 extern int StrCnt;
289 extern double RegisterA, RegisterB;
290 extern char OpFlag;
292 if(Verbose) printf("In function subtnums: ");
294 if (OpFlag != ' ') {
295 equalfunc();
297 else {
298 if(Verbose) printf("%g - ?? = ??\n", RegisterB);
299 RegisterB = RegisterA;
302 StrCnt = CALCDONE;
303 OpFlag = '-';
305 } /***** End of function subtnums() *****************************/
307 /****************************************************************
308 * Function: multnums
309 ****************************************************************
310 Description:
311 Multiply number in RegisterA by the accumulated total.
312 Change History:
313 Date Modification
314 11/03/00 Updated function header
315 ****************************************************************/
316 void multnums(void) {
317 extern int Verbose;
318 extern int StrCnt;
319 extern char OpFlag;
320 extern double RegisterA, RegisterB;
322 if(Verbose) printf("In function multnums: ");
324 if(OpFlag != ' ') {
325 equalfunc();
327 else {
328 if(Verbose) printf("%g * ?? = ??\n", RegisterB);
329 RegisterB = RegisterA;
332 StrCnt = CALCDONE;
333 OpFlag = '*';
334 } /***** End of function multnums() *****************************/
336 /****************************************************************
337 * Function: divnums
338 ****************************************************************
339 Description:
340 Divide the accumulated total by the current number in RegisterA
342 Change History:
343 Date Modification
344 11/04/00 Updated Function Header
345 ****************************************************************/
346 void divnums(void) {
347 extern int Verbose;
348 extern int StrCnt;
349 extern double RegisterA, RegisterB;
350 extern char OpFlag;
352 if(Verbose) printf("In function divnums: ");
354 if(OpFlag != ' ') {
355 equalfunc();
357 else {
358 if(Verbose) printf("%g / ?? = ??\n", RegisterB);
359 RegisterB = RegisterA;
362 StrCnt = CALCDONE;
363 OpFlag = '/';
364 } /* End of Function divnums() ********************************/
366 /****************************************************************
367 * Function:
368 ****************************************************************
369 Description:
370 Calculate result of entered calculation.
371 Change History:
372 Date Modification
373 11/04/00 Updated Function Header
374 ****************************************************************/
375 void equalfunc (void) {
376 extern int Verbose;
377 extern int StrCnt;
378 extern char DispString[];
379 extern double RegisterA, RegisterB;
380 extern char OpFlag;
382 if (Verbose) printf("Equal Function: Operation >> %c <<\n", OpFlag);
383 switch (OpFlag) {
384 case '+':
385 RegisterA = RegisterB + RegisterA;
386 sprintf(DispString, "%10.10g", RegisterA);
387 break;
388 case '-':
389 RegisterA = RegisterB - RegisterA;
390 sprintf(DispString, "%10.10g", RegisterA);
391 break;
392 case '*':
393 RegisterA = RegisterB * RegisterA;
394 sprintf(DispString, "%10.10g", RegisterA);
395 break;
396 case '/':
397 RegisterA = RegisterB / RegisterA;
398 sprintf(DispString, "%10.10g", RegisterA);
399 break;
400 default:
401 break;
403 OpFlag = ' ';
404 StrCnt = CALCDONE;
405 } /***** End of function equalfunc() ******************************/
408 /****************************************************************
409 * Function: clrallmem
410 ****************************************************************
411 Description:
412 Clear all the values in memory
413 Change History:
414 Date Modification
415 11/04/00 Updated Function Header
416 11/04/00 Incorporated clrmem() function into this one, to
417 optimize code.
418 ****************************************************************/
419 void clrallmem(void) {
420 extern int Verbose;
421 extern double MemArray[];
422 extern int MemLock[];
423 int i;
425 if (Verbose) printf("Clear All Memory Function\n");
427 for (i = 0; i < NUM_MEM_CELLS; i++) {
428 if (MemLock[i] != 1) {
429 MemArray[i] = 0.0;
430 if (Verbose) printf(" %f ", MemArray[i]);
433 if (Verbose) printf("\n");
435 write_config();
436 } /***** End of function clrallmem() ****************************/
439 /****************************************************************
440 * Function: stormem
441 ****************************************************************
442 Description:
443 Store value to memory cell #N
445 Change History:
446 Date Modification
447 11/04/00 Updated function header
448 11/05/00 Added Locked Memory capabilities
449 ****************************************************************/
450 void stormem(int mem_loc) {
451 extern double MemArray[];
452 extern int MemLock[];
453 extern int Verbose;
454 extern double RegisterA;
455 int i;
457 if (Verbose)
458 printf("Store Value %f in Memory Cell %d\nMemory:", RegisterA, mem_loc);
460 if (MemLock[mem_loc] != 1) {
461 MemArray[mem_loc] = RegisterA;
462 write_config();
464 else {
465 if (Verbose) printf("Memory location %d Locked at %f\n",
466 mem_loc, MemArray[mem_loc]);
469 if (Verbose) {
470 for (i = 0; i < NUM_MEM_CELLS; i++)
471 printf(" %f ", MemArray[i]);
472 printf("\n");
475 } /***** End of function stormem() ******************************/
478 /****************************************************************
479 * Function: recallmem
480 ****************************************************************
481 Description:
482 Store value to memory cell #N
483 Change History:
484 Date Modification
485 11/04/00 Updated function header
486 ****************************************************************/
487 void recallmem(int mem_loc) {
488 extern double MemArray[];
489 extern int Verbose;
490 extern double RegisterA;
491 extern char DispString[];
492 int i;
494 if (Verbose)
495 printf("Recall Value in Memory Cell %d\nMemory:", mem_loc);
497 RegisterA = MemArray[mem_loc];
499 sprintf(DispString, "%10.5g", RegisterA);
501 if (Verbose) {
502 for (i = 0; i < NUM_MEM_CELLS; i++)
503 printf(" %f ", MemArray[i]);
504 printf("\n");
506 } /***** End of function recallmem() ***************************/
509 /****************************************************************
510 * Function: startcalc
511 ****************************************************************
512 Description:
513 Change the sign of the number currently being entered
514 Change History:
515 Date Modification
516 11/04/00 Updated function header
517 ****************************************************************/
518 void startcalc(void) {
519 extern int Verbose;
520 extern char SysCalcCmd[];
522 if (Verbose)
523 fprintf(stderr, "Starting external calculator %s\n", SysCalcCmd);
525 if (system(SysCalcCmd) == -1)
526 fprintf(stderr, "%s returned an error.\n", SysCalcCmd);
527 } /***** End of function startcalc *****************************/