表达式的计算

要求:

1.判断一个中缀表达式是否正确

2.转换为后缀表达式,并且正确计算结果

代码段:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class Test {

		static ArrayList<Object> opt= new ArrayList<>();

		public static void main(String[] args) {	
			opt.add('+');
			opt.add('-');
			opt.add('*');
			opt.add('/');
			Scanner sc = new Scanner(System.in);
			String str=sc.next();
			boolean f=handle(str);
			if(f) {
				System.out.println("该表达式正确的表达式");
				String st=prefix(str);			
				double db = caculate(st);	
				System.out.println("后缀表达式为:"+st+"="+db);
			}else {
				System.out.println("该表达式错误");
			}
			
		}
	public static boolean handle(String str) {
		boolean flage=true;
		
		Stack<Character> op = new Stack<Character>();
		int i=0;
		while(true) {			
			char ch=str.charAt(i);
			switch (ch) {
			case '(':
				op.push(ch);
				break;
			case ')':
				if(!op.isEmpty()) {
					while(!op.isEmpty()) {
						op.pop();
					}
				}else {
					flage=false;
				}
			case '+':
				
			case '-':
				
			case '*':
				
			case '/':
				int s=i;
				if(opt.contains(str.charAt(s+1))) {
					flage=false;
				}
				break;
			default:
				break;
			}
			i++;
			if(i==str.length()) {
				break;
			}
			
		}
		if(!op.isEmpty()) {
			flage=false;
		}
		return flage;
	}

	public static String prefix(String str) {
		String express="";
		Stack<Character> stack = new Stack<Character>();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c == '+' || c == '-') {
				if (stack.isEmpty() || stack.peek() == '(') {
					stack.push(c);
				} else {
					while (!stack.isEmpty()&& (stack.peek() == '*' || stack.peek() == '/'|| stack.peek() == '+' || stack.peek() == '-')) {
						express=express+stack.pop();
					}
					stack.push(c);
				}
			} else if (c == '*' || c == '/') {
				if (stack.isEmpty() || stack.peek() == '+'|| stack.peek() == '-' || stack.peek() == '(') {
					stack.push(c);
				} else {
					while (!stack.isEmpty()&& (stack.peek() == '/' || stack.peek() == '*')) {
						express=express+stack.pop();
					}
					stack.push(c);
				}
			} else if (c == '(') {
				stack.push(c);
			} else if (c == ')') {
				char temp = ' ';
				while ((temp = stack.pop()) != '(') {
					express=express+temp;
				}
			} else {
				express=express+c;
			}
		}
		if (!stack.isEmpty()) {
			while (!stack.isEmpty()) {
				express=express+stack.pop();
			}
		}

		return express;
	}
	
	public static double caculate(String str) {
		Stack<Double> num = new Stack<>();
		Stack<Object> opp = new Stack<>();
		int i=0;
		double[] arr=new double[2]; 
		while(true) {
			char ch = str.charAt(i);
			if(!opt.contains(ch)) {
				double st=Double.parseDouble(String.valueOf(ch));
				num.push(st);
			}else {
				switch (ch) {
				case '+':
					while(!num.isEmpty()) {		
						for(int j=0;j<arr.length;j++) {
							arr[j]=num.pop();		
						}	
					}									
						num.push(arr[0]+arr[1]);
					
					break;
				case '-':
					while(!num.isEmpty()) {		
						for(int j=0;j<arr.length;j++) {
							arr[j]=num.pop();		
						}	
					}									
						num.push(arr[0]-arr[1]);
					break;
				case '*':
					while(!num.isEmpty()) {		
						for(int j=0;j<arr.length;j++) {
							arr[j]=num.pop();	
						}	
					}									
						num.push(arr[0]*arr[1]);
					break;
				case '/':
					while(!num.isEmpty()) {		
						for(int j=0;j<arr.length;j++) {
							arr[j]=num.pop();	
						}	
					}									
						num.push(arr[1]/arr[1]);

					break;
				default:
					break;
				}
			}
			i++;
			if(i==str.length()) {
				break;
			}
		}
		return num.pop();
	}
}
如有什么问题,欢迎大家在下面评论。笔者会加以改善。


猜你喜欢

转载自blog.csdn.net/qq_31987435/article/details/80898573