본문 바로가기

실시간 운영체제

[실시간 운영체제] 계산기

로그, 사인, 코사인 계산이 포함되어 있는 계산기 코드 입니다.

 

 

package com.example.advanced_calculator_hp2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText myText;
    private TextView notice;

    boolean flag_plus = false;
    boolean flag_minus = false;
    boolean flag_mul = false;
    boolean flag_div = false;

    String Line_1 = "";
    String Line_2 = "";
    String number_1_string = "";

    private RadioGroup Radian_or_Degree;
    private RadioButton Radian, Degree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myText = (EditText) findViewById(R.id.myText);
        notice = (TextView) findViewById(R.id.notice_design);

        myText.setText(Line_1+"\n"+Line_2);

        Radian_or_Degree = (RadioGroup) findViewById(R.id.radian_or_degree);
        Radian = (RadioButton) findViewById(R.id.btn_radian);
        Degree = (RadioButton) findViewById(R.id.btn_degree);
    }

    public void click_0(View view)
    {
        Line_2 = Line_2 + "0";

        myText.setText(Line_1 + "\n"+Line_2);
    }

    public void click_1(View view)
    {
        Line_2 = Line_2 + "1";

        myText.setText(Line_1 + "\n"+Line_2);
    }
    public void click_2(View view)
    {
        Line_2 = Line_2 + "2";

        myText.setText(Line_1 + "\n"+Line_2);
    }
    public void click_3(View view)
    {
        Line_2 = Line_2 + "3";

        myText.setText(Line_1 + "\n"+Line_2);
    }
    public void click_4(View view)
    {
        Line_2 = Line_2 + "4";

        myText.setText(Line_1 + "\n"+Line_2);
    }
    public void click_5(View view)
    {
        Line_2 = Line_2 + "5";

        myText.setText(Line_1 + "\n"+Line_2);
    }
    public void click_6(View view)
    {
        Line_2 = Line_2 + "6";

        myText.setText(Line_1 + "\n"+Line_2);
    }
    public void click_7(View view)
    {
        Line_2 = Line_2 + "7";

        myText.setText(Line_1 + "\n"+Line_2);
    }
    public void click_8(View view)
    {
        Line_2 = Line_2 + "8";

        myText.setText(Line_1 + "\n"+Line_2);
    }
    public void click_9(View view)
    {
        Line_2 = Line_2 + "9";

        myText.setText(Line_1 + "\n"+Line_2);
    }

    public void click_plus_minus (View view)
    {
        if (!Line_2.contains("-"))
        {
            Line_2 = "-"+Line_2;
        }
        else{
            Line_2 = Line_2.substring(1);
        }

        myText.setText(Line_1+"\n"+Line_2);
    }

    public void click_dot(View view) {

        if(!Line_1.contains(".")){
            Line_2 = Line_2 +".";
        }

        myText.setText(Line_1 + "\n"+Line_2);
    }

    public void click_c(View view)
    {
        myText.setText("");
        notice.setText("");

        Line_1 = "";
        Line_2 ="";
        number_1_string = "";

        flag_plus = false;
        flag_minus = false;
        flag_mul = false;
        flag_div = false;

        Radian_or_Degree.clearCheck();
    }

    public void click_del(View view)
    {
        if (Line_2.length()!=0) {
            Line_2 = Line_2.substring(0,Line_2.length()-1);
        }
        myText.setText(Line_1+"\n"+Line_2);
    }

    public void click_plus(View view)
    {
        flag_plus = true;
        flag_minus = false;
        flag_mul = false;
        flag_div = false;

        number_1_string = Line_2;
        Line_1 = number_1_string + "+";
        Line_2 = "";

        myText.setText(Line_1 + "\n" + Line_2);
    }

    public void click_minus(View view)
    {
        flag_plus = false;
        flag_minus = true;
        flag_mul = false;
        flag_div = false;

        number_1_string = Line_2;
        Line_1 = number_1_string + "-";
        Line_2 = "";

        myText.setText(Line_1 + "\n" + Line_2);
    }
    public void click_mul(View view)
    {
        flag_plus = false;
        flag_minus = false;
        flag_mul = true;
        flag_div = false;

        number_1_string = Line_2;
        Line_1 = number_1_string + "*";
        Line_2 = "";

        myText.setText(Line_1 + "\n" + Line_2);
    }
    public void click_div(View view)
    {
        flag_plus = false;
        flag_minus = false;
        flag_mul = false;
        flag_div = true;

        number_1_string = Line_2;
        Line_1 = number_1_string + "/";
        Line_2 = "";

        myText.setText(Line_1 + "\n" + Line_2);
    }

    public  void click_equal (View view)
    {
        float num_1, num_2,result = 0.0f;
        num_1 = Float.parseFloat(number_1_string);
        num_2 = Float.parseFloat(Line_2);

        if (flag_plus){
            result = num_1+num_2;
        }

        else if (flag_minus){
            result = num_1-num_2;
        }

        else if (flag_mul){
            result = num_1*num_2;
        }

        else if (flag_div){
            result = num_1/num_2;
        }

        Line_1 = Line_1 + Line_2 +" = ";
        Line_2 = String.valueOf(result);
        myText.setText(Line_1 + "\n" + Line_2);
    }

    public  void click_sin (View view)
    {
        double num,result = 0.0f;
        if (Line_2.length()!=0) {
            num = Double.parseDouble(Line_2);
            if (Radian.isChecked()){
                result = (double) Math.sin(num);
                Line_2 = String.format("sin(%.2f) = %.6f",num,result);
                myText.setText(Line_2 );
            }
            else if (Degree.isChecked()){
                result = (double) Math.sin(Math.toRadians(num));
                Line_2 = String.format("sin(%.2f) = %.6f",num,result);
                myText.setText(Line_2);
            }
            else{
                notice.setText("Choose Radian or Degree");
            }
        }
    }

    public  void click_cos (View view)
    {
        double num,result = 0.0f;
        if (Line_2.length()!=0) {
            num = Double.parseDouble(Line_2);
            if (Radian.isChecked()){
                result = (double) Math.cos(num);
                Line_2 = String.format("cos(%.2f) = %.6f",num,result);
                myText.setText(Line_2 );
            }
            else if (Degree.isChecked()){
                result = (double) Math.cos(Math.toRadians(num));
                Line_2 = String.format("cos(%.2f) = %.6f",num,result);
                myText.setText(Line_2 );
            }
            else{
                notice.setText("Choose Radian or Degree");
            }
        }
    }

    public void click_square(View view){
        float num,result = 0.0f;
        if(Line_2.length()!=0){
            num = Float.parseFloat(Line_2);
            result = (float)Math.pow((double)num,2.0);
            Line_1 = Line_2 + "^2 = "+ String.valueOf(result);
            Line_2 = "";
            myText.setText(Line_1 + "\n" + Line_2);
        }
    }

    public void click_log(View view){
        float num,result = 0.0f;
        if(Line_2.length()!=0){
            num = Float.parseFloat(Line_2);
            result = (float)Math.log10(num);
            Line_1 = "Log(" + Line_2 +") = " + String.valueOf(result);
            Line_2 = "";
            myText.setText(Line_1 + "\n" + Line_2);
        }
    }

}