练习——制作计算器
自制一个计算器,实现计算器的基本功能,按顺序依次计算,可进行拓展练习添加其他内容,尽可能完善
思路:1、界面布局 2、数字键功能 3、运算符功能 4、清屏键、退格键、小数点等功能 5、查缺补漏
代码文件:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 计算器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool isysf = false;//公共变量 判断上一步按的是否是运算符 private decimal ys = 0;//记录上一步的运算结果 private bool error = false;//记录运算指令是否有错(除数为0) //数字键点击事件 private void button1_Click(object sender, EventArgs e) { if (error)//如果上一步运算指令有错 则先清空 { button17_Click(button17,null); } Button btn = sender as Button; if (isysf)//如果上一步点击的是运算符 则清空label2 { label2.Text = btn.Text; isysf = false; } else//上一步点击的不是运算符 { if (label2.Text == "0")//如果label2中是0 则直接改值 { label2.Text = btn.Text; } else//label2中不是0 向后拼接 { if(label2.Text.Length<16) label2.Text += btn.Text; } } } //小数点按钮点击事件 private void button12_Click(object sender, EventArgs e) { if (!error) { if (isysf)//如果上一步点击的是运算符 则label2清零后加小数点 { label2.Text = "0."; isysf = false; } else { if (!label2.Text.Contains(".")) { label2.Text += "."; } } } } //C按钮点击事件 所有清零并初始化标记变量 private void button17_Click(object sender, EventArgs e) { label1.Text = ""; label2.Text = "0"; isysf = false; error = false; ys = 0; } //正负号点击事件 private void button10_Click(object sender, EventArgs e) { if (!error) { if (label2.Text != "0") { if (!label2.Text.Contains("-")) { label2.Text = "-" + label2.Text; } else { label2.Text = label2.Text.Substring(1); } } } } //退格键点击事件 private void button18_Click(object sender, EventArgs e) { if (!error) { if (!isysf) { if (label2.Text.Length > 1) label2.Text = label2.Text.Substring(0, label2.Text.Length - 1); else label2.Text = "0"; } } else { button17_Click(button17,null); } } //加减乘除膜运算符 private void button14_Click(object sender, EventArgs e) { if (!error)//上一步运算指令无错误才执行 { Button btn = sender as Button; if (isysf&&label1.Text!="")//如果上一步点击的是运算符 只修改label1中的最后一个字符 { label1.Text = label1.Text.Substring(0, label1.Text.Length - 1) + btn.Text; isysf = true; } else//上一步点击的不是运算符 { if (label1.Text == "")//如果是本次运算第一次点击运算符,给中间变量ys赋值并且将内容拼接到label1中 { ys = Convert.ToDecimal(label2.Text); label1.Text = " " + label2.Text + " " + btn.Text; isysf = true; } else//如果不是第一次点击运算符(label1中有内容)则对上一步的运算结果进行继续计算并修改两个 label中的text { if (label1.Text.Substring(label1.Text.Length - 1) == "+") { ys += Convert.ToDecimal(label2.Text); label1.Text = label1.Text + " " + label2.Text + " " + btn.Text; label2.Text = ys.ToString(); isysf = true; } else if (label1.Text.Substring(label1.Text.Length - 1) == "-") { ys -= Convert.ToDecimal(label2.Text); label1.Text = label1.Text + " " + label2.Text + " " + btn.Text; label2.Text = ys.ToString(); isysf = true; } else if (label1.Text.Substring(label1.Text.Length - 1) == "*") { ys *= Convert.ToDecimal(label2.Text); label1.Text = label1.Text + " " + label2.Text + " " + btn.Text; label2.Text = ys.ToString(); isysf = true; } else if (label1.Text.Substring(label1.Text.Length - 1) == "/") { if (!label2.Text.Contains("1") && !label2.Text.Contains("2") && !label2.Text.Contains("3") && !label2.Text.Contains("4") && !label2.Text.Contains("5") && !label2.Text.Contains("6") && !label2.Text.Contains("7") && !label2.Text.Contains("8") && !label2.Text.Contains("9")) { label2.Text = "除数不能为零"; error = true; } else { ys /= Convert.ToDecimal(label2.Text); label1.Text = label1.Text + " " + label2.Text + " " + btn.Text; label2.Text = ys.ToString(); isysf = true; } } else if (label1.Text.Substring(label1.Text.Length - 1) == "%") { if (!label2.Text.Contains("1") && !label2.Text.Contains("2") && !label2.Text.Contains("3") && !label2.Text.Contains("4") && !label2.Text.Contains("5") && !label2.Text.Contains("6") && !label2.Text.Contains("7") && !label2.Text.Contains("8") && !label2.Text.Contains("9")) { label2.Text = "除数不能为零"; error = true; } else { ys %= Convert.ToDecimal(label2.Text); label1.Text = label1.Text + " " + label2.Text + " " + btn.Text; label2.Text = ys.ToString(); isysf = true; } } } } } } //label2的text改动事件,当字符串过长时调整字体大小 private void label2_TextChanged(object sender, EventArgs e) { if(label2.Text.Length<=9) label2.Font = new Font(label2.Font.FontFamily.Name, 20, FontStyle.Bold); else if (label2.Text.Length > 9&&label2.Text.Length<=11) label2.Font = new Font(label2.Font.FontFamily.Name, 18, FontStyle.Bold); else if (label2.Text.Length > 11 && label2.Text.Length <= 13) label2.Font = new Font(label2.Font.FontFamily.Name, 16, FontStyle.Bold); else if (label2.Text.Length > 13 && label2.Text.Length <= 15) label2.Font = new Font(label2.Font.FontFamily.Name, 14, FontStyle.Bold); else if (label2.Text.Length > 15) { label2.Font = new Font(label2.Font.FontFamily.Name, 13, FontStyle.Bold); label2.Text = label2.Text.Substring(0,16); } } //label1的text改动事件,当字符串过长事 只显示后面的26个字符 private void label1_TextChanged(object sender, EventArgs e) { if (label1.Text.Length > 26) label1.Text = label1.Text.Substring(label1.Text.Length-26); } //等号按钮点击事件 private void button20_Click(object sender, EventArgs e) { if (!error) { if (label1.Text != "") { if (label1.Text.Substring(label1.Text.Length - 1) == "+") { ys += Convert.ToDecimal(label2.Text); label1.Text = ""; label2.Text = ys.ToString(); } else if (label1.Text.Substring(label1.Text.Length - 1) == "-") { ys -= Convert.ToDecimal(label2.Text); label1.Text = ""; label2.Text = ys.ToString(); } else if (label1.Text.Substring(label1.Text.Length - 1) == "*") { ys *= Convert.ToDecimal(label2.Text); label1.Text = ""; label2.Text = ys.ToString(); } else if (label1.Text.Substring(label1.Text.Length - 1) == "/") { if (!label2.Text.Contains("1")&&!label2.Text.Contains("2")&&!label2.Text.Contains("3")&&!label2.Text.Contains("4")&&!label2.Text.Contains("5")&&!label2.Text.Contains("6")&&!label2.Text.Contains("7")&&!label2.Text.Contains("8")&&!label2.Text.Contains("9")) { label2.Text = "除数不能为零"; error = true; } else { ys /= Convert.ToDecimal(label2.Text); label1.Text = ""; label2.Text = ys.ToString(); } } else { if (!label2.Text.Contains("1") && !label2.Text.Contains("2") && !label2.Text.Contains("3") && !label2.Text.Contains("4") && !label2.Text.Contains("5") && !label2.Text.Contains("6") && !label2.Text.Contains("7") && !label2.Text.Contains("8") && !label2.Text.Contains("9")) { label2.Text = "除数不能为零"; error = true; } else { ys %= Convert.ToDecimal(label2.Text); label1.Text = ""; label2.Text = ys.ToString(); } } isysf = true; } } } //按键事件(键盘操作) private void button1_KeyDown(object sender, KeyEventArgs e) { string key = e.KeyData.ToString(); if (key == "D1" || key == "NumPad1") button1_Click(button1,null); else if (key == "D2" || key == "NumPad2") button1_Click(button2, null); else if (key == "D3" || key == "NumPad3") button1_Click(button3, null); else if (key == "D4" || key == "NumPad4") button1_Click(button4, null); else if (key == "D5" || key == "NumPad5") button1_Click(button5, null); else if (key == "D6" || key == "NumPad6") button1_Click(button6, null); else if (key == "D7" || key == "NumPad7") button1_Click(button7, null); else if (key == "D8" || key == "NumPad8") button1_Click(button8, null); else if (key == "D9" || key == "NumPad9") button1_Click(button9, null); else if (key == "D0" || key == "NumPad0") button1_Click(button11, null); else if (key == "Add") button14_Click(button14, null); else if (key == "Subtract" || key == "OemMinus") button14_Click(button13, null); else if (key == "Multiply") button14_Click(button15, null); else if (key == "Divide") button14_Click(button16, null); else if (key == "Oemplus") button20_Click(button20, null); } //托盘菜单关闭按钮点击事件 private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } //托盘菜单隐藏窗体点击事件 private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Minimized; this.ShowInTaskbar = false; 隐藏ToolStripMenuItem.Text = "恢复显示"; string picPath = Application.StartupPath + "\\btn_restore_highlight.png"; 隐藏ToolStripMenuItem.Image = Image.FromFile(picPath); } else { this.WindowState = FormWindowState.Normal; this.ShowInTaskbar = true; 隐藏ToolStripMenuItem.Text = "隐藏到托盘"; string picPath = Application.StartupPath + "\\btn_mini_highlight.png"; 隐藏ToolStripMenuItem.Image = Image.FromFile(picPath); } } } }
设计界面:
效果: