# Queue Based Calculator For Basic Mathematical Function Using C++

Posted by Suraj Singh on January 24, 2018 · 30 mins read

### Today, I am going to share with you piece of code that can evaluate the basic mathematical expression.

#### Introduction

Basically, In this code, my code will take a mathematical expression as a string than evaluation function will process it to calculate the exact answer of that equation.  Actually,  I create this script only to polish my c++ coding skill.

### Example code

Main Function

`main(){    cout << "\n\    ###################################################################\n\    \n\                     Calculating Program\n\\n\            ++++++++++++++++++++++++++++++++++++++++++++++\n\            +   Author : Suraj Singh Bisht               +\n\            +   Email  : surajsinghbisht054@gmail.com    +\n\            +   Github : github.com/surajsinghbisht054   +\n\            ++++++++++++++++++++++++++++++++++++++++++++++\n\            \n\            This Version Only Supports All Operators With Signs  \n\                   My tested Equation : \n\                                    \n\                          =  -1-2\n\                          =  ((((5+2)*(7-1))-7) - (5*2))\n\                          =  2*(5+6)          \n\                          \n\    ###################################################################\n\    " << endl;    string in;    cout << "[+] Please Enter Your Mathematical Expression : ";    /// Get Complete line as string    getline(cin, in);    //in = "-1-2";    //in = "((((5+2)*(7-1))-7) - (5*2))";    // in = "2*(5+6)";    if (in.size()==0){    in = "((((5+2)*(7-1))-7) - (5*2))";    }    int n;    n = evaluate(in);    cout << "\n\n\n\n\n[+] Your Expression : " << in <<endl;    cout << "\n\n\n[-] Your Final Answer is : "<< n << endl;}`

Main Function Codes are Just To Print Various Text line and pass user input string to evaluate function. Nothing Complex!

IsOperator Function

`/// Bool Function to find Operatorbool isoperator(char str){    if (    (str=='+')||    (str=='-')||    (str=='*')||    (str=='/')    ){    return true;    }    return false;}`

the isoperator function is to check that passed character is an operator or not. if yes, then return true else return false.

Evaluation Function (Extract Values)

I divided Evaluation Function Into 2 Section. Below Codes Previews are from the first section. basically, In this section, my codes are just extracting and storing operator character into a string and numeric digits into a stack. I know that many guys from you will notice that my function can also work without stack so, why I used it. Well, Guys, i used it because Stack is an Interesting topic and I'm also new learner in C++. But if anyone from you want to share more suitable codes, than you can freely comment your codes in below section. so, that everyone can easily find it.

`/// Evaluating Commandint evaluate(const string s){    /// Create A Stack [Using Stack Because it's new for me.. hahaha]    stack<int> S;    /// Postfix    string postfix = "";    /// Infix    string infix="";    string b_infix = "";    /// Length Of String    int len = s.size(), brk=0;    /// Char    char a;    /// For loop [Reverse Mode]    for(len; len>0; len--){        /// Access Value        a = s[len-1];        //cout << "[+] In Loop : " <<a << endl;        if((a=='-')&&(len==1)){        infix = a + infix;        }        else if ((a==' ')||(a==',')){            //cout << "[-] Space Found : Escaped "<<endl;            continue;        }        else if(isoperator(a)&& (brk==0)){            //cout << "[+] Operator Found " << a << endl;            postfix=postfix+a;            //cout << "[+] Postfix Status " << postfix<< endl;            int d = atoi(infix.c_str());// -'0'            //cout << "[+] Add to Stack " << d << endl;            if (d){            S.push(d);            }            infix = "";        }        else if (a==')'){            brk = brk + 1 ;            if (brk>1){                b_infix = a+b_infix ;            }            //cout << "[+] Open Bracket Found " << endl;        }        else if ((brk>0)&&(a!='(')){            //cout << "[+] Bracket value " << a << endl;            b_infix = a+b_infix ;        }        else if(a=='('){            brk = brk - 1;            //cout << "[+] Close Bracket Found " << a << endl;            if (brk==0){                cout << "[-] Evaluating : "<< b_infix << endl;                int d = evaluate(b_infix.c_str());                //cout << "[+] Add to Stack " << d << endl;                //cout << d << endl;                S.push(d);                b_infix = "";                infix = "";            }else{            //cout << a << brk << endl;            b_infix = a+b_infix ;            }        }        else{        /// Add Value Into Infix        infix = a + infix;        //cout << "[+] Add Infix Status " << infix<< endl;        }    }        if (infix.c_str()){            int d = atoi(infix.c_str());// -'0'            if (d){            // cout << "[+] 1 Add to Stack " << d << endl;            //cout << d << endl;            S.push(d);            }            infix = "";        }    //cout << "Status Of Infix : " << postfix << endl;    /// Now, Our Numeric Values Are Available On Stack And Operator On String`

Evaluation Function (Process Values)

In Second Section Of Evaluation Function, My codes are using if and else conditions to calculate and perform the mathematical operation. For more details Guys, Read Comments Carefully.

`    // Declear Interger    int i;    /// Get postfix Size    i = postfix.size();    /// Take Two Variables    int ia, ib;    /// Get Top Value    ia = S.top();    S.pop();    //cout << "Default Stack Value "<< ia << endl;    for(i; i>0; i--){        //cout <<postfix[i-1]<<endl;        ib = S.top();        S.pop();        //cout << "Second Default Stack Value "<< ib << endl;        //cout << "  "<< ia << " " << ib << endl;        if (postfix[i-1]=='+'){                ia = ia + ib;        }        else if (postfix[i-1]=='-'){                ia = ia - ib;        }        else if (postfix[i-1]=='*'){                ia = ia * ib;        }        else if (postfix[i-1]=='/'){                ia = ia / ib;        }        else{        //cout << postfix[i-1] <<endl;        }            }    cout << " [+]  And Evaluating Equation Answer is "<< ia << endl;    //cout << S.pop() << endl;    return ia;`

Final Codes

At The End, To make Complete Codes Clear and Easy To Understand I am going to paste complete Codes Here.

But For Latest Update Check Github Here

 ` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238` `// including header files#include #include #include #include using namespace std;/*__author__ = suraj singh bisht__email__ = surajsinghbisht054@gmail.com__Github__ = https://github.com/surajsinghbisht054*//// Bool Function to find Operatorbool isoperator(char str){ if ( (str=='+')|| (str=='-')|| (str=='*')|| (str=='/') ){ return true; } return false;}/// Evaluating Commandint evaluate(const string s){ /// Create A Stack [Using Stack Because it's new for me.. hahaha] stack S; /// Postfix string postfix = ""; /// Infix string infix=""; string b_infix = ""; /// Length Of String int len = s.size(), brk=0; /// Char char a; /// For loop [Reverse Mode] for(len; len>0; len--){ /// Access Value a = s[len-1]; //cout << "[+] In Loop : " <1){ b_infix = a+b_infix ; } //cout << "[+] Open Bracket Found " << endl; } else if ((brk>0)&&(a!='(')){ //cout << "[+] Bracket value " << a << endl; b_infix = a+b_infix ; } else if(a=='('){ brk = brk - 1; //cout << "[+] Close Bracket Found " << a << endl; if (brk==0){ cout << "[-] Evaluating : "<< b_infix << endl; int d = evaluate(b_infix.c_str()); //cout << "[+] Add to Stack " << d << endl; //cout << d << endl; S.push(d); b_infix = ""; infix = ""; }else{ //cout << a << brk << endl; b_infix = a+b_infix ; } } else{ /// Add Value Into Infix infix = a + infix; //cout << "[+] Add Infix Status " << infix<< endl; } } if (infix.c_str()){ int d = atoi(infix.c_str());// -'0' if (d){ // cout << "[+] 1 Add to Stack " << d << endl; //cout << d << endl; S.push(d); } infix = ""; } //cout << "Status Of Infix : " << postfix << endl; /// Now, Our Numeric Values Are Available On Stack And Operator On String // Declear Interger int i; /// Get postfix Size i = postfix.size(); /// Take Two Variables int ia, ib; /// Get Top Value ia = S.top(); S.pop(); //cout << "Default Stack Value "<< ia << endl; for(i; i>0; i--){ //cout <

I Hope guys This topic was Interesting For You.

Have a nice day