代码简单,先上运行截图:
编码过程:
新建项目 基于 .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 算法:
相关推荐
IPHONE开源计算器程序(IOS开发入门级)
一个开源网下的计算器的代码;不错,但是仍有点小bug,需要改进。
20210512-开源证券-开源证券A股中期投资策略:觉醒年代,背黑暗而向光明.pdf
今天给大家介绍一款即时通讯应用,这个开源项目是:Tailchat,它是一个基于 React + Typescript 的现代开源 noIM 应用程序。 Tailchat 自身定位为 noIM (not only IM), 与其他IM应用最大的区别在于他底层是由一套...
重磅开源:NLP 新手入门指南 .md
开源的计算器工程文件,供学习使用,简洁美观的界面,内含基本计算器功能,进制转换,求最大公约数,以及数独破解等功能。数独采用回溯法解决,大大减少了运算时间。
投手罗曼斯(Romes)发现,在线提供的工具比较笨拙,并且不允许“漂亮的多任务处理” 不断地需要可视化和操纵钻头,很明显,必须有更好的解决方案安装家酿首先,创建一个新的水龙头$ brew tap alt-romes/pcalc然后...
可进行多位数加减乘多位数,普通运算会受数据类型影响而有上下限,此大数运算不存在此影响,可解决诸如华为笔试题1234567891011121314151617181920*2019181716151413121110987654321等
开源CMS 很不错的开源CMS(源代码) 很不错的开源CMS(源代码)
全功能图形科学计算器,以实际的商用手持式科学计算器为蓝本。 经过全面测试且无错误(据我所知)。 随附HTML文档,测试文件和示例计算器脚本。 赢得Xp / Linux。 适用于Windows 7 Professional和Ultimate版。
Tinger Calc是一个功能强大的科学计算器环境。 它支持基本数学(+,-,*,/,^),三角函数(sin,sinh,asin,cos,cosh,acos,tan,tanh,atan),指数函数(exp,ln,lg,sqrt),数学常数(pi,e),系统功能...
QT Excel处理开源项目,github地址:https://github.com/QtExcel/QXlsx
自己写的Android小例子,自认为注释还是比较全的,希望喜欢,不喜勿喷
Vue.js打造一个开源的CNode社区,实现了浏览、发帖、收藏、回复、点赞、个人中心等等功能。
当然也可以把该矩阵计算器当成普通的数学表达式计算器使用,包括复数的运算。在输入 数学表达式后点击“行列式的值”便可以计算出表达式的结果。 如果只是矩阵的简单操作,使用该计矩阵算器还是比较划算的。虽然...
利用C++开源库xlnt读写excel的vs2015工程。详细教程参考博客:https://humfrey.blog.csdn.net/article/details/107527883
开源代码使用指南 开源代码(Open Source):是促进软件发展的一种... Open Source许可证(Open Source License):能成为Open Source的Source都有一个许可证, 许可证说明规定了这段Open Source允许被应用的方法
addercalc一个用国内开源的Duilib库写的加法计算器源码,该UI库基于DirectUI思想,能够很好的实现用户自定义皮肤,并且已经能够支持多种控件,是以后程序界面发展的方向。该源码非常适合Duilib和想了解DirectUI思想...
OpenNMS是一个企业级基于Java/XML的分布式网络和系统监控管理平台。是网络管理系统Network Management System 的简称,是一种开源软件网络监视工具。
在GitHub上下载的源码,是微软windows系统的计算器,最近开放给公众了。