wmclockmon: update change-log
[dockapps.git] / wmcalc / wmcalcfunc.c
bloba8248ff3181d5ee0fa70aea354243e2bbb8d2853
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 DispString[DISPSIZE] = '\0';
67 OpFlag = ' ';
68 } /***** End of function clearcalc() *****************************/
70 /****************************************************************
71 * Function: clearnum
72 ****************************************************************
73 Description:
74 Clears the current number being entered.
76 Change History:
77 Date Modification
78 11/03/00 Updated function header
79 11/04/00 Replaced magic numbers with DISPSIZE
80 ****************************************************************/
81 void clearnum(void) {
82 extern int StrCnt;
83 extern char PlusMinusFlag;
84 extern int ExpFlag;
85 extern int DecFlag;
86 extern double RegisterA;
87 extern char DispString[];
88 int i;
90 RegisterA = 0.0;
91 ExpFlag = 0;
92 DecFlag = 0;
93 PlusMinusFlag = '+';
94 StrCnt = 0;
95 for (i=0; i < DISPSIZE; i++) {
96 DispString[i] = ' ';
98 DispString[DISPSIZE] = '\0';
100 } /***** End of function clearnum() ******************************/
102 /****************************************************************
103 * Function: charkey
104 ****************************************************************
105 Description:
106 Add characters to the number being entered.
108 Change History:
109 Date Modification
110 11/03/00 Updated function header
111 11/04/00 Replaced magic numbers with DISPSIZE
112 ****************************************************************/
113 void charkey(char ch) {
114 extern int Verbose;
115 extern int StrCnt;
116 extern int DecFlag;
117 extern double RegisterA, RegisterB;
118 extern char DispString[];
119 int i;
121 if (Verbose) printf("In function charkey\n");
123 if (StrCnt < DISPSIZE) {
124 if (ch == '.') {
125 if (DecFlag == 0) {
126 for (i = 1; i < DISPSIZE; i++)
127 DispString[i-1] = DispString[i];
128 DecFlag = 1;
129 StrCnt++;
130 DispString[DISPSIZE - 1] = ch;
133 else {
134 for (i = 1; i < DISPSIZE; i++)
135 DispString[i-1] = DispString[i];
136 DispString[9] = ch;
137 StrCnt++;
139 } /* endif (StrCnt < DISPSIZE) */
140 else if (StrCnt == CALCDONE) {
141 RegisterB = RegisterA;
142 clearnum();
143 if (ch == '.') {
144 for (i = 1; i < DISPSIZE; i++)
145 DispString[i-1] = DispString[i];
146 DecFlag = 1;
147 StrCnt++;
148 DispString[DISPSIZE -1] = ch;
150 else {
151 for (i = 1; i < DISPSIZE; i++)
152 DispString[i-1] = DispString[i];
153 DispString[DISPSIZE - 1] = ch;
154 StrCnt++;
156 } /* endif (StrCnt == CALCDONE) */
157 RegisterA = atof(DispString);
158 } /***** End of Function charkey() *******************************/
161 /****************************************************************
162 * Function: chgsignnum
163 ****************************************************************
164 Description:
165 Change the sign of the number currently being entered
167 Change History:
168 Date Modification
169 11/03/00 Updated Function header
170 ****************************************************************/
171 void chgsignnum(void) {
172 extern int Verbose;
173 extern double RegisterA;
174 extern char DispString[];
176 if (Verbose) printf("In function chgsignnum\n");
178 RegisterA = -RegisterA;
179 sprintf(DispString, "%10.5g", RegisterA);
181 } /***** End of function chgsignnum() *****************************/
184 /****************************************************************
185 * Function: sqrnum
186 ****************************************************************
187 Description:
188 Square the number in RegisterA
189 Change History:
190 Date Modification
191 11/03/00 Updated Function header
192 ****************************************************************/
193 void sqrnum(void) {
194 extern int Verbose;
195 extern int StrCnt;
196 extern double RegisterA;
197 extern char DispString[];
198 extern int ImgFlag;
200 if (Verbose) printf("In function sqrnum\n");
201 RegisterA = atof(DispString);
202 RegisterA = pow(RegisterA, 2.0);
203 if (ImgFlag) {
204 RegisterA = -RegisterA;
205 ImgFlag = 0;
207 sprintf(DispString, "%10.5g", RegisterA);
208 StrCnt = CALCDONE;
210 } /***** End of Function sqrnum() *******************************/
212 /****************************************************************
213 * Function: sqrtnum
214 ****************************************************************
215 Description:
216 Take the square root of the number in RegisterA
217 Change History:
218 Date Modification
219 11/03/00 Updated function header
220 11/04/00 Replaced magic numbers with DISPSIZE
221 ****************************************************************/
222 void sqrtnum(void) {
223 extern int Verbose;
224 extern int StrCnt;
225 extern double RegisterA;
226 extern int ImgFlag;
227 extern char ImagChar;
228 extern char DispString[];
229 int i;
231 if (Verbose) printf("In function sqrtnum\n");
232 RegisterA = atof(DispString);
233 if (RegisterA >= 0) {
234 RegisterA = pow(RegisterA, 0.5);
235 sprintf(DispString, "%10.5g", RegisterA);
237 else {
238 RegisterA = pow(-RegisterA, 0.5);
239 ImgFlag = 1;
240 sprintf(DispString, "%10.4g", RegisterA);
241 for(i=1; i < DISPSIZE - 1; i++)
242 DispString[i] = DispString[i+1];
243 DispString[DISPSIZE - 1] = ImagChar;
245 StrCnt = CALCDONE;
247 } /***** End of function sqrtnum() ********************************/
249 /****************************************************************
250 * Function: addnums
251 ****************************************************************
252 Description:
253 Add the number in Registers A to Register B.
254 Change History:
255 Date Modification
256 11/03/00 Updated Function header
257 ****************************************************************/
258 void addnums(void) {
259 extern int Verbose;
260 extern int StrCnt;
261 extern double RegisterA, RegisterB;
262 extern char OpFlag;
264 if(Verbose) printf("In function addnums: ");
266 if(OpFlag != ' ') {
267 equalfunc();
269 else {
270 if(Verbose) printf("%g + ?? = ??\n", RegisterB);
271 RegisterB = RegisterA;
274 StrCnt = CALCDONE;
275 OpFlag = '+';
276 } /***** End of function addnums() *********************************/
279 /****************************************************************
280 * Function: subtnums
281 ****************************************************************
282 Description:
283 Subtract current number (in RegisterA) from accumulated total.
284 Change History:
285 Date Modification
286 11/03/00 Updated Function header
287 ****************************************************************/
288 void subtnums(void) {
289 extern int Verbose;
290 extern int StrCnt;
291 extern double RegisterA, RegisterB;
292 extern char OpFlag;
294 if(Verbose) printf("In function subtnums: ");
296 if (OpFlag != ' ') {
297 equalfunc();
299 else {
300 if(Verbose) printf("%g - ?? = ??\n", RegisterB);
301 RegisterB = RegisterA;
304 StrCnt = CALCDONE;
305 OpFlag = '-';
307 } /***** End of function subtnums() *****************************/
309 /****************************************************************
310 * Function: multnums
311 ****************************************************************
312 Description:
313 Multiply number in RegisterA by the accumulated total.
314 Change History:
315 Date Modification
316 11/03/00 Updated function header
317 ****************************************************************/
318 void multnums(void) {
319 extern int Verbose;
320 extern int StrCnt;
321 extern char OpFlag;
322 extern double RegisterA, RegisterB;
324 if(Verbose) printf("In function multnums: ");
326 if(OpFlag != ' ') {
327 equalfunc();
329 else {
330 if(Verbose) printf("%g * ?? = ??\n", RegisterB);
331 RegisterB = RegisterA;
334 StrCnt = CALCDONE;
335 OpFlag = '*';
336 } /***** End of function multnums() *****************************/
338 /****************************************************************
339 * Function: divnums
340 ****************************************************************
341 Description:
342 Divide the accumulated total by the current number in RegisterA
344 Change History:
345 Date Modification
346 11/04/00 Updated Function Header
347 ****************************************************************/
348 void divnums(void) {
349 extern int Verbose;
350 extern int StrCnt;
351 extern double RegisterA, RegisterB;
352 extern char OpFlag;
354 if(Verbose) printf("In function divnums: ");
356 if(OpFlag != ' ') {
357 equalfunc();
359 else {
360 if(Verbose) printf("%g / ?? = ??\n", RegisterB);
361 RegisterB = RegisterA;
364 StrCnt = CALCDONE;
365 OpFlag = '/';
366 } /* End of Function divnums() ********************************/
368 /****************************************************************
369 * Function:
370 ****************************************************************
371 Description:
372 Calculate result of entered calculation.
373 Change History:
374 Date Modification
375 11/04/00 Updated Function Header
376 ****************************************************************/
377 void equalfunc (void) {
378 extern int Verbose;
379 extern int StrCnt;
380 extern char DispString[];
381 extern double RegisterA, RegisterB;
382 extern char OpFlag;
384 if (Verbose) printf("Equal Function: Operation >> %c <<\n", OpFlag);
385 switch (OpFlag) {
386 case '+':
387 RegisterA = RegisterB + RegisterA;
388 sprintf(DispString, "%10.10g", RegisterA);
389 break;
390 case '-':
391 RegisterA = RegisterB - RegisterA;
392 sprintf(DispString, "%10.10g", RegisterA);
393 break;
394 case '*':
395 RegisterA = RegisterB * RegisterA;
396 sprintf(DispString, "%10.10g", RegisterA);
397 break;
398 case '/':
399 RegisterA = RegisterB / RegisterA;
400 sprintf(DispString, "%10.10g", RegisterA);
401 break;
402 default:
403 break;
405 OpFlag = ' ';
406 StrCnt = CALCDONE;
407 } /***** End of function equalfunc() ******************************/
410 /****************************************************************
411 * Function: clrallmem
412 ****************************************************************
413 Description:
414 Clear all the values in memory
415 Change History:
416 Date Modification
417 11/04/00 Updated Function Header
418 11/04/00 Incorporated clrmem() function into this one, to
419 optimize code.
420 ****************************************************************/
421 void clrallmem(void) {
422 extern int Verbose;
423 extern double MemArray[];
424 extern int MemLock[];
425 int i;
427 if (Verbose) printf("Clear All Memory Function\n");
429 for (i = 0; i < NUM_MEM_CELLS; i++) {
430 if (MemLock[i] != 1) {
431 MemArray[i] = 0.0;
432 if (Verbose) printf(" %f ", MemArray[i]);
435 if (Verbose) printf("\n");
437 write_config();
438 } /***** End of function clrallmem() ****************************/
441 /****************************************************************
442 * Function: stormem
443 ****************************************************************
444 Description:
445 Store value to memory cell #N
447 Change History:
448 Date Modification
449 11/04/00 Updated function header
450 11/05/00 Added Locked Memory capabilities
451 ****************************************************************/
452 void stormem(int mem_loc) {
453 extern double MemArray[];
454 extern int MemLock[];
455 extern int Verbose;
456 extern double RegisterA;
457 int i;
459 if (Verbose)
460 printf("Store Value %f in Memory Cell %d\nMemory:", RegisterA, mem_loc);
462 if (MemLock[mem_loc] != 1) {
463 MemArray[mem_loc] = RegisterA;
464 write_config();
466 else {
467 if (Verbose) printf("Memory location %d Locked at %f\n",
468 mem_loc, MemArray[mem_loc]);
471 if (Verbose) {
472 for (i = 0; i < NUM_MEM_CELLS; i++)
473 printf(" %f ", MemArray[i]);
474 printf("\n");
477 } /***** End of function stormem() ******************************/
480 /****************************************************************
481 * Function: recallmem
482 ****************************************************************
483 Description:
484 Store value to memory cell #N
485 Change History:
486 Date Modification
487 11/04/00 Updated function header
488 ****************************************************************/
489 void recallmem(int mem_loc) {
490 extern double MemArray[];
491 extern int Verbose;
492 extern double RegisterA;
493 extern char DispString[];
494 int i;
496 if (Verbose)
497 printf("Recall Value in Memory Cell %d\nMemory:", mem_loc);
499 RegisterA = MemArray[mem_loc];
501 sprintf(DispString, "%10.5g", RegisterA);
503 if (Verbose) {
504 for (i = 0; i < NUM_MEM_CELLS; i++)
505 printf(" %f ", MemArray[i]);
506 printf("\n");
508 } /***** End of function recallmem() ***************************/
511 /****************************************************************
512 * Function: startcalc
513 ****************************************************************
514 Description:
515 Change the sign of the number currently being entered
516 Change History:
517 Date Modification
518 11/04/00 Updated function header
519 ****************************************************************/
520 void startcalc(void) {
521 extern int Verbose;
522 extern char SysCalcCmd[];
524 if (Verbose)
525 fprintf(stderr, "Starting external calculator %s\n", SysCalcCmd);
527 if (system(SysCalcCmd) == -1)
528 fprintf(stderr, "%s returned an error.\n", SysCalcCmd);
529 } /***** End of function startcalc *****************************/