java堆栈下溢
我正在处理这个任务,并且线程中不断出现异常 “main”java.lang.RuntimeException:Stack.pop处的堆栈下溢(Postfix.java:74) 位于Postfix.eval(Postfix.java:221)和Postfix.main(Postfix.java:112) 我不知道为什么我看了一下堆栈并把它写对了,我看不出为什么当(3*4)/5时它会弹出的问题java堆栈下溢,java,stack,underflow,Java,Stack,Underflow,我正在处理这个任务,并且线程中不断出现异常 “main”java.lang.RuntimeException:Stack.pop处的堆栈下溢(Postfix.java:74) 位于Postfix.eval(Postfix.java:221)和Postfix.main(Postfix.java:112) 我不知道为什么我看了一下堆栈并把它写对了,我看不出为什么当(3*4)/5时它会弹出的问题 import java.io.IOException; 类字符堆栈 { 私有最终整数堆栈大小=80; 私人
import java.io.IOException;
类字符堆栈
{
私有最终整数堆栈大小=80;
私人int top;
私人物品;
公共字符堆栈(){
items=新字符[STACKSIZE];
top=-1;
}
公共布尔空(){
如果(顶部==-1){
返回true;
}
返回false;
}
公共字符pop(){
if(空()){
抛出新的运行时异常(“堆栈下溢”);
}
返回项目[顶部--];
}
公共无效推送(字符符号)
{
if(top==堆栈大小-1){
抛出新的运行时异常(“堆栈溢出”);
}
项目[++顶部]=符号;
}
公共字符peek(){
if(空()){
抛出新的运行时异常(“堆栈下溢”);
}
返回项目[顶部];
}
}
类堆栈{
私有最终整数堆栈大小=80;
私人int top;
私人双[]项目;
公共堆栈(){
项目=新的双[堆叠大小];
top=-1;
}
公共无效推送(双x)
{
if(top==堆栈大小-1){
抛出新的运行时异常(“堆栈溢出”);
}
项目[++顶部]=x;
}
公共双pop(){
if(空()){
系统输出打印(顶部);
抛出新的运行时异常(“堆栈下溢”);
}
返回项目[顶部--];
}
公共双峰{
if(空()){
抛出新的运行时异常(“堆栈下溢”);
}
返回项目[顶部];
}
布尔空()
{
如果(顶部==-1){
返回true;
}
返回false;
}
}
公共类后缀{
公共最终静态int MAXCOLS=80;
公共静态void main(字符串[]args)引发IOException{
字符串中缀,pfix;
System.out.println(“输入中缀字符串:”);
中缀=readString().trim();
System.out.println(“原始中缀表达式为:“+infix”);
pfix=后缀(中缀);
System.out.println(“后缀表达式为:“+pfix”);
System.out.println(“值为:”+eval(pfix));
}//末端总管
公共静态布尔等标量(char x)
{
如果(x=='+')
{
返回false;
}
else如果(x='-')
{
返回false;
}
else如果(x=='*')
{
返回false;
}
else如果(x=='/'))
{
返回false;
}
如果(x='$'),则为else
{
返回false;
}
返回true;
}
公共静态整型操作优先级(字符操作)
{
if(oper=='+'| | oper=='-')
{
返回1;
}
else if(oper=='*'| | oper=='/'))
{
返回2;
}
else if(oper=='$')
{
返回3;
}
返回0;
}
公共静态布尔优先级(字符顶端,字符符号)
{
if((top!='('| | top!='))&&symb=='(')
{
返回false;
}
if(top='('&&(symb!='('| | symb!='))
{
返回false;
}
else if((top!='(“| | top!=')”)和&symb='))
{
返回true;
}
int opcode1,opcode2;
opcode1=操作优先级(顶部);
opcode2=操作优先级(symb);
如果(opcode1>=opcode2){
返回true;
}
返回false;
}
公共静态字符串readString()引发IOException{
char[]charArray=新字符[80];
int位置=0;
字符c;
而((c=(char)System.in.read())!='\n'){
charArray[position++]=c;
}
return String.copyValueOf(charArray,0,position);//将字符数组转换为字符串,从0到position-1之间开始
}//结束读取字符串
公共静态双值(字符串中缀){
字符c;
内部位置;
双opnd1,opnd2,值;
Stack optndstk=新堆栈();
对于(位置=0;位置import java.io.IOException;
class CharStack
{
private final int STACKSIZE= 80;
private int top;
private char[] items;
public CharStack(){
items = new char[STACKSIZE];
top =-1;
}
public boolean empty() {
if(top==-1){
return true;
}
return false;
}
public char pop() {
if(empty()){
throw new RuntimeException("Stack Underflow");
}
return items[top--];
}
public void push(char symb)
{
if(top == STACKSIZE -1) {
throw new RuntimeException("Stack Overflow");
}
items[++top] =symb;
}
public char peek() {
if(empty()){
throw new RuntimeException("Stack Underflow");
}
return items[top];
}
}
class Stack {
private final int STACKSIZE= 80;
private int top;
private double[] items;
public Stack(){
items = new double[STACKSIZE];
top =-1;
}
public void push(double x)
{
if(top == STACKSIZE -1) {
throw new RuntimeException("Stack Overflow");
}
items[++top] =x;
}
public double pop(){
if(empty()){
System.out.print(top);
throw new RuntimeException("Stack Underflow");
}
return items[top--];
}
public double peek() {
if(empty()){
throw new RuntimeException("Stack Underflow");
}
return items[top];
}
boolean empty()
{
if(top==-1){
return true;
}
return false;
}
}
public class Postfix {
public final static int MAXCOLS = 80;
public static void main(String[] args) throws IOException {
String infix, pfix;
System.out.println("Enter a infix String: ");
infix = readString().trim();
System.out.println("The original infix expr is: " + infix);
pfix = postfix(infix);
System.out.println("The Postfix expr is: " + pfix);
System.out.println("The value is : " + eval(pfix));
} // end main
public static boolean isOperand(char x)
{
if(x == '+')
{
return false;
}
else if(x == '-')
{
return false;
}
else if (x == '*')
{
return false;
}
else if (x == '/')
{
return false;
}
else if ( x== '$')
{
return false;
}
return true;
}
public static int operPrecedence(char oper)
{
if(oper == '+'||oper == '-' )
{
return 1;
}
else if (oper == '*' || oper == '/')
{
return 2;
}
else if (oper == '$')
{
return 3;
}
return 0;
}
public static boolean precedence(char top, char symb)
{
if ((top != '('||top != ')')&&symb == '(')
{
return false;
}
if (top == '(' && (symb != '('||symb != ')') )
{
return false;
}
else if((top != '('||top != ')')&&symb ==')' )
{
return true;
}
int opcode1, opcode2;
opcode1 =operPrecedence(top) ;
opcode2 =operPrecedence(symb) ;
if(opcode1>=opcode2){
return true;
}
return false;
}
public static String readString() throws IOException {
char[] charArray = new char[80];
int position = 0;
char c;
while ((c = (char) System.in.read()) != '\n') {
charArray[position++] = c;
}
return String.copyValueOf(charArray, 0, position); // turns a character array into a string, starting between zero and position-1
}// end read string
public static double eval(String infix) {
char c;
int position;
double opnd1, opnd2, value;
Stack opndstk = new Stack();
for (position = 0; position < infix.length(); position++) {
c = infix.charAt(position);
if (Character.isDigit(c)) // operand-convert the character represent of
// the digit into double and push it into the
// stack
{
opndstk.push((double) Character.digit(c, 10));
} else {
// operator
opnd2 = opndstk.pop();
opnd1 = opndstk.pop();
value = oper(c, opnd1, opnd2);
opndstk.push(value);
} // else
} // end for
return opndstk.pop();
}// end eval
public static String postfix(String infix) {
int position, outpos = 0;
char symb;
char[] postr = new char[MAXCOLS];
CharStack opstk = new CharStack();
for (position = 0; position < infix.length(); position++) {
symb = infix.charAt(position);
if (isOperand(symb)) {
postr[outpos++] = symb;
} else {
while (!opstk.empty() && precedence(opstk.peek(), symb)) {
postr[outpos++] = opstk.pop();
} // end while
if (symb != ')') {
opstk.push(symb);
} else {
opstk.pop();
}
} // end else
} // end for
while (!opstk.empty()) {
postr[outpos++] = opstk.pop();
}
return String.copyValueOf(postr, 0, outpos);
}// end pos
public static double oper(char symb, double op1, double op2) {
double value = 0;
switch (symb) {
case '+':
value = op1 + op2;
break;
case '-':
value = op1 - op2;
break;
case '*':
value = op1 * op2;
break;
case '/':
value = op1 / op2;
break;
case '$':
value = Math.pow(op1, op2);
break;
default:
throw new RuntimeException("illegal operator: " + symb);
}// end switch
return value;
}// end oper
}
else if (x == '(')
{
return true;
}
else if (x == ')')
{
return true;
}
Exception in thread "main" java.lang.RuntimeException: Stack Underflow
at Stack.pop(Postfix.java:74)
at Postfix.eval(Postfix.java:221)
at Postfix.main(Postfix.java:112)