计算器

利用栈结构实现的一个简易计算器

代码实现

  1. /**
  2. * @author laoduan
  3. * @create 2020-04-11-17:58
  4. */
  5. public class Calculator {
  6. public static void main(String[] args) {
  7. String expressinon = "3+2*6-2";
  8. Arraystack2 numStack = new Arraystack2(10);
  9. Arraystack2 operStack = new Arraystack2(10);
  10. int index=0;//用于扫描
  11. int num1=0;
  12. int num2=0;
  13. int oper=0;
  14. int res=0;
  15. char ch = ' ';//将每次扫描得到的char保存到ch
  16. while (true){
  17. ch = expressinon.substring(index,index+1).charAt(0);
  18. if(operStack.isOper(ch)){
  19. if(!operStack.isEmpty()){
  20. //不空则判断优先级
  21. //如果优先级小于或等于栈中的操作符,就从数栈pop两个数
  22. //从符合惨pop一个符合进行运算,将得到的结果入数栈,将当前操作符入符栈
  23. if(operStack.priority(ch)<=operStack.priority(operStack.peek())){
  24. num1=numStack.pop();
  25. num2=numStack.pop();
  26. oper=operStack.pop();
  27. res=numStack.cal(num1,num2,oper);
  28. //把运算的结果入数栈
  29. numStack.push(res);
  30. //当前的操作入符号栈
  31. operStack.push(ch);
  32. }
  33. else {
  34. //当前的操作符优先级大于栈中的操作符,直接入符号栈
  35. operStack.push(ch);
  36. }
  37. }else{
  38. //栈空则入栈
  39. operStack.push(ch);
  40. }
  41. }
  42. else {
  43. numStack.push(ch-48);//ASCII表里面'1'正好比1大48
  44. }
  45. index++;
  46. if(index>=expressinon.length()){
  47. break;
  48. }
  49. }
  50. while (true){
  51. if(operStack.isEmpty()){
  52. break;
  53. }
  54. num1=numStack.pop();
  55. num2=numStack.pop();
  56. oper=operStack.pop();
  57. res=numStack.cal(num1,num2,oper);
  58. numStack.push(res);
  59. }
  60. int Finres = numStack.pop();
  61. System.out.printf("表达式%s=%d",expressinon,Finres);
  62. }
  63. }
  64. //
  65. class Arraystack2{
  66. private int maxSize;//栈的大小
  67. private int[] stack;//数组
  68. private int top = -1;
  69. public Arraystack2(int maxSize){
  70. this.maxSize=maxSize;
  71. stack=new int [this.maxSize];
  72. }
  73. //增加方法,返回栈顶的值,但不是pop
  74. public int peek(){
  75. return stack[top];
  76. }
  77. //栈满
  78. public boolean isFull(){
  79. return top==maxSize-1;
  80. }
  81. //栈空
  82. public boolean isEmpty(){
  83. return top==-1;
  84. }
  85. //入栈
  86. public void push(int value){
  87. if(isFull()){
  88. System.out.println("栈满,不能入栈");
  89. return;
  90. }
  91. top++;
  92. stack[top]=value;
  93. }
  94. //出栈
  95. public int pop(){
  96. if (isEmpty()){
  97. throw new RuntimeException("栈空");
  98. }
  99. top--;
  100. return stack[top+1];
  101. }
  102. //遍历栈
  103. public void list(){
  104. if (isEmpty()){
  105. System.out.println("栈空,不能遍历");
  106. }
  107. for (int i=top;i>-1;i--){
  108. System.out.printf("stacl[%d]=%d\n",i,stack[i]);
  109. }
  110. }
  111. //返回运算符的优先级,用数字表示
  112. //数字越大优先级越高
  113. public int priority(int oper){
  114. if(oper=='*'||oper=='/'){
  115. return 1;
  116. }else if(oper=='+'||oper=='-'){
  117. return 0;
  118. }else{
  119. return -1;//假定目前表达式只有+ - * /
  120. }
  121. }
  122. //判断是不是一个运算符
  123. public boolean isOper(char val){
  124. return val=='+'||val=='-'||val=='*'||val=='/';
  125. }
  126. //计算方法
  127. public int cal(int num1,int num2,int oper){
  128. int res = 0;//res用于存放计算ed结果
  129. switch (oper){
  130. case'+':
  131. res = num1+num2;
  132. break;
  133. case'-':
  134. res = num2-num1;
  135. break;
  136. case'*':
  137. res = num1*num2;
  138. break;
  139. case'/':
  140. res = num2+num1;
  141. break;
  142. default:
  143. break;
  144. }
  145. return res;
  146. }
  147. }