分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
/*
* FormulaCalculator.java
*
* Created on 2007年9月18日, 上午11:40
* @title 转化字符串表达式为数学公式并算出结果
* @description 转化字符串表达式为数学公式并算出结果
* 注意:为了区分负号,这边使用#代替减号
* 使用方法:
* FormulaCalculator calculator=new FormulaCalculator();
* calculator.getResult("10.23#20.67*(5.12+7.82)/2",2);
* v1.0.0 created by chenfc
*
*/
package org.jceun;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FormulaCalculator ...{
private boolean isRightFormat = true;
public String DIVISOR_EQUALS_ZERO="0.0";//除数为零时的返回值
/** *//**
* 为了区分负号,这边使用#代替减号
* @param formula 字符串表达式
* @return 返回公式计算结果
*/
public double getResult(String formula)...{
double returnValue = 0;
try ... {
returnValue = doAnalysis(formula);
}catch (NumberFormatException nfe)...{
System.out.println("公式格式有误,请检查:" + formula);
}catch (Exception e)...{
e.printStackTrace();
}
if ( ! isRightFormat)...{
System.out.println("公式格式有误,请检查:" + formula);
}
return returnValue;
}
/** *//**
* 采用BigDecimal.ROUND_HALF_UP方式返回指定精度的运算结果
* @param formula 公式
* @param decimalPlace 要保留的小数位数
* @return 返回公式计算结果
*/
public String getResult(String formula,int decimalPlace)...{
return getResult(formula,decimalPlace,BigDecimal.ROUND_HALF_UP);
}
/** *//**
* 返回指定精度及舍去尾数的策略的运算结果
* @param formula 公式
* @param decimalPlace 要保留的小数位数
* @param roundMethod 舍去尾数的策略
* 可取值有BigDecimal.ROUND_HALF_UP BigDecimal.ROUND_HALF_DOWN祥见BigDecimal
* @return 返回公式计算结果
*/
public String getResult(String formula,int decimalPlace,int roundMethod)...{
double result=getResult(formula);
if(result==Double.MAX_VALUE)
return DIVISOR_EQUALS_ZERO;
else
return numberAround(result,decimalPlace,roundMethod);
}
private double doAnalysis(String formula)...{
double returnValue = 0;
LinkedList<Integer> stack = new LinkedList<Integer>();
int curPos = 0;
String beforePart = "";
String afterPart = "";
String calculator = "";
isRightFormat = true;
while (isRightFormat&& (formula.indexOf(' ( ' ) >= 0||formula.indexOf(')') >= 0))...{
curPos = 0;
for ( char s : formula.toCharArray())...{
if (s == '(')... {
stack.add(curPos);
}else if (s == ')')...{
if (stack.size() > 0 )...{
beforePart = formula.substring(0, stack.getLast());
afterPart = formula.substring(curPos + 1);
calculator = formula.substring(stack.getLast() + 1, curPos);
formula = beforePart + doCalculation(calculator) + afterPart;
stack.clear();
break;
}else ... {
System.out.println("有未关闭的右括号!");
isRightFormat = false;
}
}
curPos++;
}
if (stack.size() > 0 )...{
System.out.println("有未关闭的左括号!");
break;
}
}
if (isRightFormat)...{
returnValue = doCalculation(formula);
}
return returnValue;
}
/** *//**
* 为了区分负号,这边使用#代替减号
*/
private double doCalculation(String formula) ...{
ArrayList<Double> values = new ArrayList<Double>();
ArrayList<String> operators = new ArrayList<String>();
int curPos = 0;
int prePos = 0;
for ( char s : formula.toCharArray()) ...{
if (s == '+' || s == '#' || s == '*' || s == '/') ... {
values.add(Double.parseDouble(formula.substring(prePos, curPos)
.trim()));
operators.add("" + s);
prePos = curPos + 1;
}
curPos++;
}
values.add(Double.parseDouble(formula.substring(prePos).trim()));
char op;
for (curPos = operators.size() - 1 ; curPos >= 0; curPos--) ...{
op = operators.get(curPos).charAt(0);
switch (op) ...{
case '*':
values.add(curPos, values.get(curPos) * values.get(curPos + 1));
values.remove(curPos + 1);
values.remove(curPos + 1);
operators.remove(curPos);
break;
case '/':
if(values.get(curPos + 1).doubleValue()==0.0)//除数为零时
values.add(curPos, new Double(getDefaultValue()));
else
values.add(curPos, values.get(curPos) / values.get(curPos + 1));
values.remove(curPos + 1);
values.remove(curPos + 1);
operators.remove(curPos);
break;
}
}
for (curPos = operators.size() - 1 ; curPos >= 0; curPos--) ...{
op = operators.get(curPos).charAt(0);
switch (op) ...{
case '+':
values.add(curPos, values.get(curPos) + values.get(curPos + 1));
values.remove(curPos + 1);
values.remove(curPos + 1);
operators.remove(curPos);
break;
case '#':
values.add(curPos, values.get(curPos) - values.get(curPos + 1));
values.remove(curPos + 1);
values.remove(curPos + 1);
operators.remove(curPos);
break;
}
}
return values.get(0).doubleValue();
}
/** *//**
* 对数字进行四舍五入
* @param dN 要四舍五入的数
* @param decimalPlace 精度
* @param roundMethod 舍去尾数的策略
* 可取值有BigDecimal.ROUND_HALF_UP BigDecimal.ROUND_HALF_DOWN祥见BigDecimal
*/
public String numberAround(double dN,int decimalPlace, int roundMethod)...{
BigDecimal bd = new BigDecimal(String.valueOf(dN));
bd = bd.setScale(decimalPlace, roundMethod);
return String.valueOf(bd);
}
/** *//**
* 对给定的字符串进行模式匹配
* @param str 要匹配的字符串
* @param regix 模式
* @return 返回匹配结果,成功为true,否则为false
**/
public boolean check(String str,String regix)...{
boolean result=false;
Pattern p=Pattern.compile(regix);
Matcher m =p.matcher(str);
result=m.matches();
return result;
}
}
//:表达式解析-两栈算法.txt
//:Arithmetic.java
package citi;
import java.util.Iterator;
import java.util.Stack;
import java.util.ArrayList;
public class Arithmetic{
//定义操作符,为简单起见,只涉及四则运算,可相应扩充之
static String Operators= "+-*/()# ";
//定义操作符的比较优先级,
//其中1表示前面的操作符优于后面的操作符
// -1表示前面的操作符低于后面的操作符
// 0表示前面的操作符等于后面的操作符
// 2表示前面的操作符不可能与后面的操作符相比较,如果碰到,则表达式有错
//PrecedeList[0][]表示+和+-*/()#这七个操作符相比较的优先级
//PrecedeList[1][]表示-和+-*/()#这七个操作符相比较的优先级
//以此类推
static byte PrecedeList[][]={
{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{-1,-1,-1,-1,-1, 0, 2},
{ 1, 1, 1, 1, 2, 1, 1},
{-1,-1,-1,-1,-1, 2, 0}};
//定义数据中可能出现的数字和小数点,可以扩展
static String Numbers= "0123456789. ";
private Stack Operator,Operand;
private ArrayList Expression;
public Arithmetic(String inputStr){
Operator=new Stack();
Operator.push( "# ");
Operand=new Stack();
Expression=new ArrayList();
Parse(inputStr);
}
//解析输入的表达式,将操作符和数据分开
//如输入表达式2+3*(32-2)则解析成2 + 3 * ( 32 - 2 )这九个字符串
private void Parse(String instr){
String single;
int temp;
String tempstr= "# ";
for(int i=0;i
//排除非操作符、数字的非法输入,如2+3m
//Operators.indexOf(single)==6排除#
if(Numbers.indexOf(single) <0 && (Operators.indexOf(single) <0 || Operators.indexOf(single)==6)){
System.out.println( "Input have wrong char: "+single);
Expression.clear();
return;
}
//获得前一个输入字符
temp=Expression.size()-1;
if(temp> -1){
tempstr=(String)Expression.get(temp);
}
//排除连续两个操作符的情况,如3**2
if(Operators.indexOf(single)> -1 && temp> -1 && Operators.indexOf(tempstr)> -1){
System.out.println( "Input have wrong format,two Operators are conjoint ");
Expression.clear();
return;
}
//如果当前字符是数字(包括.)而且前一字符也是数字,则将当前字符加到前一字符后面
//其他情况均新添加一个元素
if(Operators.indexOf(single) <0 && temp> -1 && Operators.indexOf(tempstr) <0){
Expression.set(temp,tempstr+single);
}
else{
Expression.add(single);//其他情况均新添加一个元素
}
}
//为了算法处理方便起见,添加特殊字符#
Expression.add( "# ");
}
//比较两个操作符的优先级
private byte Precede(String firstOperator,String secondOperator){
return PrecedeList[Operators.indexOf(firstOperator)][Operators.indexOf(secondOperator)];
}
//对两个数据字符串进行运算
private double Operate(String firstOperand,String Operator,String secondOperand){
if(Operator.equals( "+ ")){
return (Double.parseDouble(firstOperand)+Double.parseDouble(secondOperand));
}
else if(Operator.equals( "- ")){
return (Double.parseDouble(firstOperand)-Double.parseDouble(secondOperand));
}
else if(Operator.equals( "* ")){
return (Double.parseDouble(firstOperand)*Double.parseDouble(secondOperand));
}
else if(Operator.equals( "/ ")){
return (Double.parseDouble(firstOperand)/Double.parseDouble(secondOperand));
}
else{
System.out.println( "Operator is wrong!Can throw a Exception ");
return 0;
}
}
//采用两个栈对接解析后的表达式进行运算
public double Compute(){
if(Expression.isEmpty()){
System.out.println( "Expresion is empty ");
return 0;
}
Iterator it = Expression.iterator();
String single;
String firstOperand,secondOperand;
single=(String)it.next();
while(!(single.equals( "# ") && Operator.peek().equals( "# "))){
if(Operators.indexOf(single) <0){
Operand.push(single);single=(String)it.next();
}
else{
switch(Precede((String)Operator.peek(),single)){
case -1:Operator.push(single);single=(String)it.next();break;
case 0: Operator.pop();single=(String)it.next();break;
case 1:
secondOperand=(String)Operand.pop();
firstOperand=(String)Operand.pop();
Operand.push(String.valueOf(Operate(firstOperand,(String)Operator.pop(),secondOperand)));break;
case 2: System.out.println( "Expression is wrong!Can throw a Exception ");break;
}
}
}
return Double.parseDouble((String)Operand.pop());
}
public static void main(String[] args){
long t1 = System.currentTimeMillis();
Arithmetic t=new Arithmetic(args[0]);
System.out.println(t.Compute());
long t2 = System.currentTimeMillis();
System.out.println( "Time needed: " + (t2 - t1));
}
}
其中StackSimple是自己写的一个简单的栈实现
C# code
这是那个栈Demo
C#的动态编译,并返回结果
private void btn_Compute_Click(object sender, EventArgs e)
{
// 声明要计算C#代码,要求必须完整的C#代码
string strCode = @"
using System;
namespace ParseEx
{
public class ParseExC
{
public static double GetValue() //GetValue(int x) //可以带参数
{
return 1+5*8-(Math.Sin(12)*2.5);
}
}
}";
Stopwatch sw = new Stopwatch();
sw.Start();
string strCode = @"
using System;
namespace ParseEx
{
public class ParseExC
{
public static double GetValue()
{"
+ "return " + this.textBox_Text.Text.Trim() + ";" +
@"}
}
}";
CodeDomProvider comp = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
StringBuilder codeBuilder = new StringBuilder();
codeBuilder.Appen
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow