The CALC.PRG program simulates a simple scientific calculator. It illustrates the use of the built-in FoxPro 2.5 functions. Figure 5.2 in the book shows the calculator. You can use either the mouse or the keyboard to enter numbers and choose operations.
The FoxPro Screen Designer, MODIFY SCREEN created the screen for CALC.PRG. The Scientific Calculator displays in a window and uses "push buttons" for the numbers and functions. CALC.PRG displays the push buttons using a special form of the @ ... GET command, which is discussed in Chapter 16, "Menus, Popup Menus, and Dialog Boxes."
Here is what CALC.PRG does. First, CALC.PRG saves some environmental parameters, defines the window, initializes some constants, and displays the screen image. It then executes the READ CYCLE command until you press the Quit push button to exit the program. Whenever you "push" a push button to enter a number or operation, a corresponding @ ... GET command VALID clause executes. The VALID clause consists of a call to a user-defined function (UDF). For example, when you press the number 1, in the following statement
@ 9,5 GET i1 ;
PICTURE "@*HN \<1" ;
SIZE 1,5,1 ;
DEFAULT 1 ;
VALID VNUMBER( 1 )
the program calls the VNUMBER() UDF to process the number 1. The two characters (\<) preceding the 1 in the PICTURE clause make the number 1 a hot key. When you press a hot key, the push button activates. Of course, you can also activate the push button with a mouse click or by pressing Enter. The numlock() function turns on the Num Lock mode so that the user can use the number and +, -, *, and / keys on the numeric keypad. The Num Lock status returns to its original state when the program exits.
VNUMBER() emulates the way you enter a number in a calculator. The number is entered from right to left. Another UDF, VOP(), is called when you push an operator button (+, -, *, or /). It creates in a character string containing an expression that consists of the previously entered operator and operands. It then calls the EVALUATE() function. EVALUATE evaluates the expression and returns the answer. When you push a function button (sin, cos, rand, and so forth), the VFUNC() UDF is called, which uses the same technique used by the VOP() UDF to calculate the results. The ShowCalc procedure displays the results.