Skip to content

Instantly share code, notes, and snippets.

@thasan3003
Last active November 17, 2019 04:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thasan3003/3ed209b574f77bfeadb602afb22f4f21 to your computer and use it in GitHub Desktop.
Save thasan3003/3ed209b574f77bfeadb602afb22f4f21 to your computer and use it in GitHub Desktop.
This can solve simple mathematical expressions including parentheses, division, multiplication, addition and subtraction respectively with spaces and without spaces. But it cannot detect wrong expressions.
/*
Md Tahmid Hasan
CSE, BSMRSTU
tahmidhasan3003
*/
#include<bits/stdc++.h>
#include<sstream>
using namespace std;
double stringToDouble(string s)
{
double temp = 0, multiplicationFactor = 10;
int n = 1, d = 0;
for(int i = 0; i < s.length(); i++)
{
if(s[i] == '-')
{
n = -1;
continue;
}
else if(s[i] == '.')
{
multiplicationFactor = .1;
d = 1;
continue;
}
if(!d)
{
temp *= multiplicationFactor;
temp += (s[i]-'0');
}
else
{
temp += (s[i]-'0') * multiplicationFactor;
multiplicationFactor *= .1;
}
}
return temp * n;
}
string toString(double d)
{
stringstream strs;
strs << d;
string str = strs.str();
return str;
}
string solve(string expr)
{
int bkt = 0, div = 0, mul = 0, as = 0;
string tempExp, opnd, opnd2;
for(int i = 0; i < expr.length(); i++)
{
if(expr[i] == ' ')
continue; //removing spaces
else if(expr[i] == '(')
{
bkt++;
string subExpr, subExprResult;
for(i++; bkt != 0; i++)
{
if(expr[i] == ' ')
continue;
else if(expr[i] == ')')
{
bkt--;
if(bkt == 0)
{
subExprResult = solve(subExpr); //get the value of parenthesized expression
break;
}
}
else if(expr[i] == '(')
bkt++;
subExpr += expr[i];
}
tempExp += subExprResult;
}
else if(expr[i] == '/')
{
div++;
tempExp += expr[i];
}
else if(expr[i] == '*')
{
mul++;
tempExp += expr[i];
}
else if((expr[i] == '+') || (expr[i] == '-'))
{
as++;
tempExp += expr[i];
}
else
tempExp += expr[i];
}
expr = tempExp; //update the expression
//cout<<"After remove brackets & spaces: "<<expr<<endl;
if(div != 0) //division
{
opnd = "";
tempExp = "";
for(int i = 0; i < expr.length(); i++)
{
if(expr[i] == '/')
{
opnd2 = "";
for(i++; i<expr.length(); i++)
{
if((expr[i] == '/') || (expr[i] == '*') || (expr[i] == '+') || (expr[i] == '-'))
{
i--;
break;
}
opnd2 += expr[i];
}
tempExp += toString(stringToDouble(opnd) / stringToDouble(opnd2));
opnd = "";
}
else if((expr[i] == '*') || (expr[i] == '+') || (expr[i] == '-'))
{
tempExp += opnd;
tempExp += expr[i];
opnd = "";
}
else
opnd += expr[i];
}
expr = tempExp + opnd;
//cout<<"After division: "<<expr<<endl;
}
if(mul != 0) //multiplication
{
opnd = "";
tempExp = "";
for(int i = 0; i < expr.length(); i++)
{
//cout<<expr[i];
if(expr[i] == '*')
{
opnd2 = "";
for(i++; i < expr.length(); i++)
{
if((expr[i] == '*') || (expr[i] == '+') || (expr[i] == '-'))
{
i--;
break;
}
opnd2 += expr[i];
}
tempExp += toString(stringToDouble(opnd) * stringToDouble(opnd2));
opnd = "";
}
else if((expr[i] == '+') || (expr[i] == '-'))
{
tempExp += opnd;
tempExp += expr[i];
opnd = "";
}
else
opnd += expr[i];
}
expr = tempExp+opnd;
//cout<<"After multiplication: "<<expr<<endl;
}
if(as != 0) //addition & subtraction
{
opnd = "";
double result = 0;
bool op = false;
tempExp = "";
for(int i = 0; i < expr.length(); i++)
{
if(expr[i] == '+')
{
opnd2 = "";
for(i++; i < expr.length(); i++)
{
if((expr[i] == '+') || (expr[i] == '-'))
{
i--;
break;
}
opnd2 += expr[i];
}
if(!op)
{
result = stringToDouble(opnd);
op = true;
}
result += stringToDouble(opnd2);
//cout<<"After Add: "<<result<<endl;
}
else if(expr[i] == '-')
{
opnd2 = "";
for(i++; i < expr.length(); i++)
{
if((expr[i] == '+') || (expr[i] == '-'))
{
i--;
break;
}
opnd2 += expr[i];
}
if(!op)
{
result = stringToDouble(opnd);
op = true;
}
result -= stringToDouble(opnd2);
//cout<<"After Sub: "<<result<<endl;
}
else
opnd += expr[i];
}
expr = toString(result);
//cout<<"After addition & subtraction: "<<expr<<endl;
}
return expr;
}
int main()
{
//freopen("Sample Input.txt","r",stdin);
string expr, result;
getline(cin, expr);
cout<<"Input: "<<expr<<endl;
result = solve(expr);
cout<<"Result: "<<result<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment