1. 使用数组模拟栈
class ArrayStack{
public int maxSize
;
public int[] stack
;
public int top
= -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()){
System
.out
.println("栈空");
return -1;
}
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
]);
}
}
}
2. 创建stack
import java
.util
.Scanner
;
public class ArrayStackDemo {
public static void main(String
[] args
) {
ArrayStack arrayStack
= 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":
arrayStack
.list();
break;
case "push":
System
.out
.println("请输入一个数");
int value
= scanner
.nextInt();
arrayStack
.push(value
);
break;
case "pop":
int pop
= arrayStack
.pop();
System
.out
.println("出栈的数据为:"+pop
);
break;
case "exit":
scanner
.close();
loop
= false;
break;
default:
break;
}
}
System
.out
.println("程序退出了");
}
}
结果
3. 使用数组模拟stack并模拟计算器
思路:重点
class ArrayStack2{
public int maxSize
;
public int[] stack
;
public 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()){
System
.out
.println("栈满");
return;
}
top
++;
stack
[top
]=value
;
}
public int pop(){
if (isEmpty()){
System
.out
.println("栈空");
return -1;
}
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
= num1
* num2
;
break;
case '/':
res
= num2
/ num1
;
break;
default:
break;
}
return res
;
}
public int peek(){
return stack
[top
];
}
}
4. 模拟计算器
public class Calculator {
public static void main(String
[] args
) {
String expression
= "70+2*6-4";
ArrayStack2 numStack
= new ArrayStack2(20);
ArrayStack2 operStack
= new ArrayStack2(20);
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);
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 {
keepNum
+= 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
));
keepNum
= "";
}
}
}
index
++;
if (index
>=expression
.length()){
break;
}
}
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());
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-21746.html