📄 ex2.java
字号:
System.out.println(string);
}
for(int k = 1;k < operator3.Get_suffix() - operator1.Get_suffix();k++) {
str = str.concat("T");//将处理过的数据和操作符置换成字符T
suffix[k + operator1.Get_suffix()] = NO;
}
NO++;
//合并成新的表达式,未处理部分保持不变
str = str3.concat(str);
expression = str.concat(str4);
//System.out.println(operator2.Get_op());
list.remove(1);
}
else if(pri1 < pri2 && pri2 < pri3) {
String str1 = expression.substring(operator2.Get_suffix()+1,operator3.Get_suffix());
String str2 = expression.substring(operator3.Get_suffix()+1);
String str3 = expression.substring(0,operator2.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() + operator3.Get_suffix() - operator2.Get_suffix();k++) {
if(expression.charAt(k + operator2.Get_suffix()) != 'T') {
suffix[k + operator2.Get_suffix()] = NO;//修改四元式的结果表示符号的下标数组中的相应元素
}
}
//当前要处理的操作符左右两边的数据都为已经处理过的结果
if(expression.charAt(operator3.Get_suffix() - 1) == 'T' && expression.charAt(operator3.Get_suffix() + 1) == 'T') {
String string = "("+operator3.Get_op()+","+"T"+suffix[operator3.Get_suffix() - 1]+","+"T"+suffix[operator3.Get_suffix() + 1]+","+"T"+suffix[operator3.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//当前要处理的操作符左边的数据都为已经处理过的结果
else if(expression.charAt(operator3.Get_suffix() - 1) == 'T') {
String string = "("+operator3.Get_op()+","+"T"+suffix[operator3.Get_suffix() - 1]+","+str2+","+"T"+suffix[operator3.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//当前要处理的操作符右边的数据都为已经处理过的结果
else if(expression.charAt(operator3.Get_suffix() + 1) == 'T') {
String string = "("+operator3.Get_op()+","+str1+","+"T"+suffix[operator3.Get_suffix() + 1]+","+"T"+suffix[operator3.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//当前要处理的操作符左右两边的数据都未被处理过
else {
String string = "("+operator3.Get_op()+","+str1+","+str2+","+"T"+NO+")";
list1.add(string);
System.out.println(string);
}
for(int k = 1;k < str2.length() + operator3.Get_suffix() - operator2.Get_suffix();k++) {
str = str.concat("T");//将处理过的数据和操作符置换成字符T
suffix[k + operator2.Get_suffix()] = NO;
}
NO++;
expression = str3.concat(str);//合并成新的表达式,未处理部分保持不变
//System.out.println(expression);
//System.out.println(operator3.Get_op());
list.remove(2);
}
}
else if(list.size() >= 4) {
for(int j = 0;j < list.size()-2;j++) {
Operator operator1 = (Operator)list.get(j);//左边的符号
Operator operator2 = (Operator)list.get(j+1);//中间的符号
Operator operator3 = (Operator)list.get(j+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);//消除括号
//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);
System.out.println(string);
}
for(int k = 1;k < operator3.Get_suffix() - operator1.Get_suffix();k++) {
str = str.concat("T");//将处理过的数据和操作符置换成字符T
suffix[k + operator1.Get_suffix()] = NO;
}
NO++;
//合并成新的表达式,未处理部分保持不变
str = str3.concat(str);
expression = str.concat(str4);
//System.out.println(operator2.Get_op());
list.remove(j+1);
break;//跳出for循环,进入while循环
}
else if(pri1 < pri2 && pri2 < pri3) {
if(j+3 < list.size()) {
Operator operator4 = (Operator)list.get(j+3);
int pri4 = operator4.Get_pri();
if(pri3 < pri4) {
continue;//结束本次for循环
}
}
else {
String str1 = expression.substring(operator2.Get_suffix()+1,
operator3.Get_suffix());
String str2 = expression.substring(operator3.Get_suffix()+1);
String str3 = expression.substring(0,operator2.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() + operator3.Get_suffix() - operator2.Get_suffix();k++) {
if(expression.charAt(k + operator2.Get_suffix()) != 'T') {
suffix[k + operator2.Get_suffix()] = NO;//修改四元式的结果表示符号的下标数组中的相应元素
}
}
//当前要处理的操作符左右两边的数据都为已经处理过的结果
if(expression.charAt(operator3.Get_suffix() - 1) == 'T' && expression.charAt(operator3.Get_suffix() + 1) == 'T') {
String string = "("+operator3.Get_op()+","+"T"+suffix[operator3.Get_suffix() - 1]+","+"T"+suffix[operator3.Get_suffix() + 1]+","+"T"+suffix[operator3.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//当前要处理的操作符左边的数据都为已经处理过的结果
else if(expression.charAt(operator3.Get_suffix() - 1) == 'T') {
String string = "("+operator3.Get_op()+","+"T"+suffix[operator3.Get_suffix() - 1]+","+str2+","+"T"+suffix[operator3.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//当前要处理的操作符右边的数据都为已经处理过的结果
else if(expression.charAt(operator3.Get_suffix() + 1) == 'T') {
String string = "("+operator3.Get_op()+","+str1+","+"T"+suffix[operator3.Get_suffix() + 1]+","+"T"+suffix[operator3.Get_suffix()]+")";
list1.add(string);
System.out.println(string);
}
//当前要处理的操作符左右两边的数据都未被处理过
else {
String string = "("+operator3.Get_op()+","+str1+","+str2+","+"T"+NO+")";
list1.add(string);
System.out.println(string);
}
for(int k = 1;k < str2.length() + operator3.Get_suffix() - operator2.Get_suffix();k++) {
str = str.concat("T");//将处理过的数据和操作符置换成字符T
suffix[k + operator2.Get_suffix()] = NO;
}
NO++;
expression = str3.concat(str);//合并成新的表达式,未处理部分保持不变
System.out.println(expression);
//System.out.println(operator3.Get_op());
list.remove(j+2);
break;//跳出for循环,进入while循环
}
}
}
}
i++;
if(i == size) {
break;//跳出while循环
}
}
//System.out.println(list.size());
}
public LinkedList Get_list() {//以列表的形式返回所有的四元式
return list1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -