?? ex2.java
字號:
/**
* 該類的主要任務是生成四元式
* 設置操作符的優先級
* 將操作符從表達式中提取出來,將操作符及其相關信息掛到列表中,并進行去括號處理
*
*/
package ex2;
import java.util.LinkedList;
/**
* @author 阿贊
*
*/
public class EX2 {
private int length;//表達式的長度
private int[] suffix;//四元式的結果表示符號的下標數組
private String expression;//表達式
Operator operator;//創建Operator類對象
LinkedList list = new LinkedList();//用來儲存操作符及其相關信息
LinkedList list1 = new LinkedList();//用來儲存四元式
public EX2(String expression) {//構造方法
this.expression = expression;
length = this.expression.length();
suffix = new int[length];
Operator();
Create();
}
public int PRI(char c) {//設置操作符的優先級
if(c == '=') {// = 的優先級為 1
return 1;
}
else if(c == '+'||c == '-') {// + 和 - 的優先級為 2
return 2;
}
else if(c == '*'||c == '/') {// * 和 / 的優先級為 3
return 3;
}
else if(c == '('||c == ')') {// ( 和 ) 的優先級為 4
return 4;
}
else {
return 0;
}
}
public int Operator() {//將操作符從表達式中提取出來,將操作符及其相關信息掛到列表中,并進行去括號處理
for(int i =0;i < length;i++) {
char op = expression.charAt(i);//獲取表達式的第i個字符
if(op == '='||op == '+'||op == '-'||op == '*'||op == '/'||op == '('||op == ')') {
operator = new Operator(""+op,i,PRI(op));//創建Operator類對象,以操作符、操作符在表達式中的下標、操作符的優先級為實參
list.add(operator);//將Operator類的對象加到列表list尾部
}
}
int term = 0;//權值
for(int i = 0;i < list.size();i++) {//進行去括號處理,將括號操作符及其相關信息從列表中移除,對括號內的非括號操作符進行加權
Operator op =(Operator)list.get(i);//獲取list中第i個元素
if(op.Get_op().equals("(")) {//遇到一個"(",權值加4
term = term + 4;
list.remove(i);//從列表list中移除第i個元素
i--;//循環變量減1
}
else if(op.Get_op().equals(")")) {//遇到一個")",權值減4
term = term - 4;
list.remove(i);
i--;
}
else if(term != 0) {
int pri = op.Get_pri() + term;
op.Set_pri(pri);
list.remove(i);
list.add(i,op);
}
}
System.out.println(list.size());
/*for(int i = 0;i < list.size();i++) {
operator = (Operator)list.get(i);
System.out.println(operator.Get_op());
System.out.println(operator.Get_suffix());
System.out.println(operator.Get_pri());
}*/
return list.size();
}
public void Create() {//生成四元式
int size = list.size();//獲取表達式列表的長度
int i = 0;//控制while循環的循環變量
int NO = 1;//四元式的結果表示符號的下標
while(true) {//當i=size時,結束循環
if(list.size() == 1) {
Operator operator1 = (Operator)list.get(0);//獲取第1個操作符,也是最后一個需要處理的操作符,即"="
if(expression.charAt(operator1.Get_suffix() + 1) == 'T') {
//生成四元式
String string = "("+operator1.Get_op()+","+"T"+(--NO)+","+0+","+expression.charAt(0)+")";
list1.add(string);//將四元式掛到列表list1的尾部
System.out.println(string);
list.remove(0);//將操作符從列表list中移除
//System.out.println(operator1.Get_op());
//break;
}
else {
String str = expression.substring(operator1.Get_suffix() + 1);
String string = "("+operator1.Get_op()+","+str+","+0+","+expression.charAt(0)+")";
list1.add(string);//將四元式掛到列表list1的尾部
System.out.println(string);
list.remove(0);//將操作符從列表list中移除
//System.out.println(operator1.Get_op());
//break;
}
}
else if(list.size() == 2) {
Operator operator1 = (Operator)list.get(0);//獲取操作符
Operator operator2 = (Operator)list.get(1);
int pri1 = operator1.Get_pri();//獲取操作符的優先級
int pri2 = operator2.Get_pri();
if(pri1 < pri2) {//處理優先級高的操作符
//將表達式拆分成幾個子串
String str1 = expression.substring(operator1.Get_suffix()+1,
operator2.Get_suffix());
String str2 = expression.substring(operator2.Get_suffix()+1);
String str3 = expression.substring(0,operator1.Get_suffix()+1);
String str = new String();
for(int k = 0;k < str1.length();k++) {
if(str1.charAt(k) == '(') {
str1 = str1.substring(k + 1);//消除括號
k--;
}
}
for(int k = 0;k < str2.length();k++) {
if(str2.charAt(k) == ')') {
str2 = str2.substring(0,k);//消除括號
}
}
/*
System.out.println(expression);
for(int k = 0;k < suffix.length;k++) {
System.out.print(suffix[k]);
}
System.out.println();
*/
for(int k = 1;k < str2.length() + operator2.Get_suffix() - operator1.Get_suffix();k++) {
if(expression.charAt(k + operator1.Get_suffix()) != 'T') {
suffix[k + operator1.Get_suffix()] = NO;//修改四元式的結果表示符號的下標數組中的相應元素
}
}
//當前要處理的操作符左右兩邊的數據都為已經處理過的結果
if(expression.charAt(operator2.Get_suffix() - 1) == 'T' && expression.charAt(operator2.Get_suffix() + 1) == 'T') {
String string = "("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//當前要處理的操作符左邊的數據都為已經處理過的結果
else if(expression.charAt(operator2.Get_suffix() - 1) == 'T') {
String string = "("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+str2+","+"T"+suffix[operator2.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//當前要處理的操作符右邊的數據都為已經處理過的結果
else if(expression.charAt(operator2.Get_suffix() + 1) == 'T') {
String string = "("+operator2.Get_op()+","+str1+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//當前要處理的操作符左右兩邊的數據都未被處理過
else {
String string = "("+operator2.Get_op()+","+str1+","+str2+","+"T"+NO+")";
list1.add(string);
System.out.println(string);
}
for(int k = 1;k < str2.length() + operator2.Get_suffix() - operator1.Get_suffix();k++) {
str = str.concat("T");//將處理過的數據和操作符置換成字符T
suffix[k + operator1.Get_suffix()] = NO;
}
NO++;//四元式的結果表示符號的下標加1
expression = str3.concat(str);//合并成新的表達式,未處理部分保持不變
//System.out.println(expression);
list.remove(1);//將已處理的操作符移除操作符列表
//System.out.println(operator2.Get_op());
}
else {
list.remove(0);
//System.out.println(operator1.Get_op());
}
}
else if(list.size() == 3) {
Operator operator1 = (Operator)list.get(0);//獲取操作符
Operator operator2 = (Operator)list.get(1);
Operator operator3 = (Operator)list.get(2);
int pri1 = operator1.Get_pri();//獲取操作符的優先級
int pri2 = operator2.Get_pri();
int pri3 = operator3.Get_pri();
if(pri1 < pri2 && pri2 >= pri3) {//處理優先級高的操作符
//將表達式拆分成幾個子串
String str1 = expression.substring(operator1.Get_suffix()+1,
operator2.Get_suffix());
String str2 = expression.substring(operator2.Get_suffix()+1,
operator3.Get_suffix());
String str3 = expression.substring(0,operator1.Get_suffix()+1);
String str4 = expression.substring(operator3.Get_suffix());
String str = new String();
for(int k = 0;k < str1.length();k++) {
if(str1.charAt(k) == '(') {
str1 = str1.substring(k + 1);//消除括號
k--;
}
}
for(int k = 0;k < str2.length();k++) {
if(str2.charAt(k) == ')') {
str2 = str2.substring(0,k);//消除括號
}
}
/*
System.out.println(expression);
for(int k = 0;k < suffix.length;k++) {
System.out.print(suffix[k]);
}
System.out.println();
*/
for(int k = 1;k < operator3.Get_suffix() - operator1.Get_suffix();k++) {
if(expression.charAt(k + operator1.Get_suffix()) != 'T') {
suffix[k + operator1.Get_suffix()] = NO;//修改四元式的結果表示符號的下標數組中的相應元素
}
}
//當前要處理的操作符左右兩邊的數據都為已經處理過的結果
if(expression.charAt(operator2.Get_suffix() - 1) == 'T' && expression.charAt(operator2.Get_suffix() + 1) == 'T') {
String string = "("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//當前要處理的操作符左邊的數據都為已經處理過的結果
else if(expression.charAt(operator2.Get_suffix() - 1) == 'T') {
String string = "("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+str2+","+"T"+suffix[operator2.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//當前要處理的操作符右邊的數據都為已經處理過的結果
else if(expression.charAt(operator2.Get_suffix() + 1) == 'T') {
String string = "("+operator2.Get_op()+","+str1+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//當前要處理的操作符左右兩邊的數據都未被處理過
else {
String string = "("+operator2.Get_op()+","+str1+","+str2+","+"T"+NO+")";
list1.add(string);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -