数组模拟栈:
- 使用数组模拟栈
- 定义变量top表示栈顶,初始化为-1
- 入栈的操作,当有数据加入到栈,top++,stack[top] = data;
- 出栈的操作,int value = stack[top] ,top–,return value
import java.util.Scanner;
class ArrayStackDemo
{
public static void main(String[] args)
{
ArrayStack stack = new ArrayStack(4);
String key = "";
boolean loop = true; //控制是否退出栈
Scanner scanner = new Scanner(System.in);
while (loop)
{
System.out.println("show:表示显示栈");
System.out.println("exit:退出程序");
System.out.println("push:进栈");
System.out.println("pop:出栈");
System.out.println("请输入你的选择:");
key = scanner.next();
switch (key)
{
case "show":
stack.list();
break;
case "push":
System.out.println("请输入一个数:");
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try
{
int res = stack.pop();
System.out.printf("出栈的数据是:%d\n",res);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
stack.list();
break;
case "exit":
scanner.close();
loop = false;
break;
}
}
System.out.println("程序退出");
}
}
//表示栈
class ArrayStack
{
private int maxSize; //栈的大小
private int[] stack; //数组模拟栈,数据放在数组
private int top = -1; //表示栈顶,初始化-1
//构造函数
public ArrayStack(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())
{
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
//出栈
public int pop() {
if (isEmpty())
{
//抛出异常信息,不需要有return
throw new RuntimeException("栈空,没有数据");
}
int value = stack[top];
top--;
return value;
}
//遍历栈,从栈顶开始显示数据
public void list() {
if (isEmpty())
{
//抛出异常信息,不需要有return
throw new RuntimeException("栈空,没有数据");
}
for (int i = top;i >= 0 ;i-- )
{
System.out.printf("stack[%d]=%d\n",i,stack[i]);
}
}
}
栈实现综合计算器:
package dk.hello;
public class Calculator {
public static void main(String[] args) {
// 根据前面思路完成运算
String expression = "30+2*6-2"; //只能运算个位数
// 创建两个栈:数栈,符号栈
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
// 定义相关变量
int index = 0;
int num1 = 0;
int num2 = 0;
int oper = 0; // 字符也可以定义为int
int res = 0;
char ch = ' '; // 将每次扫描char保存到ch
String keepNum = ""; //用于拼接多位数
// 开始用while扫描expression
while (true) {
// 依次得到expression的每一个字符
ch = expression.substring(index, index + 1).charAt(0); // substring为一个字符串取出来一个字符
// 判断是什么
if (operStack.isOper(ch)) { // 是否为字符
if (!operStack.isEmpty()) { //不为空
//如果符号栈有操作符,就进行比较,如果当前操作符优先级小于等于栈中的操作符
//就需要从数栈中pop出两个数,再从符号栈中pop出一个符号进行运算得到结果入数栈
//然后将当前的操作符入符号栈
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 {
//如果当前操作符优先级大于栈中操作符,就直接入符号栈
numStack.push(ch);
}
}else {
// 如果为空就直接入符号栈
operStack.push(ch);
}
}else { //如果为数,则直接入数栈
//numStack.push(ch - 48); //ASCII中数字1是49,相差48
//不能发现一个数就立即入数栈,有可能是多位数
//需要向后面再看一位,如果是数就不能入栈
//需要定义一个字符串变量用于拼接
//处理多位数
keepNum += ch;
//如果ch是最后一位就直接入栈
if (index == expression.length()-1) {
numStack.push(Integer.parseInt(keepNum)); //将keepNum转化为Int再入栈
} else {
// 判断下一个字符是不是数字,如果是数字就继续扫描,如果是运算符就入栈
if (operStack.isOper(expression.substring(index+1, index+2).charAt(0))) {
//后一位是运算符入栈
numStack.push(Integer.parseInt(keepNum)); //将keepNum转化为Int再入栈
keepNum = "";
}
}
}
//扫描index,当大于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.println("结果;"+numStack.pop());
}
}
//创建栈
class ArrayStack2 {
private int maxSize; // 栈的大小
private int[] stack; // 数组模拟栈,数据放在数组
private int top = -1; // 表示栈顶,初始化-1
// 构造函数
public ArrayStack2(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//增加一个方法,可以返回当前栈顶的值,但是不是真正的pop
public int peek() {
return stack[top];
}
// 判断栈满
public boolean isFull() {
return top == maxSize - 1;
}
// 判断栈空
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()) {
// 抛出异常信息,不需要有return
throw new RuntimeException("栈空,没有数据");
}
int value = stack[top];
top--;
return value;
}
// 遍历栈,从栈顶开始显示数据
public void list() {
if (isEmpty()) {
// 抛出异常信息,不需要有return
throw new RuntimeException("栈空,没有数据");
}
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 val) { // val是字符,int和char可以混用
int res = 0; // 存放计算结果
switch (val) {
case '*':
res = num1 * num2;
break;
case '/':
res = num2 / num1; // 注意顺序
break;
case '+':
res = num1 + num2;
break;
case '-':
res = num2 - num1; // 注意顺序
break;
default:
break;
}
return res;
}
}