用数组实现栈
package arrayStack;
public class ArrayStack {
public int max;
public int top = -1;
public int[] s;
public ArrayStack(int max){
this.max=max;
s = new int[this.max];
}
public boolean isFull(){
return top == max-1;
}
public boolean isEmpty(){
return top==-1;
}
public void push(int value){
if(isFull()){
System.out.println("已满,不可再加数据");
}
top++;
s[top]=value;
}
public int pop(){
if (isEmpty()){
System.out.println("空了");
}
int value = s[top];
top--;
return value;
}
//取栈顶元素
public int getTop(){
return s[top];
}
public void list(){
for(int i=top;i>=0;i--){
System.out.println(s[i]);
}
}
}
用栈实现计算单位数的计算器(只有加减乘除)
在上一个类里添加一些需要用到的方法
package arrayStack;
public class ArrayStack2 {
public int max;
public int top = -1;
public int[] s;
public ArrayStack2(int max){
this.max=max;
s = new int[this.max];
}
public boolean isFull(){
return top == max-1;
}
public boolean isEmpty(){
return top==-1;
}
public void push(int value){
if(isFull()){
System.out.println("已满,不可再加数据");
}
top++;
s[top]=value;
}
public int pop(){
if (isEmpty()){
System.out.println("空了");
}
int value = s[top];
top--;
return value;
}
//取栈顶元素
public int getTop(){
return s[top];
}
public void list(){
for(int i=top;i>=0;i--){
System.out.println(s[i]);
}
}
public int priority(int oper){
if(oper == '*' || oper == '/'){
return 1;
}else if(oper == '+' || oper == '-'){
return 0;
}else{
return -1;
}
}
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;
}
public boolean isOper(int x){
return x =='+' || x =='-' || x =='*' || x == '/';
}
}
实现
package arrayStack;
public class Calculator {
public static void main(String[] args) {
String expression = "3+2*5-9";
int index = 0;
int num1 = 0;
int num2 = 0;
int oper = 0;
int result = 0;
ArrayStack2 numStack = new ArrayStack2(5);
ArrayStack2 operStack = new ArrayStack2(5);
char ch = ' ';//必须有空格
while (true) {
//substring() 方法返回字符串的子字符串,charAt(0)检索str中的第一个字符(为了把字符串转换成字符才用这个)
ch = expression.substring(index,index+1).charAt(0);
if(operStack.isOper(ch)){
if(!operStack.isEmpty()){
if(operStack.priority(ch)<=operStack.priority(operStack.getTop())){
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
result = numStack.cal(num1,num2,oper);
numStack.push(result);
operStack.push(ch);
}else{
operStack.push(ch);
}
}else{
operStack.push(ch);
}
}else{
numStack.push(ch-48);
}
index++;
if(index >= expression.length()){
break;
}
}
while(true){
if(operStack.isEmpty()){
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
result = numStack.cal(num1,num2,oper);
numStack.push(result);
}
System.out.println(numStack.pop());
}
}
多位数明天再写
package arrayStack;
public class Calculator {
public static void main(String[] args) {
String expression = "30+2*5-9";
int index = 0;
int num1 = 0;
int num2 = 0;
int oper = 0;
int result = 0;
String keepNum= "";// 校验数字是否为多位数时用的
ArrayStack2 numStack = new ArrayStack2(5);
ArrayStack2 operStack = new ArrayStack2(5);
char ch = ' ';//必须有空格
while (true) {
//substring() 方法返回字符串的子字符串,charAt(0)检索str中的第一个字符(为了把字符串转换成字符才用这个)
ch = expression.substring(index,index+1).charAt(0);
if(operStack.isOper(ch)){
if(!operStack.isEmpty()){
if(operStack.priority(ch)<=operStack.priority(operStack.getTop())){
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
result = numStack.cal(num1,num2,oper);
numStack.push(result);
operStack.push(ch);
}else{
operStack.push(ch);
}
}else{
operStack.push(ch);
}
}else{
keepNum+=ch;
if(index==expression.length()-1){
// 如果当前已经是最后一个字符了,用不着校验他后面是数字还是运算符了直接push
numStack.push(Integer.parseInt(keepNum));
}else{
if(operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
// 如果下一个字符是运算符,结束拼接!直接push
numStack.push(Integer.parseInt(keepNum));
// 下一次还要用,别忘记清零
keepNum="";
}
}
}
index++;
if(index >= expression.length()){
break;
}
}
while(true){
if(operStack.isEmpty()){
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
result = numStack.cal(num1,num2,oper);
numStack.push(result);
}
System.out.println(numStack.pop());
}
}