设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

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

2013-11-14 14:42| 发布者: 红黑魂| 查看: 5985| 评论: 0|来自: 博客园

摘要: 前言:看到 博客园 写了一个 计算器的制作过程《c#制作计算器全过程》,心血来潮 顺手也写了一个;代码简单,先上运行截图:编码过程:新建项目 基于 .Net 2.0:在窗体上拖拽 文本框 作为显示屏,并拖拽 按键:为了 ...

前言:

看到 博客园 写了一个 计算器的制作过程《c#制作计算器全过程》,心血来潮 顺手也写了一个;

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

 

编码过程:

新建项目 基于 .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                 try11                 {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             else41             {42                 try43                 {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             else23             {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

鲜花

鸡蛋

漂亮

刚表态过的朋友 (1 人)

  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部