大家好,欢迎来到IT知识分享网。
开始前先对计算器的架构进行分析。计算器需要按钮和对对应按钮的逻辑处理这两部分。
则可以用到VS2022的Windows窗体应用(.NET Framework)模板如下图
一.找到并创建对应的模板
二.找到视图选项中的工具箱,对form1窗体添加组件,显示框为textBox按钮组件,数字以及运算符用button按钮组件,将数字按钮的text值改为对应的数字例如1,2,3,4等,运算符同理。
三.添加点击事件
在编译点击事件之前先定义一些布尔常量,以及常量值等。如下图
1.数字点击事件的实现
点击对应的按钮,将值赋值给文本显示框进行显示,利用strAII进行字符串存储。其余数字点击事件同理。如下图
2.运算符点击事件
先判断前面的字符存储是否为空,判断是否有+字符,判断为真后,最后再改变对应运算符布尔的值,对文本内容进行空显示,方便接下来的点击事件文本显示。
3.结果点击事件
结果点事件也就是“=”运算符的点击事件,对应的代码如下图,最后一步完成。完整的代码在下文。
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 WindowsFormsApp1 { public partial class Form1 : Form { bool cheng = false; bool chu = false; bool jia = false; bool jian = false; double num = 0; string strAll = ""; public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { textBox1.Text += button2.Text; strAll += button2.Text; } private void button8_Click(object sender, EventArgs e) { if (strAll.Length == 0) { strAll += ""; } else if (strAll[strAll.Length - 1] == '+') { strAll += ""; // "" } else { jia = true; textBox1.Text = ""; strAll += button8.Text; } } private void button12_Click(object sender, EventArgs e) { if (strAll.Length == 0) { strAll += ""; } else if (strAll[strAll.Length - 1] == '-') { strAll += ""; } else { jian = true; textBox1.Text = ""; strAll += button12.Text; } } private void button5_Click(object sender, EventArgs e) { textBox1.Text += button5.Text; strAll += button5.Text; } private void button13_Click(object sender, EventArgs e) { textBox1.Text = ""; strAll = ""; } private void button1_Click(object sender, EventArgs e) { textBox1.Text += button1.Text; strAll += button1.Text; } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button4_Click(object sender, EventArgs e) { if (strAll == "") { textBox1.Text = strAll; } else if (strAll[strAll.Length - 1] < '0' || strAll[strAll.Length - 1] > '9') { strAll = strAll.Remove(strAll.Length - 1, 1); textBox1.Text = strAll; } else { DataTable a = new DataTable(); textBox1.Text = a.Compute(strAll, "").ToString(); cheng = false; chu = false; jian = false; jia = false; strAll = textBox1.Text; } } private void button3_Click(object sender, EventArgs e) { textBox1.Text += button3.Text; strAll += button3.Text; } private void button6_Click(object sender, EventArgs e) { textBox1.Text += button6.Text; strAll += button6.Text; } private void button7_Click(object sender, EventArgs e) { textBox1.Text += button7.Text; strAll += button7.Text; } private void button9_Click(object sender, EventArgs e) { textBox1.Text += button9.Text; strAll += button9.Text; } private void button10_Click(object sender, EventArgs e) { textBox1.Text += button10.Text; strAll += button10.Text; } private void button11_Click(object sender, EventArgs e) { textBox1.Text += button11.Text; strAll += button11.Text; } private void button14_Click(object sender, EventArgs e) { textBox1.Text += button14.Text; strAll += button14.Text; } private void button16_Click(object sender, EventArgs e) { if (strAll.Length == 0) { strAll += ""; } else if (strAll[strAll.Length - 1] == '*') { strAll += ""; // "" } else { cheng = true; textBox1.Text = ""; strAll += button16.Text; } } private void button15_Click(object sender, EventArgs e) { if (strAll.Length == 0) { strAll += ""; } else if (strAll[strAll.Length - 1] == '/') { strAll += ""; // "" } else { chu= true; textBox1.Text = ""; strAll += button15.Text; } } } }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/146233.html