I got this as an assignment in software lab, the code which i am presenting is not a good and highly optimized but the logic which is used is something good and that what i want to share with you,,,
so this is the package that contain readme.pdf and code file named skycal1.1.tcl
but wait, first you need to learn how to run the code
using command line,,,,
$ wish skycal1.1.tcl
download the package.
but readme.pdf you can study form here also
Subject : EEP 702 (Software Lab)
Assignment #1 : Simple Calculator ( SkyCal 1.1) using TCL/TK
Author : Narendra Sisodiya 2006EET2473
Its a free Application
Backspace button is supported
+/- button is supproted.
Clear Button for resetting the calculation
Result is carried is next calculation. It means if user presses 12+23 then * 10 then /2 it will give result as 175.
floating point calculation is supported
Event Driven State Model is used. (Author did not copied this architecture from any where, he himself derived this and named it like this). It is a highly Modular and Scalable Architecture. As initial code (named Skycal 1.0 )was not supporting the +/- button and Backspace button. They were added now , without modifying the existing code , only a little code was added with least efforts.
What is Event Driven State Model:
It is a simple State Model in which a system is triggered from one state to another state using key events. Every event is corresponds to some action. Because of this feature when i wanted to introduce two new button for +/- and BackSpace, I added two new events to each state. One more feature is that the event also has argument here.
Various Events :
Event 1 : num_button click event. if user clicked any of numeric button from 0 to 9 or '.' it has key label as argument
Event 2 : Equal button click event if user ckicked '=' button for calculating the result
Event 3 : Clear Button click event
Event 4 : Operator button click event
Event 5 : +/- button is clicked. it also has key label as argument.
Event 6 : Backspace button is clicked
State_0 : IDLE : Calulator starts from this state. will remain in this state until any num button is clicked.
State_1 : Taking_Operand1: at this state user is keep entrying the value of operand1. during this if we encounter any operator then we jump to next state2
State_2 : Taking_Operator :at this user may enter many operator. only last entered opearator is considered, if we find any numeric button then it goes to next state for taking the next operand.
State_3 : Taking_operand2 : now at this state user will give second argument to program and if user press '=' button then it evaluate and goes to state_4 : FreeState. if user press any operator then it evaluates the expression and put result in operand1 and goes to state_2
State_4 : Free State : at this state if user press numeric key then it goes to state 1 to take operand 1 if user press operator key then last obtained result is hold by operand2 and program goes to state 3
Varoius Events Handler:
the whole code is operated through events handlers. We have six events so that we are having 6 events handler (simply they are functions called whenever a key is pressed).
the operation is very simple. Thers is a globel variable state by which every function can see at what state the system is present. Whenever any key is pressed it envokes a correspondind event hander function. Event handler funcion see what is the state and according to state it performs some actions,
State Diagram :
next figure shows state diagram. Note that this is an old state diagram , that does not include the event those were added later,,,main thing you need to do,,, understand the state diagram and correlate with coding architecture.
download this image