import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;public class Stack
{
public static void main(String args[])
{
StackArray Operator=new StackArray();
StackArray Operand=new StackArray();
String Expression=new String();
int Position=0;
int Operator1;
int Operand1=0;
int Operand2=0;
int Evaluate=0;
//(Operator.AStack[Operator.Top]) System.out.print("Please input your inorder expression:");
ConsoleReader console=new ConsoleReader(System.in);
Expression=console.readLine();
while(true)
{
if(Operator.IsOperator((int)Expression.charAt(Position)))
{
if(Operator.Top!=-1)
{
if(Operator.Prioraty((int) Expression.charAt(Position))<=Operator.Prioraty(Operator.AStack[Operator.Top]))
{
Operand1=Operand.Pop();
Operand2=Operand.Pop();
Operator1=Operator.Pop();
Operand.Push(Operator.TwoResult(Operator1,Operand1,Operand2));
}
}
Operator.Push((int)Expression.charAt(Position));
}
else
{
Operand.Push((int)(Expression.charAt(Position)-48));
}
Position++;
if(Position>=Expression.length())
break;
} while(Operator.Top!=-1)
{
Operand1=Operand.Pop();
Operand2=Operand.Pop();
Operator1=Operator.Pop();
Operand.Push(Operand.TwoResult(Operator1,Operator1,Operand2));
Evaluate=Operand.Pop();
System.out.print("The expression ["+Expression+"]");
System.out.print("result is "+Evaluate);
}
}
}
class StackArray
{
int MaxSize=20;
int[] AStack=new int[MaxSize];
int Top=-1;
public void Push(int Value)
{
if(Top>=MaxSize)
System.out.println("The stack is full!!");
else
{
Top++;
AStack[Top]=Value;
}
} public int Pop()
{
int Temp;
int i; if(Top<0)
{
System.out.println("The stack is empty!"+"你的问题!");
//System.out.println("你的问题!");
return -1;
}
Temp=AStack[Top];
Top--;
return Temp;
} public int Prioraty(int Operator)
{
if(Operator==43||Operator==45)
return 1;
else if(Operator==42||Operator==47)
return 2;
else
return 0;
} public boolean IsOperator(int Operator) {
if(Operator==43||Operator==45||Operator==42||Operator==47)
return true;
else
return false;
}
public int TwoResult(int operator,int operand1,int operand2)
{
switch(operator)
{
case 43:
return(operand2+operand1);
case 45:
return(operand2-operand1);
case 42:
return(operand2*operand1);
case 47:
return(operand2/operand1);
}
return 0;
}
}
class ConsoleReader
{ public ConsoleReader(InputStream inStream)
{ reader = new BufferedReader
(new InputStreamReader(inStream));
}
public int readInt()
{ String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble()
{ String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
public String readLine()
{ String inputLine = ""; try
{ inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
} return inputLine;
} private BufferedReader reader;
}
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;public class Stack
{
public static void main(String args[])
{
StackArray Operator=new StackArray();
StackArray Operand=new StackArray();
String Expression=new String();
int Position=0;
int Operator1;
int Operand1=0;
int Operand2=0;
int Evaluate=0;
//(Operator.AStack[Operator.Top]) System.out.print("Please input your inorder expression:");
ConsoleReader console=new ConsoleReader(System.in);
Expression=console.readLine();
while(true)
{
if(Operator.IsOperator((int)Expression.charAt(Position)))
{
if(Operator.Top!=-1)
{
if(Operator.Prioraty((int) Expression.charAt(Position))<=Operator.Prioraty(Operator.AStack[Operator.Top]))
{
Operand1=Operand.Pop();
Operand2=Operand.Pop();
Operator1=Operator.Pop();
Operand.Push(Operator.TwoResult(Operator1,Operand1,Operand2));
}
}
Operator.Push((int)Expression.charAt(Position));
}
else
{
Operand.Push((int)(Expression.charAt(Position)-48));
}
Position++;
if(Position>=Expression.length())
break;
} while(Operator.Top!=-1)
{
Operand1=Operand.Pop();
Operand2=Operand.Pop();
Operator1=Operator.Pop();
Operand.Push(Operand.TwoResult(Operator1,Operator1,Operand2));
Evaluate=Operand.Pop();
System.out.print("The expression ["+Expression+"]");
System.out.print("result is "+Evaluate);
}
}
}
class StackArray
{
int MaxSize=20;
int[] AStack=new int[MaxSize];
int Top=-1;
public void Push(int Value)
{
if(Top>=MaxSize)
System.out.println("The stack is full!!");
else
{
Top++;
AStack[Top]=Value;
}
} public int Pop()
{
int Temp;
int i; if(Top<0)
{
System.out.println("The stack is empty!"+"你的问题!");
//System.out.println("你的问题!");
return -1;
}
Temp=AStack[Top];
Top--;
return Temp;
} public int Prioraty(int Operator)
{
if(Operator==43||Operator==45)
return 1;
else if(Operator==42||Operator==47)
return 2;
else
return 0;
} public boolean IsOperator(int Operator) {
if(Operator==43||Operator==45||Operator==42||Operator==47)
return true;
else
return false;
}
public int TwoResult(int operator,int operand1,int operand2)
{
switch(operator)
{
case 43:
return(operand2+operand1);
case 45:
return(operand2-operand1);
case 42:
return(operand2*operand1);
case 47:
return(operand2/operand1);
}
return 0;
}
}
class ConsoleReader
{ public ConsoleReader(InputStream inStream)
{ reader = new BufferedReader
(new InputStreamReader(inStream));
}
public int readInt()
{ String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble()
{ String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
public String readLine()
{ String inputLine = ""; try
{ inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
} return inputLine;
} private BufferedReader reader;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货