📄 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 + -