数据结构-后缀表达式

数据结构-后缀表达式_第1张图片

编写一个整数位只有一位的加减乘除计算器

运算系统

package tulun.work;

/**
 * @Created with IntelliJ IDEA
 * @Description:  运算系统
 * @Package: tulun.work
 * @author: FLy-Fly-Zhang
 * @Date: 2018/12/3
 * @Time: 23:16
 */
public class Operate {
    private String strLast="";
    private String midStr;
    private char[] midChar;
    private char[] lastChar;
    private StrStack lastStack=new StrStack();
    private StrStack OpSum=new StrStack();
    public Operate(String str){
        this.midStr=str;
        this.midChar=midStr.toCharArray();
    }
    private void strLastSum (String strLast) throws NullPointerException{
        double num1=0;
        double num2=0;
        double sum=0;
        lastChar=strLast.toCharArray();
        if(lastChar[0]=='+'|| lastChar[0]=='-'|| lastChar[0]=='*' ||lastChar[0]=='/' || lastChar[1]=='+'|| lastChar[1]=='-'|| lastChar[1]=='*' ||lastChar[1]=='/'  ){
            throw new NullPointerException("后缀表达式输入错误");
        }
        for(int i=0 ; i

运算库

package tulun.work;

/**
 * @Created with IntelliJ IDEA
 * @Description:
 * @Package: tulun.work
 * @author: FLy-Fly-Zhang
 * @Date: 2018/12/3
 * @Time: 22:42
 */
public class Priority {
    public static final int ADDANDSUB=1;
    public static final int RIDEANDDIVIDE=3;
    public static final int BRACKETLEFT=5;
    public static final int BRACKETRIGTH=0;
    public static final char ADD='+';
    public static final char SUB='-';
    public static final char RIDE='*';
    public static final char DIVIDE='/';
    public static final char BLEFT='(';
    public static final char BRIGTH=')';

    public static String  setAddandsub(StrStack lastStack,char ch,String strLast){

        if(lastStack.getElemSer()>Priority.ADDANDSUB){ //栈内高于栈外,出栈;
            while(lastStack.getTop()!=0 && lastStack.getElemSer()>Priority.ADDANDSUB){ //栈内高于栈外一直出栈;
                strLast = strLast + lastStack.getElemVal(); //加入后缀表达式;
                lastStack.pop();//出栈

            }
            lastStack.push(String.valueOf(ch), Priority.ADDANDSUB + 1); //入栈
        }else{  //栈外高于栈内直接进栈;
            lastStack.push(String.valueOf(ch),Priority.ADDANDSUB+1);
        }
        return strLast;
    }
    public static String setRideanddivide(StrStack lastStack,char ch,String strLast){
        if(  lastStack.getElemSer()>Priority.RIDEANDDIVIDE){ //栈内高于栈外,出栈;
            while(lastStack.getTop()!=0 &&lastStack.getElemSer()>Priority.RIDEANDDIVIDE) {
                strLast = strLast + lastStack.getElemVal(); //加入后缀表达式;
                lastStack.pop();//出栈
            }
            lastStack.push(String.valueOf(ch), Priority.RIDEANDDIVIDE + 1); //入栈
        }else{  //栈外高于栈内直接进栈;
            lastStack.push(String.valueOf(ch),Priority.RIDEANDDIVIDE+1);
        }
        return strLast;
    }

    public int getResult(char opera,int num1,int num2){
        if(opera=='+'){
            return num1+num2;
        }else if(opera=='-'){
            return num2-num1;
        }else if(opera=='*'){
            return num1*num2;

    }else {
            return num2/num1;
        }
    }

}

package tulun.work;

import java.util.Arrays;

/**
 * @Created with IntelliJ IDEA
 * @Description:
 * @Package: tulun.work
 * @author: FLy-Fly-Zhang
 * @Date: 2018/12/3
 * @Time: 22:43
 */
public class StrStack {
    private int top;
    private String [][] elem;
    public StrStack(){
        this.elem=new String[5][2];
        this.top=0;
    }
    public boolean isFull(){
        if(elem.length==top){
            return true;
        }
        return false;
    }
    public void push(String val,int series){
        if(isFull()){
            elem=Arrays.copyOf(elem,elem.length);
        }
        this.elem[top][0]=val;
        this.elem[top][1]=String.valueOf(series);
        top++;
    }
    public boolean isEmpty(){
        if(top==0){
            return true;
        }
        return false;
    }
    public void pop() throws NullPointerException{
        if(isEmpty()){
            throw new NullPointerException("栈为空");
        }
        top--;
    }
    public int  getTop(){
        return top;
    }

    public int  getElemSer() {
        return Integer.parseInt(elem[top-1][1]);
    }
    public String  getElemVal() {
        return elem[top-1][0];
    }
    public void show(){
        System.out.println("栈内元素为[ ");
        for (int i = 0; i 

启动函数

package tulun.work;

import java.util.Scanner;

/**
 * @Created with IntelliJ IDEA
 * @Description:
 * @Package: tulun.work
 * @author: FLy-Fly-Zhang
 * @Date: 2018/12/4
 * @Time: 0:39
 */
public class Main {
        public static void main(String[] args) {
            System.out.println("-------------请输入运算公式-------------");
            String s;//="2+3*5-4*(5-3)";
            Scanner scan=new Scanner(System.in);
            s=scan.nextLine();
            Operate operate=new Operate(s);
            operate.start();

    }
}

你可能感兴趣的:(笔记)