C# 编写计算器

本文探讨了多种字符串表达式计算的技术方案,包括SQL计算、JavaScript引擎计算、后序表达式计算及构建表达式树的方法,并提供了扩展阅读资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整个字符串输入完成后转换的情景,可选方案:


1. 最简单的方式,由SQL语句计算

2. 使用Microsoft.Javascript计算,也可以使用其他编译引擎,比如 roslyn,lua ironPython等引擎

3. 使用后序表达式计算(数据结构)


类似windows计算器的那种即时输入的情景,可以有另外一种思路就是构建表达式树:

1.借助C#提供的Expression类封装,表达式参考

2.自己实现一套简单的专用Expression(推荐)


扩展阅读:

后续表达式来计算字符串表达式的方案,可以根据算法,用C#来实现,算法参考链接

在使用 C# 开发一个计算器应用程序时,可以基于 Windows 窗体应用程序(WinForms)来实现。这种类型的应用程序适合初学者,并且可以通过按钮和文本框等控件提供用户交互界面。 以下是一个完整的示例代码,展示了如何使用 C# 和 WinForms 实现一个简易的计算器: ```csharp using System; using System.Windows.Forms; namespace CalculatorApp { public partial class Form1 : Form { private double resultValue = 0; private string operationPerformed = ""; private bool isOperationPerformed = false; public Form1() { InitializeComponent(); } private void Number_Click(object sender, EventArgs e) { if ((textBox_Result.Text == "0") || (isOperationPerformed)) textBox_Result.Clear(); isOperationPerformed = false; Button button = (Button)sender; textBox_Result.Text += button.Text; } private void Operator_Click(object sender, EventArgs e) { Button button = (Button)sender; operationPerformed = button.Text; resultValue = double.Parse(textBox_Result.Text); isOperationPerformed = true; } private void Equal_Click(object sender, EventArgs e) { double secondValue = double.Parse(textBox_Result.Text); switch (operationPerformed) { case "+": textBox_Result.Text = (resultValue + secondValue).ToString(); break; case "-": textBox_Result.Text = (resultValue - secondValue).ToString(); break; case "*": textBox_Result.Text = (resultValue * secondValue).ToString(); break; case "/": if (secondValue != 0) textBox_Result.Text = (resultValue / secondValue).ToString(); else textBox_Result.Text = "Divide by zero!"; break; } } private void Clear_Click(object sender, EventArgs e) { textBox_Result.Text = "0"; resultValue = 0; } private void InitializeComponent() { this.textBox_Result = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // textBox_Result // this.textBox_Result.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.textBox_Result.Location = new System.Drawing.Point(12, 12); this.textBox_Result.Name = "textBox_Result"; this.textBox_Result.ReadOnly = true; this.textBox_Result.Size = new System.Drawing.Size(260, 40); this.textBox_Result.TabIndex = 0; this.textBox_Result.Text = "0"; this.textBox_Result.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; // // Form1 // this.ClientSize = new System.Drawing.Size(284, 311); this.Controls.Add(this.textBox_Result); this.Name = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.TextBox textBox_Result; } } ``` ### 计算器功能说明 1. **数字输入**: - 用户点击数字按钮时会触发 `Number_Click` 事件。 - 文本框中显示当前输入的数字。 2. **运算符选择**:
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值