当前位置: 技术问答>java相关
一极菜的问题!我的程序运行完之后,输出一段表达式,如何从屏幕取得该完整的表达式?谢谢!!
来源: 互联网 发布时间:2015-05-23
本文导语: 一极菜的问题!我的程序运行完之后,输出一段表达式,如何从屏幕取得该完整的表达式,并保存到一个栈里(栈已经创建)?谢谢!! | public class Stack1 { //~ Instance/static variables .......
一极菜的问题!我的程序运行完之后,输出一段表达式,如何从屏幕取得该完整的表达式,并保存到一个栈里(栈已经创建)?谢谢!!
|
public class Stack1
{
//~ Instance/static variables .............................................
private StringBuffer _result = new StringBuffer();
//~ Methods ...............................................................
public String getResult()
{
return this._result.toString();
}
public static void main( String[] args )
{
execute();
}
public void execute()
{
StackArray operator = new StackArray();
String inorder = new String();
int inposition = 0;
int operator1 = 0;
System.out.println( "please input the inorder expression" );
BufferedReader input = new BufferedReader( new InputStreamReader(
System.in ) );
try
{
inorder = input.readLine();
}
catch ( Exception e )
{
System.out.println( e );
}
System.out.println( "the postorder expression is" );
while ( true )
{
if ( operator.isoperator( inorder.charAt( inposition ) ) )
{
if ( ( operator.top == -1 ) ||
( char ) inorder.charAt( inposition ) == '(' )
{
operator.push( inorder.charAt( inposition ) );
}
else
{
if ( ( char ) inorder.charAt( inposition ) == ')' )
{
if ( operator.astack[ operator.top ] != 40 )
{
operator1 = operator.pop();
System.out.print( ( char ) operator1 );
append( operator1 );
}
}
else
{
if ( operator.priority( inorder.charAt( inposition ) ) = inorder.length() )
{
break;
}
}
while ( operator.top != -1 )
{
operator1 = operator.pop();
System.out.print( ( char ) operator1 );
append( operator1 );
}
}
private void append( char ch )
{
_result.append( ch );
}
}
class StackArray
{
//~ Instance/static variables .............................................
int maxsize = 20;
int top = -1;
int[] astack = new int[ maxsize ];
//~ Methods ...............................................................
public boolean isoperator( int operator )
{
if ( operator == 43 || operator == 45 || operator == 42 ||
operator == 47 || operator == 40 || operator == 41 )
{
return true;
}
else
{
return false;
}
}
public int pop()
{
int temp;
int i;
if ( top = maxsize )
{
System.out.println( "the stack is full" );
}
else
{
top++;
astack[ top ] = value;
}
}
}
{
//~ Instance/static variables .............................................
private StringBuffer _result = new StringBuffer();
//~ Methods ...............................................................
public String getResult()
{
return this._result.toString();
}
public static void main( String[] args )
{
execute();
}
public void execute()
{
StackArray operator = new StackArray();
String inorder = new String();
int inposition = 0;
int operator1 = 0;
System.out.println( "please input the inorder expression" );
BufferedReader input = new BufferedReader( new InputStreamReader(
System.in ) );
try
{
inorder = input.readLine();
}
catch ( Exception e )
{
System.out.println( e );
}
System.out.println( "the postorder expression is" );
while ( true )
{
if ( operator.isoperator( inorder.charAt( inposition ) ) )
{
if ( ( operator.top == -1 ) ||
( char ) inorder.charAt( inposition ) == '(' )
{
operator.push( inorder.charAt( inposition ) );
}
else
{
if ( ( char ) inorder.charAt( inposition ) == ')' )
{
if ( operator.astack[ operator.top ] != 40 )
{
operator1 = operator.pop();
System.out.print( ( char ) operator1 );
append( operator1 );
}
}
else
{
if ( operator.priority( inorder.charAt( inposition ) ) = inorder.length() )
{
break;
}
}
while ( operator.top != -1 )
{
operator1 = operator.pop();
System.out.print( ( char ) operator1 );
append( operator1 );
}
}
private void append( char ch )
{
_result.append( ch );
}
}
class StackArray
{
//~ Instance/static variables .............................................
int maxsize = 20;
int top = -1;
int[] astack = new int[ maxsize ];
//~ Methods ...............................................................
public boolean isoperator( int operator )
{
if ( operator == 43 || operator == 45 || operator == 42 ||
operator == 47 || operator == 40 || operator == 41 )
{
return true;
}
else
{
return false;
}
}
public int pop()
{
int temp;
int i;
if ( top = maxsize )
{
System.out.println( "the stack is full" );
}
else
{
top++;
astack[ top ] = value;
}
}
}