每日随笔——c#编写计算器
今天我使用c#语言开始编写计算器小程序
namespace WinFormsApp1 { public partial class Form1 : Form { double a = 0; double b = 0; bool c = false; string d; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "1"; } private void button2_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "2"; } private void button3_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "3"; } private void button4_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "4"; } private void button5_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "5"; } private void button6_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "6"; } private void button7_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "7"; } private void button8_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "8"; } private void button9_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "9"; } private void button10_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "0"; if (d == "/") { textBox1.Clear(); MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void button11_Click(object sender, EventArgs e) { c = true; b = double.Parse(textBox1.Text); d = "+"; } private void button12_Click(object sender, EventArgs e) { c = true; b = double.Parse(textBox1.Text); d = "-"; } private void button13_Click(object sender, EventArgs e) { c = true; b = double.Parse(textBox1.Text); d = "*"; } private void button14_Click(object sender, EventArgs e) { c = true; b = double.Parse(textBox1.Text); d = "/"; } private void button15_Click(object sender, EventArgs e) { switch (d) { case "+": a = b + double.Parse(textBox1.Text); break; case "-": a = b - double.Parse(textBox1.Text); break; case "*": a = b * double.Parse(textBox1.Text); break; case "/": a = b / double.Parse(textBox1.Text); break; case "X2": a = b * b; break; case "sqrt": a = Math.Sqrt(b); break; case "log": a = Math.Log(double.Parse(textBox1.Text),b); break; case "ln": a = Math.Log(b,Math.E); break; } textBox1.Text = a + ""; c = true; } private void button16_Click(object sender, EventArgs e) { textBox1.Text = ""; } private void button17_Click(object sender, EventArgs e) { c = true; b = double.Parse(textBox1.Text); d = "X2"; } private void button18_Click(object sender, EventArgs e) { c = true; b = double.Parse(textBox1.Text); d = "sqrt"; } private void button19_Click(object sender, EventArgs e) { c = true; b = double.Parse(textBox1.Text); d = "log"; } private void button20_Click(object sender, EventArgs e) { c = true; b = double.Parse(textBox1.Text); d = "ln"; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?