基于C#桌面应用制作计算器做出了少量改动,其主要改动为新增加了一个label控件,使其每一步运算结果由label2展示出来,而当点击“=”时,最终运算结果将由label1展示出来,此时label清空。
修改后运行效果
修改后全篇代码
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 WindowsFormsApp计算器
{public partial class Form1 : Form{string symbol, num1, num2,Num1,Num2;double rt;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){label1.Text = null;label2.Text = null;num1=null;num2=null;symbol = null;}private void button2_Click(object sender, EventArgs e){if (label1.Text.Length == 0&&symbol==null){label1.Text = null;}else if (label1.Text.Length == 0 && symbol != null){label1.Text = symbol;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = num1;symbol = null;}else{string del = label1.Text;label1.Text = del.Substring(0, del.Length - 1);label2.Text = label1.Text;if (label2.Text.Length>0&& label1.Text.Length != 0){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}else{if (label1.Text.Length <= 1 && symbol == null){label2.Text=null;}else{label2.Text = num1;}}if (label1.Text.Length < 30){if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}}}}private void button3_Click(object sender, EventArgs e){if (label1.Text.Length == 0){MessageBox.Show("error");}else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("error");}else{double reserve = double.Parse(label1.Text);reserve = -reserve;label1.Text = reserve.ToString();if (num1 != null && symbol != null){num2 = reserve.ToString();if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}}private void button4_Click(object sender, EventArgs e){string divide = "÷";if (symbol == null && label1.Text.Length<1&&num1==null){MessageBox.Show("无运算数字");}else{if (symbol == null){if(num1 == null){num1 = label1.Text;}symbol = divide;label1.Text = symbol;label2.Text = num1;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷") {MessageBox.Show("您已输入运算符");}else{num2 = label1.Text;num1 = rt.ToString();symbol = divide;label1.Text = symbol;label2.Text = rt.ToString();num2 = null;}}}}private void button8_Click(object sender, EventArgs e){string seven = "7";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;//如果前面有运算符,则先消除运算符再把数字投射到屏幕上}label1.Text = label1.Text + seven;//将数字投射到屏幕上if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if(num1!=null&&symbol!=null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button7_Click(object sender, EventArgs e){string eight = "8";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + eight;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button6_Click(object sender, EventArgs e){string nine = "9";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + nine;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button5_Click(object sender, EventArgs e){string multiply = "×";if (symbol == null && label1.Text.Length < 1 && num1 == null){MessageBox.Show("无运算数字");}else{if (symbol == null){if (num1 == null){num1 = label1.Text;}symbol = multiply;label1.Text = symbol;label2.Text = num1;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("您已输入运算符");}else{num2 = label1.Text;num1 = rt.ToString();symbol = multiply;label1.Text = symbol;label2.Text = rt.ToString();num2 = null;}}}}private void button12_Click(object sender, EventArgs e){string four = "4";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + four;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button11_Click(object sender, EventArgs e){string five = "5";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + five;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button10_Click(object sender, EventArgs e){string six = "6";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + six;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button9_Click(object sender, EventArgs e){string subtract = "-";if (symbol == null && label1.Text.Length < 1 && num1 == null){MessageBox.Show("无运算数字");}else{if (symbol == null){if (num1 == null){num1 = label1.Text;}symbol = subtract;label1.Text = symbol;label2.Text = num1;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("您已输入运算符");}else{num2 = label1.Text;num1 = rt.ToString();symbol = subtract;label1.Text = symbol;label2.Text = rt.ToString();num2 = null;}}}}private void button16_Click(object sender, EventArgs e){string one = "1";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + one;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button15_Click(object sender, EventArgs e){string two = "2";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + two;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button14_Click(object sender, EventArgs e){string three = "3";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + three;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button13_Click(object sender, EventArgs e){string add = "+";if (symbol == null && label1.Text.Length < 1 && num1 == null){MessageBox.Show("无运算数字");}else{if (symbol == null){if (num1 == null){num1 = label1.Text;}symbol = add;label1.Text = symbol;label2.Text = num1;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("您已输入运算符");}else{num2 = label1.Text;num1 = rt.ToString();symbol = add;label1.Text = symbol;label2.Text = rt.ToString();num2 = null;}}}}private void label2_Click(object sender, EventArgs e){}private void label1_Click(object sender, EventArgs e){}private void button20_Click(object sender, EventArgs e){if (label1.Text.Length == 0){MessageBox.Show("error");}else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("error");}else{double reserve = double.Parse(label1.Text);reserve = reserve / 100;label1.Text = reserve.ToString();if (num1 != null && symbol != null){num2 = reserve.ToString();if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}}private void button19_Click(object sender, EventArgs e){string zero = "0";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + zero;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button18_Click(object sender, EventArgs e){string dot = ".";int f=label1.Text.IndexOf(".");if(f == -1){if(label1.Text.Length == 0){MessageBox.Show("error");}else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("error");}else{if (label1.Text.Length < 30){label1.Text = label1.Text + dot;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}}}else{MessageBox.Show("error");}}private void button17_Click(object sender, EventArgs e){if (symbol == null && label1.Text.Length < 1){MessageBox.Show("无运算数字");}else{if (symbol == null){num1 = label1.Text;}else{if (label1.Text != null){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("error");}else{num2 = label1.Text;}}}}if (num2 != null){if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label1.Text = rt.ToString();label2.Text=null;num1 = rt.ToString();symbol = null;num2 = null;}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label1.Text = rt.ToString();label2.Text=null;num1 = rt.ToString();symbol = null;num2 = null;}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label1.Text = rt.ToString();label2.Text=null;num1 = rt.ToString();symbol = null;num2 = null;}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label1.Text = rt.ToString();label2.Text=null;num1 = rt.ToString();symbol = null;num2 = null;}}}}
}