您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

java数据结构和算法——中缀转后缀表达式代码示例

小志的博客 发布时间:2020-07-29 22:21:53 ,浏览量:0

一、中缀转后缀表达式思路示意图

在这里插入图片描述

二、中缀转后缀表达式代码示例

示例需求:中缀表达式:1+((2+3)×4)-5 转换成后缀表达式:1 2 3 + 4 × + 5 –

1、代码

package com.rf.springboot01.dataStructure.stack.reversePolishNotation;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @description:  中缀表达式转后缀表达式
 * @author: xiaozhi
 * @create: 2020-07-29 14:24
 */
public class infixConvertorSuffix {
    public static void main(String[] args) {
       // 定义一个中缀表达式, 示例:1+((2+3)×4)-5 =>1 2 3 + 4 × + 5 –
        String expression = "1+((2+3)*4)-5";
        //中缀表达式转成list  示例:1+((2+3)×4)-5 =>[1, +, (, (, 2, +, 3, ), *, 4, ), -, 5]
        List infixConvertorList = infixConvertorList(expression);
        System.out.println("中缀表达式对应的list======"+infixConvertorList);
        List suffixList=infixListConvertorSuffixList(infixConvertorList);
        System.out.println("后缀表达式对应的list======"+suffixList);
        int result=computer(suffixList);
        System.out.println("中缀表达式计算的结果为:"+result);
    }

    /** 
    * @Description: 中缀表达式转list
    * @Param: String
    * @Author: xz  
    * @return: java.util.List
    * @Date: 2020/7/29 14:27  
    */ 
    public static List infixConvertorList(String str){
        List list=new ArrayList();
        String s;//用于多位数的拼接
        char c;// 每遍历到一个字符,就放入到c
        int i=0;
        do{
            if((c=str.charAt(i))57 ){//如果非数字
                list.add(c+"");//字节转字符串,在添加到list
                i++;
            }else{//如果是数字,考虑多位数拼接
                s="";
                while(i=48 && (c=str.charAt(i))            
关注
打赏
1661269038
查看更多评论
0.0405s