برنامج الالة الحاسبة ب اندرويد
package com.example.kkk; ...........................................
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
boolean clear_screen = true;
boolean operator_state = false ;
boolean insert_state = false;
boolean last_click = false;
float Operand1 = 0f;
float Operand2 = 0f;
float Answer = 0f;
String Operator = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void insert_text(String text){
EditText screen = (EditText) findViewById(R.id.menuesetting);
if (this.clear_screen){
screen.setText("");
this.clear_screen = false;
}
this.insert_state = true;
this.last_click = true;
screen.append(text);
}
public void set_operator(String operator){
EditText screen = (EditText) findViewById(R.id.menuesetting);
if (screen.getText().toString().equals(".")) screen.setText("0");
if (this.insert_state && this.operator_state && this.last_click){
calculator();
}
if (screen.getText().toString().length() > 0 ){
this.Operand1 = Float.parseFloat(screen.getText().toString());
}
this.operator_state = true;
this.clear_screen = true;
this.last_click = false;
if (operator.equals("+")) this.Operator = "+";
else if (operator.equals("-")) this.Operator = "-";
else if (operator.equals("*")) this.Operator = "*";
else if (operator.equals("/")) this.Operator = "/";
else if (operator.equals("√")){
this.Answer = (float) Math.sqrt(Float.parseFloat(screen.getText().toString()));
screen.setText(this.Answer + "");
this.clear_screen = true;
this.Operand1 = 0f;
this.Operand2 = 0f;
this.Operator = "";
this.last_click = true;
this.operator_state = false ;
}else if (operator.equals("d")){
this.Answer = 1 / Float.parseFloat(screen.getText().toString());
screen.setText(this.Answer + "");
this.clear_screen = true;
this.Operand1 = 0f;
this.Operand2 = 0f;
this.Operator = "";
this.last_click = true;
this.operator_state = false ;
}
else if (operator.equals("^")) this.Operator = "^";
else if (operator.equals("%")) this.Operator = "%";
}
public void calculator(){
EditText screen = (EditText) findViewById(R.id.editText1);
if (screen.getText().toString().equals(".")){
screen.setText("0");
}
if (screen.getText().toString().length() > 0){
this.Operand2 = Float.parseFloat(screen.getText().toString());
}
if (this.Operator.equals("+")) {
this.Answer = this.Operand1 + this.Operand2;
} else if (this.Operator.equals("-")){
this.Answer = this.Operand1 - this.Operand2;
} else if (this.Operator.equals("*")){
this.Answer =this.Operand1 * this.Operand2;
} else if (this.Operator.equals("/")){
this.Answer = this.Operand1 / this.Operand2;
}else if (this.Operator.equals("^")){
this.Answer = (float) Math.pow(this.Operand1, this.Operand2);
}else if (this.Operator.equals("%")){
this.Answer = Operand1 % this.Operand2;
}else{
this.Answer = Float.parseFloat(screen.getText().toString());
}
screen.setText(this.Answer + "");
}
public void ButtonClickHandler(View v){
EditText screen = (EditText) findViewById(R.id.editText1);
switch(v.getId()){
case R.id.Button0 : insert_text("0"); break;
case R.id.button1 : insert_text("1"); break;
case R.id.Button2 : insert_text("2"); break;
case R.id.Button3 : insert_text("3"); break;
case R.id.Button4 : insert_text("4"); break;
case R.id.Button5 : insert_text("5"); break;
case R.id.Button6 : insert_text("6"); break;
case R.id.Button7 : insert_text("7"); break;
case R.id.Button8 : insert_text("8"); break;
case R.id.Button9 : insert_text("9"); break;
case R.id.Buttonpoint :
if (!screen.getText().toString().contains(".") || this.operator_state){
insert_text(".");
}
break;
case R.id.Buttonadd : set_operator("+"); break;
case R.id.Buttonsub : set_operator("-"); break;
case R.id.Buttonmulti: set_operator("*"); break;
case R.id.Buttondiv: set_operator("/"); break;
case R.id.Buttonsqr: set_operator("√"); break;
case R.id.Buttonpow: set_operator("^"); break;
case R.id.Buttonmod: set_operator("%"); break;
case R.id.Buttondel:
if(screen.getText().toString().length() > 1){
String screen_new = screen.getText().toString().substring(0, screen.getText().toString().length()-1);
screen.setText(screen_new);
this.clear_screen = false;
}else{
screen.setText("0");
this.clear_screen = true;
}
break;
case R.id.buttonClear:
this.Operand1 = 0f;
this.Operand2 = 0f;
this.Answer = 0f;
this.Operator = "";
this.operator_state = false ;
this.insert_state = false;
this.last_click = false;
this.clear_screen = true;
screen.setText("0");
break;
}
}
}
ليست هناك تعليقات:
إرسال تعليق