Last active
May 11, 2018 17:22
-
-
Save edenizk/679b9eb40deb26cd9b28dce8bf4ac7dd to your computer and use it in GitHub Desktop.
basic calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.deniz.calculator04; | |
import android.annotation.SuppressLint; | |
import android.media.Image; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ImageButton; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
private Button one, two, three, four, five, six, seven, eight, nine, zero, equal, mult, div, sub, sum, delete; | |
private ImageButton back; | |
private TextView result,data; | |
private EditText input; | |
float choosen1=0,choosen2=0; | |
String choosenone="",choosentwo=""; | |
int check=1; | |
int multcheck=0; | |
String sign = null; | |
int b=1,pass=1,finish=0,zerochoosed=1,equalcounter=0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
one = (Button) findViewById(R.id.one); | |
two = (Button) findViewById(R.id.two); | |
three = (Button) findViewById(R.id.three); | |
four = (Button) findViewById(R.id.four); | |
five = (Button) findViewById(R.id.five); | |
six = (Button) findViewById(R.id.six); | |
seven = (Button) findViewById(R.id.seven); | |
eight = (Button) findViewById(R.id.eight); | |
nine = (Button) findViewById(R.id.nine); | |
zero = (Button) findViewById(R.id.zero); | |
equal = (Button) findViewById(R.id.equal); | |
mult = (Button) findViewById(R.id.mult); | |
div = (Button) findViewById(R.id.div); | |
sub = (Button) findViewById(R.id.sub); | |
sum = (Button) findViewById(R.id.sum); | |
delete = (Button) findViewById(R.id.delete); | |
back = (ImageButton) findViewById(R.id.back); | |
result = (TextView) findViewById(R.id.result); | |
input = (EditText) findViewById(R.id.input); | |
data = (TextView) findViewById(R.id.data); | |
} | |
public void onClick(View v){ | |
if (check==1){ | |
if(equalcounter==1){ | |
equalcounter=0; | |
if (v.getId() == R.id.sum || v.getId() == R.id.sub || v.getId() == R.id.mult || v.getId() == R.id.div) { | |
result.setText(Float.toString(choosen1)); | |
check = 0; | |
sign = setsign(v, sign, choosen1); | |
input.setText(Float.toString(choosen1) + ((Button) v).getText()); | |
} else if (v.getId() == R.id.delete) { | |
input.setText(""); | |
data.setText(""); | |
result.setText("RESULT:"); | |
choosenone = ""; | |
sign = null; | |
choosen1 = 0; | |
check = 1; | |
} | |
else if (v.getId() == R.id.equal) { | |
input.setText(Float.toString(choosen1)); | |
} | |
} | |
else { | |
choosenone = setnum(v, choosenone); | |
result.setText(choosenone); | |
choosen1 = Float.parseFloat(choosenone); | |
input.setText(choosenone); | |
if (v.getId() == R.id.sum || v.getId() == R.id.sub || v.getId() == R.id.mult || v.getId() == R.id.div) { | |
result.setText(Float.toString(choosen1)); | |
check = 0; | |
sign = setsign(v, sign, choosen1); | |
input.setText(Float.toString(choosen1) + ((Button) v).getText()); | |
} else if (v.getId() == R.id.delete) { | |
input.setText(""); | |
data.setText(""); | |
result.setText("RESULT:"); | |
choosenone = ""; | |
sign = null; | |
choosen1 = 0; | |
check = 1; | |
} | |
else if (v.getId() == R.id.equal) { | |
input.setText(Float.toString(choosen1)); | |
} | |
} | |
} | |
else if(check==0) { | |
choosentwo=setnum(v,choosentwo); | |
choosen2 = Float.parseFloat(choosentwo); | |
calculation(sign, choosen1, choosen2, zerochoosed); | |
if(choosen2==0){ | |
zerochoosed=0; | |
} | |
input.setText(input.getText().toString() + ((Button) v).getText()); | |
if(v.getId()==R.id.sum||v.getId() == R.id.sub||v.getId()==R.id.mult||v.getId()==R.id.div) | |
{ | |
choosen1=calculation(sign, choosen1, choosen2, zerochoosed); | |
result.setText(Float.toString(choosen1)); | |
choosen2=0; | |
choosentwo=""; | |
sign=setsign(v,sign,choosen1); | |
} | |
else if(v.getId() == R.id.delete){ | |
input.setText(""); | |
data.setText(""); | |
result.setText("RESULT:"); | |
choosenone=""; | |
sign= null; | |
choosen1=0; | |
check=1; | |
} | |
else if(v.getId() == R.id.equal){ | |
choosen1=calculation(sign, choosen1, choosen2, zerochoosed); | |
choosen2=0; | |
choosentwo=""; | |
input.setText(Float.toString(choosen1)); | |
result.setText(Float.toString(choosen1)); | |
data.setText(Float.toString(choosen1)+sign+Float.toString(choosen2)); | |
check=1; | |
equalcounter=1; | |
} | |
} | |
} | |
public float calculation(String a,float choosen1,float choosen2,int zerochoosed){ | |
switch(a){ | |
case "+": | |
choosen1+=choosen2; | |
result.setText(Float.toString(choosen1)); | |
data.setText(Float.toString(choosen1)+sign+Float.toString(choosen2)); | |
return choosen1; | |
case "-": | |
choosen1-=choosen2; | |
result.setText(Float.toString(choosen1)); | |
return choosen1; | |
case "*": | |
if(choosen2==0 && zerochoosed==1){ | |
choosen2=1; | |
} | |
choosen1*=choosen2; | |
result.setText(Float.toString(choosen1)); | |
return choosen1; | |
case "/": | |
if(choosen2==0 && zerochoosed==1){ | |
choosen2=1; | |
} | |
choosen1/=choosen2; | |
result.setText(Float.toString(choosen1)); | |
return choosen1; | |
} | |
return choosen1; | |
} | |
public String setnum(View v,String choosens){ | |
switch (v.getId()) { | |
case R.id.one: | |
return choosens += "1"; | |
case R.id.two: | |
return choosens += "2"; | |
case R.id.three: | |
return choosens += "3"; | |
case R.id.four: | |
return choosens += "4"; | |
case R.id.five: | |
return choosens += "5"; | |
case R.id.six: | |
return choosens += "6"; | |
case R.id.seven: | |
return choosens += "7"; | |
case R.id.eight: | |
return choosens += "8"; | |
case R.id.nine: | |
return choosens += "9"; | |
case R.id.zero: | |
return choosens += "0"; | |
} | |
return choosens; | |
} | |
public String setsign(View v,String sign,float choosen1){ | |
switch(v.getId()){ | |
case R.id.sum: | |
return sign="+"; | |
case R.id.sub: | |
return sign="-"; | |
case R.id.mult: | |
return sign="*"; | |
case R.id.div: | |
return sign="/"; | |
} | |
return sign; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment