简易计算器 栈应用 尾递归

模拟简单计算器 

public class CalSuffix {
    private MyIntStack stack;
    private String input;
    public CalSuffix(String input){
        this.input=input;
        int length=input.length();
        stack=new MyIntStack(length);
    }

    public int doCalc(){
        int num1,num2,result;
        for (int i = 0; i <input.length() ; i++) {
            char c=input.charAt(i);
            if (c>='0'&&c<='9'){
                stack.push(c-'0');  //如果是数字 直接压入栈中
            }else {
                num2=stack.pop();  //  先弹出第二个操作数
                num1=stack.pop();  // 第一个操作数
            switch (c){
                case '+':
                    result=num1+num2;
                    break;
                case '-':
                    result=num1-num2;
                    break;
                case '*':
                    result=num1*num2;
                    break;
                case '/':
                    if (num2==0) {
                        result=0;
                          break;}
                    result=num1 / num2;
                    break;
                default:
                    result=0;
                    break;
            }//end switch
                stack.push(result);
            }// end else
        } // end for
        result = stack.pop();
        return result;
    }

    public static void main(String[] args) {
        InfixToSuffix in=new InfixToSuffix("1*(2+3)-5/(2+3)");
        MyCharStack my=in.doTransfer();
        my.displayStack();
        String s="";
        while (!my.isEmpty()){
            s+=my.pop();
        }
        CalSuffix cs=new CalSuffix(s);  //"123+*523+/-"
        System.out.println("结果");
        System.out.println(cs.doCalc());
    }
}
// 前缀表达式转换为后缀表达式
public class InfixToSuffix {
    private MyCharStack s1;   //定义运算符栈
    private MyCharStack s2;   //定义存储结果栈
    private String input;

    //默认构造方法,参数为输入的中缀表达式
   public InfixToSuffix(String in){
        input=in;
        s1=new MyCharStack(input.length());
        s2=new MyCharStack(input.length());
    }



    //中缀表达式转换为后缀表达式,将结果存储在栈中返回,逆序显示后缀表达式
    public MyCharStack doTransfer(){
        for (int i = 0; i <input.length() ; i++) {
            System.out.print("s1栈元素为:");
            s1.displayStack();
            System.out.print("s2栈元素为:");
            s2.displayStack();

            char ch=input.charAt(i);
            System.out.println("当前解析字符:"+ch);
            switch (ch){
                case '+':
                case '-':
                    gotOper(ch,1);
                    break;
                case '*':
                case '/':
                    gotOper(ch,2);
                    break;
                case '(':
                    s1.push(ch); //如果当前字符是‘(’,则将其入栈
                    break;
                case ')':
                    gotParen(ch);
                    break;
                default:   //  如果是操作数,则直接压入s2
                    s2.push(ch);
                    break;
            }  //end switch
        } // end for
        while (!s1.isEmpty()){
            s2.push(s1.pop());
        }
        return s2;
    }

    private void gotOper(char ch, int i) {
        while(!s1.isEmpty()){
            char optop=s1.pop();
            if (optop=='('){//  如果栈顶是(,直接将操作符压入栈s1
                s1.push(optop);
                break;
            }else {
                int j;
                if (optop=='+'||optop=='-'){
                    j = 1;
                }else {
                    j =2;
                }
                if (j<i){ //如果当前运算符比S1栈顶运算符优先级高,则将运算符压入s1
                    s1.push(optop);
                    break;
                }else {//如果当前运算符与栈顶运算符相同或者小于优先级别,那么僵s1栈顶元素的运算符弹出并压入到s2中
                    //并且要再次转到while循环中与s1中新的栈顶元素相比较
                    s2.push(optop);

                }
            }// else
        } // end while
        //如果s1为空,将当前解析的运算符直接压入s1
        s1.push(ch);
    }

    //如果字符是‘)’时,如果栈顶‘(’,则将这一对括号丢弃,否则依次弹出s1栈顶的字符,压入s2,知道遇到‘(’
    public void gotParen(char ch){
        while (!s1.isEmpty()){
            char chx=s1.pop();
            if (chx=='('){
                break;

            }else {
                s2.push(chx);

            }
        }
    }


}
//定义一个栈
public class MyCharStack {
    private char[] array;
    private int maxSize;
    private int top;

    public MyCharStack() {
    }

    public MyCharStack(int size) {
        array = new char[size];
        this.maxSize = size;
        top =-1 ;
    }
    //压入数据
    public void push(char value){
        if (top<maxSize-1){
            array[++top]=value;
        }
    }

    //弹出栈顶数据
    public char pop(){
        return array[top--];
    }

    //访问栈顶数据
    public char peek(){
        return array[top];
    }

    //查看指定位置的元素
    public char peekN(int n){
        return array[n];
    }

    // 为了便于后面分解展示栈中的内容,我们增加了一个遍历栈的方法(实际上栈只能访问栈顶元素)
    public void displayStack(){
        System.out.println("Stack(bottom-->top):");
        for (int i = 0; i <top+1 ; i++) {
            System.out.print(peekN(i));
            System.out.print(" ");
        }
        System.out.println();
    }

    //判断栈是否为空
    public boolean isEmpty(){
        return top==-1;
    }

    //判断栈是否满了
    public boolean isFull(){
        return top==(maxSize-1);
    }

}
public class MyIntStack {
    private int[] array;
    private int maxSize;
    private int top;

    public MyIntStack() {
    }

    public MyIntStack(int size) {
        array = new int[size];
        maxSize = size;
        top =-1 ;
    }
    //压入数据
    public void push(int value){
        if (top<maxSize-1){
            array[++top]=value;
        }
    }

    //弹出栈顶数据
    public int pop(){
        if (top==-1) return 0;
        return array[top--];
    }

    //访问栈顶数据
    public int peek(){
        return array[top];
    }

    //查看指定位置的元素
    public int peekN(int n){
        return array[n];
    }

    // 为了便于后面分解展示栈中的内容,我们增加了一个遍历栈的方法(实际上栈只能访问栈顶元素)
    public void displayStack(){
        System.out.println("Stack(bottom-->top):");
        for (int i = 0; i <top+1 ; i++) {
            System.out.print(peekN(i));
            System.out.print(" ");
        }
        System.out.println();
    }

    //判断栈是否为空
    public boolean isEmpty(){
        return top==-1;
    }

    //判断栈是否满了
    public boolean isFull(){
        return top==(maxSize-1);
    }
}

 

尾递归
 int f(int n){
        if (n<=1) return n;
        else
            return f(n-1)+f(n-2);
    }
    int f2(int n){
        int pre=0;
        int now=1,next=0,j=2;
        if (n<=1) return n;
        else {
            for (j = 2; j <=n ;j ++) {
                next= pre+now; 
                pre=now;
                now=next;
            }
            return next;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值