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

📄 expressiondeal.java

📁 1.用堆栈实现表达式求值 2.随机生成带括号的表达式
💻 JAVA
字号:
/**
 * 
 */
package sasa.service;

/**
 * @author sasa
 * 
 * @version 1.0 2007-4-22 下午01:35:33
 */
public class ExpressionDeal {

	private String message;
	
	public void setMessage(String message){
		this.message = message;
	}
	
	public String getMessage(){
		return message;
	}

	// 返回运算符优先级关系
	public char compare(char ch1, char ch2) {
		char ch = '0';
		if (ch1 == '+' || ch1 == '-') {
			switch (ch2) {
			case '+':
				ch = '>';
				break;
			case '-':
				ch = '>';
				break;
			case '*':
				ch = '<';
				break;
			case '/':
				ch = '<';
				break;
			case '(':
				ch = '<';
				break;
			case ')':
				ch = '>';
				break;
			case '#':
				ch = '>';
				break;
			}
		} else if (ch1 == '*' || ch1 == '/') {
			switch (ch2) {
			case '+':
				ch = '>';
				break;
			case '-':
				ch = '>';
				break;
			case '*':
				ch = '>';
				break;
			case '/':
				ch = '>';
				break;
			case '(':
				ch = '<';
				break;
			case ')':
				ch = '>';
				break;
			case '#':
				ch = '>';
				break;
			}
		} else if (ch1 == '(') {
			switch (ch2) {
			case '+':
				ch = '<';
				break;
			case '-':
				ch = '<';
				break;
			case '*':
				ch = '<';
				break;
			case '/':
				ch = '<';
				break;
			case '(':
				ch = '<';
				break;
			case ')':
				ch = '=';
				break;
			case '#': // 不可能出现
				ch = '0';
				break;
			}
		} else if (ch1 == ')') {
			switch (ch2) {
			case '+':
				ch = '>';
				break;
			case '-':
				ch = '>';
				break;
			case '*':
				ch = '>';
				break;
			case '/':
				ch = '>';
				break;
			case '(': // 不可能出现
				ch = '0';
				break;
			case ')':
				ch = '>';
				break;
			case '#':
				ch = '>';
				break;
			}
		} else if (ch1 == '#') {
			switch (ch2) {
			case '+':
				ch = '<';
				break;
			case '-':
				ch = '<';
				break;
			case '*':
				ch = '<';
				break;
			case '/':
				ch = '<';
				break;
			case '(':
				ch = '<';
				break;
			case ')': // 不可能出现
				ch = '0';
				break;
			case '#':
				ch = '=';
				break;
			}
		} else
			ch = '0'; // 既不是运算符也不是数字
		return ch;
	}

	public float operate(float a, char theta, float b) {
		float ab = 0f;
		switch (theta) {
		case '+':
			ab = a + b;
			break;
		case '-':
			ab = a - b;
			break;
		case '*':
			ab = a * b;
			break;
		case '/':
			ab = a / b;
			break;
		}
		return ab;
	}

	// 计算表达式的值
	public float evaluateExpression(String ex) throws Exception {
		ex = ex + "#";
		if (ex.length() > 30)
			setMessage("您输入的字符串太长!");
		char[] ch = ex.toCharArray();
		Stack optr = new Stack(30);
		optr.push('#');
		Stack opnd = new Stack(30);
		int flag = 1, i = 0;
		String temp = "";
		char theta;
		Float a, b;
		while (flag == 1 && i < ch.length) {
			if (Character.isDigit(ch[i])) {
				temp = temp + String.valueOf(ch[i]);
				i++;
			} else {
				if (temp != "") {
					opnd.push(Float.valueOf(temp));	//操作数进栈
					temp = "";	//清空
				}
				switch (compare((Character) optr.getTop(), ch[i])) {
				// 栈顶元素优先权低
				case '<':
					optr.push(ch[i]);
					i++;
					break;
				// 脱去括号或表达式结束
				case '=':
					optr.pop();
					i++;
					break;
				// 退栈并将运算结果入栈
				case '>':
					theta = (Character) optr.pop();
					b = (Float) opnd.pop();
					a = (Float) opnd.pop();
					opnd.push(operate(a, theta, b));
					break;
				case '0':
					setMessage("您输入的表达式不正确或含小数点");
					flag = 0;
					break;
				}
			}
		}
		if (opnd.notEmpty()) {
			return (Float) opnd.getTop();
		} else {
			setMessage("您输入的表达式没有数值计算!");
			return 0;
		}
	}
	
	//处理结果计算结果,保留小数点后两位有效数字
	public float userEvaluate(String ex) throws Exception{
		float f = evaluateExpression(ex);
		if(Math.abs(f*100)>Math.abs((int)f*100)){
			int temp = Math.round(f*100);	//四舍五入
			f = (float)temp/100;
		}
		return f;
	}
	
	//判断字符串是否为数字
	public boolean isNum(String str){
		str = str + "#";
		int i = 0;
		char[] num = str.toCharArray();
		boolean flag = true;
		while(flag == true&&num[i]!='#'){
			if(!Character.isDigit(num[i]))	//判断是否为数字
				flag = false;
			if(num[i]=='.'||num[i]=='-')	//判断是否含有小数点或负号
				flag = true;
			i++;
		}
		return flag;
	}
	
	//simpleTest
	public static void main(String[] args) {
		String test = "34/3-(6/2)+(3*(6-4))";
		ExpressionDeal exTest = new ExpressionDeal();
		try {
			System.out.println("运行!");
			float a = exTest.evaluateExpression(test);
			System.out.println(a);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// System.out.println(exTest.operate(50, '/', 5));
	}
}

⌨️ 快捷键说明

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