Click here to Skip to main content
15,878,814 members
Articles / Programming Languages / Visual Basic

A multipurpose scientific calculator

Rate me:
Please Sign up or sign in to vote.
2.56/5 (36 votes)
17 Apr 2008CPOL5 min read 188.5K   14.2K   50   16
A calculator handling complex numbers and scientific functions

Sample Image - scientific_calculator.jpg

Introduction

This is a scientific calculator involving many functions such as:

  1. Normal arithmetic operations.
  2. Functions such as factorial, permutation, combination etc.
  3. Scientific functions (sin, cos, sin-1, cos-1, sinh, cosh, sinh-1,etc.).
  4. Operations on complex numbers.
  5. Storage of values in variables and operations on these variables.
  6. Use of abbreviated form of representing values (such as m for milli, M for mega, etc.).
  7. Evaluation of simple series.
  8. Expressions involving any combination of the above.

Concepts used

Recursive parser:

The calculator uses a recursive parser for evaluation of expressions. A part of the recursive parser is represented in the diagram below. The parser uses a proper operator precedence. The complete precedence can be known by looking at the code.

Sample image - parser.jpg

Advantage of using the parser for expression evaluation

The complexity of procedure required is broken into many parts thus making each part simpler to understand and the overall structure robust. It becomes easier to to change the operator precedence implemented.

An alternative approach

We might use a stack to do the expression evaluation. But then, breaking down of the complexity to many procedures is not as straightforward as in the recursive function approach and we might stand to lose the advantages of the recursyve function approach.

Some Special Features

Easy-to-use interface

The calculator provides an easy-to-use interface. The expressions can be entered easily using the keyboard or by clicking the buttons using the mouse. For expressions using variables(X,Y,A,B,etc.) and symbols like pi(π), it is better to use the mouse since these characters are special characters.

Display of evaluated value in fraction form

Values of expressions can be viewed in normal decimal form or in the fraction form. Default is the decimal form. To change to fraction form check the option fraction in the toolstrip menu options->display.

Display of evaluated value in polar form

Complex numbers can be viewed either in the default form(a+bi) or in the form rLθ. To change to polar form, check the option polar in the toolstrip menu options->display.

Angle measurements in different units

Angle measurements may be in degree, radian, or grade. The selection may be done using the DomainUpDown at the top right of the main form. This selection effects the trignometric calculations as well as the measure of amplitude of complex numbers when they are displayed in polar form. The expression in the expression textbox is re-evaluated as soon as a change in the choice in the DomainUpDown is made.

Memory calculations

Answer memory

    • Whenever a value appears on the value textbox, it automatically updates Ans memory contents by storing the result.
    • The value can be used in the next expression using the variable Ans at any position of the expression.
    • Ans memory is not updated in case of error in evaluation.

Variables

    • The calculator provides 9 memory varaibles.
    • Values can be stored in these variables and they may be used later in expressions.
    • The values remain stored in the computer in a file even after closing the application and retain the values the next time we run the application.
    • The value stored in variables can be that of real numbers or co mplex numbers.

Series

We may evaluate series such as sum of first ten natural numbers, or product of even numbers from 20 to 40.

How to use

    1. Just run the demo project. To do this, download the file scientific_calculator_demo.zip. Unzip the file. Run the file scientific_calculator_demo/release/calculator.
    2. Enter any expression as stated earlier.
    3. Press = button, or hit the = or [Enter] key on the keyboard to see the answer. If the answer is lenghty scroll down the textbox to view the complete answer.

To store values in variables and use them

      1. Check the mem radio button.
      2. We may directly add or subtract to the memory variables. Depending on whether store,add or subtract checkboxes are checked, values are stored to, added to or subtracted from the variables whose buttons are clicked. If none of check boxes on the memory variables-form are checked, a hit on a button in this form inserts corresponding variable to the expression.
      3. The memory variables may contain real values or complex values.

To evaluate expressions containing trignometric functions

    1. Check the Trignometry radio button.
    2. Now, to simply click the button in the trignometry form to get the funcions to the expression.
    3. To get the inverse functions to the expression click inv checkbox and then the button of the trignometric function whose inverse is required in the expression.

To evaluate expressions containing series

    1. Check the series radio button.
    2. Enter the lower value of the variable in the series expression in expression textbox and check Setlow and then I button. Similarly set the high value of the variable in the sries expression.
    3. The upper and lower limit of the variable have to be integers and upper limit should be greater than lower limit numerically.
    4. Now enter the expression containing the series expression and press = to view the result.
    5. For example, to find sum of first ten natural numbers:type 1 -> Setlow -> I; type 10 -> Sethigh -> I; type "SΣI"->=;to view the answer 55 in value textbox.
    6. At present, for all SΣ and Sπ that appear in a single expression, the range of variable will be same. With a further enhancement, we may get rid of this limitation.

Android app

Here is an android app that uses the same parser mentioned in this article: Calculator on Google Play. The only difference is that the App implements the same parser in JS and uses PhoneGap.

Website

Here is a website that uses the same parser mentioned in this article: https://calc.multisocialshare.com

About me

You can visit my webpage to know more about me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Yahoo!
India India
I've studied info. sc. engg. from Sir MVIT, Bangalore.

I'm interested in programming.

Some of my technical blogs:

http://perl-blog.kbsbng.com/
http://java-blog.kbsbng.com/

I also enjoy writing some webpages such as http://sites.google.com/site/plantencyclopedia/

More about me at http://www.kbsbng.com and http://blog.kbsbng.com.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Espiritu John7-Nov-09 20:03
professionalEspiritu John7-Nov-09 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.