3 Replies - 41317 Views - Last Post: 17 May 2008 - 01:29 PM Rate Topic: -----

#4 D.java  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 6
  • Joined: 21-August 07

Re: INFIX TO POSTFIX

Posted 02 December 2007 - 11:28 AM

** STACK APPLICATIONS **

1- convert infix to postfix

2- matching or not matching

3- result the postfix



class Node  {
	public Object data;
	public Node next;
	public Node () {
	   data =' ';  next = null; }
	public Node (Object val) {
	   data = val;  next = null; }
}

public class LinkStack {
	private Node top;
	/*  Creates a new instance of LinkStack */
	public LinkStack() {
		top = null; }
	public boolean empty(){
		return top == null; }
   
	
	public boolean full(){
		return false;
	}
public void push(Object e){
		Node tmp = new Node(e);
		tmp.next = top;
		top = tmp;
	}
public Object pop(){

	Object e =  top.data;
	top = top.next;
	return e;
}
public Object peek(){

	Object e =  top.data;
   
	return e;
}


public void matching(String x)
{	
	LinkStack S=new LinkStack();

	for(int i=0;i<x.length();i++)
	{
		char c=x.charAt(i);
		if(c=='(')
			S.push(c);
		else
		{
			if(c==')')
			if(S.empty())
				System.out.println("NOT MATCHING !!!");
			else
				S.pop();
		}
	}
	if(!S.empty())
		System.out.println("NOT MATCHING !!!");
	else
		System.out.println("MATCHING !!!");
}
public void Evaluation(String x)
{
	
	LinkStack S=new LinkStack();
	for(int i=0;i<x.length();i++)
	{
		char c=x.charAt(i);
		String s="0"+c;
		
		if(c=='+')
		{
			int z=Integer.parseInt((String)S.pop())+Integer.parseInt((String)S.pop());
			S.push(Integer.toString(z));
		}
		else if(c=='*')
		{
			int z=Integer.parseInt((String)S.pop())*Integer.parseInt((String)S.pop());
			S.push(Integer.toString(z));
			
		}
		else if(c=='/')
		{	int u=Integer.parseInt((String)S.pop());
			
			int z=Integer.parseInt((String)S.pop())/u;
			S.push(Integer.toString(z));
			
		}
		else if(c=='-')
		{	int u=Integer.parseInt((String)S.pop());
			int z=Integer.parseInt((String)S.pop())-u;
			S.push(Integer.toString(z));
		}
		else
		S.push(s);
	}
	System.out.println("THE POSTFIX = "+x);
	System.out.println("THE RESULT = "+S.pop());
}
public void postfix(String x)
{
	String output="";
	LinkStack S=new LinkStack();
	for(int i=0;i<x.length();i++)
	{
		char c=x.charAt(i);
		
		if(c==('+')||c==('*')||c==('-')||c==('/'))
			{while(!S.empty() && priority(S.peek())>= priority(c))
				output+=S.pop();
			S.push(c);
			}
		else if(c=='(')
		{
			S.push(c);
		}
		else if(c==')')
		{
			while(!S.peek().equals('('))
					output+=S.pop();
			S.pop();
		}
		else
			output+=c;
	}
	while(!S.empty())
		output+=S.pop();
	System.out.println("THE INFIX = "+x);
	System.out.println("THE POSTFIX = "+output);
}
public int priority(Object x)
{
	if(x.equals('+')||x.equals('-'))
		return 1;
	else if(x.equals('*')||x.equals('/'))
		return 2;
	else
		return 0;
}

public static void main(String args[])
{
	
	
	LinkStack s=new LinkStack();
	s.postfix("A*B+((C+D)*(F-G))");
	System.out.println("------------------------------------------");
	s.matching("A*B+((C+D)*(F-G))");
	System.out.println("------------------------------------------");
	s.Evaluation("23*23+32-*+");
	System.out.println("------------------------------------------");
}
}







Was This Post Helpful? 2

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: INFIX TO POSTFIX

Posted 02 December 2007 - 11:31 AM

What's the problem?
Was This Post Helpful? 0
  • +
  • -

#6 D.java  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 6
  • Joined: 21-August 07

Re: INFIX TO POSTFIX

Posted 02 December 2007 - 02:54 PM

View Postskyhawk133, on 2 Dec, 2007 - 11:31 AM, said:

What's the problem?




nothing this one of my programs.
Was This Post Helpful? 0
  • +
  • -

#10 maomao  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 17-May 08

Re: INFIX TO POSTFIX

Posted 17 May 2008 - 01:29 PM

Your code works perfectly, except for the matching method. When running the method, the output for the matching method will always output the correct output ("matching!" or "not matching"), but then will output another line that always says "matching". To fix this problem, I created a global boolean variable called matching and modified the program accordingly.
	public static boolean matching=true;
	public static void main(String[] args)//method that tests matching  
	{
		LinkStack s=new LinkStack();
			   s.matching("A*B+C+(D*F-G)");
		if(matching==true)
			System.out.println("MATCHING!");
		else
			System.out.println("NOT MATCHING!");
	   
	}//end main method

public void matching(String x)
	{	
		Stack S=new Stack();

		for(int i=0;i<x.length();i++)
		{
			char c=x.charAt(i);
			if(c=='(')
				S.push(c);
			else
			{
				if(c==')')
				if(S.isempty())
					matching=false;
				else
					S.pop();
			}
		}
		if(!S.isempty())
			matching=false;
	}


Was This Post Helpful? 1

Page 1 of 1