Developing a Mini Calculator Part-1

 

Hello, today I will be talking about writing a simple mini calculator application which is capable of performing basic operations like addition, subtraction, multiplication and division.

The following are the application requirements

  • Addition, Subtraction, Multiplication and Division
  • Support for negative numbers and fractions
  • Record the operations performed by the user
  • Clear screen or history at any moment

Below is the possible structure of our calculator application based on the above requirement

capture

In the above image we can see five modules and two end points. To give user the expected result a best possible connection or cooperation or communication between this five modules are required. Below we can find the details of each module

Calculation Logic

This is one of the main application modules. It contain a set of calculation logic. Since our calculator only have four operations defined which require two operands and one operator, the calculation logic will have three inputs.This will do the required calculation using the inputs and return the final result to Core module

Core

This is where we maintain the application state. By state we mean the set of variables which makes application functional and so to give expected results. It will tell other modules do that or do this on demand. This is done by the use of state maintained in the core. Next application state will be a result of current application state and current user input. Simply states are memory of this application.

Input Service

This is responsible mainly for two related things, accepting user input and preventing user from giving a bad input. By bad input we mean any key press that will push the application into an abnormal state. Say for example you are entering a real number ‘65.06’. After entering six and five user may enter the period ‘.’ character twice. This is wrong because ’65..06 is not a valid number. So the second period is not valid and so we have prevent this. This service also manage the UI. All are done with the help of Core module.

Output Service

This module manage the state of screen or display and that is where we see the expected output based on our input. Core module will give the content to Output Service. By content we mean the state changes that are allowed to be on the display. Output Service is responsible to filter this public information from the content given by Core module

History Recorder

Responsible for recording the calculator history or simply it logs every right input which end up giving an expected result. Say ‘a + b = c’. it will record this as an entry in the log table

Here ends the first part. Hope You like it 🙂

In the next part we will see how to represent this things in to object oriented program 🙂