C#实现简单的计算器小程序
作为一个程序员,如何从浅入深的训练自己的技术,可以通过一些小程序来训练
下面简单介绍一下如何制作一个简单的计算器小程序
如下为制作之后的小程序截图
代码段 其中计算用了dataTable的一个函数
dt.Compute(CurStr, string.Empty)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Calculate { public partial class CalculateFrom : Form { public CalculateFrom() { InitializeComponent(); } private string FhStr = string.Empty; private string CurStr = string.Empty;//计算字符串 //等于的 等于按钮处理 private void Click_DENG_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); if (string.IsNullOrEmpty(CurStr)) { return; } List<string> KeyList = new List<string> { "+", "-", "*", "/" }; if (KeyList.Contains(CurStr.Substring(CurStr.Length - 1)))//判断最后一位为四则运算符,则去掉 { CurStr = CurStr.Substring(CurStr.Length - 1); } if (string.IsNullOrEmpty(CurStr))//若此时为空则赋值计算结果零值 { this.Text_JM.Text = "0"; return; } else { CurStr = Convert.ToString(dt.Compute(CurStr, string.Empty));//若满足条件 则计算 使用 DataTable的计算形式 this.Text_JM.Text = CurStr;//计算结果赋值给Text } } //数值加入 // 里面包含数据只计算 private void ValueClick(string ClickV) { //进行判断,判断点击的是否是非空数值或者小数位数 if (!string.IsNullOrEmpty(ClickV)) { if (ClickV == "." && string.IsNullOrEmpty(CurStr))//第一位为小数位数 { CurStr = "0.";//如果首先点击了小数位数 自动在前面增加零值 } else if (ClickV == ".")//如果已经存在小数位数,不可以再输入 { if (!CurStr.Contains(".")) { CurStr = CurStr + "."; } } else CurStr = CurStr + ClickV; this.Text_JM.Text = CurStr;//把字符串复制给前台显示的text中 } } //四则运算符号处理 private void ValueCalculate(string Key) { if (string.IsNullOrEmpty(CurStr)) { return; } if (CurStr.Substring(CurStr.Length - 1, 1) == ".") { CurStr = CurStr + "0";//若是最后一位为小数 自动后面补齐一个零值 } List<string> KeyList=new List<string>{"+","-","*","/"}; if (KeyList.Contains(CurStr.Substring(CurStr.Length - 1, 1)))//若最后一位已经是四则运算符号,则不再操作 { return;//最后一位目前已经是符号 } CurStr = CurStr + Key; this.Text_JM.Text = CurStr; } #region 数值 private void Click_1_Click(object sender, EventArgs e) { ValueClick("1"); } private void Click_2_Click(object sender, EventArgs e) { ValueClick("2"); } private void Click_3_Click(object sender, EventArgs e) { ValueClick("3"); } private void Click_4_Click(object sender, EventArgs e) { ValueClick("4"); } private void Click_5_Click(object sender, EventArgs e) { ValueClick("5"); } private void Click_6_Click(object sender, EventArgs e) { ValueClick("6"); } private void Click_7_Click(object sender, EventArgs e) { ValueClick("7"); } private void Click_8_Click(object sender, EventArgs e) { ValueClick("8"); } private void Click_9_Click(object sender, EventArgs e) { ValueClick("9"); } private void Click_0_Click(object sender, EventArgs e) { ValueClick("0"); } //小数点 private void Click_C_Click(object sender, EventArgs e) { ValueClick("."); } #endregion #region 加减乘除 private void Click_A_Click(object sender, EventArgs e) { ValueCalculate("+"); } private void Click_R_Click(object sender, EventArgs e) { ValueCalculate("-"); } private void Click_X_Click(object sender, EventArgs e) { ValueCalculate("*"); } private void Click_L_Click(object sender, EventArgs e) { ValueCalculate("/"); } #endregion } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)