多功能C#科学计算器、重磅开源了
- 时间:2015年04月02日 15:19:26 来源:魔法猪系统重装大师官网 人气:10815
第一版 字符串计算 架构思路:
第一版 命名空间 BaseUtil.Compute —— 只是 BaseUtil 程序集 中的 一个子功能
第一版 抽象思想:
运算分为 运算符(IComputeSymbol + ComputeSymbolAttribute) + 函数(IComputeMethod + ComputeMethodAttribute)
使用 常用的 接口 + 特性 的方式 进行插件扩展
内置插件
运算符插件: + - * / % ^ LIKE > < >= <= = == === OR || AND && [?:]
函数插件: REPLACE LEN
基础数据类型 的解析 为 代码内置:通过数据识别顺序 判定类型
基础数据类型: string double bool
基础数据判定顺序: string double bool
第二版 字符串计算 架构思路:
第二版 命名空间 Laura.Compute —— 功能完全独立
第二版 改进:
基础数据类型: string double bool array
支持 数组 : 伴随支持 IN 运算符插件(第一版 的 IComputeSymbol IComputeMethod 无法扩展 IN 语法)
重大思想变革: 取消 函数概念: 比如之前函数 REPLACE(A, B, C) 思想转变成 REPLACE {Array} —— 只要是 (A,B,C ...) 这样的模式 统统被 认定为 数组
取消函数概念 + 数组概念 造成的影响: 影响可能巨大,且可能导致的思想局限或者错误暂时未知(2013-07-13)
取消参数 类型的 预设定: 第一版 因为不知道 表达式 中,哪些是 参数表达式,所以需要 给表达式 提前设置 数据类型,所以 增加了使用复杂度。第二版 采用 参数表达式 自动识别
第二版 抽象思想:
依然使用 接口 + 特性 的方式 进行插件扩展
核心思想: 类似 SQL 脚本的 语法模式
概念合并: 函数思想 合并 到 运算符思想 中 —— 至此:函数也是一种 运算符 [这是一个 思想合并的过程 : 即为 再抽象], 伴随的是 接口 的 合并
————————————————————————————————————————————————————————————————
2013-07-16 00:21
————————————————————————————————————————————————————————————————
第二版 字符串计算 算法 已经写完
算法完成 速度之快 主要是因为:
>概念再抽象之后,核心思想 的简洁
>在并序运算的代码 直接复制 第一版的 并序运算代码 (ExpressSchema.cs 301-318 行代码)
>很多插件 代码 都是在 第一版插件代码 基础上 略做修改 而成,节省了不少时间
基本测试已经通过:
>运行速度 达到 每秒 10000-12000 次左右(配置:CPU-I3 Momery-6G),远超 第一版 字符串计算算法(第一版 每秒 500-1000次左右)
>参数化 字符串计算 尚未测试:预想BUG应该不大(因为 第二版 采用 先完全解析,再执行 的思想,预期性能依然在 第一版 10倍以上)
算法亮点:
>第二版语法 更人性化,除 可以省略的 this 关键字外,语法类似 SqlScript 如:[FSchoolName] == "孝感学院" AND [FStudentName] == "ShuXiaolong"
>第二版语法支持 索引,子属性 超复杂表达式 如: this.[FSchools]["孝感学院"].[FClasses][1].[FClassName] (可省略 this)
>第二版语法 取消了 第一版语法 对 自定义属性格式 的支持:所有 参数属性 必须以 [] 括起来
>第二版语法 支持 插件扩展 (第一版也支持),凡是 继承 ICompute+ComputeExpressAttribute 的类 会被自动识别为 算法插件
>第二版语法 支持 IN 关键字插件 (第一版语法 因为 概念思想 的局限,无法扩展 IN插件) 如 \"ShuXiaolong\" IN (\"ShuXiaolong\",\"QuFuli\")"
算法用途:
>内存检索(当然,速度 远远不及 SQL引擎 或 DataTable 的 检索),在语法的 灵活性上 胜过 DataTable 的内存检索计算
>动态编程(接下来 有一个 概念器 的 流程引擎 的设计),让动态配置式 编程 更具有灵活性
第二版 和 第一版 算法插件 的 运算优先级 如下:
StringLengthComputeMethod LEN 1000000
StringReplaceComputeMethod REPLACE 1000000
PowComputeSymbol ^ 100000
MultiplyComputeSymbol * 10000
RemainComputeSymbol % 10000
DivideComputeSymbol / 10000
PlusComputeSymbol + 1000
MinusComputeSymbol - 1000
LikeEqualComputeSymbol LIKE 700
LessThanEqualComputeSymbol <= 685
GreaterThanEqualComputeSymbol >= 680
LessThanComputeSymbol < 675
GreaterThanComputeSymbol > 670
StrictEqualComputeSymbol === 610
EqualComputeSymbol == 605
BaseEqualComputeSymbol = 600
AndComputeSymbol AND 525
AndSignComputeSymbol && 525
OrComputeSymbol OR 520
OrSignComputeSymbol || 520
TernaryComputeSymbol ?: 100
InComputeMethod IN 未定(默认为 0)代码简单,先上运行截图:

编码过程:
新建项目 基于 .Net 2.0:

在窗体上拖拽 文本框 作为显示屏,并拖拽 按键:

为了节省代码,所以每个按钮 公用 btnInput_Click 事件;为了作为区分,所以 我们设置每个 按钮的Tag 值:

在共用的按钮事件中,我们进行编码:
1 private void btnInput_Click(object sender, EventArgs e)
2 {
3 Button currentButton = sender as Button;
4 if (currentButton != null && currentButton.Tag != null)
5 {
6 string input = currentButton.Tag.ToString();
7 txtExpress.Text = txtExpress.Text + input;
8 }
9 }最后计算:
计算过程,就交给 Laura.Compute 算法了:本算法为字符串计算算法,用本算法计算结果。
1 private void btnResult_Click(object sender, EventArgs e)
2 {
3 string express = txtExpress.Text ?? string.Empty;
4 if (string.IsNullOrEmpty(express) || string.IsNullOrEmpty(express.Trim()))
5 {
6 txtResult.Text = "ERROR:INPUT THE EXPRESS!";
7 }
8 else
9 {
10 try
11 {
12 object result = ComputeHelper.Compute(txtExpress.Text);
13 txtResult.Text = (result ?? string.Empty).ToString();
14 }
15 catch(Exception exp)
16 {
17 txtResult.Text = "ERROR:" + exp.Message;
18 }
19 }
20 }程序代码:
代码极其简单,65行源码。
1 using System;
2 using System.Windows.Forms;
3 using Laura.Compute;
4
5 namespace Laura.Calculator
6 {
7 public partial class MainForm : Form
8 {
9 public MainForm()
10 {
11 InitializeComponent();
12 }
13
14 private void btnInput_Click(object sender, EventArgs e)
15 {
16 Button currentButton = sender as Button;
17 if (currentButton != null && currentButton.Tag != null)
18 {
19 string input = currentButton.Tag.ToString();
20 txtExpress.Text = txtExpress.Text + input;
21 }
22 }
23
24 private void btnCancel_Click(object sender, EventArgs e)
25 {
26 string express = txtExpress.Text ?? string.Empty;
27 if (!string.IsNullOrEmpty(express))
28 {
29 txtExpress.Text = express.Substring(0, express.Length - 1);
30 }
31 }
32
33 private void btnResult_Click(object sender, EventArgs e)
34 {
35 string express = txtExpress.Text ?? string.Empty;
36 if (string.IsNullOrEmpty(express) || string.IsNullOrEmpty(express.Trim()))
37 {
38 txtResult.Text = "ERROR:INPUT THE EXPRESS!";
39 }
40 else
41 {
42 try
43 {
44 object result = ComputeHelper.Compute(txtExpress.Text);
45 txtResult.Text = (result ?? string.Empty).ToString();
46 }
47 catch(Exception exp)
48 {
49 txtResult.Text = "ERROR:" + exp.Message;
50 }
51 }
52 }
53
54 private void btnClean_Click(object sender, EventArgs e)
55 {
56 txtExpress.Text = txtResult.Text = string.Empty;
57 }
58
59 private void linkAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
60 {
61 AboutForm aboutForm = new AboutForm();
62 aboutForm.ShowDialog();
63 }
64 }
65 }Ps.如果想扩展 Laura.Compute 算法,在 任何程序集,任何命名空间,任何类名下 类似如下扩展即可:
1 using System;
2 using Laura.Compute.Utils;
3
4 namespace Laura.Compute.Extend.MathMethod
5 {
6 [Serializable]
7 [ComputeExpress(Express = "{A} + {A}", Keywords = new[] { "+" }, Level = 1000, ComputeType = typeof(PlusComputeSymbol))]
8 public class PlusComputeSymbol : ComputeBase
9 {
10 public override object Compute(ExpressSchema expressSchema, object objOrHash)
11 {
12 object argObj1 = ArgumentsObject(0, expressSchema, objOrHash);
13 object argObj2 = ArgumentsObject(1, expressSchema, objOrHash);
14
15 if (ArgumentsType(0) == ExpressType.String || ArgumentsType(1) == ExpressType.String || argObj1 is string || argObj2 is string)
16 {
17 string arg1 = Tools.ToString(argObj1);
18 string arg2 = Tools.ToString(argObj2);
19 string value = arg1 + arg2;
20 return value;
21 }
22 else
23 {
24 double arg1 = Tools.ToDouble(argObj1);
25 double arg2 = Tools.ToDouble(argObj2);
26 double value = arg1 + arg2;
27 return value;
28 }
29 }
30 }
31 }当然,最后就是 本计算器源码开源,包括重磅 的 Laura.Compute 算法:http://pan.baidu.com/s/103Xic





