Calculator Demo -- Commentary and Description
Program Description and General Comments
Okay, I'll admit that this is a flashy program, but there is some
substance to it. The purpose of the program is pretty much self explanatory.
Its interface follows the interface of the typical pocket calculator and it
functions in the same way. The buttons in the form are pressed to build the
mathematical expression which would then be solved once the "=" key is pressed.
Overview of Calculator Class
There is really not much to this class. The methods of the class are:
EnterButton
EvaluateEntry
The EnterButton method accepts user input from the form and sets up the data
members of the calculator object according to what is passed in.
The EvaluateEntry method does the job of processing the mathematical expression
that is built from the user input.
Overview of the EnterButton method
The EnterButton method will append to the EntryString data member some predefined
string corresponding to the user input. The string will correspond to the
standard mathematical operation that is displayed on the pressed calculator key.
The EntryString data member will in turn be assigned as output to the
AnswerForm's AnswerBox text area control. Some keys will not be operational
keys in that they are not used for specifying math operations. These are the
"Store", "Recall", "<--" and "Clear" keys. These utility keys will allow the
user to:
- store a value into memory
- recall a value from memory
- back up one space and delete
- clear the output screen
Store will simply store the value currently on screen (which is the same as
the value in the EntryString data member) into the SavedValue data member.
No output is produced.
Recall will replace the value on screen with its own value. The length of the
EntryString member is changed to the length of the string stored in memory.
<-- means back up one space and delete the character in that space. This is
accomplished by simply decrementing the length of the EntryString member by 1
as this data member is used to store the output to screen
Clear will clear the contents of the output screen by assigning the EntryString
data member to an empty string.
Overview of the EvaluateEntry method
When the equal key "=" is pressed the entry string will be passed to
EvaluateEntry member function which will in turn pass it to the TranslateString
function to translate the entry string into a form which is recognizable to
the built-in eval math expression parsing function. The eval function will do
the actual work of parsing the string input and finding the result of the math
expression. Once the result is found the answer will be placed in the next
line in the text area following the user input. The entry string storing the
user input will be erased.
Summary
Because JavaScript is a high level script language I can rely on its built in
objects and functions not normally available in the standard distribution of
lower level languages like C/C++. One such facility is the eval function which
is the built-in math expression parser available in JavaScript. In a much more
sophisticated program like a full-feature scientific calculator the parsing
method will have to be custom designed. More complex data structures for
storing, parsing and evaluating expressions will also have to be built. In that
case I would prefer to use a more powerful, heavy duty OOP language like C++
or Java to develop the program.