数组模拟栈实现计算器功能(加减乘除)
package stack;
public class Calculator {
public static void main(String[] args) {
//根据思路实现表达式的运算
String expression="30+3*2-6+6/2+356*2";
//创建两个栈
ArrayStack2 numstack=new ArrayStack2(10);
ArrayStack2 operstack=new ArrayStack2(10);
//定义需要的相关变量
int index=0;
int num1=0;
int num2=0;
int oper=0;
int res=0;
char ch=' ';//将每次扫描得到的char保存到ch中
String keepNum="";//用于拼接多位数
//开始while循环扫描expression
while(true){
//先得到expression中的每一个字符
ch = expression.substring(index,index+1).charAt(0);
// System.out.println(ch);
//判断ch是什么,做相应的处理
if(operstack.isOper(ch)){
//如果是运算符
//判断当前的栈是否为空
if(!operstack.isEmpty()){
//处理
if(operstack.priority(ch) <= operstack.priority(operstack.peek())){
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{
//如果是数,则直接入数栈
//注意此时是一个字符,需要计算
//需要判断
// numstack.push(ch-48);
keepNum+=ch;
if(index==expression.length()-1){
numstack.push(Integer.parseInt(keepNum));
}else {
//判断下一个字符是不是数字,如果是主子,就继续扫描,如果是运算符了就直接入栈
//这个一定要注意判断是否是最后一位
//注意这里不用index++
if (operstack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
numstack.push(Integer.parseInt(keepNum));
keepNum = "";
//不要忘记清空
}
}
}
//让index+1,并判断是否扫描到expression最后
index++;
if(index >= expression.length()){
break;
}
}
//扫描完毕吼,计算
while(true){
//如果符号栈为空,则计算到最后的结果,数栈中只有一个数字,即结果
if(operstack.isEmpty()){
break;
}
else{
num1=numstack.pop();
num2=numstack.pop();
oper=operstack.pop();
res=numstack.cal(num1,num2,oper);
numstack.push(res);//入栈
}
}
System.out.println("表达式是"+expression+"结果:"+numstack.pop());
}
}
class ArrayStack2{
private int maxsize;//栈的大小
private int[] stack;//数组,数组模拟数据
private int top=-1;//表示栈顶,初始化
//构造器
public ArrayStack2(int maxsize){
this.maxsize=maxsize;
stack=new int[this.maxsize];
}
//栈满
public boolean isFull(){
return top==maxsize-1;
}
//栈空
public boolean isEmpty(){
return top==-1;
}
//入栈
public void push(int value){
if(isFull()){
throw new RuntimeException("栈已经满了");
}
top++;
stack[top]=value;
}
//出栈
public int pop(){
if(isEmpty()){
//抛出异常
System.out.println("栈为空,没有数据");
}
int value = stack[top];
top--;
return value;
}
//显示栈的情况[遍历栈],遍历时,需要从栈顶开始i显示数据
public void list(){
if(isEmpty()){
System.out.println("栈为空,没有数据!!");
return;
}
//需要从栈顶开始显示数据
for(int i=top;i>=0;i--){
System.out.println("stack["+i+"]"+"->"+stack[i]);
}
}
//返回运算符的优先级,优先级时程序员来确定的
//优先级使用数字表示,数字越大则优先级越高
public int priority(int oper){
if(oper == '*' || oper== '/'){
return 1;
}else if(oper == '+' || oper=='-'){
return 0;
}else{
return -1;//假定目前的运算符只有+,-,*,/
}
}
//增加一个方法,可以返回当前栈顶的值,但不是真正的pop
public int peek(){
return stack[top];
}
//判断是否是一个运算符
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;
}
}