大虾进来看看,帮个忙啊~~~~

xbdarling 2009-04-12 11:46:33
题目是使用堆栈,将中缀算术表达式转换成后缀表达式。
我写的程序具体如下:

public class InfixToPostfix {

static String[] myStack=new String[50];
static int stackLength=0;
static final boolean STACK_FULL=true;
static final String STACK_EMPTY="--------------------------------";
//static String myResult;

public static void main(String[] args) {
// TODO Auto-generated method stub
String expStr="A/B-C*(D-A*100.123)/B-C";
String expStr1="A:/:B:-:C:*:(:D:-:A:*:100.123:):/:B:-:C";
String[]arrStr;
arrStr=expStr1.split(":");
for(int i=0;i<arrStr.length;i++){
if(arrStr[i].equals("+")||arrStr[i].equals("-")||arrStr[i].equals("*")||arrStr[i].equals("/")||arrStr[i].equals("%")||arrStr[i].equals("(")||arrStr[i].equals(")")){
if(isEmpty()||arrStr[i].equals("(")){//栈空或左括号进栈
push(arrStr[i]);
}
else{
if(!isEmpty()&&(Precedence(arrStr[i].charAt(0))>Precedence(myStack[stackLength-1].charAt(0)))){
push(arrStr[i]);
}
if(!isEmpty()&&Precedence(arrStr[i].charAt(0))!=0&&(Precedence(arrStr[i].charAt(0))<=Precedence(myStack[length()-1].charAt(0)))){
//这里如果没有Precedence(arrStr[i].charAt(0))!=0,会打出右括号
System.out.println(topValue());
stackLength--;
push(arrStr[i]);
}
if(!isEmpty()&&arrStr[i].compareTo(")")==0){while(!myStack[stackLength-1].equals("("))
{System.out.println(pop());}stackLength--;}}//stackLength--用来清除左括号
}

else
//如果为操作数,直接显示
System.out.println(arrStr[i]);

}
System.out.println(pop());//打出栈中内容
}
static int Precedence(char op) { //返回运算符op所对应的优先级数值
switch(op) {
case '+':
case '-':
return 1; //定义加减运算的优先级为1
case '*':
case '/':
case '%':
return 2;
case '(':
default:
return 0;//定义乘除运算的优先级为
}
}

static int myCompiler()
{
return 0;
}
static boolean push(String str)
{
if(stackLength<50)
{
myStack[stackLength++]=str;
return !STACK_FULL;
}
else
return STACK_FULL;
}

static String pop()
{
if(!isEmpty())
return myStack[--stackLength];
else
return STACK_EMPTY;
}
static boolean isFull()
{
if (stackLength==50)
return true;
else
return false;
}
static boolean isEmpty()
{
if (stackLength==0)
return true;
else
return false;
}
static int length()
{
return stackLength;
}
static String topValue()
{
if(!isEmpty())
return myStack[stackLength-1];
else
return STACK_EMPTY;
}

}



结果打出为:
A
B
/
C
*
D
-
A
*
100.123
*
-
*
B
/
C
-       正确的结果应该是AB/CDA100.123*-*B/C--吧,我错在哪啊,~~~
...全文
75 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
obama0909 2009-08-11
...up
回复
Steve 2009-04-13
ToPostfix概念不清,呵呵.
回复
Steve 2009-04-13
public static void main(String[] args) {
String expStr="A/B-C*(D-A*100.123)/B-C";
String expStr1="A:/:B:-:C:*:(:D:-:A:*:100.123:):/:B:-:C";
String[]arrStr;
arrStr=expStr1.split(":");
for(int i=0;i <arrStr.length;i++)
{
if(arrStr[i].equals("+")||arrStr[i].equals("-")||arrStr[i].equals("*")||arrStr[i].equals("/")||arrStr[i].equals("%")||arrStr[i].equals("(")||arrStr[i].equals(")")){
if(isEmpty()||arrStr[i].equals("(")){// stack empty or (
push(arrStr[i]);
}
else{
if(!isEmpty()&&(Precedence(arrStr[i].charAt(0))>=Precedence(myStack[stackLength-1].charAt(0)) || myStack[stackLength-1].charAt(0)=='(') ){
push(arrStr[i]);
} else if (!isEmpty()&&Precedence(arrStr[i].charAt(0))!=0&&(Precedence(arrStr[i].charAt(0)) <Precedence(myStack[length()-1].charAt(0)))){
//if not Precedence(arrStr[i].charAt(0)!=0, then print)
while (!isEmpty() && Precedence(arrStr[i].charAt(0)) <Precedence(myStack[length()-1].charAt(0))) {
System.out.println(topValue());
stackLength--;
}

push(arrStr[i]);
} else if(!isEmpty()&&arrStr[i].compareTo(")")==0){
while(!myStack[stackLength-1].equals("("))
{System.out.println(pop());}stackLength--;
System.out.println(pop());
}
}
} //if
else //display number
System.out.println(arrStr[i]);

} //for
while (!isEmpty())
System.out.println(pop());//打出栈中内容
} // main
回复
发帖
Eclipse
创建于2007-09-28

5.8w+

社区成员

Java Eclipse
申请成为版主
帖子事件
创建了帖子
2009-04-12 11:46
社区公告
暂无公告