当前位置:   article > 正文

基于Android实现计算器_android 计算器源代码 github

android 计算器源代码 github

最近发现我在CSDN里面分享的一些文件都被提高积分了,有的提高的有点夸张。所以以后我尽量把代码在博客里面贴全。

代码下载

github地址:https://github.com/AtaoistPriest/Calculator

如果积分不够,复制以下代码也可以。当然欢迎积分下载。

这几天时间比较充裕,我用Android实现了一个计算器,以前也实现过,但那个版本功能简单,代码论乱,不易理解,关键是还有BUG。所以,我完善一下。实现计算器的难点主要是,输入算式时的排错和最后根据算术表达式求出数值。因为有括号,运算符优先级等问题,所以还是有点难度的。

计算器的界面如下:

好了,下面开始了。

界面布局文件:activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/Equation"
  9. android:textSize="16sp"
  10. android:hint="输入算式进行计算"
  11. android:layout_gravity="fill"
  12. android:maxLines="4"
  13. android:layout_marginTop="155sp"
  14. android:layout_alignParentTop="true"
  15. android:paddingRight="15sp"
  16. android:layout_columnSpan="4"
  17. android:gravity="right"/>
  18. <TextView
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_below="@+id/Equation"
  22. android:id="@+id/Result"
  23. android:textSize="16sp"
  24. android:hint="结果"
  25. android:layout_gravity="fill"
  26. android:maxLines="1"
  27. android:layout_marginTop="20sp"
  28. android:layout_marginBottom="15sp"
  29. android:paddingRight="15sp"
  30. android:layout_columnSpan="4"
  31. android:gravity="right"/>
  32. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_below="@+id/Result"
  36. android:columnCount="4"
  37. android:layout_gravity="center"
  38. android:layout_marginLeft="13sp"
  39. android:orientation="horizontal">
  40. <Button
  41. android:id="@+id/CE"
  42. android:text="CE"
  43. android:textSize="15sp"
  44. android:layout_gravity="fill"
  45. android:layout_marginTop="35sp"
  46. android:layout_marginRight="5sp"
  47. android:layout_marginBottom="5sp"
  48. />
  49. <Button
  50. android:id="@+id/BACKSPACE"
  51. android:text="BACKSPACE"
  52. android:textSize="15sp"
  53. android:layout_gravity="fill"
  54. android:layout_marginTop="35sp"
  55. android:layout_marginBottom="5sp"
  56. android:layout_marginRight="5sp"
  57. android:layout_columnSpan="2"
  58. />
  59. <Button
  60. android:id="@+id/Remainder"
  61. android:text="%"
  62. android:textSize="15sp"
  63. android:layout_marginTop="35sp"
  64. android:layout_marginBottom="5sp"
  65. />
  66. <Button
  67. android:id="@+id/Involution"
  68. android:text="x^y"
  69. android:textSize="15sp"
  70. android:layout_marginBottom="5sp"
  71. />
  72. <Button
  73. android:id="@+id/LBracket"
  74. android:text="("
  75. android:textSize="15sp"
  76. android:layout_marginBottom="5sp"
  77. />
  78. <Button
  79. android:id="@+id/RBracket"
  80. android:text=")"
  81. android:textSize="15sp"
  82. android:layout_marginBottom="5sp"
  83. />
  84. <Button
  85. android:id="@+id/Factorial"
  86. android:text="X!"
  87. android:textSize="15sp"
  88. android:layout_marginBottom="5sp"
  89. />
  90. <Button
  91. android:id="@+id/Reciprocal"
  92. android:text="1/X"
  93. android:textSize="15sp"
  94. android:layout_marginBottom="5sp"
  95. />
  96. <Button
  97. android:id="@+id/P"
  98. android:text="π"
  99. android:textSize="15sp"
  100. android:layout_marginBottom="5sp"
  101. />
  102. <Button
  103. android:id="@+id/E"
  104. android:text="e"
  105. android:textSize="15sp"
  106. android:layout_marginBottom="5sp"
  107. />
  108. <Button
  109. android:id="@+id/Divide"
  110. android:text="/"
  111. android:textSize="15sp"
  112. android:layout_marginBottom="5sp"
  113. />
  114. <Button
  115. android:id="@+id/N7"
  116. android:text="7"
  117. android:textSize="15sp"
  118. android:layout_marginRight="5sp"
  119. android:layout_marginBottom="5sp"
  120. />
  121. <Button
  122. android:id="@+id/N8"
  123. android:text="8"
  124. android:textSize="15sp"
  125. android:layout_marginRight="5sp"
  126. android:layout_marginBottom="5sp"
  127. />
  128. <Button
  129. android:id="@+id/N9"
  130. android:text="9"
  131. android:textSize="15sp"
  132. android:layout_marginRight="5sp"
  133. android:layout_marginBottom="5sp"
  134. />
  135. <Button
  136. android:id="@+id/Multiply"
  137. android:text="*"
  138. android:textSize="15sp"
  139. android:layout_marginBottom="5sp"
  140. />
  141. <Button
  142. android:id="@+id/N4"
  143. android:text="4"
  144. android:textSize="15sp"
  145. android:layout_marginRight="5sp"
  146. android:layout_marginBottom="5sp"
  147. />
  148. <Button
  149. android:id="@+id/N5"
  150. android:text="5"
  151. android:textSize="15sp"
  152. android:layout_marginRight="5sp"
  153. android:layout_marginBottom="5sp"
  154. />
  155. <Button
  156. android:id="@+id/N6"
  157. android:text="6"
  158. android:textSize="15sp"
  159. android:layout_marginRight="5sp"
  160. android:layout_marginBottom="5sp"
  161. />
  162. <Button
  163. android:id="@+id/Subtract"
  164. android:text="-"
  165. android:textSize="15sp"
  166. android:layout_marginBottom="5sp"
  167. />
  168. <Button
  169. android:id="@+id/N1"
  170. android:text="1"
  171. android:textSize="15sp"
  172. android:layout_marginRight="5sp"
  173. android:layout_marginBottom="5sp"
  174. />
  175. <Button
  176. android:id="@+id/N2"
  177. android:text="2"
  178. android:textSize="15sp"
  179. android:layout_marginRight="5sp"
  180. android:layout_marginBottom="5sp"
  181. />
  182. <Button
  183. android:id="@+id/N3"
  184. android:text="3"
  185. android:textSize="15sp"
  186. android:layout_marginRight="5sp"
  187. android:layout_marginBottom="5sp"
  188. />
  189. <Button
  190. android:id="@+id/Equal"
  191. android:text="="
  192. android:textSize="15sp"
  193. android:layout_gravity="fill_vertical"
  194. android:layout_height="wrap_content"
  195. android:layout_width="wrap_content"
  196. android:layout_rowSpan="2"
  197. />
  198. <Button
  199. android:id="@+id/Zero"
  200. android:text="0"
  201. android:textSize="15sp"
  202. android:layout_marginRight="5sp"
  203. />
  204. <Button
  205. android:id="@+id/Point"
  206. android:text="."
  207. android:textSize="15sp"
  208. android:layout_marginRight="5sp"
  209. />
  210. <Button
  211. android:id="@+id/Add"
  212. android:text="+"
  213. android:textSize="15sp"
  214. android:layout_marginRight="5sp"
  215. />
  216. </GridLayout>
  217. </RelativeLayout>

接下来是程序入口MainActivity.java

  1. package com.example.ffy.orginal;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.TextView;
  7. public class MainActivity extends Activity implements View.OnClickListener {
  8. private TextView equationText,resultText;
  9. private Button buttons[] = new Button[26];
  10. private CheckInput checkInput;
  11. private CounterByEquation counterByEquation;
  12. //布局中所有按钮的ID
  13. private int buttonIds[] = new int[]{
  14. R.id.CE,R.id.BACKSPACE,R.id.Remainder,R.id.Involution,R.id.LBracket,R.id.RBracket,R.id.Factorial,
  15. R.id.Reciprocal,R.id.P,R.id.E, R.id.Divide,R.id.N7,R.id.N8,R.id.N9,R.id.Multiply,R.id.N4,
  16. R.id.N5,R.id.N6,R.id.Subtract,R.id.N1,R.id.N2,R.id.N3,R.id.Equal,R.id.Zero,R.id.Point, R.id.Add
  17. };
  18. //等式
  19. private String equation = "";
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. init(); //初始化控件
  25. }
  26. /*
  27. * 点击计算器键盘
  28. *
  29. * */
  30. @Override
  31. public void onClick(View v) {
  32. int id = v.getId();
  33. Button button = (Button)findViewById(id);
  34. String text = button.getText().toString();
  35. switch (id){
  36. /*
  37. * 按数字按钮
  38. * */
  39. case R.id.N1:
  40. case R.id.N2:
  41. case R.id.N3:
  42. case R.id.N4:
  43. case R.id.N5:
  44. case R.id.N6:
  45. case R.id.N7:
  46. case R.id.N8:
  47. case R.id.N9:
  48. case R.id.Zero:
  49. checkInput.setEquation(equation);
  50. if(checkInput.checkNumberInput()){
  51. equation = checkInput.getEquation();
  52. equation += text;
  53. }
  54. break;
  55. /*
  56. * 点击 + - * /
  57. * */
  58. case R.id.Add:
  59. case R.id.Subtract:
  60. case R.id.Multiply:
  61. case R.id.Divide:
  62. checkInput.setEquation(equation);
  63. if(checkInput.checkOperationsInput()){
  64. equation = checkInput.getEquation();
  65. equation += text;
  66. }
  67. break;
  68. /*
  69. * 点击退格键
  70. *
  71. * */
  72. case R.id.BACKSPACE:
  73. checkInput.setEquation(equation);
  74. checkInput.backSpace();
  75. equation = checkInput.getEquation();
  76. break;
  77. /*
  78. * 点击 x^y
  79. * */
  80. case R.id.Involution:
  81. checkInput.setEquation(equation);
  82. if(checkInput.checkInvolutionInput())
  83. equation+="^";
  84. break;
  85. /*
  86. * 倒数
  87. * */
  88. case R.id.Reciprocal:
  89. checkInput.setEquation(equation);
  90. if(checkInput.checkReciprocalInput())
  91. equation+="^(-1)";
  92. break;
  93. /*
  94. * 左括号
  95. * */
  96. case R.id.LBracket:
  97. checkInput.setEquation(equation);
  98. if(checkInput.checkLBracketInput())
  99. equation+="(";
  100. break;
  101. /*
  102. * 右括号
  103. * */
  104. case R.id.RBracket:
  105. checkInput.setEquation(equation);
  106. if(checkInput.checkRBracketInput())
  107. equation+=')';
  108. break;
  109. /*
  110. * 取余
  111. * */
  112. case R.id.Remainder:
  113. checkInput.setEquation(equation);
  114. if(checkInput.checkRemainderInput())
  115. equation+='%';
  116. break;
  117. /*
  118. * 求阶乘
  119. * */
  120. case R.id.Factorial:
  121. checkInput.setEquation(equation);
  122. if(checkInput.checkFactorialInput())
  123. equation+='!';
  124. break;
  125. /*
  126. * 清空按钮
  127. * */
  128. case R.id.CE:
  129. equation = "";
  130. resultText.setText("");
  131. break;
  132. /*
  133. * 小数点按钮
  134. * */
  135. case R.id.Point:
  136. checkInput.setEquation(equation);
  137. if(checkInput.checkPointInput()){
  138. equation = checkInput.getEquation();
  139. equation+=text;
  140. }
  141. break;
  142. /*
  143. * 点击π
  144. * */
  145. case R.id.P:
  146. checkInput.setEquation(equation);
  147. if (checkInput.checkSpecialSymbolBack()){
  148. equation+="π";
  149. }
  150. break;
  151. /*
  152. * 点击e
  153. * */
  154. case R.id.E:
  155. checkInput.setEquation(equation);
  156. if (checkInput.checkSpecialSymbolBack()){
  157. equation+="e";
  158. }
  159. break;
  160. case R.id.Equal:
  161. resultText.setText(new CounterByEquation(equation).solveEquation());
  162. break;
  163. }
  164. equationText.setText(equation);
  165. }
  166. /*
  167. * init()初始化绑定所有的控件,并添加事件
  168. * */
  169. void init(){
  170. int length = buttons.length;
  171. for(int i=0;i<length;i++){
  172. buttons[i] = (Button)findViewById(buttonIds[i]);
  173. buttons[i].setOnClickListener(this);
  174. }
  175. equationText = (TextView)findViewById(R.id.Equation);
  176. resultText = (TextView)findViewById(R.id.Result);
  177. checkInput = new CheckInput();
  178. counterByEquation = new CounterByEquation(equation);
  179. }
  180. }

接下来是输入排错的功能,CheckInput.java.主要是加各种约束。

  1. package com.example.ffy.orginal;
  2. public class CheckInput {
  3. private String equation;
  4. public void setEquation(String equation){
  5. this.equation = equation;
  6. }
  7. public String getEquation(){
  8. return equation;
  9. }
  10. /*
  11. * 检查特殊符号输入问题,π或e,类似1e,2e,3e这样的按照出错处理
  12. * */
  13. boolean checkSpecialSymbolBack(){
  14. int length = equation.length();
  15. if(length==0) return true;
  16. char perChar = equation.charAt(length-1); //获取前一位字符
  17. if(perChar=='+'||perChar=='-'||perChar=='*'||perChar=='/') return true;
  18. return false;
  19. }
  20. /*
  21. * 检查输入数字
  22. * */
  23. boolean checkNumberInput(){
  24. int length = equation.length();
  25. if(length==0) return true;
  26. char perChar = equation.charAt(length-1);
  27. //避免出现 π1,e1,)1现象
  28. if(perChar=='π'||perChar=='e'||perChar==')'||perChar=='!') return false;
  29. //避免出现01,+001,2^00002等现象
  30. if(length==1&&perChar=='0') equation = "";
  31. if(length>1){
  32. char perPerChar = equation.charAt(length-2);
  33. if((perPerChar=='+'||perPerChar=='-'||perPerChar=='*'||perPerChar=='/'||perPerChar=='^')&&perChar=='0'){
  34. equation = equation.substring(0,length-1);
  35. }
  36. }
  37. return true;
  38. }
  39. /*
  40. *小数点输入检查
  41. * */
  42. boolean checkPointInput(){
  43. int length = equation.length();
  44. //自动补0
  45. if(length==0) equation+="0";
  46. char perChar = equation.charAt(length-1);
  47. //自动补0
  48. if(perChar=='+'||perChar=='-'||perChar=='*'||perChar=='/'||perChar=='%') equation+="0";
  49. if(perChar=='π'||perChar=='e'||perChar==')'||perChar=='('||perChar=='^'||perChar=='!') return false;
  50. int pointNum = 0;
  51. for(int i = length-1;i>=0;i--){
  52. char checkChar = equation.charAt(i);
  53. if(checkChar=='.') pointNum++;
  54. if(checkChar=='^') return false;
  55. if(checkChar=='+'||checkChar=='-'||checkChar=='*'||checkChar=='/'||checkChar=='%') break;
  56. }
  57. if(pointNum>0) return false;
  58. return true;
  59. }
  60. /*
  61. * 输入+ - * / 时进行检查,在他们的前面不能有 .和%
  62. * */
  63. boolean checkOperationsInput(){
  64. int length = equation.length();
  65. if(length==0) return false;
  66. char perChar = equation.charAt(length-1);
  67. if(perChar=='%'||perChar=='.'||perChar=='^'||perChar=='!'||perChar=='(') return false;
  68. //替换
  69. if(perChar=='+'||perChar=='-'||perChar=='*'||perChar=='/')
  70. equation = equation.substring(0,length-1);
  71. return true;
  72. }
  73. /*
  74. * 退格处理
  75. * */
  76. void backSpace(){
  77. int length = equation.length();
  78. if(length==0) return;
  79. char charPer = equation.charAt(length-1);
  80. equation = equation.substring(0,length-1);
  81. }
  82. /*
  83. * 检查 x^y
  84. * */
  85. boolean checkInvolutionInput(){
  86. int length = equation.length();
  87. if(length==0) return false;
  88. char perChar = equation.charAt(length-1);
  89. if(perChar!='1'&&perChar!='2'&&perChar!='3'&&perChar!='4'&&perChar!='5'&&perChar!='6'&&perChar!='7'
  90. &&perChar!='8'&&perChar!='9'&&perChar!=')'&&perChar!='e'&&perChar!='π'&&perChar!='0') return false;
  91. //一个式子里只能有 ^
  92. int iNum = 0;
  93. for(int i = length-1;i>0;i--){
  94. char iChar = equation.charAt(i);
  95. if(iChar=='^') iNum++;
  96. if (iChar=='+'||iChar=='-'||iChar=='*'||iChar=='/'||iChar=='%') break;
  97. }
  98. if(iNum>0) return false;
  99. return true;
  100. }
  101. /*
  102. * 倒数
  103. * */
  104. boolean checkReciprocalInput(){
  105. int length = equation.length();
  106. if(length==0) return false;
  107. char perChar = equation.charAt(length-1);
  108. if(perChar!='1'&&perChar!='2'&&perChar!='3'&&perChar!='4'&&perChar!='5'&&perChar!='6'&&perChar!='7'
  109. &&perChar!='8'&&perChar!='9'&&perChar!=')'&&perChar!='0') return false;
  110. //单独一个0不能求倒数
  111. int iNum = 0;
  112. for(int i = length-1;i>=0;i--){
  113. char iChar = equation.charAt(i);
  114. iNum++;
  115. if (iChar=='+'||iChar=='-'||iChar=='*'||iChar=='/'||iChar=='%') break;
  116. }
  117. if(iNum==1&&perChar=='0') return false;
  118. return true;
  119. }
  120. /*
  121. * 输入左括号
  122. * */
  123. boolean checkLBracketInput(){
  124. int length = equation.length();
  125. if(length==0) return true;
  126. char perChar = equation.charAt(length-1);
  127. if(perChar=='+'||perChar=='-'||perChar=='*'||perChar=='/'||perChar=='%'||perChar=='^'||perChar=='(') return true;
  128. return false;
  129. }
  130. /*
  131. * 输入右括号
  132. * */
  133. boolean checkRBracketInput(){
  134. int length = equation.length();
  135. if(length==0) return false;
  136. char perChar = equation.charAt(length-1);
  137. //左右符号要匹配
  138. int lNum = 0,rNum = 0;
  139. for(int i = length-1;i>=0;i--){
  140. char checkChar = equation.charAt(i);
  141. if(checkChar=='(') lNum++;
  142. if(checkChar==')') rNum++;
  143. if(lNum-rNum==1) break;
  144. }
  145. if((lNum-rNum==1)&&(perChar=='1'||perChar=='2'||perChar=='3'||perChar=='4'||perChar=='5'||perChar=='6'||perChar=='7'
  146. ||perChar=='8'||perChar=='9'||perChar=='0'||perChar==')')) return true;
  147. return false;
  148. }
  149. /*
  150. * 取余
  151. * */
  152. boolean checkRemainderInput(){
  153. int length = equation.length();
  154. if(length==0) return false;
  155. char perChar = equation.charAt(length-1);
  156. if(perChar=='1'||perChar=='2'||perChar=='3'||perChar=='4'||perChar=='5'||perChar=='6'||perChar=='7'
  157. ||perChar=='8'||perChar=='9'||perChar==')') return true;
  158. return false;
  159. }
  160. /*
  161. * 求阶乘
  162. * */
  163. boolean checkFactorialInput(){
  164. int length = equation.length();
  165. if(length==0) return false;
  166. char perChar = equation.charAt(length-1);
  167. if(perChar=='!'||perChar=='%'||perChar=='^'||perChar=='('||perChar=='+'
  168. ||perChar=='-'||perChar=='*'||perChar=='/') return false;
  169. //小数点阶乘不可以存在
  170. for(int i=length-1;i>=0;i--){
  171. char checkChar = equation.charAt(i);
  172. if(checkChar=='.') return false;
  173. if(checkChar=='+'||checkChar=='-'||checkChar=='*'||checkChar=='/') break;
  174. }
  175. return true;
  176. }
  177. }

最后是根据算术表达式求出最终结果。主要思路是,先转换为后缀表达式,再根据后缀表达式求值。CounterByEquation.java

  1. package com.example.ffy.orginal;
  2. import android.util.Log;
  3. import java.util.Stack;
  4. import java.util.Queue;
  5. public class CounterByEquation {
  6. private String equation;
  7. private Stack<String> stack; //存储符号
  8. private String []queue = new String[50]; //接受中缀表达式
  9. private int count=0;
  10. public CounterByEquation(String equation){
  11. this.equation = equation;
  12. }
  13. /*
  14. * ( ) * - / + ^ ! x^(-1) % e π 3.11 5
  15. *
  16. * ((3^5)+5^(-1)+((6%4)+e)*6)*2
  17. * 3^5 5^-1 + 6%4 e + 6 * +
  18. * */
  19. String solveEquation(){
  20. int length = equation.length();
  21. /*
  22. * 由中缀表达式求后缀表达式
  23. * */
  24. stack = new Stack<>();
  25. for(int i=0;i<length;i++){
  26. char curChar = equation.charAt(i);
  27. if(curChar=='('||curChar=='+'||curChar=='-'||curChar=='*'||curChar=='/'||curChar=='%'||curChar=='^'){
  28. String top = "";
  29. if(!stack.empty())
  30. top = stack.peek();
  31. if(curChar=='(') {
  32. stack.push(curChar+"");
  33. } else if (curChar == '%' || curChar == '^') {
  34. if(!stack.empty()&&(top.equals("^")||top.equals("%"))){
  35. queue[count++] = stack.pop();
  36. }
  37. stack.push(curChar+"");
  38. } else if ((curChar == '+' || curChar == '-')) {
  39. while (true) {
  40. if (stack.empty()) break;
  41. if (stack.peek().equals("(")) break;
  42. queue[count++] = stack.pop();
  43. }
  44. stack.push(curChar+"");
  45. } else if (curChar == '*' || curChar == '/') {
  46. if (!stack.empty()){
  47. while (true) {
  48. if (stack.peek().equals("*") || stack.peek().equals("/") || stack.peek().equals("%") || stack.peek().equals("^")) {
  49. queue[count++] = stack.pop();
  50. }else{
  51. break;
  52. }
  53. if (stack.empty()) break;
  54. if (stack.peek().equals("(")) break;
  55. }
  56. }
  57. stack.push(curChar+"");
  58. }
  59. }
  60. else if (curChar==')'){
  61. while(true){
  62. String tmp = stack.pop();
  63. if(tmp.equals("(")) break;
  64. else queue[count++] = tmp;
  65. }
  66. }
  67. else{ //数字
  68. String numStr = curChar+"";
  69. for(i++;i<length;i++)
  70. {
  71. char curBackChar = equation.charAt(i);
  72. if(curBackChar==')'||curBackChar=='+'||curBackChar=='-'||curBackChar=='*'||curBackChar=='/'||curBackChar=='%'||curBackChar=='^'){
  73. i--;
  74. break;
  75. }
  76. else
  77. numStr+=curBackChar;
  78. }
  79. queue[count++] = numStr;
  80. }
  81. }
  82. while(true){
  83. if(stack.empty()) break;
  84. queue[count++] = stack.pop();
  85. }
  86. String logStr = "";
  87. for(int i=0;i<count;i++){
  88. logStr+=queue[i]+" ";
  89. }
  90. Log.e("ERROR!!!!!!!!!!!!",logStr);
  91. /*由中缀表达式求值*/
  92. for(int i=0;i<count;i++){
  93. String member = queue[i];
  94. int numLength = member.length();
  95. if(member.charAt(numLength-1)=='!'){
  96. member = member.substring(0,numLength-1);
  97. int num = Integer.parseInt(member);
  98. int sum = 1;
  99. while(num>0){
  100. sum *= (num--);
  101. }
  102. queue[i] = sum+"";
  103. continue;
  104. }
  105. if(member.equals("π")) {
  106. queue[i] = "3.1415926";
  107. continue;
  108. }
  109. if(member.equals("e")) {
  110. queue[i] = "2.71828183";
  111. continue;
  112. }
  113. if(member.equals("+")||member.equals("-")||member.equals("*")||member.equals("/")||member.equals("%")||member.equals("^")){
  114. double first = Double.parseDouble(queue[i-2]);
  115. double second = 0;
  116. if(!queue[i-1].equals("-"))
  117. second = Double.parseDouble(queue[i-1]);
  118. if (member.equals("+")) first += second;
  119. if (member.equals("-")){
  120. //处理 x^(-1)的情况
  121. if(queue[i+1]!=null&&queue[i+1].equals("^")){
  122. first = 1 / first;
  123. //为了不影响接下来的运算,在数组去除^
  124. for(int j = i+1;j<count-1;j++)
  125. queue[j] = queue[j+1];
  126. count--;
  127. }else{
  128. first -= second;
  129. }
  130. }
  131. if (member.equals("*")) first *= second;
  132. if (member.equals("/")) first /= second;
  133. if (member.equals("%")) first %= second;
  134. if (member.equals("^")){
  135. double sum = 1;
  136. for(int j = (int)second;j>0;j--){
  137. sum*=first;
  138. }
  139. first = sum;
  140. }
  141. queue[i-2] = first+"";
  142. //前移
  143. for(int j = i-1;j<count-2;j++){
  144. queue[j] = queue[j+2];
  145. }
  146. count-=2;
  147. i-=2;
  148. }
  149. }
  150. return queue[0];
  151. }
  152. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/223229
推荐阅读
相关标签
  

闽ICP备14008679号