---
title: "java中的表达式计算--eval函数"
author: "speed_guo"
date: "2011-03-09T22:41:44Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "java-eval"
draft: false
lastmod: "2011-03-09T22:45:22Z"
id: 949372
url: "https://speed-guo.iteye.com/blog/949372"
---

```
/**
	 * @param args
	 */
	public static void main(String[] args) {
		//Expression exp = new Expression("0.8*(x-2)/(5-2)+0.2");
		//Expression exp1 = new Expression("Math.pow(0.8,-(x-200)/100)+0.2");
		TestMain0308  t = new TestMain0308();
		String s = "0.8*(1/e pow ((x-200)/100))+0.2";
		System.out.println(s.contains("e"));//判断字符串中是否包含指定的字符
		
		double tvalue = 0.8*Math.pow(Math.E,-(220-200)/100)+0.2;
		//Expression exp1 = new Expression("0.8*(2.71828 pow (-(x-200)/100))+0.2");
		Expression exp1 = new Expression("0.8*(1/e pow ((x-200)/100))+0.2");
		
		
		
		
		Map<String, BigDecimal> variables = new	 HashMap<String, BigDecimal>();
		variables.put("x", new	BigDecimal("200"));
		variables.put("e", new	BigDecimal(Math.E));
		
		BigDecimal result = exp1.eval(variables);    //计算表达式的值
		String str = String.valueOf(result);   //转换成字符串
                     System.out.println(result);
		System.out.println(tvalue);
		System.out.println("result="+str);
		
				
		
				

	}
```