2.5 Bringing it All Together
The process described above is what one expression must go through
before the answer can be known. Not all of these functions are connected,
however. So, there must be a function by which all of the loose ends are tied
together, and the process is taken step by step. Additionally, this process does not
work in all cases. Most notably, if the expression is some kind of store (i.e. A=5),
the process will not evaluate correctly. So, in the same function, there must be
some checks to ensure that only what needs to be done is done. The file
function.h handles performing these functions, as well as all of the interaction
between the functionality and the GUI. The primary function provided is
createString(). This function is called every time a button is pressed. As long as
the button is not the Enter, Clear, or arrow key button, createString() simply adds
the character that is passed to it into a static string that stores the expression to be
evaluated. If the button pressed is an arrow key, a cursor variable is changed to
reflect the movement of the cursor. If the Clear button is pressed, then the string
is made empty so that the user can start again with a new expression. The
createString() function also handles any buttons pressed when the user is in insert
mode, and handles the Delete key. Finally, createString() handles the Enter key.
If the string is empty, then it returns to the GUI implementation the value of the
last expression to be evaluated. If, however, there is an expression in the string,
createString() calls the function fx(). In fx(), there are more possibilities to be
considered. If the expression is a store (as mentioned above), then fx()
circumvents most of the processes that would normally be used for evaluating
expressions. Instead, it checks to see if the right side is an expression. If it is,
then the right side is evaluated and is stored in the variable indicated by the left
side. If it isn’t, then the right side is stored directly in the left side. The last case
is for a string that is an actual expression. In this case, then the whole process
must be performed. First, the string is made into an array and passed to convert.
Convert checks for multi-digit numbers and does the proper conversions. The
array is then moved into postfix order and placed in a stack. Next, the tree of a
Calculation object is populated based on the postfix array. Finally, that tree is
evaluated and the answer is placed in final_answer. The last step before fx() is
done is for the answer to be converted into a char * with the correct precision.
When that is done, then the char * is returned to createString() and createString()
returns it to the GUI for output to the screen. This is the process that the
functionality uses to implement the user input given it by the GUI.