![]() |
|
|||||||
![]() |
|
More
|
|
|
Bookmarks | Article Tools | Search this Article | Display Modes |
InFix to PostFix and PostFix expression evaluation.
On 21st October, 2006
|
Authorshabbir ( Go4Expert Founder )
Recent Articles
Similar Articles
Introduction Infix Expression : Notation in which the operator separates its operands. Eg (a + b) * c. Infix notation requires the use of brackets to specify the order of evaluation. Postfix Expression : Reverse Polish Notation or Suffix Notation Notation in which the operator follows its operands. Eg a + b * c represented as abc*+. Infix to Postfix Conversion Algo :
Code: C
Now after making the conversion what we have gained. As PostFix strings are parenthesis-free notation mathematical calculations and precedence is already defined within the string and so calculation is done very easily. Postfix Expression evaluation Algo :
Code: C
|
|
|
#2 |
|
Contributor
|
Re: InFix to PostFix and PostFix expression evaluation.
Nice Job
__________________
C and C++ is the best languages in the world. |
|
|
|
|
|
#3 | |
|
Go4Expert Founder
![]() |
Re: InFix to PostFix and PostFix expression evaluation.Quote:
|
|
|
|
|
|
|
#4 |
|
Newbie Member
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
Re: InFix to PostFix and PostFix expression evaluation.
I would love to see the programs that changes infix to postfix and then evaluates it in C++ not in C. Could you give it to me or post it or soemthing i am anxious to see it, thanks!
|
|
|
|
|
|
#5 | |
|
Go4Expert Founder
![]() |
Re: InFix to PostFix and PostFix expression evaluation.Quote:
|
|
|
|
|
|
|
#6 |
|
Newbie Member
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
Re: InFix to PostFix and PostFix expression evaluation.
Here is what i got i need the main, i want two functions that you did the one that changes infix to post fix and one that evaluates, i am really stuck can you pleaseeee help me! Here is my .h Code:
#include <iostream>
using namespace std;
template <typename ElementType>
#ifndef STACK
#define STACK
class Stack
{
private:
class Node
{
public:
ElementType info;
Node *next;
};
typedef Node * Nodeptr;
public:
Stack();
Stack(const Stack<ElementType> &);
~Stack();
//Stack<ElementType> operator=(const Stack<ElementType> &);
bool empty() const;
void Push(ElementType item);
ElementType Pop();
ElementType Top() const;
void display(ostream & out) const;
private:
Node *miStack;
};
template <typename ElementType>
ostream & operator<< (ostream & out, const Stack <ElementType> & astack); //ya
#include "stack.template"
#endif
Code:
#include<iostream>
using namespace std;
template <typename ElementType>
Stack<ElementType>::Stack()
{
miStack=NULL;
}
template <typename ElementType>
Stack<ElementType>::Stack(const Stack<ElementType> & orig)
{
nodePtr p=orig.miStack;
nodePtr pnuevo, anterior;
while (p!=NULL)
{
pnuevo=new node;
pnuevo->info=p->info;
if (orig.miStack==p)
miStack=pnuevo;
else
anterior->next=pnuevo;
anterior=pnuevo;
p=p->next;
}
if (orig.miStack==NULL)
miStack=NULL;
else
pnuevo->next=NULL;
}
template <typename ElementType>
Stack<ElementType>::~Stack()
{
nodePtr p = miStack, anterior;
while (p!=NULL)
{
anterior=p;
p=p->next;
delete anterior;
}
}
/* template <typename ElementType>
Stack<ElementType> Stack<ElementType>::operator = (const Stack<ElementType> & orig)
{
if (this != & orig)
{
nodePtr p=miStack, anterior;
while (p!=NULL)
{
anterior=p;
p=p->next;
delete anterior;
}
nodePtr q=orig.miStack;
nodePtr pnuevo;
while (q!=NULL)
{
pnuevo=new node;
pnuevo->info=p->info;
if (orig.miStack==p)
miStack=pnuevo;
else
anterior->next=pnuevo;
anterior=pnuevo;
q=q->next;
}
if (orig.miStack==NULL)
miStack=NULL;
else
miStack=NULL;
}
return *this;
}*/
template <typename ElementType>
bool Stack<ElementType>::empty() const
{
return (miStack==NULL);
}
template <typename ElementType>
void Stack<ElementType>::Push(ElementType item)
{
Nodeptr p = new Node;
p->info=item;
p->next=miStack;
miStack=p;
}
template <typename ElementType>
ElementType Stack<ElementType>::Pop()
{
Nodeptr p = miStack, elementType item;
if(miStack != null)
item = p->info;
miStack = miStack->next;
delete p;
return item;
else
{ cerr<<"Stack vacio. No se hizo pop "<<endl;
exit(1);
}
}
template <typename ElementType>
ElementType Stack<ElementType>::Top()
{
return( )//I dont know what to put here, help me also
}
template <typename ElementType>
void Stack<ElementType>::display (ostream & out)const
{
nodePtr p;
p=miStack;
out<<"Los Elementos de la lista son:";
while (p!=NULL)
{
out<<p->info<<" ";
p=p->next;
}
cout<<endl;
}
template<typename ElementType>
ostream & operator<<(ostream & out, const Stack<ElementType> & astack)
{
astack.display(out);
return out;
}
Code:
#include "stack.h"
using namespace std;
void main()
{
//HELP!!
}
|
|
|
|
|
|
#7 |
|
Go4Expert Founder
![]() |
Re: InFix to PostFix and PostFix expression evaluation.
What you are trying to do is have some classes but don't know how to call them. How did you get the classes? If you got from somewhere I am sure they will have the sample implementation as well.
|
|
|
|
|
|
#8 |
|
Light Poster
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
Re: InFix to PostFix and PostFix expression evaluation.
if i am trying to use more than four variables in postfix expression, output is not coming to me. for ex: ab+ cd+* can u guide me sorry if i am wrong |
|
|
|
|
|
#9 |
|
Go4Expert Founder
![]() |
Re: InFix to PostFix and PostFix expression evaluation.
Copy the code into your compiler (I have used MS VC 6) Run the Program and enter you will see the following text Code:
Enter an infix expression Code:
(a+b)*(c+d) Code:
infix = (a+b)*(c+d) post fix =ab+cd+* Do you wish to continue Code:
(a+b)*(c+d)*(e+f) |
|
|
|
|
|
#10 |
|
Light Poster
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 ![]() |
Re: InFix to PostFix and PostFix expression evaluation.
sorry i didn't mentioned clearly, While using postfix evaluation program, if i am typing postfix expression ab+cd+* , result is not coming properly, if ab+ result is ok. can u verify once. where i am doing wrong thanks in advance |
|
|
|
![]() |
|
More
|
| Currently Active Users Reading This Article: 1 (0 members and 1 guests) | |
| Article Tools | Search this Article |
| Display Modes | |
| Bookmarks | |
|
|