1 /****************************************************************
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 ****************************************************************
14 This file contains the code for the actual calculator functions,
15 such as a add, subt, clear, etc.
19 11/03/00 File Header added
20 27/12/06 Increased significant digits (Antony Gelberg, antony@wayforth.co.uk)
22 ****************************************************************/
24 /***** Includes *************************************************/
30 #include "wmcalc_err.h"
34 /****************************************************************
36 ****************************************************************
38 This function will clear the calculator display, and internal
43 11/03/00 Function header added
44 11/04/00 Replaced magic numbers with DISPSIZE
45 ****************************************************************/
46 void clearcalc(void) {
48 extern char PlusMinusFlag
;
51 extern double RegisterA
;
52 extern double RegisterB
;
53 extern char DispString
[];
63 for (i
=0; i
< DISPSIZE
; i
++) {
66 DispString
[DISPSIZE
] = '\0';
68 } /***** End of function clearcalc() *****************************/
70 /****************************************************************
72 ****************************************************************
74 Clears the current number being entered.
78 11/03/00 Updated function header
79 11/04/00 Replaced magic numbers with DISPSIZE
80 ****************************************************************/
83 extern char PlusMinusFlag
;
86 extern double RegisterA
;
87 extern char DispString
[];
95 for (i
=0; i
< DISPSIZE
; i
++) {
98 DispString
[DISPSIZE
] = '\0';
100 } /***** End of function clearnum() ******************************/
102 /****************************************************************
104 ****************************************************************
106 Add characters to the number being entered.
110 11/03/00 Updated function header
111 11/04/00 Replaced magic numbers with DISPSIZE
112 ****************************************************************/
113 void charkey(char ch
) {
117 extern double RegisterA
, RegisterB
;
118 extern char DispString
[];
121 if (Verbose
) printf("In function charkey\n");
123 if (StrCnt
< DISPSIZE
) {
126 for (i
= 1; i
< DISPSIZE
; i
++)
127 DispString
[i
-1] = DispString
[i
];
130 DispString
[DISPSIZE
- 1] = ch
;
134 for (i
= 1; i
< DISPSIZE
; i
++)
135 DispString
[i
-1] = DispString
[i
];
139 } /* endif (StrCnt < DISPSIZE) */
140 else if (StrCnt
== CALCDONE
) {
141 RegisterB
= RegisterA
;
144 for (i
= 1; i
< DISPSIZE
; i
++)
145 DispString
[i
-1] = DispString
[i
];
148 DispString
[DISPSIZE
-1] = ch
;
151 for (i
= 1; i
< DISPSIZE
; i
++)
152 DispString
[i
-1] = DispString
[i
];
153 DispString
[DISPSIZE
- 1] = ch
;
156 } /* endif (StrCnt == CALCDONE) */
157 RegisterA
= atof(DispString
);
158 } /***** End of Function charkey() *******************************/
161 /****************************************************************
162 * Function: chgsignnum
163 ****************************************************************
165 Change the sign of the number currently being entered
169 11/03/00 Updated Function header
170 ****************************************************************/
171 void chgsignnum(void) {
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 /****************************************************************
186 ****************************************************************
188 Square the number in RegisterA
191 11/03/00 Updated Function header
192 ****************************************************************/
196 extern double RegisterA
;
197 extern char DispString
[];
200 if (Verbose
) printf("In function sqrnum\n");
201 RegisterA
= atof(DispString
);
202 RegisterA
= pow(RegisterA
, 2.0);
204 RegisterA
= -RegisterA
;
207 sprintf(DispString
, "%10.5g", RegisterA
);
210 } /***** End of Function sqrnum() *******************************/
212 /****************************************************************
214 ****************************************************************
216 Take the square root of the number in RegisterA
219 11/03/00 Updated function header
220 11/04/00 Replaced magic numbers with DISPSIZE
221 ****************************************************************/
225 extern double RegisterA
;
227 extern char ImagChar
;
228 extern char DispString
[];
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
);
238 RegisterA
= pow(-RegisterA
, 0.5);
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
;
247 } /***** End of function sqrtnum() ********************************/
249 /****************************************************************
251 ****************************************************************
253 Add the number in Registers A to Register B.
256 11/03/00 Updated Function header
257 ****************************************************************/
261 extern double RegisterA
, RegisterB
;
264 if(Verbose
) printf("In function addnums: ");
270 if(Verbose
) printf("%g + ?? = ??\n", RegisterB
);
271 RegisterB
= RegisterA
;
276 } /***** End of function addnums() *********************************/
279 /****************************************************************
281 ****************************************************************
283 Subtract current number (in RegisterA) from accumulated total.
286 11/03/00 Updated Function header
287 ****************************************************************/
288 void subtnums(void) {
291 extern double RegisterA
, RegisterB
;
294 if(Verbose
) printf("In function subtnums: ");
300 if(Verbose
) printf("%g - ?? = ??\n", RegisterB
);
301 RegisterB
= RegisterA
;
307 } /***** End of function subtnums() *****************************/
309 /****************************************************************
311 ****************************************************************
313 Multiply number in RegisterA by the accumulated total.
316 11/03/00 Updated function header
317 ****************************************************************/
318 void multnums(void) {
322 extern double RegisterA
, RegisterB
;
324 if(Verbose
) printf("In function multnums: ");
330 if(Verbose
) printf("%g * ?? = ??\n", RegisterB
);
331 RegisterB
= RegisterA
;
336 } /***** End of function multnums() *****************************/
338 /****************************************************************
340 ****************************************************************
342 Divide the accumulated total by the current number in RegisterA
346 11/04/00 Updated Function Header
347 ****************************************************************/
351 extern double RegisterA
, RegisterB
;
354 if(Verbose
) printf("In function divnums: ");
360 if(Verbose
) printf("%g / ?? = ??\n", RegisterB
);
361 RegisterB
= RegisterA
;
366 } /* End of Function divnums() ********************************/
368 /****************************************************************
370 ****************************************************************
372 Calculate result of entered calculation.
375 11/04/00 Updated Function Header
376 ****************************************************************/
377 void equalfunc (void) {
380 extern char DispString
[];
381 extern double RegisterA
, RegisterB
;
384 if (Verbose
) printf("Equal Function: Operation >> %c <<\n", OpFlag
);
387 RegisterA
= RegisterB
+ RegisterA
;
388 sprintf(DispString
, "%10.10g", RegisterA
);
391 RegisterA
= RegisterB
- RegisterA
;
392 sprintf(DispString
, "%10.10g", RegisterA
);
395 RegisterA
= RegisterB
* RegisterA
;
396 sprintf(DispString
, "%10.10g", RegisterA
);
399 RegisterA
= RegisterB
/ RegisterA
;
400 sprintf(DispString
, "%10.10g", RegisterA
);
407 } /***** End of function equalfunc() ******************************/
410 /****************************************************************
411 * Function: clrallmem
412 ****************************************************************
414 Clear all the values in memory
417 11/04/00 Updated Function Header
418 11/04/00 Incorporated clrmem() function into this one, to
420 ****************************************************************/
421 void clrallmem(void) {
423 extern double MemArray
[];
424 extern int MemLock
[];
427 if (Verbose
) printf("Clear All Memory Function\n");
429 for (i
= 0; i
< NUM_MEM_CELLS
; i
++) {
430 if (MemLock
[i
] != 1) {
432 if (Verbose
) printf(" %f ", MemArray
[i
]);
435 if (Verbose
) printf("\n");
438 } /***** End of function clrallmem() ****************************/
441 /****************************************************************
443 ****************************************************************
445 Store value to memory cell #N
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
[];
456 extern double RegisterA
;
460 printf("Store Value %f in Memory Cell %d\nMemory:", RegisterA
, mem_loc
);
462 if (MemLock
[mem_loc
] != 1) {
463 MemArray
[mem_loc
] = RegisterA
;
467 if (Verbose
) printf("Memory location %d Locked at %f\n",
468 mem_loc
, MemArray
[mem_loc
]);
472 for (i
= 0; i
< NUM_MEM_CELLS
; i
++)
473 printf(" %f ", MemArray
[i
]);
477 } /***** End of function stormem() ******************************/
480 /****************************************************************
481 * Function: recallmem
482 ****************************************************************
484 Store value to memory cell #N
487 11/04/00 Updated function header
488 ****************************************************************/
489 void recallmem(int mem_loc
) {
490 extern double MemArray
[];
492 extern double RegisterA
;
493 extern char DispString
[];
497 printf("Recall Value in Memory Cell %d\nMemory:", mem_loc
);
499 RegisterA
= MemArray
[mem_loc
];
501 sprintf(DispString
, "%10.5g", RegisterA
);
504 for (i
= 0; i
< NUM_MEM_CELLS
; i
++)
505 printf(" %f ", MemArray
[i
]);
508 } /***** End of function recallmem() ***************************/
511 /****************************************************************
512 * Function: startcalc
513 ****************************************************************
515 Change the sign of the number currently being entered
518 11/04/00 Updated function header
519 ****************************************************************/
520 void startcalc(void) {
522 extern char SysCalcCmd
[];
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 *****************************/