java之栈的实现
文章内容选自尚硅谷,jdk8,eclipse环境
栈的实现
代码如下
package stack;
import java.util.Scanner;
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(5);
Scanner scanner = new Scanner(System.in);
boolean loop = true;
String key = "";
while(loop){
System.out.println("show(显示栈)");
System.out.println("exit(退出栈)");
System.out.println("push(数据入栈)");
System.out.println("pop(数据出栈)");
key = scanner.next();
switch(key){
case "show":
stack.show();
break;
case "exit":
scanner.close();
loop = false;
break;
case "push":
System.out.println("请输入一个数据");
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try{
int tmp = stack.pop();
System.out.printf("取出的数据为%d\n",tmp);
}catch(RuntimeException e){
System.out.println(e.getMessage());
}
break;
default:
break;
}
}
System.out.println("程序运行结束");
}
}
class ArrayStack{
private int maxSize;
private int[] array;
private int top=-1;
public ArrayStack(int maxSize){
this.maxSize = maxSize;
array = new int[maxSize];
}
public boolean isFull(){
return top == maxSize-1;
}
public boolean isEmpty(){
return top == -1;
}
public void push(int n){
boolean isFull = isFull();
if(isFull){
System.out.println("栈满,不能放入数据");
return;
}
top ++;
array[top] = n;
}
public int pop(){
boolean isEmpty = isEmpty();
if(isEmpty){
throw new RuntimeException("栈空,不能取出数据");
}
int value = array[top];
top--;
return value;
}
public void show(){
boolean isEmpty = isEmpty();
if(isEmpty){
System.out.println("栈空,不能取出数据");
return;
}
for(int i = top;i>-1;i--){
System.out.printf("栈内的数据为array[%d]=%d\n",i,array[i]);
}
}
}
不到一百行代码,乏善可陈。
java栈实现综合计算器(四则运算)
按照尚硅谷给出的代码,最多只能实现两位数的四则运算,我略作修改,实现多位数的四则运算。
package stack;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
int res = 0;
int num1 = 0;
int num2 = 0;
int oper = 0;
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
char ch = ' ';
System.out.println("请输入数学表达式");
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
int index = 0;
String keepString = "";
while(true){
if(operStack.isOper(str.substring(index,index+1).charAt(0))){
ch = str.substring(index,index+1).charAt(0);
if(operStack.isEmpty()){
operStack.push(ch);
}else{
if(operStack.priority(ch)<=operStack.priority(operStack.peek())){
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.calculate(num1, num2, oper);
numStack.push(res);
operStack.push(ch);
}else{
operStack.push(ch);
}
}
}else{
keepString += str.substring(index,index+1).charAt(0);
if(index == str.length()-1 ){
numStack.push(Integer.parseInt(keepString));
break;
}else{
for (int i = 1; ; i++) {
if (!operStack.isOper(str.substring(index + i, index + i+1).charAt(0))) {
if((index+i) == (str.length()-1)){
keepString += str.substring(index + i).charAt(0);
numStack.push(Integer.parseInt(keepString));
index = index + i ;
break;
}
keepString += str.substring(index + i, index + i+1).charAt(0);
// index++;
// numStack.push(Integer.parseInt(keepString));
} else {
numStack.push(Integer.parseInt(keepString));
index = index + i -1;
break;
}
}
}
}
keepString = "";
index ++;
if(index == str.length())
break;
}
scanner.close();
while(!operStack.isEmpty()){
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.calculate(num1, num2, oper);
numStack.push(res);
}
System.out.println("输出结果为"+res);
}
}
class ArrayStack2{
private int maxSize;
private int[] array;
private int top=-1;
public ArrayStack2(int maxSize){
this.maxSize = maxSize;
array = new int[maxSize];
}
public boolean isFull(){
return top == maxSize-1;
}
public boolean isEmpty(){
return top == -1;
}
public void push(int n){
boolean isFull = isFull();
if(isFull){
System.out.println("栈满,不能放入数据");
return;
}
top ++;
array[top] = n;
}
public int pop(){
boolean isEmpty = isEmpty();
if(isEmpty){
throw new RuntimeException("栈空,不能取出数据");
}
int value = array[top];
top--;
return value;
}
public void show(){
boolean isEmpty = isEmpty();
if(isEmpty){
System.out.println("栈空,不能取出数据");
return;
}
for(int i = top;i>-1;i--){
System.out.printf("栈内的数据为array[%d]=%d\n",i,array[i]);
}
}
public int priority(int oper){
if((oper == '*') || (oper =='/')){
return 1;
}else if((oper == '+') || (oper == '-')){
return 0;
}else
return -1;
}
public boolean isOper(int oper){
return (oper == '*') || (oper == '/') || (oper == '+') || (oper == '-');
}
public int calculate(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;
}
return res;
}
public int peek(){
return array[top];
}
}
运算结果
请输入数学表达式
123+98 * 89+99 * 33-89
输出结果为12023
思路分析
图片选自尚硅谷
综合计算器就是把输入的字符串转换为数字和运算符,并实现计算。就是要创建两个栈,一个符号栈,一个数栈,然后根据符号的优先级,决定运算的顺序。
在实现多位数的四则运算的时候,我用一个for循环来判断一个多位数的位数,实现多位数的运算。