Simple Calculator with multiple operations - Andro

2019-04-12 14:37发布

I have created a simple calculator that calculates two inputs (ex. 2 + 2 = 4). What I want to do now is to make the app to calculate multiple operations like (ex. 2 + 2 * 4 - 1 = 15). Can someone help me with my code? Here is my code.

public class MainActivity extends Activity {

public String str ="";

 Character op = 'q';
 int i,num,numtemp;
    EditText showResult;
    String displayStr = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        showResult = (EditText)findViewById(R.id.result_id);


    }
      public void btn1Clicked(View v){
       insert(1);

      }

 public void btn2Clicked(View v){
       insert(2);

      }
      public void btn3Clicked(View v){
       insert(3);

      }
      public void btn4Clicked(View v){
       insert(4);

      }
      public void btn5Clicked(View v){
       insert(5);

      }
      public void btn6Clicked(View v){
       insert(6);
      }
      public void btn7Clicked(View v){
       insert(7);

      }
      public void btn8Clicked(View v){
       insert(8);

      }
      public void btn9Clicked(View v){
       insert(9);

      }

      public void btn0Clicked(View v){
           insert(0);

          }


      public void btnplusClicked(View v){
       perform();
       op = '+';
       displayStr += "+";
      }

 public void btnminusClicked(View v){
     perform();
        op = '-';
        displayStr += "-";    

      }
      public void btndivideClicked(View v){
       perform();
       op = '/';
       displayStr += "/";
      }
      public void btnmultiClicked(View v){
       perform();
       op = '*';
       displayStr += "*";
      }
      public void btnequalClicked(View v){
       calculate();

      }

 public void btnclearClicked(View v){
       reset();
      }
      private void reset() {
  // TODO Auto-generated method stub
   str ="";
   op ='q';
   num = 0;
   numtemp = 0;
   displayStr = "";
   showResult.setText("");
 }
 private void insert(int j) {
    // TODO Auto-generated method stub
       str = str+Integer.toString(j);
       num = Integer.valueOf(str).intValue();

       displayStr += Integer.toString(j); 
       showResult.setText(displayStr);

   }
      private void perform() {
    // TODO Auto-generated method stub
    str = "";
    numtemp = num;
   }
      private void calculate() {
    // TODO Auto-generated method stub
        String first=String.valueOf(numtemp);
     String second=String.valueOf(num);

          if(op == '+')
     num = numtemp+num;
    else if(op == '-')
     num = numtemp-num;
    else if(op == '/')
     num = numtemp/num;
    else if(op == '*')
     num = numtemp*num;
showResult.setText(first+op+second+"="+num);
   }

}

2条回答
你好瞎i
2楼-- · 2019-04-12 15:24

You need to use stack operations for performing this kind of arithmetic evaluations programatically. Please check this link Arithmetic Operations using Stack for the algorithm you need to write.

查看更多
3楼-- · 2019-04-12 15:26
登录 后发表回答