wmtime: Use #defines to avoid variable length arrays.
[dockapps.git] / wmcalc / wmcalcfunc.c
blob692fa3c68d724a7cca06b546a19c212a41c67461
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
21 ****************************************************************/
23 /***** Includes *************************************************/
24 #include <string.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <ctype.h>
28 #include "wmcalc_c.h"
29 #include "wmcalc_err.h"
30 #include "wmcalc_f.h"
33 /****************************************************************
34 * Function: clearcalc
35 ****************************************************************
36 Description:
37 This function will clear the calculator display, and internal
38 flags.
40 Change History:
41 Date Modification
42 11/03/00 Function header added
43 11/04/00 Replaced magic numbers with DISPSIZE
44 ****************************************************************/
45 void clearcalc(void) {
46 extern int StrCnt;
47 extern char PlusMinusFlag;
48 extern int ExpFlag;
49 extern int DecFlag;
50 extern double RegisterA;
51 extern double RegisterB;
52 extern char DispString[];
53 extern char OpFlag;
54 int i;
56 RegisterA = 0.0;
57 RegisterB = 0.0;
58 ExpFlag = 0;
59 DecFlag = 0;
60 PlusMinusFlag = '+';
61 StrCnt = 0;
62 for (i=0; i < DISPSIZE; i++) {
63 DispString[i] = ' ';
65 OpFlag = ' ';
66 } /***** End of function clearcalc() *****************************/
68 /****************************************************************
69 * Function: clearnum
70 ****************************************************************
71 Description:
72 Clears the current number being entered.
74 Change History:
75 Date Modification
76 11/03/00 Updated function header
77 11/04/00 Replaced magic numbers with DISPSIZE
78 ****************************************************************/
79 void clearnum(void) {
80 extern int StrCnt;
81 extern char PlusMinusFlag;
82 extern int ExpFlag;
83 extern int DecFlag;
84 extern double RegisterA;
85 extern char DispString[];
86 int i;
88 RegisterA = 0.0;
89 ExpFlag = 0;
90 DecFlag = 0;
91 PlusMinusFlag = '+';
92 StrCnt = 0;
93 for (i=0; i < DISPSIZE; i++) {
94 DispString[i] = ' ';
97 } /***** End of function clearnum() ******************************/
99 /****************************************************************
100 * Function: charkey
101 ****************************************************************
102 Description:
103 Add characters to the number being entered.
105 Change History:
106 Date Modification
107 11/03/00 Updated function header
108 11/04/00 Replaced magic numbers with DISPSIZE
109 ****************************************************************/
110 void charkey(char ch) {
111 extern int Verbose;
112 extern int StrCnt;
113 extern int DecFlag;
114 extern double RegisterA, RegisterB;
115 extern char DispString[];
116 int i;
118 if (Verbose) printf("In function charkey\n");
120 if (StrCnt < DISPSIZE) {
121 if (ch == '.') {
122 if (DecFlag == 0) {
123 for (i = 1; i < DISPSIZE; i++)
124 DispString[i-1] = DispString[i];
125 DecFlag = 1;
126 StrCnt++;
127 DispString[DISPSIZE - 1] = ch;
130 else {
131 for (i = 1; i < DISPSIZE; i++)
132 DispString[i-1] = DispString[i];
133 DispString[9] = ch;
134 StrCnt++;
136 } /* endif (StrCnt < DISPSIZE) */
137 else if (StrCnt == CALCDONE) {
138 RegisterB = RegisterA;
139 clearnum();
140 if (ch == '.') {
141 for (i = 1; i < DISPSIZE; i++)
142 DispString[i-1] = DispString[i];
143 DecFlag = 1;
144 StrCnt++;
145 DispString[DISPSIZE -1] = ch;
147 else {
148 for (i = 1; i < DISPSIZE; i++)
149 DispString[i-1] = DispString[i];
150 DispString[DISPSIZE - 1] = ch;
151 StrCnt++;
153 } /* endif (StrCnt == CALCDONE) */
154 RegisterA = atof(DispString);
155 } /***** End of Function charkey() *******************************/
158 /****************************************************************
159 * Function: chgsignnum
160 ****************************************************************
161 Description:
162 Change the sign of the number currently being entered
164 Change History:
165 Date Modification
166 11/03/00 Updated Function header
167 ****************************************************************/
168 void chgsignnum(void) {
169 extern int Verbose;
170 extern double RegisterA;
171 extern char DispString[];
173 if (Verbose) printf("In function chgsignnum\n");
175 RegisterA = -RegisterA;
176 sprintf(DispString, "%10.5g", RegisterA);
178 } /***** End of function chgsignnum() *****************************/
181 /****************************************************************
182 * Function: sqrnum
183 ****************************************************************
184 Description:
185 Square the number in RegisterA
186 Change History:
187 Date Modification
188 11/03/00 Updated Function header
189 ****************************************************************/
190 void sqrnum(void) {
191 extern int Verbose;
192 extern int StrCnt;
193 extern double RegisterA;
194 extern char DispString[];
195 extern int ImgFlag;
197 if (Verbose) printf("In function sqrnum\n");
198 RegisterA = atof(DispString);
199 RegisterA = pow(RegisterA, 2.0);
200 if (ImgFlag) {
201 RegisterA = -RegisterA;
202 ImgFlag = 0;
204 sprintf(DispString, "%10.5g", RegisterA);
205 StrCnt = CALCDONE;
207 } /***** End of Function sqrnum() *******************************/
209 /****************************************************************
210 * Function: sqrtnum
211 ****************************************************************
212 Description:
213 Take the square root of the number in RegisterA
214 Change History:
215 Date Modification
216 11/03/00 Updated function header
217 11/04/00 Replaced magic numbers with DISPSIZE
218 ****************************************************************/
219 void sqrtnum(void) {
220 extern int Verbose;
221 extern int StrCnt;
222 extern double RegisterA;
223 extern int ImgFlag;
224 extern char ImagChar;
225 extern char DispString[];
226 int i;
228 if (Verbose) printf("In function sqrtnum\n");
229 RegisterA = atof(DispString);
230 if (RegisterA >= 0) {
231 RegisterA = pow(RegisterA, 0.5);
232 sprintf(DispString, "%10.5g", RegisterA);
234 else {
235 RegisterA = pow(-RegisterA, 0.5);
236 ImgFlag = 1;
237 sprintf(DispString, "%10.4g", RegisterA);
238 for(i=1; i < DISPSIZE - 1; i++)
239 DispString[i] = DispString[i+1];
240 DispString[DISPSIZE - 1] = ImagChar;
242 StrCnt = CALCDONE;
244 } /***** End of function sqrtnum() ********************************/
246 /****************************************************************
247 * Function: addnums
248 ****************************************************************
249 Description:
250 Add the number in Registers A to Register B.
251 Change History:
252 Date Modification
253 11/03/00 Updated Function header
254 ****************************************************************/
255 void addnums(void) {
256 extern int Verbose;
257 extern int StrCnt;
258 extern double RegisterA, RegisterB;
259 extern char OpFlag;
261 if(Verbose) printf("In function addnums: ");
263 if(OpFlag != ' ') {
264 equalfunc();
266 else {
267 if(Verbose) printf("%g + ?? = ??\n", RegisterB);
268 RegisterB = RegisterA;
271 StrCnt = CALCDONE;
272 OpFlag = '+';
273 } /***** End of function addnums() *********************************/
276 /****************************************************************
277 * Function: subtnums
278 ****************************************************************
279 Description:
280 Subtract current number (in RegisterA) from accumulated total.
281 Change History:
282 Date Modification
283 11/03/00 Updated Function header
284 ****************************************************************/
285 void subtnums(void) {
286 extern int Verbose;
287 extern int StrCnt;
288 extern double RegisterA, RegisterB;
289 extern char OpFlag;
291 if(Verbose) printf("In function subtnums: ");
293 if (OpFlag != ' ') {
294 equalfunc();
296 else {
297 if(Verbose) printf("%g - ?? = ??\n", RegisterB);
298 RegisterB = RegisterA;
301 StrCnt = CALCDONE;
302 OpFlag = '-';
304 } /***** End of function subtnums() *****************************/
306 /****************************************************************
307 * Function: multnums
308 ****************************************************************
309 Description:
310 Multiply number in RegisterA by the accumulated total.
311 Change History:
312 Date Modification
313 11/03/00 Updated function header
314 ****************************************************************/
315 void multnums(void) {
316 extern int Verbose;
317 extern int StrCnt;
318 extern char OpFlag;
319 extern double RegisterA, RegisterB;
321 if(Verbose) printf("In function multnums: ");
323 if(OpFlag != ' ') {
324 equalfunc();
326 else {
327 if(Verbose) printf("%g * ?? = ??\n", RegisterB);
328 RegisterB = RegisterA;
331 StrCnt = CALCDONE;
332 OpFlag = '*';
333 } /***** End of function multnums() *****************************/
335 /****************************************************************
336 * Function: divnums
337 ****************************************************************
338 Description:
339 Divide the accumulated total by the current number in RegisterA
341 Change History:
342 Date Modification
343 11/04/00 Updated Function Header
344 ****************************************************************/
345 void divnums(void) {
346 extern int Verbose;
347 extern int StrCnt;
348 extern double RegisterA, RegisterB;
349 extern char OpFlag;
351 if(Verbose) printf("In function divnums: ");
353 if(OpFlag != ' ') {
354 equalfunc();
356 else {
357 if(Verbose) printf("%g / ?? = ??\n", RegisterB);
358 RegisterB = RegisterA;
361 StrCnt = CALCDONE;
362 OpFlag = '/';
363 } /* End of Function divnums() ********************************/
365 /****************************************************************
366 * Function:
367 ****************************************************************
368 Description:
369 Calculate result of entered calculation.
370 Change History:
371 Date Modification
372 11/04/00 Updated Function Header
373 ****************************************************************/
374 void equalfunc (void) {
375 extern int Verbose;
376 extern int StrCnt;
377 extern char DispString[];
378 extern double RegisterA, RegisterB;
379 extern char OpFlag;
381 if (Verbose) printf("Equal Function: Operation >> %c <<\n", OpFlag);
382 switch (OpFlag) {
383 case '+':
384 RegisterA = RegisterB + RegisterA;
385 sprintf(DispString, "%10.5g", RegisterA);
386 break;
387 case '-':
388 RegisterA = RegisterB - RegisterA;
389 sprintf(DispString, "%10.5g", RegisterA);
390 break;
391 case '*':
392 RegisterA = RegisterB * RegisterA;
393 sprintf(DispString, "%10.5g", RegisterA);
394 break;
395 case '/':
396 RegisterA = RegisterB / RegisterA;
397 sprintf(DispString, "%10.5g", RegisterA);
398 break;
399 default:
400 break;
402 OpFlag = ' ';
403 StrCnt = CALCDONE;
404 } /***** End of function equalfunc() ******************************/
407 /****************************************************************
408 * Function: clrallmem
409 ****************************************************************
410 Description:
411 Clear all the values in memory
412 Change History:
413 Date Modification
414 11/04/00 Updated Function Header
415 11/04/00 Incorporated clrmem() function into this one, to
416 optimize code.
417 ****************************************************************/
418 void clrallmem(void) {
419 extern int Verbose;
420 extern double MemArray[];
421 extern int MemLock[];
422 int i;
424 if (Verbose) printf("Clear All Memory Function\n");
426 for (i = 0; i < NUM_MEM_CELLS; i++) {
427 if (MemLock[i] != 1) {
428 MemArray[i] = 0.0;
429 if (Verbose) printf(" %f ", MemArray[i]);
432 if (Verbose) printf("\n");
434 write_config();
435 } /***** End of function clrallmem() ****************************/
438 /****************************************************************
439 * Function: stormem
440 ****************************************************************
441 Description:
442 Store value to memory cell #N
444 Change History:
445 Date Modification
446 11/04/00 Updated function header
447 11/05/00 Added Locked Memory capabilities
448 ****************************************************************/
449 void stormem(int mem_loc) {
450 extern double MemArray[];
451 extern int MemLock[];
452 extern int Verbose;
453 extern double RegisterA;
454 int i;
456 if (Verbose)
457 printf("Store Value %f in Memory Cell %d\nMemory:", RegisterA, mem_loc);
459 if (MemLock[mem_loc] != 1) {
460 MemArray[mem_loc] = RegisterA;
461 write_config();
463 else {
464 if (Verbose) printf("Memory location %d Locked at %f\n",
465 mem_loc, MemArray[mem_loc]);
468 if (Verbose) {
469 for (i = 0; i < NUM_MEM_CELLS; i++)
470 printf(" %f ", MemArray[i]);
471 printf("\n");
474 } /***** End of function stormem() ******************************/
477 /****************************************************************
478 * Function: recallmem
479 ****************************************************************
480 Description:
481 Store value to memory cell #N
482 Change History:
483 Date Modification
484 11/04/00 Updated function header
485 ****************************************************************/
486 void recallmem(int mem_loc) {
487 extern double MemArray[];
488 extern int Verbose;
489 extern double RegisterA;
490 extern char DispString[];
491 int i;
493 if (Verbose)
494 printf("Recall Value in Memory Cell %d\nMemory:", mem_loc);
496 RegisterA = MemArray[mem_loc];
498 sprintf(DispString, "%10.5g", RegisterA);
500 if (Verbose) {
501 for (i = 0; i < NUM_MEM_CELLS; i++)
502 printf(" %f ", MemArray[i]);
503 printf("\n");
505 } /***** End of function recallmem() ***************************/
508 /****************************************************************
509 * Function: startcalc
510 ****************************************************************
511 Description:
512 Change the sign of the number currently being entered
513 Change History:
514 Date Modification
515 11/04/00 Updated function header
516 ****************************************************************/
517 void startcalc(void) {
518 extern int Verbose;
519 extern char SysCalcCmd[];
521 if (Verbose)
522 fprintf(stderr, "Starting external calculator %s\n", SysCalcCmd);
524 if (system(SysCalcCmd) == -1)
525 fprintf(stderr, "%s returned an error.\n", SysCalcCmd);
526 } /***** End of function startcalc *****************************/