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
++) {
67 } /***** End of function clearcalc() *****************************/
69 /****************************************************************
71 ****************************************************************
73 Clears the current number being entered.
77 11/03/00 Updated function header
78 11/04/00 Replaced magic numbers with DISPSIZE
79 ****************************************************************/
82 extern char PlusMinusFlag
;
85 extern double RegisterA
;
86 extern char DispString
[];
94 for (i
=0; i
< DISPSIZE
; i
++) {
98 } /***** End of function clearnum() ******************************/
100 /****************************************************************
102 ****************************************************************
104 Add characters to the number being entered.
108 11/03/00 Updated function header
109 11/04/00 Replaced magic numbers with DISPSIZE
110 ****************************************************************/
111 void charkey(char ch
) {
115 extern double RegisterA
, RegisterB
;
116 extern char DispString
[];
119 if (Verbose
) printf("In function charkey\n");
121 if (StrCnt
< DISPSIZE
) {
124 for (i
= 1; i
< DISPSIZE
; i
++)
125 DispString
[i
-1] = DispString
[i
];
128 DispString
[DISPSIZE
- 1] = ch
;
132 for (i
= 1; i
< DISPSIZE
; i
++)
133 DispString
[i
-1] = DispString
[i
];
137 } /* endif (StrCnt < DISPSIZE) */
138 else if (StrCnt
== CALCDONE
) {
139 RegisterB
= RegisterA
;
142 for (i
= 1; i
< DISPSIZE
; i
++)
143 DispString
[i
-1] = DispString
[i
];
146 DispString
[DISPSIZE
-1] = ch
;
149 for (i
= 1; i
< DISPSIZE
; i
++)
150 DispString
[i
-1] = DispString
[i
];
151 DispString
[DISPSIZE
- 1] = ch
;
154 } /* endif (StrCnt == CALCDONE) */
155 RegisterA
= atof(DispString
);
156 } /***** End of Function charkey() *******************************/
159 /****************************************************************
160 * Function: chgsignnum
161 ****************************************************************
163 Change the sign of the number currently being entered
167 11/03/00 Updated Function header
168 ****************************************************************/
169 void chgsignnum(void) {
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 /****************************************************************
184 ****************************************************************
186 Square the number in RegisterA
189 11/03/00 Updated Function header
190 ****************************************************************/
194 extern double RegisterA
;
195 extern char DispString
[];
198 if (Verbose
) printf("In function sqrnum\n");
199 RegisterA
= atof(DispString
);
200 RegisterA
= pow(RegisterA
, 2.0);
202 RegisterA
= -RegisterA
;
205 sprintf(DispString
, "%10.5g", RegisterA
);
208 } /***** End of Function sqrnum() *******************************/
210 /****************************************************************
212 ****************************************************************
214 Take the square root of the number in RegisterA
217 11/03/00 Updated function header
218 11/04/00 Replaced magic numbers with DISPSIZE
219 ****************************************************************/
223 extern double RegisterA
;
225 extern char ImagChar
;
226 extern char DispString
[];
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
);
236 RegisterA
= pow(-RegisterA
, 0.5);
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
;
245 } /***** End of function sqrtnum() ********************************/
247 /****************************************************************
249 ****************************************************************
251 Add the number in Registers A to Register B.
254 11/03/00 Updated Function header
255 ****************************************************************/
259 extern double RegisterA
, RegisterB
;
262 if(Verbose
) printf("In function addnums: ");
268 if(Verbose
) printf("%g + ?? = ??\n", RegisterB
);
269 RegisterB
= RegisterA
;
274 } /***** End of function addnums() *********************************/
277 /****************************************************************
279 ****************************************************************
281 Subtract current number (in RegisterA) from accumulated total.
284 11/03/00 Updated Function header
285 ****************************************************************/
286 void subtnums(void) {
289 extern double RegisterA
, RegisterB
;
292 if(Verbose
) printf("In function subtnums: ");
298 if(Verbose
) printf("%g - ?? = ??\n", RegisterB
);
299 RegisterB
= RegisterA
;
305 } /***** End of function subtnums() *****************************/
307 /****************************************************************
309 ****************************************************************
311 Multiply number in RegisterA by the accumulated total.
314 11/03/00 Updated function header
315 ****************************************************************/
316 void multnums(void) {
320 extern double RegisterA
, RegisterB
;
322 if(Verbose
) printf("In function multnums: ");
328 if(Verbose
) printf("%g * ?? = ??\n", RegisterB
);
329 RegisterB
= RegisterA
;
334 } /***** End of function multnums() *****************************/
336 /****************************************************************
338 ****************************************************************
340 Divide the accumulated total by the current number in RegisterA
344 11/04/00 Updated Function Header
345 ****************************************************************/
349 extern double RegisterA
, RegisterB
;
352 if(Verbose
) printf("In function divnums: ");
358 if(Verbose
) printf("%g / ?? = ??\n", RegisterB
);
359 RegisterB
= RegisterA
;
364 } /* End of Function divnums() ********************************/
366 /****************************************************************
368 ****************************************************************
370 Calculate result of entered calculation.
373 11/04/00 Updated Function Header
374 ****************************************************************/
375 void equalfunc (void) {
378 extern char DispString
[];
379 extern double RegisterA
, RegisterB
;
382 if (Verbose
) printf("Equal Function: Operation >> %c <<\n", OpFlag
);
385 RegisterA
= RegisterB
+ RegisterA
;
386 sprintf(DispString
, "%10.10g", RegisterA
);
389 RegisterA
= RegisterB
- RegisterA
;
390 sprintf(DispString
, "%10.10g", RegisterA
);
393 RegisterA
= RegisterB
* RegisterA
;
394 sprintf(DispString
, "%10.10g", RegisterA
);
397 RegisterA
= RegisterB
/ RegisterA
;
398 sprintf(DispString
, "%10.10g", RegisterA
);
405 } /***** End of function equalfunc() ******************************/
408 /****************************************************************
409 * Function: clrallmem
410 ****************************************************************
412 Clear all the values in memory
415 11/04/00 Updated Function Header
416 11/04/00 Incorporated clrmem() function into this one, to
418 ****************************************************************/
419 void clrallmem(void) {
421 extern double MemArray
[];
422 extern int MemLock
[];
425 if (Verbose
) printf("Clear All Memory Function\n");
427 for (i
= 0; i
< NUM_MEM_CELLS
; i
++) {
428 if (MemLock
[i
] != 1) {
430 if (Verbose
) printf(" %f ", MemArray
[i
]);
433 if (Verbose
) printf("\n");
436 } /***** End of function clrallmem() ****************************/
439 /****************************************************************
441 ****************************************************************
443 Store value to memory cell #N
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
[];
454 extern double RegisterA
;
458 printf("Store Value %f in Memory Cell %d\nMemory:", RegisterA
, mem_loc
);
460 if (MemLock
[mem_loc
] != 1) {
461 MemArray
[mem_loc
] = RegisterA
;
465 if (Verbose
) printf("Memory location %d Locked at %f\n",
466 mem_loc
, MemArray
[mem_loc
]);
470 for (i
= 0; i
< NUM_MEM_CELLS
; i
++)
471 printf(" %f ", MemArray
[i
]);
475 } /***** End of function stormem() ******************************/
478 /****************************************************************
479 * Function: recallmem
480 ****************************************************************
482 Store value to memory cell #N
485 11/04/00 Updated function header
486 ****************************************************************/
487 void recallmem(int mem_loc
) {
488 extern double MemArray
[];
490 extern double RegisterA
;
491 extern char DispString
[];
495 printf("Recall Value in Memory Cell %d\nMemory:", mem_loc
);
497 RegisterA
= MemArray
[mem_loc
];
499 sprintf(DispString
, "%10.5g", RegisterA
);
502 for (i
= 0; i
< NUM_MEM_CELLS
; i
++)
503 printf(" %f ", MemArray
[i
]);
506 } /***** End of function recallmem() ***************************/
509 /****************************************************************
510 * Function: startcalc
511 ****************************************************************
513 Change the sign of the number currently being entered
516 11/04/00 Updated function header
517 ****************************************************************/
518 void startcalc(void) {
520 extern char SysCalcCmd
[];
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 *****************************/