Android calculator attempt, android Calculator

Source: Internet
Author: User

Android calculator attempt, android Calculator

After learning Android for a while, I have been reading it all the time and have no attempt. Recently I learned how to use gridview with simpleadpter, so I want to practice it, you chose to write a simple calculator.

It only contains the +-*/four basic operations, but even such a simple small program has been done for more than half a day. This is also because the first write is relatively casual, there is no flow chart, and the method is well encapsulated. The result is particularly uncomfortable when it is optimized and debugged later, and the last one is deleted again in anger, this time, I focused on the overall process and the encapsulation of methods. The logic is much clearer. In a short time, I will debug and implement the expected functions. It can be said that this is a good attempt!

Therefore, we should try to draw a flowchart before writing the code, and then pay attention to the code specifications and encapsulation to avoid the excessive length of the text.

The following is the Mainactivity code.

[Code name = java]

Package com. fantasy. calculator;

Import java. util. ArrayList;
Import java. util. HashMap;
Import android. annotation. SuppressLint;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. GridView;
Import android. widget. SimpleAdapter;
Import android. widget. TextView;

Public class MainActivity extends Activity implements OnItemClickListener {
Private GridView gv; // keyboard
Private TextView display; // Screen
Private float a, B, sum = 0; // a, B, is the number of two operations, sum is the calculation result
Private int operator = 0; // operator type identifier
Private int flag = 0; // mark by the number of times "="
String sa = "", temp = ""; // receives the input String
/*
* Main Interface image resources
*/
Int [] image = {R. drawable. number7, R. drawable. number8, R. drawable. number9,
R. drawable. division, R. drawable. number4, R. drawable. number5,
R. drawable. number6, R. drawable. multiply, R. drawable. number1,
R. drawable. number2, R. drawable. number3, R. drawable. minus,
R. drawable. number0, R. drawable. dot, R. drawable. equals,
R. drawable. add, R. drawable. clear, R. drawable. off };

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Display = (TextView) findViewById (R. id. textView1 );
Gv = (GridView) findViewById (R. id. gridView1 );
Gv. setAdapter (getAdapter (image ));
Gv. setOnItemClickListener (this );

}

/*
* Get the adapted simpleadapter for int [] res
*/
Private SimpleAdapter getAdapter (int [] res ){

ArrayList <HashMap <String, Object> data = new ArrayList <HashMap <String, Object> ();
For (int I = 0; I <res. length; I ++ ){
HashMap <String, Object> hashMap = new HashMap <String, Object> ();
HashMap. put ("button", res [I]);
Data. add (hashMap );
}
SimpleAdapter myAdapter = new SimpleAdapter (this, data,
R. layout. buttonitem, new String [] {"button "},
New int [] {R. id. ib_keyboard });
Return myAdapter;

}


@ Override
Public void onItemClick (AdapterView <?> Parent, View view, int position,
Long id ){
// TODO Auto-generated method stub

// If the number key is pressed, use temp to receive and display
If (position! = 3 & position! = 7 & position! = 11 & position! = 15
& Position! = 14 & position! = 16 & position! = 17 ){

Temp = getResources (). getStringArray (R. array. text) [position];
Sa + = temp;
B = Float. parseFloat (sa );
Display. setText (sa );
}
/*
* Processing logic for pressing operators and function keys
*/
Switch (position ){
Case 3: // press the division number
EqualFlag = 0;
Display. setText ("\\");
// DividePressed ();
OperatorPressed (4 );
Break;
Case 7: // press the multiplication number
EqualFlag = 0;
Display. setText ("*");
// MultiplyPressed ();
OperatorPressed (3 );

Break;
Case 11: // press the minus sign
EqualFlag = 0;

Display. setText ("-");
// SubPressed ();
OperatorPressed (2 );
Break;
Case 15: // press the plus sign
EqualFlag = 0;

Display. setText ("+ ");
// AddPressed ();
OperatorPressed (1 );
Break;
Case 14: // press the equal sign
EqualFlag ++;
ShowResult ();
Break;
Case 16: // clear Press
Clear ();
Break;
Case 17: // exit and press
This. finish ();
Break;

Default:
Break;
}
}

Private void addPressed () {// plus sign press processing logic
// TODO Auto-generated method stub
If (a = 0 ){
A = B;
B = 0;
Sa = "";
Temp = "";
Operator = 1;
} Else if (operator! = 1 ){

ShowResult ();
A = sum;
Operator = 1;
} Else if (operator = 1 ){

ShowResult ();
A = sum;

}

}

Private void subPressed (){
// TODO Auto-generated method stub
If (a = 0 ){
A = B;
B = 0;
Sa = "";
Temp = "";
Operator = 2;
} Else if (operator! = 2 ){

ShowResult ();
A = sum;
Operator = 2;
} Else if (operator = 2 ){

ShowResult ();
A = sum;

}

}

Private void multiplyPressed (){
// TODO Auto-generated method stub
If (a = 0 ){
A = B;
B = 0;
Sa = "";
Temp = "";
Operator = 3;
} Else if (operator! = 3 ){
ShowResult ();
A = sum;
Operator = 3;
} Else if (operator = 3 ){

ShowResult ();
A = sum;
}

}

Private void dividePressed (){
// TODO Auto-generated method stub
If (a = 0 ){
A = B;
B = 0;
Sa = "";
Temp = "";
Operator = 4;
} Else if (operator! = 4 ){
ShowResult ();
A = sum;
Operator = 4;
} Else if (operator = 4 ){

ShowResult ();
A = sum;

}
}

Private void operatorPressed (int sign ){
// TODO Auto-generated method stub
If (a = 0 ){
A = B;
B = 0;
Sa = "";
Temp = "";
Operator = sign;
} Else if (operator! = Sign ){
ShowResult ();
A = sum;
Operator = sign;
} Else if (operator = sign ){

ShowResult ();
A = sum;

}
}

Private void showResult (){
// TODO Auto-generated method stub
Switch (operator) {// determine the Operation Type
Case 4: // if Division
If (distinct flag <= 1) {// if it is the first time, follow the same equal sign
Sum = divide (a, B );
Display. setText (a + "\" + B + "=" + sum );
Sa = "";
Temp = "";
} Else if (repeated flag> = 2) {// if it is a continuous equal sign, the same below
A = sum;
Sum = divide (a, B );
Display. setText (a + "\" + B + "=" + sum );
}

Break;
Case 3:
If (repeated flag <= 1 ){
Sum = multiply (a, B );
Display. setText (a + "*" + B + "=" + sum );
Sa = "";
Temp = "";
} Else if (repeated flag> = 2 ){
A = sum;
Sum = multiply (a, B );
Display. setText (a + "*" + B + "=" + sum );
}

Break;
Case 2:
If (repeated flag <= 1 ){
Sum = sub (a, B );
Display. setText (a + "-" + B + "=" + sum );
Sa = "";
Temp = "";
} Else if (repeated flag> = 2 ){
A = sum;
Sum = sub (a, B );
Display. setText (a + "-" + B + "=" + sum );
}

Break;
Case 1:
If (repeated flag <= 1 ){
Sum = add (a, B );
Display. setText (a + "+" + B + "=" + sum );
Sa = "";
Temp = "";
} Else if (repeated flag> = 2 ){
A = sum;
Sum = add (a, B );
Display. setText (a + "+" + B + "=" + sum );
}

Break;

Default:
Break;
}

}

// Clear the screen and Cache
Private void clear (){
// TODO Auto-generated method stub
A = B = sum = 0;
Operator = 0;
EqualFlag = 0;
Sa = "";
Display. setText ("");
}

// Addition
Private float add (float x, float y ){
Return x + y;
}

// Subtraction
Private float sub (float x, float y ){
Return x-y;
}

// Multiplication
Private float multiply (float x, float y ){
Return x * y;
}

// Division
Private float divide (float x, float y ){
If (y! = 0 ){
Return (x/y );
} Else {
Return Float. NaN;
}
}

}

[/Code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.