hii readers,
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 Functionmain(){
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 Operator
bool 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 Command
int 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 CodesAt 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | // including header files #include <iostream> #include <string.h> #include <stack> #include <stdlib.h>
using namespace std;
/* __author__ = suraj singh bisht __email__ = surajsinghbisht054@gmail.com __Github__ = https://github.com/surajsinghbisht054
*/
/// Bool Function to find Operator bool isoperator(char str){
if ( (str=='+')|| (str=='-')|| (str=='*')|| (str=='/') ){ return true; } return false; }
/// Evaluating Command int 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
// 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;
}
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\ = (1+2)\n\ = (1-2)+(1+2)\n\ = (1-2)*6\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;
}
|
I Hope guys This topic was Interesting For You.
Have a nice day