Reverse polish notation C++ -


i've written code in c++ problem is valid one-digit number , i'd know can change or modify valid multi-digits numbers

#include <iostream>  #include <stack>  #include <string> #include<vector> #include<sstream> using namespace std;  bool op(char b)  {     return b=='+' || b=='-' || b=='*' || b=='/' ;   }   bool prio(char a, char b)  {     if(a=='(')     {         return true;      }      if(a=='+' || a=='-')      {         return true;       }      if(b=='+' || b=='-')      {         return false;      }       return true;   }  double posfix ( const std::string& expression ) {      double l,r,ans;     std::stringstream postfix(expression);     std::vector<double> temp;     std::string s;     while ( postfix >> s )     {         if( op(s[0]) )         {             //pull out top 2 elements             r = temp.back();             temp.pop_back();             l = temp.back();             temp.pop_back();             // perform maths             switch( s[0])             {                 case '+': ans =  l + r ; break;                 case '-': ans =  l - r ; break;                 case '*': ans =  l * r ; break;                 case '/': ans =  l / r ; break; // check if r !=0             }              temp.push_back( ans ); // push result of above operation         }         else         {             temp.push_back( std::stod(s) );         }     }      return temp[0] ; //last element answer }  main()  {      string a;     string res;      stack<char> s;      int i,j=0;     double x;     cin>>a;      for(i=0; i<a.size(); i++)      {         if(a[i]!='(' && a[i]!=')' && op(a[i])==false)          {             j++;             if(j==1)             {                 res=a[i];              }             else             {                 res=res+" "+a[i];              }         }          if(a[i]=='(')          {             s.push(a[i]) ;          }          if(a[i]==')')          {             while(s.top()!='(')              {                 res=res+" "+s.top();                  s.pop();             }             s.pop();          }          if(op(a[i])==true)          {             if(s.empty() || (s.empty()==false && prio(s.top(), a[i])) )              {                 s.push(a[i]);              }             else              {                 while(s.empty()==false && prio(s.top(),a[i])==false )                  {                      res=res+" "+s.top();                      s.pop();                  }                 s.push(a[i]) ;              }          }       }      while(s.empty()==false)      {         res=res+" "+s.top();          s.pop();      }              cout<<res<<endl;     x=posfix(res);      cout<<endl<<x<<endl; }  

i tried use strtod didn't know how use in code.

add checker know if you're reading number longer 1 character. if that's case, don't add " " between characters. example of solution:

int k = 0; for(i=0; i<a.size(); i++) {     if(a[i]!='(' && a[i]!=')' && op(a[i])==false) {         j++;         k++;         if(j==1)             res=a[i];         else if (k == 1)             res=res+" "+a[i];         else             res=res+a[i];     } else {         k = 0;     }     // rest same     // ... } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

mysql - FireDac error 314 - but DLLs are in program directory -