前缀表达式转中缀中缀表达式

import java.util.Scanner;
import java.util.Stack;
 
public class Test {
	static String e;
	static Scanner scan;
	static String[] sarray1;
	static String a = "";
	public static void main(String[] args) {
		scan = new Scanner(System.in);
		
		while(true){
			System.out.println("*****************************************");
			System.out.println("*************主菜单******姓名*************");
			
			System.out.println("1.输入前缀表达式");
			System.out.println("2.显示带括号的中缀表达式");
			System.out.println("3.给表变量赋值");
			System.out.println("4.显示结果");
			System.out.println("*****************************************");
			int shuru=scan.nextInt();
			switch(shuru){
				case 1:
					scan = new Scanner(System.in);
					System.out.println("请输入前缀表达式:例如:+ + a * b - c d / e f");
					
					a=scan.nextLine().trim();
					System.out.println("构造成功!!!");
				break;
				case 2:
					if(a.equals("")){
						System.out.println("请先输入前缀表达式!!!");
					}else{
						System.out.println("中缀表达式为:"+toQianZhui(a));
					}	
				break;
				case 3:
					fuzhi();
				break;
				case 4:
					jisuan();
				break;
				default:
					System.out.println("输入有误!");
					break;
			}
		}
 
	}
	public static String toQianZhui(String s){
		e="";
		Stack<String> stack = new Stack<String>();
		Stack<String> stack1 = new Stack<String>();
		//s="/ * + a b - c d e";
		//s="+ + a * b - c d / e f";
		String[] sarray = s.split(" ");
		String regex = "[a-z]*";
		int jilu=0;
		int jiluweizi=sarray.length;
		for (int i = sarray.length - 1; i >= 0; i--) {
			if (!sarray[i].matches(regex)) {
					stack1.push(sarray[i]);
					if(jiluweizi==i+1){
						jilu=1;
					}
					jiluweizi=i;
				}
		}
		for (int i = sarray.length - 1; i >= 0; i--) {
			try {
				if (sarray[i].matches(regex)) {
					stack.push(sarray[i]);
					
				} else {
				
					if(jilu==1){
						
						String a = stack.pop();
						if(stack.empty()){
							stack.push(sarray[i]+a);
						}else{
							String b = stack.pop();
							if(sarray[i].equals("+")||sarray[i].equals("-")){
								stack.push("("+a+sarray[i]+b+")");
							}else{
								stack.push(a+sarray[i]+b);
							}
						}
						
					}else{
						String aa=stack1.pop();
						
						String a = stack.pop();
						if(stack.empty()){
							e=a+e;
						}else{
							String b = stack.pop();
							if(sarray[i].equals("+")||sarray[i].equals("-")){
								e="("+a+sarray[i]+b+")"+aa+e;
							}else{
								e=a+sarray[i]+b+e;
							}
						}
					}
				}
			} catch (Exception e) {
				
			}
		}
		if(jilu==1){
			for (int i = stack.size() - 1; i >= 0; i--) {
				try {
					if (sarray[i].matches(regex)) {
						stack.push(sarray[i]);
						
					} else {
						String aa=stack1.pop();
							String a = stack.pop();
							if(stack.empty()){
								e=a+e;
							}else{
								String b = stack.pop();
								if(sarray[i].equals("+")||sarray[i].equals("-")){
									e="("+a+sarray[i]+b+")"+aa+e;
								}else{
									e=a+sarray[i]+b+e;
								}
							}
					}
				} catch (Exception e) {
					
				}
			}
		}
		char[] cha=e.toCharArray();
		if(cha.length!=0&&cha[0]=='('&&cha[cha.length-1]==')'){
			cha[0]=' ';
			cha[cha.length-1]=' ';
			//System.out.println(String.valueOf(cha).trim());
			return String.valueOf(cha).trim();
		}else{
			return e;
		}
		
	}
	
	public static String[] fuzhi(){
		int tag=0;
		sarray1 = a.split(" ");
		String regex = "[a-z]*";
		for(int i=0;i<sarray1.length;i++){
			if(sarray1[i].matches(regex)){
				tag=1;
				System.out.println("请输入变量"+sarray1[i]+"的值:");
				scan = new Scanner(System.in);
				sarray1[i]=scan.next().trim();
			}
		}
		if(tag==0){
			System.out.println("没有变量需要赋值!!!");
		}
		return sarray1;
		
	}
	public static void jisuan(){
		Stack<Double> stack = new Stack<Double>();
		String[] sarray = sarray1;
		String regex = "[0-9]*";
		boolean temp = false;
		int fuhao = 0;
		int number = 0;
		for (int i = sarray.length - 1; i >= 0; i--) {
			try {
				if (sarray[i].matches(regex)) {
					stack.push(Double.parseDouble(sarray[i]));
					number++;
				} else {
					fuhao++;
					double a = (double) stack.pop();
					double b = (double) stack.pop();
					double result = jisuan(a, b, sarray[i]);
					stack.push(result);
				}
			} catch (Exception e) {
				temp = true;
			}
		}
		if (temp || !(number - 1 == fuhao) || stack.empty()) {
			System.out.println("ERROR");
		} else {
			double ans = (double) stack.pop();
			String s = "";
			for(int i=0;i<sarray1.length;i++){
				if(sarray1.length!=0&&i!=sarray1.length-1){
					s=s+sarray1[i]+" ";
				}else{
					s=s+sarray1[i];
				}
			}
			//System.out.println(s);
			//System.out.println("中缀表达式"+toQianZhui1(s));
			System.out.println("结果为"+ans);
		}
	}
	public static double jisuan(double a, double b, String f) {
		if(f.equals("*")){
			return a * b;
		}
		if(f.equals("/")){
			return a /b;
		}
		if(f.equals("+")){
			return a + b;
		}
		if(f.equals("-")){
			return a - b;
		}
		/*switch (f) {
		case "*":
			return a * b;
		case "/":
			return a / b;
		case "+":
			return a + b;
		case "-":
			return a - b;
		}*/
		//return 0;
		return 0;
	}
}

阅读更多
换一批

没有更多推荐了,返回首页