package com.szu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Calculator extends Activity implements OnClickListener {
	private Button add,sub,mult,divid,caculate,clear, back;
	private Button dig0,dig1,dig2,dig3,dig4,dig5,dig6,dig7,dig8,dig9,dim;
	private EditText factor1;
	private String factoronestr,factortwostr;
	double value=0;
	String symbol="a";
	String tem="";
	
	@Override
	protected void onCreate(Bundle saveInstanceState)
	{
		
		super.onCreate(saveInstanceState);
		setContentView(R.layout.calculator_activity);
		
		findView();
		
		setTitle("¼òÒ×¼ÆËãÆ÷");
	}
	private void findView() {
		// TODO Auto-generated method stub
		
		back = (Button) findViewById(R.id.back_button18);
		clear=(Button)findViewById(R.id.clear_button17);
		add=(Button)findViewById(R.id.add);
		sub=(Button)findViewById(R.id.sub);
		mult=(Button)findViewById(R.id.mul);
		divid=(Button)findViewById(R.id.div);
		caculate=(Button)findViewById(R.id.equ);
		
		dig1=(Button)findViewById(R.id.num1);
		dig2=(Button)findViewById(R.id.num2);
		dig3=(Button)findViewById(R.id.num3);
		dig4=(Button)findViewById(R.id.num4);
		dig5=(Button)findViewById(R.id.num5);
		dig6=(Button)findViewById(R.id.num6);
		dig7=(Button)findViewById(R.id.num7);
		dig8=(Button)findViewById(R.id.num8);
		dig9=(Button)findViewById(R.id.num9);
		dig0=(Button)findViewById(R.id.num0);
		dim=(Button)findViewById(R.id.point);
		factor1=(EditText)findViewById(R.id.ans_editText1);
		
		add.setOnClickListener(new addlistener());
		sub.setOnClickListener(new sublistener());
		mult.setOnClickListener(new multlistener());
		divid.setOnClickListener(new dividlistener());
		caculate.setOnClickListener(new listener());
		clear.setOnClickListener(new clearlistener());
		back.setOnClickListener(new backOnClickListener());
		dig1.setOnClickListener(this);
		dig2.setOnClickListener(this);
		dig3.setOnClickListener(this);
		dig4.setOnClickListener(this);
		dig5.setOnClickListener(this);
		dig6.setOnClickListener(this);
		dig7.setOnClickListener(this);
		dig8.setOnClickListener(this);
		dig9.setOnClickListener(this);
		dig0.setOnClickListener(this);
		dim.setOnClickListener(this);
		
	}
	public void onClick(View v)
	{
		if(v.equals(dig3))
		{
			tem=tem+"3";
			factor1.setText(tem);
		}
		if(v.equals(dig1))
		{
			tem=tem+"1";
			factor1.setText(tem);
		}
		if(v.equals(dig2))
		{
			tem=tem+"2";
			factor1.setText(tem);
		}
		if(v.equals(dig4))
		{
			tem=tem+"4";
			factor1.setText(tem);
		}
		if(v.equals(dig5))
		{
			tem=tem+"5";
			factor1.setText(tem);
		}
		if(v.equals(dig6))
		{
			tem=tem+"6";
			factor1.setText(tem);
		}
		if(v.equals(dig7))
		{
			tem=tem+"7";
			factor1.setText(tem);
		}
		if(v.equals(dig8))
		{
			tem=tem+"8";
			factor1.setText(tem);
		}
		if(v.equals(dig9))
		{
			tem=tem+"9";
			factor1.setText(tem);
		}
		if(v.equals(dig0))
		{
			tem=tem+"0";
			factor1.setText(tem);
		}
		if(v.equals(dim))
		{
			tem=tem+".";
			factor1.setText(tem);
		}
	}
	
	public class backOnClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			String displayString = factor1.getText().toString();
			if(displayString.length() > 1)
			{
				factor1.setText(displayString.substring(0, displayString.length()-1));
			}
			else if(displayString.length() == 1)
			{
				factor1.setText("0");
			}
				
		}
		
	}
	
	public class addlistener implements OnClickListener
	{
		public void onClick(View arg0)
		{
			factoronestr=factor1.getText().toString();
			if(factoronestr.equals("")){}
			else
				if(symbol=="a")
				{
					factoronestr=factor1.getText().toString();
					double factoronedouble=Double.parseDouble(factoronestr);
					value=factoronedouble;
					factor1.setText(""+value);
				}else
				if(symbol=="*")
				{
					factoronestr=factor1.getText().toString();
					double factoronedouble=Double.parseDouble(factoronestr);
					value=value*factoronedouble;
					factor1.setText(""+value);
				}
				else
					if(symbol=="-")
					{
						factoronestr=factor1.getText().toString();
						double factoronedouble=Double.parseDouble(factoronestr);
						value=value-factoronedouble;
						factor1.setText(""+value);
					}
					else
						if(symbol=="/")
						{
							factoronestr=factor1.getText().toString();
							double factoronedouble=Double.parseDouble(factoronestr);
							value=value/factoronedouble;
							factor1.setText(""+value);
						}
						else
						{
							factoronestr=factor1.getText().toString();
							double factoronedouble=Double.parseDouble(factoronestr);
							value=factoronedouble+value;
							factor1.setText(""+value);
						}
			symbol="+";
			tem="";
		}
	}
	class sublistener implements OnClickListener
	{
		public void onClick(View arg0)
		{
		factoronestr=factor1.getText().toString();
		if(factoronestr.equals("")){}
		else
			if(symbol=="a")
			{
				factoronestr=factor1.getText().toString();
				double factoronedouble=Double.parseDouble(factoronestr);
				value=factoronedouble;
				factor1.setText(""+value);
			}
			else
				if(symbol=="*")
				{
					factoronestr=factor1.getText().toString();
					double factoronedouble=Double.parseDouble(factoronestr);
					value=value*factoronedouble;
					factor1.setText(""+value);
				}
				else
					if(symbol=="+")
					{
						factoronestr=factor1.getText().toString();
						double factoronedouble=Double.parseDouble(factoronestr);
						value=value+factoronedouble;
						factor1.setText(""+value);
					}
					else
						if(symbol=="/")
						{
							factoronestr=factor1.getText().toString();
							double factoronedouble=Double.parseDouble(factoronestr);
							value=value/factoronedouble;
							factor1.setText(""+value);
						}
						else
						{
							factoronestr=factor1.getText().toString();
							double factoronedouble=Double.parseDouble(factoronestr);
							value=value-factoronedouble;
							factor1.setText(""+value);
						}
				symbol="-";
				tem="";
				}
	 }
	class multlistener implements OnClickListener
	{
		public void onClick(View arg0)
		{
			factoronestr=factor1.getText().toString();
			if(factoronestr.equals("")){}
			else
				if(symbol=="a")
				{
					factoronestr=factor1.getText().toString();
					double factoronedouble=Double.parseDouble(factoronestr);
					value=factoronedouble;
					factor1.setText(""+value);
				}
				else
					if(symbol=="+")
					{
						factoronestr=factor1.getText().toString();
						double factoronedouble=Double.parseDouble(factoronestr);
						value=value+factoronedouble;
						factor1.setText(""+value);
					}
					else
						if(symbol=="-")
						{
							factoronestr=factor1.getText().toString();
							double factoronedouble=Double.parseDouble(factoronestr);
							value=value-factoronedouble;
							factor1.setText(""+value);
						}
						else
							if(symbol=="/")
							{
								factoronestr=factor1.getText().toString();
								double factoronedouble=Double.parseDouble(factoronestr);
								value=value/factoronedouble;
								factor1.setText(""+value);
							}
							else
							{
								factoronestr=factor1.getText().toString();
								double factoronedouble=Double.parseDouble(factoronestr);
								value=factoronedouble*value;
								factor1.setText(""+value);
							}
					symbol="*";
					tem="";
		}
	}
	
	class dividlistener implements OnClickListener
	{
		public void onClick(View arg0)
		{
			factoronestr=factor1.getText().toString();
			if(factoronestr.equals("")){}
			else
				if(symbol=="a")
				{
					factoronestr=factor1.getText().toString();
					double factoronedouble=Double.parseDouble(factoronestr);
					value=factoronedouble;
					factor1.setText(""+value);
				}
				else
					if(symbol=="*")
					{
						factoronestr=factor1.getText().toString();
						double factoronedouble=Double.parseDouble(factoronestr);
						value=value*factoronedouble;
						factor1.setText(""+value);
					}
					else
						if(symbol=="-")
						{
							factoronestr=factor1.getText().toString();
							double factoronedouble=Double.parseDouble(factoronestr);
							value=value-factoronedouble;
							factor1.setText(""+value);
						}
						else
							if(symbol=="+")
							{
								factoronestr=factor1.getText().toString();
								double factoronedouble=Double.parseDouble(factoronestr);
								value=value+factoronedouble;
								factor1.setText(""+value);
							}
							else
							{
								factoronestr=factor1.getText().toString();
								double factoronedouble=Double.parseDouble(factoronestr);
								value=value/factoronedouble;
								factor1.setText(""+value);
							}
				symbol="/";
				tem="";
		}
	}
	class clearlistener implements OnClickListener
	{
		public void onClick(View v)
		{
			factor1.setText(null);
			symbol="a";
			tem="";
		}
	}
		class listener implements OnClickListener
		{
			public void onClick(View arg0)
			{
				factortwostr=factor1.getText().toString();
				double factortwodouble=Double.parseDouble(factortwostr);
				if(symbol=="+")
				{
					value=value+factortwodouble;
					factor1.setText(value+"");
					value=0;
					symbol="a";
				}
				if(symbol=="-")
				{
					value=value-factortwodouble;
					factor1.setText(value+"");
					value=0;
					symbol="a";
				}
				if(symbol=="*")
				{
					value=value*factortwodouble;
					factor1.setText(value+"");
					value=0;
					symbol="a";
				}
				if(symbol=="/")
				{
					value=value/factortwodouble;
					if(value%1==0)
					{
						factor1.setText((int)value+"");
					}
					else
					{
						factor1.setText(value+"");
					}
					value=0;
					symbol="a";
				}
				tem="";
			}
		}
	class sqrlistener implements OnClickListener
	{
		public void onClick(View v)
		{
			factoronestr=factor1.getText().toString();
			if(factoronestr.equals("")){}
			else
			{
				double factoronedouble=Double.parseDouble(factoronestr);
				value=StrictMath.pow(factoronedouble,0.5);
				factor1.setText(""+value);
				value=0;
				symbol="a";
			}
		}
	}

 }
