This article shares the specific code for Android to implement a simple calculator for your reference. The specific content is as follows
// Date: 2014/9/26 // First, people's input habit is an infix expression. For ease of calculation, the program converts the infix expression to the suffix expression //////////////////////////////////////////////////////////////////////////////////////// // The software still exists// 1. The phenomenon of crashing when the input operation number and operator do not match. (For example: only one operand)// 2. Repeat input of two decimal points in a number. (such as 4.5.6)// These two major bugs will be modified in the future //////////////////////////////////////////////////////////////////////////////////////// // There is only one layout. This will also be considered and improved in the future.// If there are other incomplete areas, please correct them. (╯▽╰) package ; import ; //I don't quite understand what it isimport ; import ; import ; import ; import ; import ; import ; import ; // Show copyright informationimport ; // Iteratorimport ; // Two-way list public class MainActivity extends Activity { private EditText content; private EditText operaline; private Double first_num = 0.0; // The first operand private Double sec_num = 0.0; // The second operand //Cannot set bool type variable?????? private static int equal_flg = 0; // The status of the equal sign: FALSE means that the equal sign has not been pressed, and TRUE means that the equal sign has been pressed private double negative_mark = 0; // Positive and negative numbers mark LinkedList<String> Infix = new LinkedList<String>(); // parse the content, that is, the linked list that stores the infix expression LinkedList<String> Suffix = new LinkedList<String>(); // Link list that stores suffix expressions LinkedList<Double> Suffix_Num = new LinkedList<Double>(); // Number link list that stores suffix expressions LinkedList<String> OP = new LinkedList<String>(); // 1. As a linked list for temporary storing operators; 2. Operators that store suffix expressions @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); // Handle the "c" screen clear button function Button clear = (Button) findViewById(); (new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub content = (EditText) findViewById(); (""); equal_flg = 0; first_num = 0.0; sec_num = 0.0; (); (); Suffix_Num.clear(); (""); (().length()); } }); // Handle CE to delete the current operation button function Button current_clear = (Button) findViewById(.current_clear); current_clear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub content = (EditText) findViewById(); (""); equal_flg = 0; first_num = 0.0; sec_num = 0.0; (); (); Suffix_Num.clear(); } }); // Handle backspace button function Button back = (Button) findViewById(); (new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub content = (EditText) findViewById(); String str = ().toString(); if (().length() != 0) ((0, () - 1)); (().length()); //I don't know what it is for now. If the edit box is empty, there is no need to handle it. Verify whether it will cause an exception to exit } }); // Consider whether symbolic keys and numeric keys need to be implemented separately // Implement monitoring of the "0123456789.+-*/()" button; while monitoring of the "=" button is performed in the onCreate() method using anonymous internal class method OnClickListener mylistener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Button num_btn = (Button) v; content = (EditText) findViewById(); // Storage interface calculation formula String content_str = ().toString(); // When there has been an operation, press the "number key" again to clear the last result if((1==equal_flg)/*&&(num_btn.getText().toString().equals("1") ||num_btn.getText().toString().equals("2") ||num_btn.getText().toString().equals("3") ||num_btn.getText().toString().equals("4") ||num_btn.getText().toString().equals("5") ||num_btn.getText().toString().equals("6") ||num_btn.getText().toString().equals("7") ||num_btn.getText().toString().equals("8") ||num_btn.getText().toString().equals("9") ||num_btn.getText().toString().equals("0"))*/) { ("0"); (().length()); (,"It's really executed!", Toast.LENGTH_LONG).show(); equal_flg=0; } // Repeat the processing of inputting operators, brackets or "." if(("+".equals(content_str.substring(content_str.length()-1,content_str.length())) ||"-".equals(content_str.substring(content_str.length()-1,content_str.length())) ||"*".equals(content_str.substring(content_str.length()-1,content_str.length())) ||"/".equals(content_str.substring(content_str.length()-1,content_str.length())) ||".".equals(content_str.substring(content_str.length()-1,content_str.length())))&&((num_btn.getText().toString().equals("+") ||num_btn.getText().toString().equals("-") ||num_btn.getText().toString().equals("*") ||num_btn.getText().toString().equals("/") ||num_btn.getText().toString().equals(".")))) { content_str=content_str.substring(0, content_str.length()-1); //(,"Did your hand shake?", Toast.LENGTH_LONG).show(); } // Repeat the processing of pressing "." /*if(num_btn.getText().toString().equals(".")) { // If the interface only has numbers, change the symbol of the current number if("0"!=content_str) { judge_str = turn_mark(judge_str); (judge_str); (().length()); // (,"GET", Toast.LENGTH_LONG).show(); } }*/ // No splicing is achieved // The current data is 0, and the next time the input is a non-0 number or bracket if("0".equals(().toString()) &&!(num_btn.getText().toString().equals("+")) &&!(num_btn.getText().toString().equals("-")) &&!(num_btn.getText().toString().equals("*")) &&!(num_btn.getText().toString().equals("/")) &&!(num_btn.getText().toString().equals("."))) { // Is cast testing dangerous? ? ? ? ? ? content_str = (String) num_btn.getText(); //(,num_btn.getText(), Toast.LENGTH_LONG).show(); } // Realize splicing // The current data is 0 and the next time it is entered is an operator or dot number else { content_str += num_btn.getText(); //(,content_str, Toast.LENGTH_LONG).show(); } (content_str); (().length()); } }; // No special processing required for numeric and symbol buttons Button num1 = (Button) findViewById(.num_1); (mylistener); Button num2 = (Button) findViewById(.num_2); (mylistener); Button num3 = (Button) findViewById(.num_3); (mylistener); Button num4 = (Button) findViewById(.num_4); (mylistener); Button num5 = (Button) findViewById(.num_5); (mylistener); Button num6 = (Button) findViewById(.num_6); (mylistener); Button num7 = (Button) findViewById(.num_7); (mylistener); Button num8 = (Button) findViewById(.num_8); (mylistener); Button num9 = (Button) findViewById(.num_9); (mylistener); Button point = (Button) findViewById(); (mylistener); Button left = (Button) findViewById(); (mylistener); Button right = (Button) findViewById(); (mylistener); Button plus = (Button) findViewById(); (mylistener); Button subtract = (Button) findViewById(); (mylistener); Button multiply = (Button) findViewById(); (mylistener); Button divide = (Button) findViewById(); (mylistener); // Processing of button 0 Button num0 = (Button) findViewById(.num_0); (new OnClickListener() { @Override public void onClick(View v) { content = (EditText) findViewById(); // The value in the current edit box is 0 if ("0".equals(().toString())||""== ().toString()) { ("0"); } // There is already non-0 data in the edit box else { String str = ().toString(); str += "0"; (str); } (().length()); // (,"GET", Toast.LENGTH_LONG).show(); } }); // Handling of "-/+" Button mark_sign = (Button) findViewById(.mark_sign); mark_sign.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { content = (EditText) findViewById(); String judge_str = ().toString(); // If the interface only has numbers, change the symbol of the current number if("0"!=judge_str&&((-1==judge_str.indexOf("+")) ||(-1==judge_str.indexOf("-"))) ||(-1==judge_str.indexOf("*")) ||(-1==judge_str.indexOf("/")) ||(-1==judge_str.indexOf("(")) ||(-1==judge_str.indexOf(")"))) { judge_str = turn_mark(judge_str); (judge_str); (().length()); // (,"GET", Toast.LENGTH_LONG).show(); } } }); // Implement the function of "=" button Button equal = (Button) findViewById(); (new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub content = (EditText) findViewById(); operaline = (EditText) findViewById(); // str_Infix is the infix expression to be converted String str_Infix = ().toString(); // Friendly interface prompt processing String equate = str_Infix; equate += "="; (equate); (().length()); // Call Analysis function to parse content (str_Infix); (Infix); // So far, the infix expression has been stored in the Infix linked list Iterator<String> it = (); while (()) { String tmp_str = (); if (isNum(tmp_str)) { // If it is a number or a decimal point, it will be directly entered into the Suffix linked list; (tmp_str); } // If it is not a number or a decimal point; else { int OP_level = () ? 0 : getLevel(()); // tmp_str is higher than the top operator of OP and enters OP if (getLevel(tmp_str) > OP_level) { (tmp_str); } // tmp_str is lower than the top operator of OP else { // tmp_str is ")", then the OP is out until it encounters "(" if (getLevel(tmp_str) == -1) { String temp_OP = (); while (getLevel(temp_OP) != -2) { (temp_OP); temp_OP = (); } } // tmp_str is "(", then it will be directly entered into the OP else if (getLevel(tmp_str) == -2) { (tmp_str); } // tmp_str is lower than OP_level and is not "(" ")", // Then the OP is out of the stack until the OP is empty or tmp_str is higher than the OP_level else { String str2 = (); while (getLevel(str2) >= OP_level) { (str2); if (()) { break; } str2 = (); } (tmp_str); } } } } ();// Clear the Infix link list // The remaining elements in the OP come out of the OP and enter Suffix while (!()) { (()); } (Suffix); // So far, the infix expressions have been converted into the suffix expression Suffix // Calculation process of suffix expressions? ? ? When no operand is specified, the default is 0 while (!(())) { String count_str = (); if (isOP(count_str)) { char compare_ch = count_str.charAt(0); first_num = Suffix_Num.removeLast(); sec_num = Suffix_Num.removeLast(); switch (compare_ch) { case '*': Suffix_Num.addLast(sec_num * first_num); break; case '/': // Test the order of divisors and divisors if (first_num != 0) { Suffix_Num.addLast(sec_num / first_num); break; } else { content = (EditText) findViewById(); // ????? Cannot edit the box anymore, set the prompt ("∞"); (().length()); } case '+': Suffix_Num.addLast(sec_num + first_num); break; case '-': Suffix_Num.addLast(sec_num - first_num); break; } } else { Suffix_Num.addLast((count_str)); } } // So far, the result obtained is in the Suffix_Num list // The logic of this part is result. Stores the data retrieved from the Suffix_Num list, and displays it on the interface after processing. Double result=Suffix_Num.removeFirst(); String res_str=(result); if("0".equals(res_str.substring(res_str.length()-1,res_str.length()))) { if(".".equals(res_str.substring(res_str.length()-2,res_str.length()-1))) { res_str=res_str.substring(0,res_str.length()-2); //(,res_str, Toast.LENGTH_LONG).show(); }; }; (res_str); (().length()); equal_flg = 1; if("-".equals(res_str.substring(0,1))) res_str=turn_mark(res_str); } }); // Clear operands???????android has an automatic release mechanism? first_num = 0.0; sec_num = 0.0; (); (); Suffix_Num.clear(); } // Custom isNum() method to detect whether the element is numeric public boolean isNum(String str) { int num = 0; for (int i = 0; i < (); i++) { String strr = (i, i + 1); if (("0") || ("1") || ("2") || ("3") || ("4") || ("5") || ("6") || ("7") || ("8") || ("9") || (".")) num = num + 1; } if (num == ()) return true; else return false; } // Custom isOP() method to detect whether the elements of the Suffix list are operators public boolean isOP(String strr) { if (("+") || ("-") || ("*") || ("/")) return true; else return false; } // Define the operator's level public int getLevel(String str) { if (("*") || ("/")) { return 2; } else if (("+") || ("-")) { return 1; } else if (("(")) { return -2; } else if ((")")) { return -1; } else { return 0; } } // Change the positive and negative signs public String turn_mark(String str) { String temp = "("; temp += "-"; temp += str; temp += ")"; str = temp; return str; } // Implement the content of the edit box separately stored by numbers and operators public void Analysis(String str) { String sub = ""; for (int i = 0; i < (); i++) { // Use substring to traverse the array to be parsed String strr = (i, i + 1); if (isNum(strr)) { sub += strr; } else { if (sub != "") { (sub); // First sub into Infix sub = ""; // Clear the sub } (strr); // "+-*/" "(" ")" will enter the Infix table directly } } // ? ? ? ? ? Test whether the IF below for is because the loop cannot determine the last number if (isNum((() - 1))) { (sub); // First sub into Infix sub = ""; // Clear the sub } } @Override public boolean onCreateOptionsMenu(Menu menu) { /* * // Inflate the menu; this adds items to the action bar if it is * present. getMenuInflater().inflate(, menu); */ (0, 1, 1, "quit"); (0, 2, 2, "about"); (0, 3, 3, "help"); return (menu); /* return true; */ } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub //finish(); if (() == 2) { // Use Toast to display prompt information (,"Author:Yi Contact:cdch@", Toast.LENGTH_LONG).show(); } if (() == 3) { // Use Toast to display prompt information (,"Applicable to general arithmetic operations!", Toast.LENGTH_LONG).show(); } return (item); }; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.