数据结构- 栈(实现综合计算器)(一位数计算 扩展到 多位数计算)
生活随笔
收集整理的這篇文章主要介紹了
数据结构- 栈(实现综合计算器)(一位数计算 扩展到 多位数计算)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路
代碼(可以看到這里的數字只能是單位數字,那么如何改成可以是多位數呢?!往下看)
package stack;public class Calculator {public static void main(String[] args) {//完成表達式運算String expression = "7+2*6-2*2";//創建兩個棧,一個數棧,一個符號棧ArrayStack1 numstack = new ArrayStack1(10);ArrayStack1 operstack = new ArrayStack1(10);//定義需要的相關變量int index = 0; //用于掃描int num1 = 0, num2 = 0;int oper = 0;int res = 0;char ch = ' ';//將每次掃描得到的char保存給ch//開始while循環的掃描expressionwhile(true){//一次得到expression 的每個字符ch = expression.substring(index,index+1).charAt(0);//判斷ch是字符還是數字if (operstack.isOper(ch)){//判斷符號棧是否為空if (!operstack.isEmpty()){//如果不為空,比較,如果當前操作符小于等于則進行下面操作if (operstack.priority(ch) <= operstack.priority(operstack.peekTop())){num1 = numstack.pop();num2 = numstack.pop();oper = operstack.pop();res = numstack.cal(num1,num2,oper);//把運算的結果加入棧numstack.push(res);//然后將當前操作符入符號棧operstack.push(ch);}else {operstack.push(ch);}}else {//為空直接入棧operstack.push(ch);}}else {//如果是數直接入棧(這里的1是字符'1',要轉成數字1,ascll碼減48)numstack.push(ch - 48);}//讓index + 1,判斷是否掃描到expression最后index ++;if (index >= expression.length()){break;}}//掃描表達式完畢后,就順序的從 數棧和符號棧中pop出相應的數和符合,并運算while (true){//如果符號棧為空,則計算得到最后的結果,數棧中只有一個數字if (operstack.isEmpty()){break;}num1 = numstack.pop();num2 = numstack.pop();oper = operstack.pop();res = numstack.cal(num1,num2,oper);numstack.push(res);}//打印結果(就是數棧中的最后一個數)System.out.printf("表達式 %s = %d",expression,numstack.pop());} }class ArrayStack1{private int maxSize; //棧大小private int[] stack;//數組模擬棧,數據放入數組里private int top = -1; //top表示棧頂,初始化為-1//構造器public ArrayStack1(int maxSize) {this.maxSize = maxSize;stack = new int[this.maxSize];}//返回棧頂的值,不出棧public int peekTop(){return stack[top];}//棧滿public boolean isFull(){return top == maxSize - 1;}//棧空public boolean isEmpty(){return top == -1;}// 入棧 pushpublic void push(int value){//判斷棧是否滿if (isFull()){System.out.println("棧滿,不能入棧");return;}top++;stack[top] = value;}//出棧public int pop(){if (isEmpty()){//拋出異常throw new RuntimeException("棧空");}int value = stack[top];top--;return value;}//遍歷棧(從棧頂開始)public void list(){if (isEmpty()){System.out.println("棧空");}for (int i = top; i>=0; i--){System.out.printf("stack[%d] = %d\n",i,stack[i]);}}//返回運算符優先級,優先級有程序員來確定,優先級使用數字表示//數字越大,優先級越高public int priority(int oper){if (oper == '*' || oper == '/'){return 1;}else if(oper == '+' || oper == '-'){return 0;}else {return -1;//假定目前的表達式只有 +-*/}}//判斷是否是一個運算符public boolean isOper(char val){return val == '+' || val == '-' || val == '*' || val == '/';}//計算方法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;break;}return res;} }找到是哪個地方有問題?,發現是一個數字就直接入棧了,那么需要在入數棧時,需要向后看,如果是數就拼接并繼續掃描,如果是符合才入棧。(那么就需要一個字符串變量用于拼接)
代碼
package stack;public class Calculator {public static void main(String[] args) {//完成表達式運算String expression = "71+2*6-2*2";//創建兩個棧,一個數棧,一個符號棧ArrayStack1 numstack = new ArrayStack1(10);ArrayStack1 operstack = new ArrayStack1(10);//定義需要的相關變量int index = 0; //用于掃描int num1 = 0, num2 = 0;int oper = 0;int res = 0;char ch = ' ';//將每次掃描得到的char保存給chString keepnum = ""; //用于拼接多位數//開始while循環的掃描expressionwhile(true){//一次得到expression 的每個字符ch = expression.substring(index,index+1).charAt(0);//判斷ch是字符還是數字if (operstack.isOper(ch)){//判斷符號棧是否為空if (!operstack.isEmpty()){//如果不為空,比較,如果當前操作符小于等于則進行下面操作if (operstack.priority(ch) <= operstack.priority(operstack.peekTop())){num1 = numstack.pop();num2 = numstack.pop();oper = operstack.pop();res = numstack.cal(num1,num2,oper);//把運算的結果加入棧numstack.push(res);//然后將當前操作符入符號棧operstack.push(ch);}else {operstack.push(ch);}}else {//為空直接入棧operstack.push(ch);}}else {//如果是數直接入棧(這里的1是字符'1',要轉成數字1,ascll碼減48)//如果有多位數,則需要改進代碼,不能直接入棧//思路:// 1.掃描到數字,需要繼續向后掃描,如果是數就拼接,繼續掃描,直到掃描是字符keepnum += ch;//如果ch是最后一位了則直接入棧if (index == expression.length()-1){numstack.push(Integer.parseInt(keepnum));}else {//判斷下一個字符是不是數字,如果是繼續掃描if (operstack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {numstack.push(Integer.parseInt(keepnum));//這里一定要清空keepnumkeepnum = "";}}}//讓index + 1,判斷是否掃描到expression最后index ++;if (index >= expression.length()){break;}}//掃描表達式完畢后,就順序的從 數棧和符號棧中pop出相應的數和符合,并運算while (true){//如果符號棧為空,則計算得到最后的結果,數棧中只有一個數字if (operstack.isEmpty()){break;}num1 = numstack.pop();num2 = numstack.pop();oper = operstack.pop();res = numstack.cal(num1,num2,oper);numstack.push(res);}//打印結果(就是數棧中的最后一個數)System.out.printf("表達式 %s = %d",expression,numstack.pop());} }class ArrayStack1{private int maxSize; //棧大小private int[] stack;//數組模擬棧,數據放入數組里private int top = -1; //top表示棧頂,初始化為-1//構造器public ArrayStack1(int maxSize) {this.maxSize = maxSize;stack = new int[this.maxSize];}//返回棧頂的值,不出棧public int peekTop(){return stack[top];}//棧滿public boolean isFull(){return top == maxSize - 1;}//棧空public boolean isEmpty(){return top == -1;}// 入棧 pushpublic void push(int value){//判斷棧是否滿if (isFull()){System.out.println("棧滿,不能入棧");return;}top++;stack[top] = value;}//出棧public int pop(){if (isEmpty()){//拋出異常throw new RuntimeException("棧空");}int value = stack[top];top--;return value;}//遍歷棧(從棧頂開始)public void list(){if (isEmpty()){System.out.println("棧空");}for (int i = top; i>=0; i--){System.out.printf("stack[%d] = %d\n",i,stack[i]);}}//返回運算符優先級,優先級有程序員來確定,優先級使用數字表示//數字越大,優先級越高public int priority(int oper){if (oper == '*' || oper == '/'){return 1;}else if(oper == '+' || oper == '-'){return 0;}else {return -1;//假定目前的表達式只有 +-*/}}//判斷是否是一個運算符public boolean isOper(char val){return val == '+' || val == '-' || val == '*' || val == '/';}//計算方法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;break;}return res;} }總結
以上是生活随笔為你收集整理的数据结构- 栈(实现综合计算器)(一位数计算 扩展到 多位数计算)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JS 逆向百例】层层嵌套,某加速商城
- 下一篇: 什么理财产品年化10%?只要学会这四种理