Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Tautalogy evaluator help

807600May 18 2007 — edited May 18 2007
What I'm trying to do is I have an expression that entered like
(P --> Q) <--> (P v ~Q) as a regular string. Then the string is parsed to see if the expression is a tautology. I have two case structures to process the operands and their priorities. And public void evaluate() does the parsing and uses a stack.

But right I'm completely lost as to figuring out the best way to do this. Can somebody give me any ideas, or point me in the right direction?

Here what I have so far for code:
import java.util.Stack;


public class TautologyExpress {
	
	public String express;

	
	
	public TautologyExpress(String string)
	{
		express="("+string+")";
		
	}
	
	public void evaluate()
	{
		Stack stack = new Stack();
		
		int a = express.length();
		
		int i;
		
		for(i = 0; i< a; i++)
		{
			
			if(express.charAt(i)== 'P')
			{
				stack.push(express.charAt(i));
					
			}
			
			if(express.charAt(i)== 'Q')
			{
				stack.push(express.charAt(i));
					
			}
			
			
			
			if(express.charAt(i)== '~')
			{
				if(!stack.isEmpty())
				{
					while(Priority((Character)stack.peek()) >= Priority(express.charAt(i)))
						express += stack.pop();
				}
			}
			
		
			
			
			
		}
 
	}
	
	public boolean check(char ch)
	{
		switch(ch)
		{
			case 'P': 
					return true;
			case 'Q':
					return true;
			
		}
		return false;
	}
	
	public boolean Operator(char ch)
	{
		switch(ch)
		{
			case '|': // represents <--->
			case '>':
			case '^':
			case 'v':
			case '~':
			case '(':
			case ')':	
			case '#':	
				
					return true;
			default:
					return false;
			
		}
	}
	
	public int Priority(char ch)
	{
		switch(ch)
		{
			case ')':
			case '#':	
					return 0;
			case '|':
					return 1;
			case '>':
					return 2;
			case 'v':
				    return 3;		
			case '^':
			        return 4;		
			case '~':
			        return 5;		
		    case '(':
		            return 6;			
						
		}
		return -1;
	}
	
	
	
	public static void main(String []args)
	{
		TautologyExpress obj=new TautologyExpress("(P > Q) | (~ P v Q)");
		obj.evaluate();
		
	}


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 15 2007
Added on May 18 2007
3 comments
232 views