Advertisement
tahmidhasan3003

Simple Math Expression.cpp

Dec 11th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.39 KB | None | 0 0
  1. /*
  2. Md Tahmid Hasan
  3. CSE, BSMRSTU
  4. tahmidhasan3003
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. #include<sstream>
  9.  
  10. using namespace std;
  11.  
  12. double stringToDouble(string s)
  13. {
  14.     double temp = 0, multiplicationFactor = 10;
  15.     int n = 1, d = 0;
  16.     for(int i = 0; i < s.length(); i++)
  17.     {
  18.         if(s[i] == '-')
  19.         {
  20.             n = -1;
  21.             continue;
  22.         }
  23.         else if(s[i] == '.')
  24.         {
  25.             multiplicationFactor = .1;
  26.             d = 1;
  27.             continue;
  28.         }
  29.  
  30.         if(!d)
  31.         {
  32.             temp *= multiplicationFactor;
  33.             temp += (s[i]-'0');
  34.         }
  35.         else
  36.         {
  37.             temp += (s[i]-'0') * multiplicationFactor;
  38.             multiplicationFactor *= .1;
  39.         }
  40.     }
  41.     return temp * n;
  42. }
  43.  
  44. string toString(double d)
  45. {
  46.     stringstream strs;
  47.     strs << d;
  48.     string str = strs.str();
  49.  
  50.     return str;
  51. }
  52.  
  53. string solve(string expr)
  54. {
  55.     int bkt = 0, div = 0, mul = 0, as = 0;
  56.     string tempExp, opnd, opnd2;
  57.  
  58.     for(int i = 0; i < expr.length(); i++)
  59.     {
  60.         if(expr[i] == ' ')
  61.             continue;                                   //removing spaces
  62.         else if(expr[i] == '(')
  63.         {
  64.             bkt++;
  65.             string subExpr, subExprResult;
  66.             for(i++; bkt != 0; i++)
  67.             {
  68.                 if(expr[i] == ' ')
  69.                     continue;
  70.                 else if(expr[i] == ')')
  71.                 {
  72.                     bkt--;
  73.                     if(bkt == 0)
  74.                     {
  75.                         subExprResult = solve(subExpr); //get the value of parenthesized expression
  76.                         break;
  77.                     }
  78.                 }
  79.                 else if(expr[i] == '(')
  80.                     bkt++;
  81.  
  82.                 subExpr += expr[i];
  83.             }
  84.             tempExp += subExprResult;
  85.         }
  86.         else if(expr[i] == '/')
  87.         {
  88.             div++;
  89.             tempExp += expr[i];
  90.         }
  91.         else if(expr[i] == '*')
  92.         {
  93.             mul++;
  94.             tempExp += expr[i];
  95.         }
  96.         else if((expr[i] == '+') || (expr[i] == '-'))
  97.         {
  98.             as++;
  99.             tempExp += expr[i];
  100.         }
  101.         else
  102.             tempExp += expr[i];
  103.     }
  104.     expr = tempExp;                                        //update the expression
  105.     //cout<<"After remove brackets & spaces: "<<expr<<endl;
  106.  
  107.     if(div != 0)                                            //division
  108.     {
  109.         opnd = "";
  110.         tempExp = "";
  111.         for(int i = 0; i < expr.length(); i++)
  112.         {
  113.             if(expr[i] == '/')
  114.             {
  115.                 opnd2 = "";
  116.                 for(i++; i<expr.length(); i++)
  117.                 {
  118.                     if((expr[i] == '/') || (expr[i] == '*') || (expr[i] == '+') || (expr[i] == '-'))
  119.                     {
  120.                         i--;
  121.                         break;
  122.                     }
  123.                     opnd2 += expr[i];
  124.                 }
  125.                 tempExp += toString(stringToDouble(opnd) / stringToDouble(opnd2));
  126.                 opnd = "";
  127.             }
  128.             else if((expr[i] == '*') || (expr[i] == '+') || (expr[i] == '-'))
  129.             {
  130.                 tempExp += opnd;
  131.                 tempExp += expr[i];
  132.                 opnd = "";
  133.             }
  134.             else
  135.                 opnd += expr[i];
  136.         }
  137.         expr = tempExp + opnd;
  138.         //cout<<"After division: "<<expr<<endl;
  139.     }
  140.  
  141.     if(mul != 0)                                    //multiplication
  142.     {
  143.         opnd = "";
  144.         tempExp = "";
  145.         for(int i = 0; i < expr.length(); i++)
  146.         {
  147.             //cout<<expr[i];
  148.             if(expr[i] == '*')
  149.             {
  150.                 opnd2 = "";
  151.                 for(i++; i < expr.length(); i++)
  152.                 {
  153.                     if((expr[i] == '*') || (expr[i] == '+') || (expr[i] == '-'))
  154.                     {
  155.                         i--;
  156.                         break;
  157.                     }
  158.                     opnd2 += expr[i];
  159.                 }
  160.                 tempExp += toString(stringToDouble(opnd) * stringToDouble(opnd2));
  161.                 opnd = "";
  162.             }
  163.             else if((expr[i] == '+') || (expr[i] == '-'))
  164.             {
  165.                 tempExp += opnd;
  166.                 tempExp += expr[i];
  167.                 opnd = "";
  168.             }
  169.             else
  170.                 opnd += expr[i];
  171.         }
  172.         expr = tempExp+opnd;
  173.         //cout<<"After multiplication: "<<expr<<endl;
  174.     }
  175.  
  176.     if(as != 0)                                 //addition & subtraction
  177.     {
  178.         opnd = "";
  179.         double result = 0;
  180.         bool op = false;
  181.         tempExp = "";
  182.  
  183.         for(int i = 0; i < expr.length(); i++)
  184.         {
  185.             if(expr[i] == '+')
  186.             {
  187.                 opnd2 = "";
  188.                 for(i++; i < expr.length(); i++)
  189.                 {
  190.                     if((expr[i] == '+') || (expr[i] == '-'))
  191.                     {
  192.                         i--;
  193.                         break;
  194.                     }
  195.                     opnd2 += expr[i];
  196.                 }
  197.                 if(!op)
  198.                 {
  199.                     result = stringToDouble(opnd);
  200.                     op = true;
  201.                 }
  202.                 result += stringToDouble(opnd2);
  203.                 //cout<<"After Add: "<<result<<endl;
  204.             }
  205.             else if(expr[i] == '-')
  206.             {
  207.                 opnd2 = "";
  208.                 for(i++; i < expr.length(); i++)
  209.                 {
  210.                     if((expr[i] == '+') || (expr[i] == '-'))
  211.                     {
  212.                         i--;
  213.                         break;
  214.                     }
  215.                     opnd2 += expr[i];
  216.                 }
  217.                 if(!op)
  218.                 {
  219.                     result = stringToDouble(opnd);
  220.                     op = true;
  221.                 }
  222.                 result -= stringToDouble(opnd2);
  223.                 //cout<<"After Sub: "<<result<<endl;
  224.             }
  225.             else
  226.                 opnd += expr[i];
  227.         }
  228.         expr = toString(result);
  229.         //cout<<"After addition & subtraction: "<<expr<<endl;
  230.     }
  231.     return expr;
  232. }
  233.  
  234. int main()
  235. {
  236.     //freopen("Sample Input.txt","r",stdin);
  237.     string expr, result;
  238.     getline(cin, expr);
  239.     cout<<"Input: "<<expr<<endl;
  240.     result = solve(expr);
  241.     cout<<"Result: "<<result<<endl;
  242.     return 0;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement