package edu.uchicago.cs.Homework1;

/**
 * @author yuhu for hw1
 *
 */


import java.util.*;


public class MyClass {
	
	// Fields	
	
	
	public MyClass()
	{
		
	}
	

	public int start()
	{
		int			firstoperandtype =0; // 0: int; 1: double
		int			secondoperandtype =0; // 0: int; 1: double
		int			operatortype =0; // 0: add; 1: minus; 2: mul; 3: div
		int			firstint =0;
		int			secondint=0;
		double		firstdouble =0.0;
		double		seconddouble=0.0; 
		Integer		firstintob = new Integer(1);
		Integer		secondintob = new Integer(1);
		Double		firstdoubleob = new Double(0.0);
		Double		seconddoubleob = new Double(0.0); 
		String		operator; 
		String		add = new String("+");
		String		minus = new String("-");
		String		mul = new String("*");
		String		div = new String("/");
		int			resultint =0;
		double		resultdouble =0.0;
		

		

	

		
		Scanner In = new Scanner(System.in);
		System.out.println("Please Input Expression in next line, for example 2.2 * 3 ?");
		
		// Get the first operand
		if ( In.hasNextInt())
		{
			firstint = In.nextInt();
			firstintob = new Integer(firstint);
			firstoperandtype = 0;	
		}
		else if (In.hasNextDouble())
		{
			firstdouble = In.nextDouble();
			firstdoubleob = new Double(firstdouble);
			firstoperandtype = 1;	
		}
		else
		{
			System.out.println("The format is wrong. Please try again...");	
			return 0; 			
		}	

		// Get Operator
		operator = In.next(); 
		if ( operator.equalsIgnoreCase(add) ||  operator.equalsIgnoreCase(minus) || operator.equalsIgnoreCase(mul) || operator.equalsIgnoreCase(div))
		{	
			if ( operator.equalsIgnoreCase(add) )
			{
				operatortype = 0;				
			}
			if ( operator.equalsIgnoreCase(minus) )
			{
				operatortype = 1;				
			}
			if ( operator.equalsIgnoreCase(mul) )
			{
				operatortype = 2;				
			}
			if ( operator.equalsIgnoreCase(div) )
			{
				operatortype = 3;				
			}
		}
		else
		{
			System.out.println("The format is wrong. Please try again...");	
			return 0; 	
		}

		// Get the first operand
		if ( In.hasNextInt())
		{
			secondint = In.nextInt();
			secondintob = new Integer(secondint);
			secondoperandtype = 0;	
		}
		else if (In.hasNextDouble())
		{
			seconddouble = In.nextDouble();
			seconddoubleob = new Double(seconddouble);
			secondoperandtype = 1;	
		}
		else
		{
			System.out.println("The format is wrong. Please try again...");	
			return 0; 			
		}	
		
		if (( firstoperandtype == 0) && (secondoperandtype == 0))
		{
			switch (operatortype )
			{
				case 0:
					resultint = firstintob.intValue() + secondintob.intValue(); 
					break;
				case 1:
					resultint = firstintob.intValue() - secondintob.intValue(); 
					break;
				case 2:
					resultint = firstintob.intValue() * secondintob.intValue(); 
					break;
				case 3:
					resultint = firstintob.intValue() / secondintob.intValue(); 
					break;
			}
			System.out.println("The Result is " + resultint);
			return 1;	
		}
		if ( firstoperandtype  == 0)
		{
			firstdoubleob = new Double(firstintob.doubleValue());
		}
		if ( secondoperandtype  == 0)
		{
			seconddoubleob = new Double(secondintob.doubleValue());
		}
		switch (operatortype )
		{
			case 0:
				resultdouble = firstdoubleob.doubleValue() + seconddoubleob.doubleValue(); 
				break;
			case 1:
				resultdouble = firstdoubleob.doubleValue() - seconddoubleob.doubleValue(); 
				break;
			case 2:
				resultdouble = firstdoubleob.doubleValue() * seconddoubleob.doubleValue(); 
				break;
			case 3:
				resultdouble = firstdoubleob.doubleValue() / seconddoubleob.doubleValue(); 
				break;
		}
		System.out.println("The Result is " + resultdouble);
		return 1;	

	}
	

}