⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex.java

📁 该程序可将一个四则混合运算分解成一组四元式指令。
💻 JAVA
字号:
/**
 * 
 */
package ex2;

import java.util.LinkedList;

/**
 * @author 阿赞
 *
 */
public class EX {
	private int length;
	private int[] suffix;
	private String expression;
	Operator operator;
	LinkedList list = new LinkedList();
	public EX(String expression) {
		this.expression = expression;
		length = this.expression.length();
		suffix = new int[length];
		Operator();
		Out();
	}
	public int PRI(char c) {
		if(c == '=') {
				return 1;
		}
		else if(c == '+'||c == '-') {
			return 2;
		}
		else if(c == '*'||c == '/') {
			return 3;
		}
		else if(c == '('||c == ')') {
			return 4;
		}
		else {
			return 0;
		}
	}
	public int Operator() {
		for(int i =0;i < length;i++) {
			char op = expression.charAt(i);
			if(op == '='||op == '+'||op == '-'||op == '*'||op == '/'||op == '('||op == ')') {
				operator = new Operator(""+op,i,PRI(op));
				list.add(operator);
			}
		}
		//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 Out() {
		int size = list.size();
		int i = 0;
		int NO = 1;
		while(true) {
			if(list.size() == 1) {
				Operator operator1 = (Operator)list.get(0);
				System.out.println("("+operator1.Get_op()+","+"T"+--NO+","+0+","+expression.charAt(0)+")");
				list.remove(0);
				//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();
					/*
					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') {
						System.out.println("("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")");
					}
					else if(expression.charAt(operator2.Get_suffix() - 1) == 'T') {
						System.out.println("("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+str2+","+"T"+suffix[operator2.Get_suffix()]+")");
					}
					else if(expression.charAt(operator2.Get_suffix() + 1) == 'T') {
						System.out.println("("+operator2.Get_op()+","+str1+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")");
					}
					else {
						System.out.println("("+operator2.Get_op()+","+str1+","+str2+","+"T"+NO+")");
					}
					for(int k = 1;k < str2.length() + operator2.Get_suffix() - operator1.Get_suffix();k++) {
						str = str.concat("T");
						suffix[k + operator1.Get_suffix()] = NO;
					}
					NO++;
					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();
					/*
					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') {
						System.out.println("("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")");
					}
					else if(expression.charAt(operator2.Get_suffix() - 1) == 'T') {
						System.out.println("("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+str2+","+"T"+suffix[operator2.Get_suffix()]+")");
					}
					else if(expression.charAt(operator2.Get_suffix() + 1) == 'T') {
						System.out.println("("+operator2.Get_op()+","+str1+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")");
					}
					else {
						System.out.println("("+operator2.Get_op()+","+str1+","+str2+","+"T"+NO+")");
					}
					for(int k = 1;k < operator3.Get_suffix() - operator1.Get_suffix();k++) {
						str = str.concat("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();
					/*
					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') {
						System.out.println("("+operator3.Get_op()+","+"T"+suffix[operator3.Get_suffix() - 1]+","+"T"+suffix[operator3.Get_suffix() + 1]+","+"T"+suffix[operator3.Get_suffix()]+")");
					}
					else if(expression.charAt(operator3.Get_suffix() - 1) == 'T') {
						System.out.println("("+operator3.Get_op()+","+"T"+suffix[operator3.Get_suffix() - 1]+","+str2+","+"T"+suffix[operator3.Get_suffix()]+")");
					}
					else if(expression.charAt(operator3.Get_suffix() + 1) == 'T') {
						System.out.println("("+operator3.Get_op()+","+str1+","+"T"+suffix[operator3.Get_suffix() + 1]+","+"T"+suffix[operator3.Get_suffix()]+")");
					}
					else {
						System.out.println("("+operator3.Get_op()+","+str1+","+str2+","+"T"+NO+")");
					}
					for(int k = 1;k < str2.length() + operator3.Get_suffix() - operator2.Get_suffix();k++) {
						str = str.concat("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();
						/*
						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') {
							System.out.println("("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")");
						}
						else if(expression.charAt(operator2.Get_suffix() - 1) == 'T') {
							System.out.println("("+operator2.Get_op()+","+"T"+suffix[operator2.Get_suffix() - 1]+","+str2+","+"T"+suffix[operator2.Get_suffix()]+")");
						}
						else if(expression.charAt(operator2.Get_suffix() + 1) == 'T') {
							System.out.println("("+operator2.Get_op()+","+str1+","+"T"+suffix[operator2.Get_suffix() + 1]+","+"T"+suffix[operator2.Get_suffix()]+")");
						}
						else {
							System.out.println("("+operator2.Get_op()+","+str1+","+str2+","+"T"+NO+")");
						}
						for(int k = 1;k < operator3.Get_suffix() - operator1.Get_suffix();k++) {
							str = str.concat("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;
					}
				}
			}
			i++;
			if(i == size) {
				break;
			}
		}
		//System.out.println(list.size());
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -