西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)C#.NET → 多功能C#科學(xué)計算器、重磅開源了

多功能C#科學(xué)計算器、重磅開源了

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2013/11/12 8:42:40字體大。A-A+

作者:西西點擊:571次評論:0次標簽: 計算器

  • 類型:行業(yè)軟件大。1.1M語言:中文 評分:5.0
  • 標簽:
立即下載

第一版 字符串計算 架構(gòu)思路:
第一版 命名空間 BaseUtil.Compute —— 只是 BaseUtil 程序集 中的 一個子功能
第一版 抽象思想:
運算分為 運算符(IComputeSymbol + ComputeSymbolAttribute) + 函數(shù)(IComputeMethod + ComputeMethodAttribute)
使用 常用的   接口 + 特性   的方式 進行插件擴展
內(nèi)置插件 
運算符插件: + - * / % ^ LIKE > < >= <= = == === OR || AND && [?:] 
函數(shù)插件: REPLACE  LEN
基礎(chǔ)數(shù)據(jù)類型 的解析 為 代碼內(nèi)置:通過數(shù)據(jù)識別順序 判定類型
基礎(chǔ)數(shù)據(jù)類型: string double bool 
基礎(chǔ)數(shù)據(jù)判定順序: string double bool

第二版 字符串計算 架構(gòu)思路:
第二版 命名空間 Laura.Compute —— 功能完全獨立
第二版 改進:
基礎(chǔ)數(shù)據(jù)類型: string double bool array
支持 數(shù)組 : 伴隨支持 IN 運算符插件(第一版 的 IComputeSymbol IComputeMethod 無法擴展 IN 語法) 
重大思想變革: 取消 函數(shù)概念: 比如之前函數(shù) REPLACE(A, B, C) 思想轉(zhuǎn)變成 REPLACE {Array} —— 只要是 (A,B,C ...) 這樣的模式 統(tǒng)統(tǒng)被 認定為 數(shù)組
取消函數(shù)概念 + 數(shù)組概念 造成的影響: 影響可能巨大,且可能導(dǎo)致的思想局限或者錯誤暫時未知(2013-07-13)
取消參數(shù) 類型的 預(yù)設(shè)定: 第一版 因為不知道 表達式 中,哪些是 參數(shù)表達式,所以需要 給表達式 提前設(shè)置 數(shù)據(jù)類型,所以 增加了使用復(fù)雜度。第二版 采用 參數(shù)表達式 自動識別

第二版 抽象思想:
依然使用   接口 + 特性   的方式 進行插件擴展
核心思想: 類似 SQL 腳本的 語法模式
概念合并:  函數(shù)思想 合并 到 運算符思想 中 —— 至此:函數(shù)也是一種 運算符  [這是一個 思想合并的過程 : 即為 再抽象], 伴隨的是 接口 的 合并
————————————————————————————————————————————————————————————————
2013-07-16 00:21
————————————————————————————————————————————————————————————————
第二版 字符串計算 算法 已經(jīng)寫完
算法完成 速度之快 主要是因為:
>概念再抽象之后,核心思想 的簡潔
>在并序運算的代碼 直接復(fù)制 第一版的 并序運算代碼 (ExpressSchema.cs 301-318 行代碼)
>很多插件 代碼 都是在 第一版插件代碼 基礎(chǔ)上 略做修改 而成,節(jié)省了不少時間
基本測試已經(jīng)通過:
>運行速度 達到 每秒 10000-12000 次左右(配置:CPU-I3 Momery-6G),遠超 第一版 字符串計算算法(第一版 每秒 500-1000次左右)
>參數(shù)化 字符串計算 尚未測試:預(yù)想BUG應(yīng)該不大(因為 第二版 采用 先完全解析,再執(zhí)行 的思想,預(yù)期性能依然在 第一版 10倍以上)

算法亮點:
>第二版語法 更人性化,除 可以省略的 this 關(guān)鍵字外,語法類似 SqlScript 如:[FSchoolName] == "孝感學(xué)院" AND [FStudentName] == "ShuXiaolong"
>第二版語法支持 索引,子屬性 超復(fù)雜表達式  如:  this.[FSchools]["孝感學(xué)院"].[FClasses][1].[FClassName] (可省略 this)
>第二版語法 取消了 第一版語法 對 自定義屬性格式 的支持:所有 參數(shù)屬性 必須以 [] 括起來
>第二版語法 支持 插件擴展 (第一版也支持),凡是 繼承 ICompute+ComputeExpressAttribute 的類 會被自動識別為 算法插件
>第二版語法 支持 IN 關(guān)鍵字插件 (第一版語法 因為 概念思想 的局限,無法擴展 IN插件) 如  \"ShuXiaolong\" IN (\"ShuXiaolong\",\"QuFuli\")"

算法用途:
>內(nèi)存檢索(當然,速度 遠遠不及 SQL引擎 或 DataTable 的 檢索),在語法的 靈活性上 勝過 DataTable 的內(nèi)存檢索計算
>動態(tài)編程(接下來 有一個 概念器 的 流程引擎 的設(shè)計),讓動態(tài)配置式 編程 更具有靈活性

第二版 和 第一版 算法插件 的 運算優(yōu)先級 如下:
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:

在窗體上拖拽 文本框 作為顯示屏,并拖拽 按鍵:

為了節(jié)省代碼,所以每個按鈕 公用 btnInput_Click 事件;為了作為區(qū)分,所以 我們設(shè)置每個 按鈕的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 算法了:本算法為字符串計算算法,用本算法計算結(jié)果。

 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

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)