I need help with RPN notation!!!

This is a discussion on I need help with RPN notation!!! within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm currently working on a program that converts infix notation to postfix notation, then calculates the postfix notation. I ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    I need help with RPN notation!!!

    Hi,
    I'm currently working on a program that converts infix notation to postfix notation, then calculates the postfix notation. I was successful in the conversion, but I can't get the calculation to work. Below is my code, can someone please help, I've been working on this for hours!

    Any comments are appreciated. Thanks!

    Code:
     
    #include<iostream>
    
    using namespace std;
    
    /*-------------------- Stack class ----------------------*/
    template<class T>
    class Stack
    {
      private:
          int size;
          int top;
          T *arrayptr;
      public:
          Stack(int=20);
          bool Push(T);
          T Pop();
          bool isfull();
          bool isempty();
    };
    
    template<class T>
    Stack<T> :: Stack(int s)
    {
      size = s > 0?s:20;
      top = -1;
      arrayptr = new T[size];
    }
    
    template<class T>
    bool Stack<T> :: Push(T val)
    { 
      if(!isfull()){
          arrayptr[++top]=val;
          return true;}
      return false;
    }
    
    template<class T>
    T Stack<T> :: Pop()
    {
    return arrayptr[top--];
    }
    
    template<class T>
    bool Stack<T>::isfull()
    {
      return top == size-1;
    
    }
    
    template<class T>
    bool Stack<T>::isempty()
    {
      return top == -1;
    }
    
    void eVal(char *str,Stack<char> s,int siz);
    
    /*------------------------ main --------------------------*/
    
    int main()
    {
      Stack<char> s1;
      int sizestr;
      char string[20];
    
      cout << "Enter an infix expression: ";
      cin >> string;
      sizestr = strlen(string);
      
      cout << "The RPN Notation is: ";
      eVal(string,s1,sizestr);
    
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    
    /*----------------------- eVal ---------------------------*/
    
    void eVal(char *str,Stack<char> s,int siz)
    {
      char pop1;
      char str2[20];
    
      for(int i = 0;i < siz;i++)
      {
          if(str[i] > '0' && str[i] <= '9')
    	  {
              cout << str[i];
    		  str2[i] = str[i];
    	  }
          else if(str[i] == '*')
          {
              if(s.isempty())
              {
                  s.Push(str[i]);
              }
              else
              {
                  pop1 = s.Pop();
                  if(pop1 == '(')
                  {
                      s.Push(pop1);
                      s.Push(str[i]);
                  }
                  else if(pop1 == '*'|| pop1 =='/')
                  {
    				  str2[i] = pop1;
                      cout << pop1;
                      s.Push(str[i]);
                  }
                  else if(pop1 == '+' || pop1 == '-')
                  {
                      s.Push(pop1);
                      s.Push(str[i]);
                  }
              }
          }
    
          else if(str[i] == '/')
          {
              if(s.isempty())
              {
                  s.Push(str[i]);
              }
              else
              {
                  pop1 = s.Pop();
                  if(pop1 == '(')
                  {
                      s.Push(pop1);
                      s.Push(str[i]);
                  }
                  else if(pop1 == '*' || pop1 == '/')
                  {
    				  str2[i] = pop1;
                      cout << pop1;
                      s.Push(str[i]);
                  }
                  else if(pop1 == '+' || pop1 == '-')
                  {
                      s.Push(pop1);
                      s.Push(str[i]);
                  }
              }
          }
    
          else if(str[i] == '+')
          {
              if(s.isempty())
              {
                  s.Push(str[i]);
              }
              else
              {
                  pop1 = s.Pop();
                  if(pop1 == '(')
                  {
                      s.Push(pop1);
                      s.Push(str[i]);
                  }
                  else if(pop1 == '*' || pop1 == '/' || pop1 == '+' || pop1 == '-')
                  {	
    				  str2[i] = pop1;
                      cout << pop1;
                      s.Push(str[i]);
                  }
              }
          }
    
          else if(str[i] == '-')
          {
              if(s.isempty())
              {
                  s.Push(str[i]);
              }
              else
              {
                  pop1 = s.Pop();
                  if(pop1 == '(' )
                  {
                      s.Push(pop1);
                      s.Push(str[i]);
                  }
                  else if(pop1 == '*' || pop1 == '/' || pop1 == '+' || pop1=='-')
                  {
    				  str2[i] = pop1;
                      cout << pop1;
                      s.Push(str[i]);
                  }
              }
          }
    
          else if(str[i] == '(')
              s.Push(str[i]);
          else if(str[i] == ')')
          {
              pop1 = s.Pop();
              while(pop1 != '(')
              {
    			  str2[i] = pop1;
                  cout << pop1;
                  pop1 = s.Pop();
              }
          }
      }
    
     while(!s.isempty())
     {
         cout << s.Pop();
     }
     cout << endl;
     int left, right;
     char type;
     int convert;
    
     for(i = 0;i < siz;i++)
     {
    	 type = str2[i];
    	 if (isdigit(type))
    	 {
    		 convert = str2[i] - '0';
    		 i = convert;
    		 s.Push(str2[i]);
    	 }
    	 else
    	 {
    		 switch(type)
    		 {
    			case '+':
    				s.Push(s.Pop() + s.Pop());
    				break;
    			case '-':
    				right = s.Pop();
    				left = s.Pop();
    				s.Push(left - right);
    				break;
    			case '*':
    				s.Push(s.Pop() * s.Pop());
    				break;
    			case '/':
    				right = s.Pop();
    				left = s.Pop();
    				s.Push(left / right);
    				break;
    			default: 
    				throw domain_error("Undefined operator");
    		 }
    	 }
     }
    cout << "Result: " << str2[0] << str2[1] << str2[2] << str2[3] << str2[4] <<  endl;
    }

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    It's not working because you wrote eVal as one big, obfuscated function, instead of breaking it up into pieces. If you broke it up into smaller, simpler functions and debugged each part, you'd be able to solve your problem.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    I understand that I need to break it up into smaller pieces, but I need some advice on how to transfer the RPN notation char's into a seperate array...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 06:44 AM
  2. CamelCase VS Hungarian notation, which is better?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2007, 09:31 PM
  3. RPN & Functions
    By Cactus_Hugger in forum Tech Board
    Replies: 3
    Last Post: 09-06-2006, 01:21 PM
  4. A doubt about changing notation
    By louis_mine in forum C Programming
    Replies: 7
    Last Post: 11-21-2004, 09:15 PM
  5. Question regarding RPN and error detection
    By Roule in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 01:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21