处理多位数字
KeepNum+=ch;
if(index == expression.length()-1){
numArrayStackk.push(Integer.parseInt(KeepNum));
}else{
if(operArrayStackk.isOper(expression.substring(index+1,index+2).charAt(0))){
numArrayStackk.push(Integer.parseInt(KeepNum));
KeepNum="";
}
}
判断优先级
public int priority(int oper) {
if(oper == '*'||oper == '/'){
return 1;
}
else if(oper =='+'||oper == '-'){
return 0;
}else{
return -1;
}
}
package stack;
public class Calculator {
public static void main(String[] args) {
String expression="70+10*3-30";
ArrayStack numArrayStackk=new ArrayStack(10);
ArrayStack operArrayStackk=new ArrayStack(10);
int index=0;
int num1=0;
int num2=0;
int oper=0;
int res=0;
char ch= ' ';
String KeepNum = "" ;
while(true){
ch=expression.substring(index,index+1).charAt(0);
System.out.println("ch:"+ch);
if(operArrayStackk.isOper(ch)){
if(!operArrayStackk.isEmpty()){
if(operArrayStackk.priority(ch)<=operArrayStackk.priority(operArrayStackk.peek())){
num1=numArrayStackk.pop();
System.out.println("值1:"+num1);
num2=numArrayStackk.pop();
System.out.println("值2:"+num1);
oper=operArrayStackk.pop();
res=numArrayStackk.cal(num1, num2, oper);
numArrayStackk.push(res);
operArrayStackk.push(ch);
}else{
operArrayStackk.push(ch);
}
}else{
operArrayStackk.push(ch);
System.out.println("符号ch:"+ch);
}
}
else{
KeepNum+=ch;
if(index == expression.length()-1){
numArrayStackk.push(Integer.parseInt(KeepNum));
}else{
if(operArrayStackk.isOper(expression.substring(index+1,index+2).charAt(0))){
numArrayStackk.push(Integer.parseInt(KeepNum));
KeepNum="";
}
}
}
index++;
if(index>=expression.length()){
break;
}
}
while(true){
if(operArrayStackk.isEmpty()){
break;
}
num1=numArrayStackk.pop();
num2=numArrayStackk.pop();
oper=operArrayStackk.pop();
res=numArrayStackk.cal(num1, num2, oper);
numArrayStackk.push(res);
}
int res2=numArrayStackk.pop();
System.out.printf("表达式%s=%d",expression,res2);
}
}
class ArrayStack{
private int maxSize;
private int[] stack;
private int top=-1;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack=new int[this.maxSize];
}
public boolean isFull() {
return top == maxSize-1;
}
public char peek() {
return (char)stack[top];
}
public boolean isEmpty() {
return top == -1;
}
public void push(int value) {
if(isFull()){
System.out.println("栈满");
return ;
}
top++;
stack[top] =value;
}
public int pop() {
if(isEmpty()){
System.out.println("栈空");
}
int value=stack[top];
top--;
return value;
}
public void list(){
if(isEmpty()){
System.out.println("栈空,没有数据");
return;
}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=num2 * num1;break;
case'/': res=num2 / num1;break;
default: break;
}
return res;
}
}