表达式计算

本文介绍了一种使用栈实现表达式求值的算法。通过两个栈分别存储数字和符号,逐步解析并计算输入的数学表达式。文章详细展示了每一步的操作流程及条件判断逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  // true压栈,false运算
 private static boolean comp(char a, char b) {
  if (b == '(') {
   return true;
  }
  if (b == '*' || b == '/') {
   if (a == '*' || a == '/') {
    return false;
   } else {
    return true;
   }
  }

  if (b == '+' || b == '-') {
   if (a == '(' || a == '#') {
    return true;
   } else {
    return false;
   }
  }
  if (b == ')') {
   return false;
  }
  if (b == '#') {
   return false;
  }
  return false;

 }


  Stack ss = new Stack();
  Stack sn = new Stack();
  String str = "(1+3*(4+5))";
  str = str + "#";
  char a = '#';
  String temp = "";
  for (int i = 0; i < str.length(); i++) {
   char c = str.charAt(i);
   if (c != '#' && c != '(' && c != ')' && c != '*' && c != '/'
     && c != '+' && c != '-') {
    temp += c;
    continue;
   }
   System.out.println("a:"+a+"/t"+"c:"+c+"/t");
   switch (a) {
   case '#':
    if (comp(a, c)) {
     ss.push(a);
     a=c;
     if(!temp.equals("")){
      sn.push(Integer.parseInt(temp));
     }
    } else {}
    temp = "";
    break;
   case '(':
    if (comp(a, c)) {
     ss.push(a);
     a=c;
     if(!temp.equals("")){
      sn.push(Integer.parseInt(temp));
     }
    } else {
     int result;
     a = (Character)ss.pop();
    }
    temp = "";
    break;
   case ')':// a不可能等于)
    if (comp(a, c)) {} else {}
    temp = "";
    break;
   case '+':
    if (comp(a, c)) {
     ss.push(a);
     a=c;
     if(!temp.equals("")){
      sn.push(Integer.parseInt(temp));
     }
    } else {
     int result;
     if(temp.equals("")){
      int no = (Integer)sn.pop();
      result = (Integer)sn.pop();
      result += no;
     }else{
      result = (Integer)sn.pop();
      result += Integer.parseInt(temp);
     }
     sn.push(result);
     a = (Character)ss.pop();
     i--;
    }
    temp = "";
    break;
   case '-':
    if (comp(a, c)) {
     ss.push(a);
     a=c;
     if(!temp.equals("")){
      sn.push(Integer.parseInt(temp));
     }
    } else {
     int result;
     if(temp.equals("")){
      int no = (Integer)sn.pop();
      result = (Integer)sn.pop();
      result -= no;
     }else{
      result = (Integer)sn.pop();
      result -= Integer.parseInt(temp);
     }
     sn.push(result);
     a = (Character)ss.pop();
     i--;
    }
    temp = "";
    break;
   case '*':
    if (comp(a, c)) {
     ss.push(a);
     a=c;
     if(!temp.equals("")){
      sn.push(Integer.parseInt(temp));
     }
    } else {
     int result;
     if(temp.equals("")){
      int no = (Integer)sn.pop();
      result = (Integer)sn.pop();
      result *= no;
     }else{
      result = (Integer)sn.pop();
      result *= Integer.parseInt(temp);
     }
     sn.push(result);
     a = (Character)ss.pop();
     i--;
    }
    temp = "";
    break;
   case '/':
    if (comp(a, c)) {
     ss.push(a);
     a=c;
     if(!temp.equals("")){
      sn.push(Integer.parseInt(temp));
     }
    } else {
     int result;
     if(temp.equals("")){
      int no = (Integer)sn.pop();
      result = (Integer)sn.pop();
      result /= no;
     }else{
      result = (Integer)sn.pop();
      result /= Integer.parseInt(temp);
     }
     sn.push(result);
     a = (Character)ss.pop();
     i--;
    }
    temp = "";
    break;
    default:
   }
   System.out.println(sn);
   System.out.println(ss);
  }

  System.out.println("reslut:" + (Integer)sn.pop());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值