?? houzui.java
字號:
import java.io.*;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class houzui {
Stack store=null;
//構造器
public houzui() {
store=new Stack();
}
/***********************
算法:
1 讀入運算對象,直接輸出
2 ( 運算符進棧
3 ) 運算符,棧內的最上一個( 以上的運算符退棧,(也退棧
4 讀入運算符,進入運算棧
4.1 后進棧的運算符 > 先進棧的運算符,運算符進棧 (優先級比較)
4.2 后進棧的運算符 <= 先進棧的運算符,將棧內的運算符退棧輸出,再進
5 # 結束符
************************/
//接收窗口的輸入,并將其轉換成后綴表達式
private String getInput(String qianzhui){
//將輸入的前綴表達式轉化成后綴表達式
store.Push("&");
String str=qianzhui, f="",h=f;
//括號匹配
int kl=0,kr=0,kf=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='(')
kl++;
if(str.charAt(i)==')')
kr++;
if(str.charAt(i)=='+'||str.charAt(i)=='-'||
str.charAt(i)=='*'||str.charAt(i)=='/')
kf++;
}
if(kl!=kr||kf==0)return "Error";
StringTokenizer m=new StringTokenizer(str);
while(m.hasMoreTokens()){
f=m.nextToken();
if(IsAChar(f))
switch(f.charAt(0)){
case '&'://5
while(store.Top().charAt(0)!='&')
h+=store.Pop()+" ";
store.Pop();
break;
case '('://2
store.Push(f);
break;// 3 + 4 / ( 25 - ( 6 * 5 ) ) * 8
//-3.4000000000000004
case ')'://32 * ( 8 * ( 4 * 2 / 8 - 2 ) + 3 )//-160
while(store.Top().charAt(0)!='(')
h+=store.Pop()+" ";
store.Pop();
break;
case '*'://4.1
case '/':
if(store.Top().charAt(0)=='*'||store.Top().charAt(0)=='/')
h+=store.Pop()+" ";
store.Push(f);
break;
case '+'://4/2
case '-':
while(store.Top().charAt(0)!='&'&&store.Top().charAt(0)!='(')
h+=store.Pop()+" ";
store.Push(f);
break;
} else//1
h+=f+" ";
}
return h+'&';
}
//input a String
String inputAString(){
String aim=" ";
try{
BufferedReader in=
new BufferedReader(new InputStreamReader(System.in));
aim=in.readLine();
}catch(IOException e){e.printStackTrace();}
return aim+ " &";
}
boolean IsAChar(String come){
if(come.length()>1)
return false;
if(Character.isDigit(come.charAt(0)))
return false;
return true;
}
/******************************
*將操作數放入堆棧,
* 取出兩個數,按照緊接他們的操作符進行計算
******************************/
//對后綴表達式進行計算,并給出計算結果
public String outResult(String houzhui){
String Get=getInput(houzhui),
c=" ";
if(Get.equals("Error"))
return "Error!!! 表達式有誤。";
char done=' ';
double a=0,b=0;
double result=0;
StringTokenizer m=new StringTokenizer(Get);
while(m.hasMoreTokens()){
c=m.nextToken();
if(IsAChar(c))
switch(c.charAt(0)){
case '&':
result=Double.parseDouble(store.Pop());
break;
case '+':
if(store.Top()==null)return "Error!!!表達式有誤。";
a=Double.parseDouble(store.Pop());
if(store.Top()==null)return "Error!!!表達式有誤。";
b=Double.parseDouble(store.Pop());
store.Push(a+b+"");
break;
case '-':
if(store.Top()==null)return "Error!!!表達式有誤。";
a=Double.parseDouble(store.Pop());
if(store.Top()==null)return "Error!!!表達式有誤。";
b=Double.parseDouble(store.Pop());
store.Push(b-a+"");
break;
case '*':
if(store.Top()==null)return "Error!!!表達式有誤。";
a=Double.parseDouble(store.Pop());
if(store.Top()==null)return "Error!!!表達式有誤。";
b=Double.parseDouble(store.Pop());
store.Push(a*b+"");
break;
case '/':
if(store.Top()==null)return "Error!!!表達式有誤。";
a=Double.parseDouble(store.Pop());
if(a==0)
return "Error!!! 除數為零。";
if(store.Top()==null)return "Error!!!表達式有誤。";
b=Double.parseDouble(store.Pop());
store.Push(b/a+"");
break;
} else
store.Push(c+"");
}
return ""+result;
}
// I'll use the Chain structure to make a stack
class Stack {
// the node class of the Chain.
class Node{
String data="";
Node link=null;
Node(String newData,Node newLink){
data=newData;
link=newLink;
}
}
// the head point
Node first=null;
//put the data pushed before the first node
public void Push(String newData){
first=new Node(newData,first);
}
//pop the first node, and return that data
public String Pop(){
if(first!=null){
Node m=first;
first=first.link;
return m.data;
}
return null;
}
//the top element of stack
public String Top(){
if(first!=null)
return first.data;
return null;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -