栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

利用栈使用简易计算器(Java实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用栈使用简易计算器(Java实现)

题目:使用栈计算类似表达式:5+2*3-2  的计算结果 

提示:简易计算器操作符号限于+,-,*,/的计算

分析思路:

1、创建一个数栈和一个符号栈,数栈用于存放数字,符号栈用于存放符号

2、创建一个索引index,用于遍历表达式

3、扫描表达式,如果是数字直接进入数栈,如果是符号,则需要进行判断。分两种情况,一是当符号栈如果为空,直接将符号入栈。二是不为空,先比较当前栈顶的符号与将要进栈的符号的优先级大小,如果将要进栈的操作符的优先级小,则将数栈的两个数弹出,符号栈的操作符弹出一个,并进行计算,计算之后的结果直接进入数栈,如果优先级大,就直接进栈。

4、扫描完表达式之后,就顺序的从数栈和符号栈顺序的弹出相应的数字和操作符,并进行计算。

5、当符号栈为空时,说明已经计算完了,此时留在数栈的只有一个数字,就是表达式计算之后的结果。

 

代码实现

package cn.mrlij.stack;
 
import java.util.Arrays;
import java.util.Scanner;
 

public class ArrayStackDemo {
  public static void main(String[] args) {
   String express = "5011+2*3-2";
   int index = 0;//定义一个索引值,用于遍历表达式
    int num1 = 0;
    int num2 = 0;
    int res = 0;//计算结果
    char ch = ' ';
    int oper = 0;
    String keepNum = "";
    ArrayStack numStack = new ArrayStack(10);//创建一个数栈
    ArrayStack operStack = new ArrayStack(10);//创建一个符号栈
    while (true){
      ch = express.substring(index,index+1).charAt(0);//不停的遍历操作符
      //判断是否是操作符
      if (operStack.isOper(ch)){
 //判断当前符号栈是否有符号存在
 if(!operStack.isEmpty()){
   //不为空则判断优先级
   if(operStack.priority(ch)<=operStack.priority(operStack.peek())){
     //当优先级小于栈顶的值时候,弹出两个数栈的值进行计算
     num1 = numStack.pop();
     num2 = numStack.pop();
     oper = operStack.pop();
     res = operStack.cal(num1,num2,oper);
     //计算以后将计算得到的值放入数栈
     numStack.push(res);
     //同时将此时的操作符放入符号栈
     operStack.push(ch);
   }else{
     //优先级
     operStack.push(ch);
   }
 }else{
   //为空直接将符号入栈
   operStack.push(ch);
 }
      }else {
 keepNum += ch;
 //处理多位数
 if(express.length()-1 == index){
   numStack.push(Integer.parseInt(keepNum));
 }else {
   if(operStack.isOper(express.substring(index+1,index+2).charAt(0))){
     numStack.push(Integer.parseInt(keepNum));
     keepNum = "";
   }
 }
 // numStack.push(ch-48);
      }
      index++;
      if(index >= express.length()){
 break;
      }
    }
    //扫描完之后,将数栈的值,与操作符中的值进行计算
    while (true){
      if(operStack.isEmpty()){
 break;
      }
      num1 = numStack.pop();
      num2 = numStack.pop();
      oper = operStack.pop();
      res = operStack.cal(num1,num2,oper);
      numStack.push(res);
    }
    System.out.println("表达式:"+express+"="+numStack.pop());
  }
}
 
class ArrayStack {
  private int MaxSize;// 定义数组的最大长度
  private int[] arr;// 定义数组,数据就放在该数组
  private int top = -1;// 定义栈顶,初始化数据为-1
 
  public ArrayStack(int maxSize) {
    this.MaxSize = maxSize;
    arr = new int[MaxSize];
  }
 
  // 判断数组是否为空
  public boolean isEmpty() {
 
    return top == -1;
  }
 
  // 判断数组是否满了
  public boolean isFull() {
    //System.out.println("栈顶:" + top + "最大长度:" + MaxSize);
    return top == MaxSize - 1;
  }
  //取出棧頂元素
  public int peek(){
    return arr[top];
  }
  // 进栈
  public void push(int val) {
    // 先判断栈是否满了,满了就不能添加进去
    if (isFull()) {
      System.out.println("栈已经满了~~");
      return;
    }
    top++;
    arr[top] = val;
  }
 
  // 出栈
  public int pop() {
    // 先判断栈是否为空
    if (isEmpty()) {
      throw new RuntimeException("栈为空,无法出栈!");
    }
    int val = arr[top];
    top--;
    return val;
  }
 
  public void show() {
    if (isEmpty()) {
      System.out.println("没有数据");
      return;
    }
    for (int i = top; i >= 0; i--) {
      System.out.print(arr[i] + "t");
    }
    System.out.println();
  }
 
  
  public boolean isOper(char oper){
    return oper == '+' || oper == '-' || oper =='*' || oper == '/';
  }
 
  
  public int priority(int oper ){
    if(oper == '*' || oper == '/'){
      return 1;
    } else if(oper == '+' || oper == '-'){
      return 0;
    }else {
      return -1;
    }
  }
 
  //计算方法
  public int cal(int num1,int num2,int oper){
    int res = 0;
    switch (oper){
      case '+': res = num1 + num2;
      break;
      case '-': res = num2 - num1;
      break;
      case '*': res = num1 * num2;
      break;
      case '/': res = num2 /num1;
    }
    return res;
  }
}

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136885.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号