『開源』也順手寫一個 科學計算器:重磅開源


前言:

看到 博客園 寫了一個 計算器的制作過程,心血來潮 順手也寫了一個;

代碼簡單,先上運行截圖:

 

編碼過程:

新建項目 基於 .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 算法:

全部源代碼點擊下載

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM