Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ispam.mark_06;
- import android.app.Activity;
- import android.graphics.Color;
- import android.graphics.drawable.ColorDrawable;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.annotation.ColorInt;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.ActionBar;
- import android.support.v7.app.AppCompatActivity;
- import android.text.Html;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import static android.R.attr.editorExtras;
- import static android.R.attr.value;
- public class Calculator extends AppCompatActivity {
- Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonAdd, buttonSub, btnAns, buttonDivision, buttonMul,
- buttonPow, backs, button10, buttonC, buttonEqual, buttonSin, buttonCos, buttonTg, buttonLog;
- EditText edt1 , edt2;
- TextView tv, tvz;
- float mValueOne, mValueTwo;
- String ans = "";
- boolean mAddition, mSubtract, mMultiplication, mPower, mDivision;
- boolean f1 , f2 , f3 , f4 , f5 , f6 , f7 , f8 , f9 , f0 , fans = true;
- boolean xAdd , xSub , xMult , xPow , xDiv;
- enum Operator {PLUS, MINUS, DIV, MULTI};
- ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_calculator);
- getSupportActionBar().hide();
- //getSupportActionBar().setTitle(Html.fromHtml("<font color='#000000'>Calculator</font>"));
- button0 = (Button) findViewById(R.id.button0);
- button1 = (Button) findViewById(R.id.button1);
- button2 = (Button) findViewById(R.id.button2);
- button3 = (Button) findViewById(R.id.button3);
- button4 = (Button) findViewById(R.id.button4);
- button5 = (Button) findViewById(R.id.button5);
- button6 = (Button) findViewById(R.id.button6);
- button7 = (Button) findViewById(R.id.button7);
- button8 = (Button) findViewById(R.id.button8);
- button9 = (Button) findViewById(R.id.button9);
- button10 = (Button) findViewById(R.id.button10);
- buttonAdd = (Button) findViewById(R.id.buttonadd);
- buttonSub = (Button) findViewById(R.id.buttonsub);
- buttonMul = (Button) findViewById(R.id.buttonmul);
- buttonPow = (Button) findViewById(R.id.buttonPower);
- buttonLog = (Button) findViewById(R.id.btnLog);
- buttonSin = (Button) findViewById(R.id.btnsin);
- buttonCos = (Button) findViewById(R.id.btncos);
- buttonTg = (Button) findViewById(R.id.btntg);
- backs = (Button) findViewById(R.id.bs);
- btnAns = (Button) findViewById(R.id.btnans);
- buttonDivision = (Button) findViewById(R.id.buttondiv);
- buttonC = (Button) findViewById(R.id.buttonC);
- buttonEqual = (Button) findViewById(R.id.buttoneql);
- edt1 = (EditText) findViewById(R.id.edt1);
- edt2 = (EditText) findViewById(R.id.edt2);
- tv = (TextView) findViewById(R.id.textView);
- tvz = (TextView) findViewById(R.id.textViewz);
- tv.setText("");
- tvz.setText("");
- edt1.setText("");
- fans = false;
- btnAns.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(fans = true){
- edt1.setText(tv.getText().toString() + ans);
- tv.setText(tv.getText().toString() + ans);
- fans = false;}
- else
- return;
- }
- });
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "1");
- tv.setText(tv.getText() + "1");
- float z = Float.parseFloat(edt1.getText().toString());
- if (mAddition) {
- f2 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- }
- });
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "2");
- tv.setText(tv.getText() + "2");
- float z = Float.parseFloat(edt1.getText().toString());
- if (mAddition) {
- f1 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- }
- });
- button3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "3");
- tv.setText(tv.getText() + "3");
- float z = Float.parseFloat(edt1.getText().toString());
- int i = 0;
- if (mAddition) {
- f3 = false;
- String s = tv.getText().toString();
- String x = "";
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- if (mSubtract) {
- f3 = false;
- String s = tv.getText().toString();
- String x = "";
- while (i < s.length()) {
- if (s.charAt(i) != '-') {
- x = x + s.charAt(i) + "";
- } else {
- if (isDouble(x)){
- float m = Float.parseFloat(x);
- z = z - m;
- x = "";
- }
- }
- i++;
- }
- tvz.setText(z + "");
- xSub = false;
- }
- }
- });
- button4.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "4");
- tv.setText(tv.getText() + "4");
- float z = Float.parseFloat(edt1.getText().toString());
- if (mAddition) {
- f4 = false;
- String s = tv.getText().toString();
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- if (mSubtract) {
- f4 = false;
- String s = tv.getText().toString();
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '-') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z - m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xSub = false;
- }
- }
- });
- button5.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "5");
- tv.setText(tv.getText() + "5");
- float z = Float.parseFloat(edt1.getText().toString());
- if (mAddition) {
- f5 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- if (mSubtract) {
- f5 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '-') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z - m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xSub = false;
- }
- }
- });
- button6.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "6");
- tv.setText(tv.getText() + "6");
- float z = Float.parseFloat(edt1.getText().toString());
- if (mAddition) {
- f6 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- }
- });
- button7.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "7");
- tv.setText(tv.getText() + "7");
- float z = Float.parseFloat(edt1.getText().toString());
- if (mAddition) {
- f7 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- }
- });
- button8.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "8");
- tv.setText(tv.getText() + "8");
- float z = Float.parseFloat(edt1.getText().toString());
- if (mAddition) {
- f8 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- }
- });
- button9.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "9");
- tv.setText(tv.getText() + "9");
- float z = Float.parseFloat(edt1.getText().toString());
- /*if (mAddition) {
- f9 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }*/
- }
- });
- button0.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + "0");
- tv.setText(tv.getText() + "0");
- float z = Float.parseFloat(edt1.getText().toString());
- if (mAddition) {
- f0 = false;
- String s = tv.getText().toString();
- String sum = "";
- String x = "";
- int i = 0;
- while (i < s.length()) {
- if (s.charAt(i) != '+') {
- x = x + s.charAt(i) + "";
- } else {
- float m = Float.parseFloat(x);
- z = z + m;
- x = "";
- }
- i++;
- }
- tvz.setText(z + "");
- xAdd = false;
- }
- }
- });
- backs.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String s1 = edt1.getText().toString();
- String s2 = tv.getText().toString();
- if (s1.length() >= 1) {
- s1 = s1.substring(0, s1.length() - 1);
- edt1.setText(s1);
- }
- if (s2.length() >= 1) {
- if (s2.charAt(s2.length() - 1) == '-' || s2.charAt(s2.length() - 1) == '+' || s2.charAt(s2.length() - 1) == '^' || s2.charAt(s2.length() - 1) == '/' || s2.charAt(s2.length() - 1) == '*') {
- mAddition = false;
- mPower = false;
- mMultiplication = false;
- mDivision = false;
- mSubtract = false;
- edt1.setText(s2);
- }
- s2 = s2.substring(0, s2.length() - 1);
- tv.setText(s2);
- if (isDouble(s2)) {
- edt1.setText(s2);
- }
- }
- }
- });
- buttonAdd.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = edt1.getText().toString();
- if (isDouble(str)) {
- if (edt1 == null) {
- edt1.setText("");
- } else {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- tv.setText(tv.getText() + "+");
- mAddition = true;
- edt1.setText(null);
- }
- f0 = false;
- f1 = false;
- f2 = false;
- f3 = false;
- f4 = false;
- f5 = false;
- f6 = false;
- f7 = false;
- f8 = false;
- f9 = false;
- fans = false;
- } else return;
- }
- });
- buttonSub.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = edt1.getText().toString();
- if (isDouble(str)) {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- mSubtract = true;
- tv.setText(tv.getText() + "-");
- edt1.setText(null);
- } else
- return;
- }
- });
- buttonPow.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = edt1.getText().toString();
- if (isDouble(str)) {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- mPower = true;
- tv.setText(tv.getText() + "^");
- edt1.setText(null);
- } else
- return;
- }
- });
- buttonLog.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = tv.getText().toString();
- if (isDouble(str)) {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- tv.setText(tv.getText().toString());
- double r = Math.log10(mValueOne);
- edt1.setText(r + "");
- tvz.setText(r + "");
- } else
- return;
- }
- });
- buttonMul.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = edt1.getText().toString();
- if (isDouble(str)) {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- mMultiplication = true;
- edt1.setText(null);
- tv.setText(tv.getText() + "*");
- } else
- return;
- }
- });
- buttonSin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = edt1.getText().toString();
- if (isDouble(str)) {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- double r = Math.sin(Math.toRadians(mValueOne));
- tvz.setText(r + "");
- ans = r + "";
- edt1.setText("");
- tv.setText("");
- }
- }
- });
- buttonCos.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = tv.getText().toString();
- if (isDouble(str)) {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- double r = Math.cos(Math.toRadians(mValueOne));
- tvz.setText(r + "");
- ans = r + "";
- edt1.setText("");
- tv.setText("");
- }
- }
- });
- buttonTg.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = tv.getText().toString();
- if (isDouble(str)) {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- if (mValueOne == 90) {
- tvz.setText("DOSN'T EXIST");
- return;
- } else {
- double r = Math.tan(Math.toRadians(mValueOne));
- tvz.setText(r + "");
- ans = r + "";
- edt1.setText("");
- tv.setText("");
- }
- }
- }
- });
- buttonDivision.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- String str = tv.getText().toString();
- if (isDouble(str)) {
- mValueOne = Float.parseFloat(edt1.getText() + "");
- mDivision = true;
- edt1.setText(null);
- tv.setText(tv.getText() + "/");
- } else
- return;
- }
- });
- buttonEqual.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(edt1.getText().toString())) {
- return;
- }
- mValueTwo = Float.parseFloat(edt1.getText() + "");
- if (mAddition == true && isDouble(mValueOne + "")) {
- ans = mValueOne + mValueTwo + "";
- tvz.setText(mValueOne + mValueTwo + "");
- mAddition = false;
- } else if (mSubtract == true) {
- ans = mValueOne - mValueTwo + "";
- tvz.setText(mValueOne - mValueTwo + "");
- mSubtract = false;
- }
- if (mMultiplication == true) {
- ans = mValueOne * mValueTwo + "";
- tvz.setText(mValueOne * mValueTwo + "");
- mMultiplication = false;
- }
- if (mDivision == true) {
- ans = mValueOne / mValueTwo + "";
- tvz.setText(mValueOne / mValueTwo + "");
- mDivision = false;
- }
- if (mPower == true) {
- ans = Math.pow(mValueOne, mValueTwo) + "";
- tvz.setText(Math.pow(mValueOne, mValueTwo) + "");
- mPower = false;
- }
- fans = true;
- edt1.setText("");
- tv.setText("");
- //getActionBar().setTitle(tvz.getText().toString());
- }
- });
- buttonC.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText("");
- tv.setText("");
- tvz.setText("");
- }
- });
- button10.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- edt1.setText(edt1.getText() + ".");
- tv.setText(tv.getText() + ".");
- }
- });
- }
- public boolean isDouble(String str) {
- try {
- Double.parseDouble(str);
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement