Please Help me..........

What is error in this code:
I have written this code for conversion of an Infix Expression into a Postfix expression using Stack,
please guide me in this....
thanks...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  What is error in this:

#include "stdafx.h"

#include<iostream>
#include<string>
using namespace std;

class stack
{
private:
	int top;
	unsigned int size;
	char * data;
	string s,s1;
public:
	stack():top(-1){}
	stack(int n)
	{
		size=n;
		data=new char[n];
	}

	bool isEmpty()
	{
		if(top==-1)
			return true;
		else return false;
	}
	bool isFull()
	{
		if(top==size-1)
			return true;
		else return false;
	}
	void push(char element)
	{
		if(!isFull())
			cout<<"Stack is Full ";
		else
			data[++top]=element;
	}
	char pop()
	{
		if(isEmpty())
			cout<<"Stack is Empty";
		else return data[top--]; 
	}
	char topValue()
	{
		if(isEmpty())
			cout<<"Stack is empty";
		else return data[top];
	}
	void getinfix()
	{
		cout<<"Enter Your Infix Expression: ";
		cin>>s;
	}
	void showinfix()
	{
		cout<<s;
	}
	bool precedence()
	{
		for(int i=0;i<size-1;i++)
		if((s[i]=='+' && topValue()=='+')||(s[i]=='+' && topValue()=='-'))
		return true;
		else if((s[i]=='-' && topValue()=='-')||(s[i]=='-' && topValue()=='+'))
			return true;
		else if((s[i]=='*' && topValue()=='*')||(s[i]=='*' && topValue()=='/')||(s[i]=='*' && topValue()=='+')||(s[i]=='*' && topValue()=='-'))
		return true;
		else if((s[i]=='/' && topValue()=='/')||(s[i]=='/' && topValue()=='*')||(s[i]=='/' && topValue()=='+')||(s[i]=='/' && topValue()=='-'))
			return true;
		else return false;
	}
	void convert_Postfix()
	{
		stack(s.size());
		for(int i=0;i<size-1;i++)
		{
			if(s[i]>=97 && s[i]<=122)
			{
				s1+=s[i];
			}
			else if(s[i]=='(')
				push(s[i]);
			else if(!precedence())
				push(s[i]);
			else if(precedence())
			{
				s1+=pop();
				push(s[i]);
			}
			else if(s[i]==')')
				{
					while(!topValue()=='(')
						s1+=pop();
					pop();
				}
		}
		while(!isEmpty())
			s+=pop();
	}
	void showpostfix()
	{
		cout<<s1;
	}
	~stack()
	{
		delete[] data;
	}	
};
int main()
{
	stack s;
	s.getinfix();
	cout<<endl;
	s.showinfix();
	cout<<endl;
	s.convert_Postfix();
	s.showpostfix();
	system("pause");
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
	stack(int n)
	{
		size=n;
		data=new char[n];
	}
//...
	void convert_Postfix()
	{
		stack(s.size());
//...
	}

You don't use constructor functions that way. You should change the function to void allocate(int n) or something.

Try adding s1 = ""; before the loop in convert_Postfix()

90
91
92
93
94
			else if(precedence())
			{
				s1+=pop();
				push(s[i]);
			}

I'm not sure, but I think that should be

1
2
3
4
5
6
			else if(precedence())
			{
				while(precedence())
					s1+=pop();
				push(s[i]);
			}


1
2
3
4
	~stack()
	{
		delete[] data;
	}

Actually, since you only allocate the array inside convert_Postfix() and that's basically the only place where you deal with the array or call other functions that deal with the array, maybe you should have delete[] data; at the end of that function instead.

Also, why is line 66 there? for(int i=0;i<size-1;i++) You're only comparing with s[0] so why the need for a loop?

Maybe you should have all the functions private except for getinfix(), showinfix(), and showpostfix(), and have a function call to convert_Postfix() at the end of getinfix().
Last edited on
Topic archived. No new replies allowed.