Search (Article Or Program)

24 January 2014

How to write a scientific calculator with an implementation in c++


Table of contents 

1. Abstract 
2. Introduction
3. Coding Mathematics' problems 
3.1. Inputs' wide possibilities 
3.2. Agreement of input formulas with standard rules of math.
3.3. Length of input formula  
 4. Overview on the Implementation
4.1. Data class
4.2.   MyChecker class 
4.3.   MyFixer class 
4.4.   Calculator class  
5. How to use the implementation  
6. Appendix  
6.1. Linked lists  
6.2. Bisection method    
7. conclusion   

 

1. Abstract  

Have you tried before to put the upper formula in one of your program, but you found its hard to calculate it? Have you stopped codding one of your programs because you need to treat with complex formulas and their isn't a route to calculate it in your programming language? have you gone to use programming language such as fortran or mathlab, because you need to use some of its mathematical power, as the same time if you have this power in your main language you may produce a better program? If you are one these slides I have told about, this article will be good for you inshallah as its give you two solutions to your problems; the first is how to program mathematical programs and the second a header file in c++ that you can use in order to calculate formula such as which in Fig.1, in order to facility your creative c++ program.   

2. Introduction  

Mathematical programming is the backbone of any programming language and the one of the most important reasons to invite computers and with subordination programming language, that is because all science in the world are depending on mathematics, so needing of easier routes to do it is continue one of the most required things in these days and will still obtainable until the end of the world, so from this point we are starting in order to implement a scientific calculator coded by myself me and will be coded by your self in the coming days inshallah, except you don't wanna.
In c++ there is some libraries work on mathematics such as math.h and c math, but these libraries don't give you all operations you may need, but at the same time they are the soul which we plant our implementation on.   

3. Coding Mathematics' problems  

 

During coding mathematical programs you will face some problems which have faced all programmers before, I put them under your hand in order to take attention, you may see this problems not a problem as the the word mean, but at all I write the problem which have faced me and caused me some difficulty during coding.

3.1. Inputs' wide possibilities  

The first problem which may face you is the Input's wide possibilities, you don't know what the user will give you as inputs, so you should determine at first what kind of math your program will treat with, and make sure your user won't enter any thing outside it especially if you receive inputs as a string or stream, or you will receive it by a console interface, so to face this problem at first you should check the inputs and then do your maintenance progress on the input, for example: 
  • would your program maintain formulas with simple operators (+,-,/,*) only?
  • or your program will go up until intersection operators with brackets ?
  • or your program will go up until trigonometric functions (sin, cos, . . . ) ?
  • and as you go, you should determine first the field you will depend on and then start.

3.2. Abbreviation or not substandard  of input formulas  

The second problem which may face you is user may enter input formulas with some abbreviation, such as 2x which means 2*x or 2(sin(45)) which means 2*sin(45) 
or 2++++++x , but your program is coded to treat with one operator only, etc.  
so you should edit your input  formula first until it be adaptable to your program and then send it to your maintain function.     

3.3. Length of input formula

The third problem which may face you is the inputs length, as the user may enter you short formula and may enter you a long one; In this condition you have two solutions: the first is using a long array which can read any length of the formula, but it's a bad solution from my opinion because it will waste memory if the user enter a small formula, The best solution from my opinion is using counters which depending on linked lists (see the appendix) as it is allocating memory as you need only, so you want allocate memory more than you need, In my case I use vectors . 

 4. Overview on the Implementation

4.1. Data class

Data class is the container class of my implementation, in this class I put the main libraries which I need in the program and the public method which I will use as assistant functions as:
  1. double charVtod (vectorch) function convert a double number form the input formula to a double number
  2. double getNum (vector &form, int &orderm, int &digit); read a number form the input formula
  3. unsigned getDigits(long num); count the digits of a long number
  4. vector dtoVchar (double num); convert double numbers to a vector of characters
 class Data  
{
protected:
 int temp;          //temperature variable
 char tempChar;     //temperature char
 
 bool hasVar;       //checking for variables  
 double charVtod (vector<char>ch); //convert from vector of character to double
 //reading a number from vector and return its digit's count int digit by reference
 double getNum (vector<char> &form, int &orderm, int &digit);
 unsigned getDigits(long num); //return the digits number of a long num

 //convert double to vector
 vector<char> dtoVchar (double num);
};//end data

4.2. MyChecker class

MyChecker class is a class which using for checking the input formula and the agreement of it to my program mathematical cover as if the user enter a wrong formula; this class should alert him about that, wrong entry  as such as: 
x**x ,si(45), /52312, etc... 
About functions:
  1. inline bool isOp(char &ch);  is using for checking for normal operators +, -, *, / 
  2. b-bool isVar(vectorvars,char &ch); is using for checking for variables
  3. bool _check (vector &ch, vectorvars); is the function which use other class function in order to check the entry formula
  4. bool isTriangle (vector &ch , int &temp); is the function which using for checking for trigonometric functions.
  5. bool editSigns (vector &ch); is the function which using to edit signs as ++ becomes + and -+ becomes -, etc.
  6. bool check(vectorform, char mainVar ); is the function which using to check entry formula with one variable, read by the other argument mainVar, such as 2x + 2 and mainVar = 'x'
  7. bool check(vectorform, vectorvars); is the function which using to check entry formula with more than one variables read by the vector vars such as 2x+3y 
class MyChecker : public Data 
{
private:
 inline bool isOp(char &ch);
 bool isVar(vector<char>vars,char &ch);
 //check trigonometric functions
 bool _check (vector<char> &ch, vector<char>vars);  //checker function
protected:
 bool isTriangle (vector<char> &ch , int &temp);
 bool editSigns (vector<char> &ch);
 bool check(vector<char>form, char mainVar );  //check with formulas
 bool check(vector<char>form, vector<char>vars); };//check with formulas and variables 

4.3. MyFixer class  

MyFixer class is the class which its minor is to edit formulas in order to make formula adaptable to my program and the next class calculator, for example: if the input is xx but * between them and the result become x*x; this function has only two functions; one of them is invoking the other:
  1. void fix(vector &ch); This function is invoked by other son classes and receive the formula and send it to the other function.
  2. void _fix (vector &ch); This function in invoked by the classes' function and do the required editing. 
class MyFixer : public MyChecker
{
protected:

 void fix(vector<char> &ch);

private: 
 int temp;
 void _fix  (vector<char> &ch);
 

};//end MyFixerer

4.4. Calculator class

Calculator class is the backbone of this program, this class's functions is the functions which give the calculation of a formula; with discovering its functions:
firstRanksecondRank and thirdRank are functions which divide formula to three phases:
the first related to power and trigonometric  functions, the second is for * and / operations and the third is for + and - operations, with putting on consider the function getBorder is using for determinate the brackets which calculation is done withing and then delete it. 
This class has 6 overloading of the function calc for working with the three cases which take characters vector, arrays and strings; the functions _calc at final is the function which takes the formula from the six overloading functions and then do calculation.    
class Calculator : public MyFixer
{
private:
 int str, end, // sub calculation borders 
               //depending on brackets
  digit,digit1,digit2,    //get number digit
  temp, temp1;     //temperature variable

 vector<char> charNum;
 double num, num1, num2; //storing double number bool final;      //final time do mathematics
 
 double getCaclNum (vector<char> &form, int &order, int &count);
 void getBorder (vector<char> &form);
 void firstRank (vector<char> &form); //trigonometric and power
 void secondRank (vector<char> &form); //do multiplying and division
 void thirdRank (vector<char> &form); //summing and subtracting
 vector<char> _calc (vector<char> formula);
 vector<char> strToVector (string form);
 vector<char> charToVector(char form [] );
public:

 Calculator () :final(false){} 
 double calc (vector<char> formula);
 double calc(char formula []);
 double calc(string formula );
 double calc (vector<char> formula, char var, double varValue);
 double calc (char formula [], char var, double varValue);
 double calc (string formula , char var, double varValue);
 
};//end calculator

5. How to use the implementation 

Now we have the part of how to use these classes:
  1. first you need to include these classes in your program
  2. make including in your cpp file for calculator.h file
  3. Make an object from calculator class
  4. use one of the six overloading calc functions
for example of you want to get a functions from the user with one variable, then get the variable's value and print the result, the code will be as the following:   
#include "Calculator.h"
#include <string>

using namespace std;

void main ()
{
 Calculator c1;  //object of Calculator
 string form;    //reading formula in
 char var;       //reading variable
 double value;   //reading variable value
  while(true)
  {
  cout<<"Please, enter your formula:";cin>>form;
  cout<<"enter your variable:";cin>>var;
  cout<<"Enter "<<var<<" value:";cin>>value;
  cout<<"result = "<'
x', value)<"cls"); } }//end main
And the result will be as in fig 2 : 
 
and later you can use it in more complex programs. 

6. Appendix   

6.1. Linked List    

In computer science, a linked list1   is a data structure consisting of a group of nodes  which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence. 

6.2. Bisection method    

The bisection method2  in mathematics is a root-finding method that repeatedly bisects an interval and then selects a sub interval in which a root must lie for further processing. It is a very simple and robust method, but it is also relatively slow. Because of this, it is often used to obtain a rough approximation to a solution which is then used as a starting point for more rapidly converging methods  

7. conclusion  

This article is to give you the lines to how to program a scientific calculators, but there isn't all thing, this field is so wide and there are a lot researches on it, but I wish this article help you to start. 
Best Wishes Smile | :)    

ref:http://www.codeproject.com/Articles/712674/How-to-write-a-scientific-calculator-with-an-imple

By , 21 Jan 2014