2 Replies - 1695 Views - Last Post: 22 March 2011 - 04:06 AM Rate Topic: -----

#1 victormartin1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-January 11

infix to postfix calculador

Posted 21 March 2011 - 05:31 PM

hi everyone.. I made a program about a calculator (infix to post fix but a ihave a problem...I print my infix like 5+6 and my postfix is 56+..that good but after that i can not get the answer 11...11 have to be in the output.. need help in that part thanks
this is my code using stack stl
//IMPLEMENTATION:

#include <string>
#include <stack>

#include "StackType.h"

using namespace std;

bool IsOperand(char ch)
   {
   if (((ch >= 'a') && (ch <= 'z')) ||
      ((ch >= 'A') && (ch <= 'Z')) ||
      ((ch >= '0') && (ch <= '9')))
      return true;
   else
      return false;
   }
bool TakesPrecedence(char OperatorA, char OperatorB)
   {
   if (OperatorA == '(')
      return false;
   else if (OperatorB == '(')
      return false;
   else if (OperatorB == ')')
      return true;
   else if ((OperatorA == '^') && (OperatorB == '^'))
      return false;
   else if (OperatorA == '^')
      return true;
   else if (OperatorB == '^')
      return false;
   else if ((OperatorA == '*') || (OperatorA == '/'))
      return true;
   else if ((OperatorB == '*') || (OperatorB == '/'))
      return false;
   else
      return true;
      
   }


/* Given:  Infix    A string representing an infix expression (no spaces).
   Task:   To find the postfix equivalent of this expression.
   Return: Postfix  A string holding this postfix equivalent.
*/
void Convert(const string & Infix, string & Postfix)
   {
   stack<char> OperatorStack;
   char TopSymbol, Symbol;
   int k;

   for (k = 0; k < Infix.size(); k++)
      {
      Symbol = Infix[k];
      if (IsOperand(Symbol))
         Postfix = Postfix + Symbol;
      else
         {
         while ((! OperatorStack.empty()) &&
            (TakesPrecedence(OperatorStack.top(), Symbol)))
            {
            TopSymbol = OperatorStack.top();
            OperatorStack.pop();
            Postfix = Postfix + TopSymbol;
            }
         if ((! OperatorStack.empty()) && (Symbol == ')'))
            OperatorStack.pop();   // discard matching (
         else
            OperatorStack.push(Symbol);
         }
      }

   while (! OperatorStack.empty())
      {
      TopSymbol = OperatorStack.top();
      OperatorStack.pop();
      Postfix = Postfix + TopSymbol;
      }
   }

//MAIN
#include <iostream>
#include <string>
#include <stack>
#include "StackType.h"
using namespace std;


void Convert(const string & Infix, string & Postfix);

bool IsOperand(char ch);

bool TakesPrecedence(char OperatorA, char OperatorB);

void printResult( stackType<int>& stack,bool isexpok); 

int main(void)
   {
    char Reply;
	
	stackType<int> stack(50);
	
   do
      {
    int result;   
	
	  stack.initializeStack();
	  string Infix, Postfix;   // local to this loop

      cout << "Enter an infix expression (e.g. (a+B)/>/c^2, with no spaces):"
         << endl;
      cin >> Infix;

      Convert(Infix, Postfix);
      cout << "The equivalent postfix expression is:" << endl
         << Postfix << endl;
      
	  cout << endl << "Do another (y/n)? ";
      cin >> Reply;
      }
   while (tolower(Reply) == 'y');

   return 0;
   }


This post has been edited by macosxnerd101: 21 March 2011 - 05:36 PM
Reason for edit:: Added code tags and moved to C++ Help. Please do not post programming questions in the Site Support forums.


Is This A Good Question/Topic? 0
  • +

Replies To: infix to postfix calculador

#2 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: infix to postfix calculador

Posted 21 March 2011 - 06:51 PM

can you show me where the code is that is supposed to evaluate the post fix code?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: infix to postfix calculador

Posted 22 March 2011 - 04:06 AM

Meh, just another copypasta poster it appears.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1