Java program that converts infix expression into postfix form

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
import java.io.*;
class stack
{
	char stack1[]=new char[20];
	int top;
	void push(char ch)
	{
		top++;
		stack1[top]=ch;
	}
	char pop()
	{
		char ch;
		ch=stack1[top];
		top--;
		return ch;
	}
	int pre(char ch)
	{
		switch(ch)
		{
			case '-':return 1;
			case '+':return 1;
			case '*':return 2;
			case '/':return 2;
		}
		return 0;
	}
	boolean operator(char ch)
	{
		if(ch=='/'||ch=='*'||ch=='+'||ch=='-')
			return true;
		else
			return false;
	}
	boolean isAlpha(char ch)
	{
		if(ch>='a'&&ch<='z'||ch>='0'&&ch=='9')
			return true;
		else
			return false;
	}
	void postfix(String str)
	{
		char output[]=new char[str.length()];
		char ch;
		int p=0,i;
		for(i=0;i<str.length();i++)
		{
			ch=str.charAt(i);	
			if(ch=='(')
			{
				push(ch);
			}
			else if(isAlpha(ch))
			{
				output[p++]=ch;
			}
			else if(operator(ch))
			{
				if(stack1[top]==0||(pre(ch)>pre(stack1[top]))||stack1[top]=='(')
			{
				push(ch);
			}
			}
			else if(pre(ch)<=pre(stack1[top]))
			{
				output[p++]=pop();
				push(ch);
			}
			else if(ch=='(')
			{
				while((ch=pop())!='(')
				{
					output[p++]=ch;
				}
			}
		}
		while(top!=0)
		{
			output[p++]=pop();
		}
		for(int j=0;j<str.length();j++)
		{
			System.out.print(output[j]);	
		}
	}
}
class intopost
{
	public static void main(String[] args)throws Exception
	{
		String s;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		stack b=new stack();
		System.out.println("Enter input string");
		s=br.readLine();
		System.out.println("Input String:"+s);
		System.out.println("Output String:");
		b.postfix(s);
	}
}

Output:

Enter input string
a+b*c
Input String:a+b*c
Output String:
abc*+

Enter input string
a+(b*c)/d
Input String:a+(b*c)/d
Output String:
abc*d/)(+


11 Responses to “Java program that converts infix expression into postfix form”

  1. Please could you repair the code, cause I’m having trouble with the “Open and closed Parenthesis”. when I input an infix with parenthesis in it. the result is wrong .. :(((

    Reply
  2. can i have a running program for infix to postfix and infix to prefix,, also a parenthesis count program, thank you

    Reply
  3. mario maurer

    please teach me how to dougie!!
    please teach me how to dougie!!
    please teach me how to dougie!!
    please teach me how to dougie!!
    please teach me how to dougie!!
    please teach me how to dougie!!
    please teach me how to dougie!!
    please teach me how to dougie!!
    please teach me how to dougie!!

    Reply
  4. justine bieber

    op op op op op oppa gangnam style!!

    heyyyyyyyyyyyyyyyyyyyyyyyyyyyy!!!! sexy lady op op op op op oppa gangnam style!!

    thats my idol!!
    by:psy the gangnam style!!

    Reply
  5. thanks a lot dude… i’ll be very happy if you guide how could i code for postfix to infix in java

    Reply
  6. “top” is undefined.

    “stack1” declaration is constantly being recognized as an illegal start of the expression.

    Reply
  7. Cheap Replica Baume & Mercier Watches

    There are certainly many kinds of celebrities, although I have had virtually no contact with the current crop. The personality structure of the individual is obviously important, as is the micro-environment they reside in. Beyond that, I think big factors to consider are their age, life experience and mentoring environment, if any. There are certainly celebs like Paul Newman (was) who stay grounded and highly functional. But even in Newman’s case there was an edge and reserve that I think were an artifact of the distrust that celebrity breeds. There are also different kinds of celebrity. Jay Leno is a celebrity, but he is not likely to be mistaken for a god, let alone treated like one. Being young and being treated like a god – that’s a recipe for bad behavior and heartbreak. One other factor to consider, which has to be seen first hand to really be appreciated, is the combination of the relative lack of privacy with being “on the job” 24-7. The fatigue most people feel after a day’s work when they come home, shuck their work clothes, and go to the pub, or maybe grab a burger with their family or friends, is pretty much unrelieved in celebrity. There is often constant hypervigilance akin to what you see in PTSD and a pressing awareness that one ill tempered word or out of persona action can be very damaging. That’s a sickening way to live, and if you can’t imagine what it would feel like – then consider yourself very lucky. Cheap Replica Baume & Mercier Watches http://www.cheapwatchonline.com/brands/baume-mercier.html

    Reply

Leave a Reply