如何用c#编写计算器代码(只要加减乘除)?

如题所述

第1个回答  2022-11-16
using System;\x0d\x0ausing System.Drawing;\x0d\x0ausing System.Collections;\x0d\x0ausing System.ComponentModel;\x0d\x0ausing System.Windows.Forms;\x0d\x0ausing System.Data;\x0d\x0a\x0d\x0anamespace jisuan\x0d\x0a{\x0d\x0a /// \x0d\x0a /// Form1 的摘要说明。\x0d\x0a /// \x0d\x0a public class Form1 : System.Windows.Forms.Form\x0d\x0a {\x0d\x0a private System.Windows.Forms.TextBox textBox1;\x0d\x0a private System.Windows.Forms.TextBox textBox2;\x0d\x0a private System.Windows.Forms.TextBox textBox3;\x0d\x0a private System.Windows.Forms.ComboBox comboBox1;\x0d\x0a private System.Windows.Forms.Button button1;\x0d\x0a private System.Windows.Forms.Button button2;\x0d\x0a private System.Windows.Forms.Button button3;\x0d\x0a /// \x0d\x0a /// 必需的设计器变量。\x0d\x0a /// \x0d\x0a private System.ComponentModel.Container components = null;\x0d\x0a\x0d\x0a public Form1()\x0d\x0a {\x0d\x0a //\x0d\x0a // Windows 窗体设计器支持所必需的\x0d\x0a //\x0d\x0a InitializeComponent();\x0d\x0a\x0d\x0a //\x0d\x0a // TODO: 在 InitializeComponent 调用后添加任何构造函数代码\x0d\x0a //\x0d\x0a }\x0d\x0a\x0d\x0a /// \x0d\x0a /// 清理所有正在使用的资源。\x0d\x0a /// \x0d\x0a protected override void Dispose( bool disposing )\x0d\x0a {\x0d\x0a if( disposing )\x0d\x0a {\x0d\x0a if (components != null) \x0d\x0a {\x0d\x0a components.Dispose();\x0d\x0a }\x0d\x0a }\x0d\x0a base.Dispose( disposing );\x0d\x0a }\x0d\x0a\x0d\x0a #region Windows 窗体设计器生成的代码\x0d\x0a /// \x0d\x0a /// 设计器支持所需的方法 - 不要使用代码编辑器修改\x0d\x0a /// 此方法的内容。\x0d\x0a /// \x0d\x0a private void InitializeComponent()\x0d\x0a {\x0d\x0a this.textBox1 = new System.Windows.Forms.TextBox();\x0d\x0a this.textBox2 = new System.Windows.Forms.TextBox();\x0d\x0a this.textBox3 = new System.Windows.Forms.TextBox();\x0d\x0a this.comboBox1 = new System.Windows.Forms.ComboBox();\x0d\x0a this.button1 = new System.Windows.Forms.Button();\x0d\x0a this.button2 = new System.Windows.Forms.Button();\x0d\x0a this.button3 = new System.Windows.Forms.Button();\x0d\x0a this.SuspendLayout();\x0d\x0a // \x0d\x0a // textBox1\x0d\x0a // \x0d\x0a this.textBox1.Location = new System.Drawing.Point(24, 72);\x0d\x0a this.textBox1.Name = "textBox1";\x0d\x0a this.textBox1.Size = new System.Drawing.Size(100, 21);\x0d\x0a this.textBox1.TabIndex = 0;\x0d\x0a this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);\x0d\x0a // \x0d\x0a // textBox2\x0d\x0a // \x0d\x0a this.textBox2.Location = new System.Drawing.Point(312, 72);\x0d\x0a this.textBox2.Name = "textBox2";\x0d\x0a this.textBox2.Size = new System.Drawing.Size(100, 21);\x0d\x0a this.textBox2.TabIndex = 1;\x0d\x0a // \x0d\x0a // textBox3\x0d\x0a // \x0d\x0a this.textBox3.Location = new System.Drawing.Point(448, 72);\x0d\x0a this.textBox3.Name = "textBox3";\x0d\x0a this.textBox3.Size = new System.Drawing.Size(88, 21);\x0d\x0a this.textBox3.TabIndex = 2;\x0d\x0a // \x0d\x0a // comboBox1\x0d\x0a // \x0d\x0a this.comboBox1.Items.AddRange(new object[] {\x0d\x0a "+",\x0d\x0a "-",\x0d\x0a "*",\x0d\x0a "/"});\x0d\x0a this.comboBox1.Location = new System.Drawing.Point(152, 72);\x0d\x0a this.comboBox1.Name = "comboBox1";\x0d\x0a this.comboBox1.Size = new System.Drawing.Size(121, 20);\x0d\x0a this.comboBox1.TabIndex = 3;\x0d\x0a this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);\x0d\x0a // \x0d\x0a // button1\x0d\x0a // \x0d\x0a this.button1.Location = new System.Drawing.Point(64, 184);\x0d\x0a this.button1.Name = "button1";\x0d\x0a this.button1.Size = new System.Drawing.Size(104, 32);\x0d\x0a this.button1.TabIndex = 4;\x0d\x0a this.button1.Text = "计算";\x0d\x0a this.button1.Click += new System.EventHandler(this.button1_Click);\x0d\x0a // \x0d\x0a // button2\x0d\x0a // \x0d\x0a this.button2.Location = new System.Drawing.Point(216, 192);\x0d\x0a this.button2.Name = "button2";\x0d\x0a this.button2.Size = new System.Drawing.Size(75, 23);\x0d\x0a this.button2.TabIndex = 5;\x0d\x0a this.button2.Text = "清除";\x0d\x0a this.button2.Click += new System.EventHandler(this.button2_Click);\x0d\x0a // \x0d\x0a // button3\x0d\x0a // \x0d\x0a this.button3.Location = new System.Drawing.Point(376, 192);\x0d\x0a this.button3.Name = "button3";\x0d\x0a this.button3.Size = new System.Drawing.Size(75, 23);\x0d\x0a this.button3.TabIndex = 6;\x0d\x0a this.button3.Text = "退出";\x0d\x0a this.button3.Click += new System.EventHandler(this.button3_Click);\x0d\x0a // \x0d\x0a // Form1\x0d\x0a // \x0d\x0a this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);\x0d\x0a this.ClientSize = new System.Drawing.Size(656, 366);\x0d\x0a this.Controls.Add(this.button3);\x0d\x0a this.Controls.Add(this.button2);\x0d\x0a this.Controls.Add(this.button1);\x0d\x0a this.Controls.Add(this.comboBox1);\x0d\x0a this.Controls.Add(this.textBox3);\x0d\x0a this.Controls.Add(this.textBox2);\x0d\x0a this.Controls.Add(this.textBox1);\x0d\x0a this.Name = "Form1";\x0d\x0a this.Text = "Form1";\x0d\x0a this.ResumeLayout(false);\x0d\x0a this.PerformLayout();\x0d\x0a\x0d\x0a }\x0d\x0a #endregion\x0d\x0a\x0d\x0a /// \x0d\x0a /// 应用程序的主入口点。\x0d\x0a /// \x0d\x0a [STAThread]\x0d\x0a static void Main() \x0d\x0a {\x0d\x0a Application.Run(new Form1());\x0d\x0a }\x0d\x0a public double jia(double a,double b)\x0d\x0a {\x0d\x0a return a+b;\x0d\x0a }\x0d\x0a public double jian(double a,double b)\x0d\x0a {\x0d\x0a return a-b;\x0d\x0a }\x0d\x0a public double cheng(double a,double b)\x0d\x0a {\x0d\x0a return a*b;\x0d\x0a }\x0d\x0a public double chu(double a,double b)\x0d\x0a {\x0d\x0a return a/b;\x0d\x0a }\x0d\x0a\x0d\x0aprivate void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)\x0d\x0a {\x0d\x0a\x0d\x0a }\x0d\x0a\x0d\x0a private void textBox1_TextChanged(object sender, System.EventArgs e)\x0d\x0a {\x0d\x0a\x0d\x0a }\x0d\x0a\x0d\x0a private void button1_Click(object sender, System.EventArgs e)\x0d\x0a {\x0d\x0a string i=this.comboBox1.SelectedItem.ToString();\x0d\x0a\x0d\x0a switch(i)\x0d\x0a {\x0d\x0a \x0d\x0a case "+":this.textBox3.Text=this.jia(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();\x0d\x0a \x0d\x0a break;\x0d\x0a\x0d\x0a case "-":this.textBox3.Text=this.jian(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();\x0d\x0a break;\x0d\x0a case "*":this.textBox3.Text=this.cheng(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();\x0d\x0a break;\x0d\x0a case"/" :this.textBox3.Text=this.chu(double.Parse(this.textBox1.Text),double.Parse(this.textBox2.Text)).ToString();\x0d\x0a break;\x0d\x0a }\x0d\x0a\x0d\x0a }\x0d\x0a\x0d\x0a private void button2_Click(object sender, System.EventArgs e)\x0d\x0a {\x0d\x0a this.textBox1.Text=null;\x0d\x0a this.textBox2.Text=null;\x0d\x0a this.textBox3.Text = null;\x0d\x0a }\x0d\x0a\x0d\x0a private void button3_Click(object sender, EventArgs e)\x0d\x0a {\x0d\x0a \x0d\x0a //this.Hide();\x0d\x0a Application.Exit();\x0d\x0a //this.Close();\x0d\x0a \x0d\x0a }\x0d\x0a }\x0d\x0a}
相似回答