代码人生的小狗窝

一行行枯燥的代码,却描绘出人生的点点滴滴

您现在的位置是:首页>_开源软件

『开源』也顺手记一个 科学计算器:重磅开源

发布时间:2019-11-21浏览(854)

    『开源』也顺手写一个 科学计算器:重磅开源

    代码简单,先上运行截图:

     

    编码过程:

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

    1 楼 20131007 前天  
    维护这么多的博客,不累?