//I make program of infix to post fix xpression plz tell me waht wrong with my code
//some it true result for arthematic opertaors b,w two oprend but with more then it does //not provide valid output plxxxxxxxxxxxx help me
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int presidence( );
void infixToPostfix(char *);
void postfixEvaluation();
template<class T>
class Stack
{
private:
char read[500];
T *data;
int capacity;
int top;
public:
Stack();
Stack(int );
T pop(T & p);
void push(T);
int isEmpty();
int isFull();
int getTop();
~Stack();
};
template<class T>
Stack<T> ::Stack()
{
capacity=0;
top=-1;
}
template<class T>
Stack<T>::Stack(int s=500)
{
capacity=s;
data=new T[capacity];
top=-1;
}
template<class T>
int Stack<T>:: isEmpty()
{
return (top==-1);
}
template<class T>
int Stack <T>:: isFull()
{
return (top==capacity-1);
}
template<class T>
void Stack<T>:: push(T p)
{
if(isFull())
{
cout<<"Stack is OverFlow";
exit(1);
}
else
{
top++;
data[top]=p;
}
}
template<class T>
T Stack<T>:: pop(T & p)
{
if(isEmpty()) {
cout<<"Under Flow";
exit(0); }
else {
p=data[top];
top--;
}
}
template<class T>
Stack<T>::~Stack(){
delete[] data;
}
template<class T>
int Stack<T>::getTop()
{
return top;
}
int presidence(char A,char B)
{
if(A=='+' && B=='-' || A=='*' && B=='/')
{
return 1;
}
if(A=='-' && B=='+' || A=='/' && B=='*')
return 1;
if(A=='+' && B=='*' || B=='-'||A=='/' && B=='+' ||A=='/' && B=='-')
{
return 1;
}
if(A=='*' && B=='+' || A=='*' && B=='-')
return 0;
}
void infixToPostfix(char *infix)
{
int len=strlen(infix);
char postfix[20];
Stack <char> P(len+1);
int pi = 0;
for(int i=0;infix[i]!='\0';i++)
{
if(infix[i]>='A' ||infix[i]>='a' ||infix[i]>='0'&&
infix[i]<='Z'||infix[i]>='z'||infix[i]>='9')
{
postfix[pi]=infix[i];
pi++;
}
if(infix[i]=='+' ||infix[i]=='-'||infix[i]=='*'||infix[i]=='/')
{
if(P.isEmpty() || infix[i-1]=='(')
{
P.push(infix[i]);
}
else if(!P.isEmpty() || infix[i-1]!='(')
{
// chage
int no=presidence(infix[i],P.pop(i));
while(!P.isEmpty() && no==1)
{
// change accour
postfix[pi]=P.pop(pi);
pi++;
}
P.push(infix[i]);
if(infix[i]!=')')
{
P.push(infix[i]);
}
else if(infix[i]==')')
{
//cahge accour
while(P.pop(i)!='(')
{
postfix[pi]=P.pop(pi);
pi++;
}
}
}
}
}
for( i=pi;!P.isEmpty() && i<=len;i++)
{
postfix[i]=P.pop(i);
}
for(int j=0;j<=pi;j++)
cout<<" "<<postfix[j];
}
void main()
{
clrscr();
char arr[] = "(A+B)*(C-D)";
infixToPostfix(arr);
Stack <int>s;
cout<<endl;
getch();
}
Süspeñce -4 Newbie Poster
Süspeñce -4 Newbie Poster
volvo877 -7 Newbie Poster
WaltP commented: OMG -doing people's homework as NOT acceptable here. Please let them get their own grade. -4
Süspeñce commented: give me lot of help +1
sunn shine 0 Light Poster
Süspeñce -4 Newbie Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Süspeñce commented: no need to tell me ..i know better -1
Süspeñce -4 Newbie Poster
WaltP commented: Don't give neg rep to someone for a comment not directed at you. -4
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.