School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,136 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,923 people online right now. Registration is fast and FREE... Join Now!



INFIX TO POSTFIX

INFIX TO POSTFIX Rate Topic: -----

#4 D.java  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 6
  • Joined: 21-August 07


Dream Kudos: 0

Posted 02 December 2007 - 10: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? 1

#5 skyhawk133  Icon User is online

  • Head DIC Head
  • Icon
  • View blog
  • Group: Webmaster
  • Posts: 17,429
  • Joined: 17-March 01


Dream Kudos: 1650

Expert In: Web Development

Posted 02 December 2007 - 10:31 AM

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

#6 D.java  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 6
  • Joined: 21-August 07


Dream Kudos: 0

Posted 02 December 2007 - 01: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
  • Pip
  • Group: New Members
  • Posts: 1
  • Joined: 17-May 08


Dream Kudos: 0

Posted 17 May 2008 - 12: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



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month