

package com.example.calculate;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Calculate extends Activity implements OnClickListener {
Button btn0;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btnAdd;
Button btnSub;
Button btnPoint;
Button btnMul;
Button btnDiv;
Button btnEqu;
Button btnCE;
EditText editText;
String Symbol;
double firnum;
double secnum;
double res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculate);
btn0=(Button)findViewById(R.id.btn0);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn4=(Button)findViewById(R.id.btn4);
btn5=(Button)findViewById(R.id.btn5);
btn6=(Button)findViewById(R.id.btn6);
btn7=(Button)findViewById(R.id.btn7);
btn8=(Button)findViewById(R.id.btn8);
btn9=(Button)findViewById(R.id.btn9);
btnSub=(Button)findViewById(R.id.btnSub);
btnAdd=(Button)findViewById(R.id.btnAdd);
; btnPoint=(Button)findViewById(R.id.btnPoint);
btnMul=(Button)findViewById(R.id.btnMul);
btnDiv=(Button)findViewById(R.id.btnDiv);
btnEqu=(Button)findViewById(R.id.btnEqu);
btnCE=(Button)findViewById(R.id.btnCE);
editText=(EditText)findViewById(R.id.editText);
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnAdd.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnPoint.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnEqu.setOnClickListener(this);
btnCE.setOnClickListener(this);
}
public void onClick(View v){
switch(v.getId()){
case R.id.btn0:
editText.setText(editText.getText()+"0");
break;
case R.id.btn1:
editText.setText(editText.getText()+"1");
break;
case R.id.btn2:
editText.setText(editText.getText()+"2");
break;
case R.id.btn3:
editText.setText(editText.getText()+"3");
break;
case R.id.btn4:
editText.setText(editText.getText()+"4");
break;
case R.id.btn5:
editText.setText(editText.getText()+"5");
break;
case R.id.btn6:
editText.setText(editText.getText()+"6");
break;
case R.id.btn7:
editText.setText(editText.getText()+"7");
break;
case R.id.btn8:
editText.setText(editText.getText()+"8");
break;
case R.id.btn9:
editText.setText(editText.getText()+"9");
break;
case R.id.btnPoint:
editText.setText(editText.getText()+".");
break;
case R.id.btnAdd:
Symbol="+";
firnum=Double.valueOf(editText.getText().toString());
editText.setText("");
break;
case R.id.btnSub:
Symbol="-";
firnum= Double.valueOf(editText.getText().toString());
editText.setText("");
break;
case R.id.btnMul:
Symbol="*";
firnum=Double.valueOf(editText.getText().toString());
editText.setText("");
break;
case R.id.btnDiv:
Symbol="/";
firnum= Double.valueOf(editText.getText().toString());
editText.setText("");
break;
case R.id.btnCE:
editText.setText("");
break;
case R.id.btnEqu:
secnum=Double.valueOf(editText.getText().toString());
if (Symbol=="+"){
res=firnum+secnum;
}
if (Symbol=="-"){
res=firnum-secnum;
}
if (Symbol=="*") {
res = firnum * secnum;
}
if (Symbol=="/") {
res = firnum / secnum;
}
editText.setText(res+" ");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}