思路
代码(可以看到这里的数字只能是单位数字,那么如何改成可以是多位数呢?!往下看)
package stack;
public class Calculator {
public static void main(String[] args) {
String expression = "7+2*6-2*2";
ArrayStack1 numstack = new ArrayStack1(10);
ArrayStack1 operstack = new ArrayStack1(10);
int index = 0;
int num1 = 0, num2 = 0;
int oper = 0;
int res = 0;
char ch = ' ';
while(true){
ch = expression.substring(index,index+1).charAt(0);
if (operstack.isOper(ch)){
if (!operstack.isEmpty()){
if (operstack.priority(ch) <= operstack.priority(operstack.peekTop())){
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);
}
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.printf("表达式 %s = %d",expression,numstack.pop());
}
}
class ArrayStack1{
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack1(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
public int peekTop(){
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()){
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
public void list(){
if (isEmpty()){
System.out.println("栈空");
}
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;
}
return res;
}
}
找到是哪个地方有问题?,发现是一个数字就直接入栈了,那么需要在入数栈时,需要向后看,如果是数就拼接并继续扫描,如果是符合才入栈。(那么就需要一个字符串变量用于拼接)
代码
package stack;
public class Calculator {
public static void main(String[] args) {
String expression = "71+2*6-2*2";
ArrayStack1 numstack = new ArrayStack1(10);
ArrayStack1 operstack = new ArrayStack1(10);
int index = 0;
int num1 = 0, 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.peekTop())){
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.printf("表达式 %s = %d",expression,numstack.pop());
}
}
class ArrayStack1{
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack1(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
public int peekTop(){
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()){
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
public void list(){
if (isEmpty()){
System.out.println("栈空");
}
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;
}
return res;
}
}