要求:是做一个能实现计算功能的程序。比如,我输入(5-2)*8,它能给出24这个结果;并且能够实现字母的运算和赋值,同时以树形图的样子显示出来,比如,我输入(a-b)*c,它能够给出
*
/ \
- c
/ \
a b
这样的能够示意逻辑结构的树形图。
如果赋值a=5,b=2,c=8,它又能给出结果24.核心代码都实现了,就是输出表示二叉树比较繁琐,请教大家
该如何做,在控制台下输出可以,当然如果能搞成界面的,就更好了,多谢
以实现的代码如下:
import java.util.Stack;public class MathExpression{
private String str_exp;
private Object[] infix;
private Object[] postfix;
private int infix_size,postfix_size;
// 定义一些便于比较的变量
final Character left=new Character('(');
final Character right=new Character(')');
final Character add=new Character('+');
final Character sub=new Character('-');
final Character mul=new Character('*');
final Character div=new Character('/'); public MathExpression() { }
public MathExpression(String expression)
{
String exp=expression;
infix=new Object[exp.length()+1];
int i=0,j=0; //把表达式分离成类元件,形式是中缀表达式
char ch;
String operand="";
while(i < exp.length())
{
ch=exp.charAt(i); if(ch=='(')
infix[j++]=new Character(ch);
else if(ch==')')
{
if(exp.charAt(i-1)!=')')
{
infix[j++]=operand;
operand="";
infix[j++]=new Character(ch);
}
else
infix[j++]=new Character(ch);
}
else if((ch=='+')|| (ch=='-')||
(ch=='*')|| (ch=='/'))
{
if(operand!="")
{
infix[j++]=operand;
operand="";
}
infix[j++]=new Character(ch);
}
else
{
operand+=ch;
}
i++;
}//end while
if(operand!="")
{
infix[j]=operand;
infix_size=j+1;
}
else
{
infix_size=j;
}//end if
//把中缀表达式转变为后缀表达式
postfix=new Object[infix_size];
j=0;
Stack stack=new Stack();
Object temp;
Object remove; for(i=0; i < infix_size; i++)
{
temp = infix[i];
if( temp instanceof Character )
{
if(temp.equals(left))
stack.push(temp);
else if(temp.equals(right))
{
while( !(stack.peek()).equals(left) && !stack.isEmpty() )
{
postfix[j++]=stack.pop();
}//end while
remove=stack.pop();
}
else // temp is an operator
{
while(!stack.isEmpty()
&& !(stack.peek()).equals(left)
&& precedence(temp) <= precedence(stack.peek()))
{
postfix[j++]=stack.pop();
}//end while
stack.push(temp);
}
}//end if
else // temp is a number
if(temp instanceof String )
{
postfix[j++]=temp;
}
}//end for
while(!stack.isEmpty())
{
postfix[j++]=stack.pop();
}//end while
postfix_size=j;
//end transfer
} //end constructor
public double value() //return the result of the expression
{
double number1=0,number2=0,result=0;
int i=0,j=0;
Stack stack=new Stack();
Object temp;
for(i=0; i<postfix_size; i++)
{ temp=postfix[i];
if( temp instanceof String )
stack.push(temp);
else
{ //temp is an operator
number2=Double.parseDouble(""+stack.pop());
number1=Double.parseDouble(""+stack.pop());
if(temp.equals(add))result=number1+number2;
if(temp.equals(sub))result=number1-number2;
if(temp.equals(mul))result=number1*number2;
if(temp.equals(div))result=number1/number2;
stack.push(""+result);
}
}//end for
return result;
}
public String getPostfix()
{
String str="";
for(int i=0; i<postfix_size; i++)
{
str+=postfix[i]+"";
}//end for
return str+"size is"+postfix_size;
}
public String getInfix()
{
String str="";
for(int i=0; i<infix_size; i++)
{
str+=infix[i]+"";
}//end for
return str+"size is"+infix_size;
}
private int precedence(Object op)
{
if(op.equals(mul) || op.equals(div))
{
return 1;
}
else
{
return 0;
}
}
public static void main(String[] args){
double result;
String infix_string;
String postfix_string;
String str_exp; System.out.println("Please input the expression:");
MathExpression testExpression = new MathExpression("5*(8-2)+9");
infix_string = testExpression.getInfix();
postfix_string = testExpression.getPostfix();
result = testExpression.value();
System.out.println("The tree is:" + infix_string);
System.out.println("The tree is:" + postfix_string);
System.out.println("The result is:" + result);
}
}
*
/ \
- c
/ \
a b
这样的能够示意逻辑结构的树形图。
如果赋值a=5,b=2,c=8,它又能给出结果24.核心代码都实现了,就是输出表示二叉树比较繁琐,请教大家
该如何做,在控制台下输出可以,当然如果能搞成界面的,就更好了,多谢
以实现的代码如下:
import java.util.Stack;public class MathExpression{
private String str_exp;
private Object[] infix;
private Object[] postfix;
private int infix_size,postfix_size;
// 定义一些便于比较的变量
final Character left=new Character('(');
final Character right=new Character(')');
final Character add=new Character('+');
final Character sub=new Character('-');
final Character mul=new Character('*');
final Character div=new Character('/'); public MathExpression() { }
public MathExpression(String expression)
{
String exp=expression;
infix=new Object[exp.length()+1];
int i=0,j=0; //把表达式分离成类元件,形式是中缀表达式
char ch;
String operand="";
while(i < exp.length())
{
ch=exp.charAt(i); if(ch=='(')
infix[j++]=new Character(ch);
else if(ch==')')
{
if(exp.charAt(i-1)!=')')
{
infix[j++]=operand;
operand="";
infix[j++]=new Character(ch);
}
else
infix[j++]=new Character(ch);
}
else if((ch=='+')|| (ch=='-')||
(ch=='*')|| (ch=='/'))
{
if(operand!="")
{
infix[j++]=operand;
operand="";
}
infix[j++]=new Character(ch);
}
else
{
operand+=ch;
}
i++;
}//end while
if(operand!="")
{
infix[j]=operand;
infix_size=j+1;
}
else
{
infix_size=j;
}//end if
//把中缀表达式转变为后缀表达式
postfix=new Object[infix_size];
j=0;
Stack stack=new Stack();
Object temp;
Object remove; for(i=0; i < infix_size; i++)
{
temp = infix[i];
if( temp instanceof Character )
{
if(temp.equals(left))
stack.push(temp);
else if(temp.equals(right))
{
while( !(stack.peek()).equals(left) && !stack.isEmpty() )
{
postfix[j++]=stack.pop();
}//end while
remove=stack.pop();
}
else // temp is an operator
{
while(!stack.isEmpty()
&& !(stack.peek()).equals(left)
&& precedence(temp) <= precedence(stack.peek()))
{
postfix[j++]=stack.pop();
}//end while
stack.push(temp);
}
}//end if
else // temp is a number
if(temp instanceof String )
{
postfix[j++]=temp;
}
}//end for
while(!stack.isEmpty())
{
postfix[j++]=stack.pop();
}//end while
postfix_size=j;
//end transfer
} //end constructor
public double value() //return the result of the expression
{
double number1=0,number2=0,result=0;
int i=0,j=0;
Stack stack=new Stack();
Object temp;
for(i=0; i<postfix_size; i++)
{ temp=postfix[i];
if( temp instanceof String )
stack.push(temp);
else
{ //temp is an operator
number2=Double.parseDouble(""+stack.pop());
number1=Double.parseDouble(""+stack.pop());
if(temp.equals(add))result=number1+number2;
if(temp.equals(sub))result=number1-number2;
if(temp.equals(mul))result=number1*number2;
if(temp.equals(div))result=number1/number2;
stack.push(""+result);
}
}//end for
return result;
}
public String getPostfix()
{
String str="";
for(int i=0; i<postfix_size; i++)
{
str+=postfix[i]+"";
}//end for
return str+"size is"+postfix_size;
}
public String getInfix()
{
String str="";
for(int i=0; i<infix_size; i++)
{
str+=infix[i]+"";
}//end for
return str+"size is"+infix_size;
}
private int precedence(Object op)
{
if(op.equals(mul) || op.equals(div))
{
return 1;
}
else
{
return 0;
}
}
public static void main(String[] args){
double result;
String infix_string;
String postfix_string;
String str_exp; System.out.println("Please input the expression:");
MathExpression testExpression = new MathExpression("5*(8-2)+9");
infix_string = testExpression.getInfix();
postfix_string = testExpression.getPostfix();
result = testExpression.value();
System.out.println("The tree is:" + infix_string);
System.out.println("The tree is:" + postfix_string);
System.out.println("The result is:" + result);
}
}
以前用c++写了一个,你看看改吧~
template<class T>
void BinaryTree<T>::Show_BinaryTree(){
vector<bool> bIsEnd;
bIsEnd.push_back(0);
cout<<"您所输入的树是:\n";
cout<<"Root:";
if (!root->LeftChild&&!root->RightChild)
{
cout<<root->data<<endl;
}
else
{
cout<<root->sign<<endl;
Print_BNode(root->LeftChild,1,bIsEnd,false);
bIsEnd[0]=1;
Print_BNode(root->RightChild,1,bIsEnd,true);
cout<<endl;
}
}
template<class T>
void BinaryTree<T>::Print_BNode(BinaryTreeNode<T> *p, int c, vector<bool>& isend,bool RorL){
if (!p)
{
return;
}
for(int j=0;j<c;j++)
{//─└├│
if(isend[j]==0)
if(j!=c-1)cout<<"│";
else cout<<"├";
else if(j!=c-1)cout<<" ";
else cout<<"└";
if(j!=c-1)cout<<" ";
else cout<<"─";
}
if (!p->LeftChild&&!p->RightChild)
{
cout<<" "<<p->data;
}
else
cout<<" "<<p->sign;
if (RorL)
{
cout<<"R";
}
else cout<<"L";
cout<<endl;
for(int i=0;i<2;i++)
{
if(isend.size()==c)isend.push_back(0);
else isend[c]=0;
if(i==1)
{
if(isend.size()==c)isend.push_back(1);
else isend[c]=1;
}
if (i==0)
{
Print_BNode(p->LeftChild,c+1,isend,false);
}
else Print_BNode(p->RightChild,c+1,isend,true);
}
}结果如图:1+(256-6)*45/(3-1)您所输入的树是:
Root:+
├─ 1L
└─ /R
├─ *L
│ ├─ -L
│ │ ├─ 256L
│ │ └─ 6R
│ └─ 45R
└─ -R
├─ 3L
└─ 1R