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