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
21 ****************************************************************/
23 /***** Includes *************************************************/
29 #include "wmcalc_err.h"
33 /****************************************************************
35 ****************************************************************
37 This function will clear the calculator display, and internal
42 11/03/00 Function header added
43 11/04/00 Replaced magic numbers with DISPSIZE
44 ****************************************************************/
45 void clearcalc(void) {
47 extern char PlusMinusFlag
;
50 extern double RegisterA
;
51 extern double RegisterB
;
52 extern char DispString
[];
62 for (i
=0; i
< DISPSIZE
; i
++) {
66 } /***** End of function clearcalc() *****************************/
68 /****************************************************************
70 ****************************************************************
72 Clears the current number being entered.
76 11/03/00 Updated function header
77 11/04/00 Replaced magic numbers with DISPSIZE
78 ****************************************************************/
81 extern char PlusMinusFlag
;
84 extern double RegisterA
;
85 extern char DispString
[];
93 for (i
=0; i
< DISPSIZE
; i
++) {
97 } /***** End of function clearnum() ******************************/
99 /****************************************************************
101 ****************************************************************
103 Add characters to the number being entered.
107 11/03/00 Updated function header
108 11/04/00 Replaced magic numbers with DISPSIZE
109 ****************************************************************/
110 void charkey(char ch
) {
114 extern double RegisterA
, RegisterB
;
115 extern char DispString
[];
118 if (Verbose
) printf("In function charkey\n");
120 if (StrCnt
< DISPSIZE
) {
123 for (i
= 1; i
< DISPSIZE
; i
++)
124 DispString
[i
-1] = DispString
[i
];
127 DispString
[DISPSIZE
- 1] = ch
;
131 for (i
= 1; i
< DISPSIZE
; i
++)
132 DispString
[i
-1] = DispString
[i
];
136 } /* endif (StrCnt < DISPSIZE) */
137 else if (StrCnt
== CALCDONE
) {
138 RegisterB
= RegisterA
;
141 for (i
= 1; i
< DISPSIZE
; i
++)
142 DispString
[i
-1] = DispString
[i
];
145 DispString
[DISPSIZE
-1] = ch
;
148 for (i
= 1; i
< DISPSIZE
; i
++)
149 DispString
[i
-1] = DispString
[i
];
150 DispString
[DISPSIZE
- 1] = ch
;
153 } /* endif (StrCnt == CALCDONE) */
154 RegisterA
= atof(DispString
);
155 } /***** End of Function charkey() *******************************/
158 /****************************************************************
159 * Function: chgsignnum
160 ****************************************************************
162 Change the sign of the number currently being entered
166 11/03/00 Updated Function header
167 ****************************************************************/
168 void chgsignnum(void) {
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 /****************************************************************
183 ****************************************************************
185 Square the number in RegisterA
188 11/03/00 Updated Function header
189 ****************************************************************/
193 extern double RegisterA
;
194 extern char DispString
[];
197 if (Verbose
) printf("In function sqrnum\n");
198 RegisterA
= atof(DispString
);
199 RegisterA
= pow(RegisterA
, 2.0);
201 RegisterA
= -RegisterA
;
204 sprintf(DispString
, "%10.5g", RegisterA
);
207 } /***** End of Function sqrnum() *******************************/
209 /****************************************************************
211 ****************************************************************
213 Take the square root of the number in RegisterA
216 11/03/00 Updated function header
217 11/04/00 Replaced magic numbers with DISPSIZE
218 ****************************************************************/
222 extern double RegisterA
;
224 extern char ImagChar
;
225 extern char DispString
[];
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
);
235 RegisterA
= pow(-RegisterA
, 0.5);
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
;
244 } /***** End of function sqrtnum() ********************************/
246 /****************************************************************
248 ****************************************************************
250 Add the number in Registers A to Register B.
253 11/03/00 Updated Function header
254 ****************************************************************/
258 extern double RegisterA
, RegisterB
;
261 if(Verbose
) printf("In function addnums: ");
267 if(Verbose
) printf("%g + ?? = ??\n", RegisterB
);
268 RegisterB
= RegisterA
;
273 } /***** End of function addnums() *********************************/
276 /****************************************************************
278 ****************************************************************
280 Subtract current number (in RegisterA) from accumulated total.
283 11/03/00 Updated Function header
284 ****************************************************************/
285 void subtnums(void) {
288 extern double RegisterA
, RegisterB
;
291 if(Verbose
) printf("In function subtnums: ");
297 if(Verbose
) printf("%g - ?? = ??\n", RegisterB
);
298 RegisterB
= RegisterA
;
304 } /***** End of function subtnums() *****************************/
306 /****************************************************************
308 ****************************************************************
310 Multiply number in RegisterA by the accumulated total.
313 11/03/00 Updated function header
314 ****************************************************************/
315 void multnums(void) {
319 extern double RegisterA
, RegisterB
;
321 if(Verbose
) printf("In function multnums: ");
327 if(Verbose
) printf("%g * ?? = ??\n", RegisterB
);
328 RegisterB
= RegisterA
;
333 } /***** End of function multnums() *****************************/
335 /****************************************************************
337 ****************************************************************
339 Divide the accumulated total by the current number in RegisterA
343 11/04/00 Updated Function Header
344 ****************************************************************/
348 extern double RegisterA
, RegisterB
;
351 if(Verbose
) printf("In function divnums: ");
357 if(Verbose
) printf("%g / ?? = ??\n", RegisterB
);
358 RegisterB
= RegisterA
;
363 } /* End of Function divnums() ********************************/
365 /****************************************************************
367 ****************************************************************
369 Calculate result of entered calculation.
372 11/04/00 Updated Function Header
373 ****************************************************************/
374 void equalfunc (void) {
377 extern char DispString
[];
378 extern double RegisterA
, RegisterB
;
381 if (Verbose
) printf("Equal Function: Operation >> %c <<\n", OpFlag
);
384 RegisterA
= RegisterB
+ RegisterA
;
385 sprintf(DispString
, "%10.5g", RegisterA
);
388 RegisterA
= RegisterB
- RegisterA
;
389 sprintf(DispString
, "%10.5g", RegisterA
);
392 RegisterA
= RegisterB
* RegisterA
;
393 sprintf(DispString
, "%10.5g", RegisterA
);
396 RegisterA
= RegisterB
/ RegisterA
;
397 sprintf(DispString
, "%10.5g", RegisterA
);
404 } /***** End of function equalfunc() ******************************/
407 /****************************************************************
408 * Function: clrallmem
409 ****************************************************************
411 Clear all the values in memory
414 11/04/00 Updated Function Header
415 11/04/00 Incorporated clrmem() function into this one, to
417 ****************************************************************/
418 void clrallmem(void) {
420 extern double MemArray
[];
421 extern int MemLock
[];
424 if (Verbose
) printf("Clear All Memory Function\n");
426 for (i
= 0; i
< NUM_MEM_CELLS
; i
++) {
427 if (MemLock
[i
] != 1) {
429 if (Verbose
) printf(" %f ", MemArray
[i
]);
432 if (Verbose
) printf("\n");
435 } /***** End of function clrallmem() ****************************/
438 /****************************************************************
440 ****************************************************************
442 Store value to memory cell #N
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
[];
453 extern double RegisterA
;
457 printf("Store Value %f in Memory Cell %d\nMemory:", RegisterA
, mem_loc
);
459 if (MemLock
[mem_loc
] != 1) {
460 MemArray
[mem_loc
] = RegisterA
;
464 if (Verbose
) printf("Memory location %d Locked at %f\n",
465 mem_loc
, MemArray
[mem_loc
]);
469 for (i
= 0; i
< NUM_MEM_CELLS
; i
++)
470 printf(" %f ", MemArray
[i
]);
474 } /***** End of function stormem() ******************************/
477 /****************************************************************
478 * Function: recallmem
479 ****************************************************************
481 Store value to memory cell #N
484 11/04/00 Updated function header
485 ****************************************************************/
486 void recallmem(int mem_loc
) {
487 extern double MemArray
[];
489 extern double RegisterA
;
490 extern char DispString
[];
494 printf("Recall Value in Memory Cell %d\nMemory:", mem_loc
);
496 RegisterA
= MemArray
[mem_loc
];
498 sprintf(DispString
, "%10.5g", RegisterA
);
501 for (i
= 0; i
< NUM_MEM_CELLS
; i
++)
502 printf(" %f ", MemArray
[i
]);
505 } /***** End of function recallmem() ***************************/
508 /****************************************************************
509 * Function: startcalc
510 ****************************************************************
512 Change the sign of the number currently being entered
515 11/04/00 Updated function header
516 ****************************************************************/
517 void startcalc(void) {
519 extern char SysCalcCmd
[];
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 *****************************/