首页 > 代码库 > 【C#】简单计算器源代码

【C#】简单计算器源代码

form1.cs

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void clear_Click(object sender, EventArgs e)
{
box1.Text = "";
box2.Text = "";
jg.Text = "";
box1.Focus();

}

private void equal_Click(object sender, EventArgs e)
{
string b1 = box1.Text;
string b2 = box2.Text;
string fh = choice.Text;
double sum;
if (string.IsNullOrEmpty(b1) || string.IsNullOrEmpty(b2))
{
MessageBox.Show("请输入要计算的数据");
return;
}

double _a = 0;
bool _aParse = double.TryParse(b1, out _a);
double _b = 0;
bool _bParse = double.TryParse(b2, out _b);
if (!_aParse || !_bParse)
{
MessageBox.Show("请输入数字");
return;
}
if (string.IsNullOrEmpty(fh))
{
MessageBox.Show("请选择计算符号");
return;
}


switch (choice.Text.ToString())
{
case "+": sum = _a + _b;
jg.Text = Convert.ToString(sum = _a + _b);
MessageBox.Show(Convert.ToString(sum = _a + _b));
break;
case "-": sum = _a - _b;
jg.Text = Convert.ToString(sum = _a - _b);
MessageBox.Show(Convert.ToString(sum = _a - _b));
break;
case "*": sum = _a * _b; 
jg.Text = Convert.ToString(sum = _a * _b);
MessageBox.Show(Convert.ToString(sum = _a * _b));
break;
case "/": sum = _a / _b; 
jg.Text = Convert.ToString(sum = _a / _b);
MessageBox.Show(Convert.ToString(sum = _a / _b));
break;
}
clear.Focus();
}

private void Form1_Load(object sender, EventArgs e)
{
choice.Items.Add("+");
choice.Items.Add("-");
choice.Items.Add("*");
choice.Items.Add("/");
}
}

}


form.Designer.cs 

namespace WindowsApplication2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.box1 = new System.Windows.Forms.TextBox();
this.jg = new System.Windows.Forms.Label();
this.equal = new System.Windows.Forms.Button();
this.box2 = new System.Windows.Forms.TextBox();
this.clear = new System.Windows.Forms.Button();
this.choice = new System.Windows.Forms.ComboBox();
this.tishi = new System.Windows.Forms.Label();
this.SuspendLayout();
// 
// box1
// 
this.box1.Location = new System.Drawing.Point(36, 115);
this.box1.Name = "box1";
this.box1.Size = new System.Drawing.Size(100, 21);
this.box1.TabIndex = 0;
// 
// jg
// 
this.jg.AutoSize = true;
this.jg.Location = new System.Drawing.Point(522, 117);
this.jg.Name = "jg";
this.jg.Size = new System.Drawing.Size(35, 12);
this.jg.TabIndex = 3;
this.jg.Text = "_____";
// 
// equal
// 
this.equal.Location = new System.Drawing.Point(416, 106);
this.equal.Name = "equal";
this.equal.Size = new System.Drawing.Size(75, 23);
this.equal.TabIndex = 3;
this.equal.Text = "等于";
this.equal.UseVisualStyleBackColor = true;
this.equal.Click += new System.EventHandler(this.equal_Click);
// 
// box2
// 
this.box2.Location = new System.Drawing.Point(290, 114);
this.box2.Name = "box2";
this.box2.Size = new System.Drawing.Size(100, 21);
this.box2.TabIndex = 2;
// 
// clear
// 
this.clear.Location = new System.Drawing.Point(416, 174);
this.clear.Name = "clear";
this.clear.Size = new System.Drawing.Size(75, 23);
this.clear.TabIndex = 4;
this.clear.Text = "清除";
this.clear.UseVisualStyleBackColor = true;
this.clear.Click += new System.EventHandler(this.clear_Click);
// 
// choice
// 
this.choice.FormattingEnabled = true;
this.choice.Location = new System.Drawing.Point(157, 117);
this.choice.Name = "choice";
this.choice.Size = new System.Drawing.Size(94, 20);
this.choice.TabIndex = 1;
this.choice.Text = "+";
// 
// tishi
// 
this.tishi.AutoSize = true;
this.tishi.Location = new System.Drawing.Point(155, 89);
this.tishi.Name = "tishi";
this.tishi.Size = new System.Drawing.Size(131, 12);
this.tishi.TabIndex = 6;
this.tishi.Text = "亲,请选择计算符号哦~";
// 
// Form1
// 
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(605, 275);
this.Controls.Add(this.tishi);
this.Controls.Add(this.choice);
this.Controls.Add(this.clear);
this.Controls.Add(this.box2);
this.Controls.Add(this.equal);
this.Controls.Add(this.jg);
this.Controls.Add(this.box1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox box1;
private System.Windows.Forms.Label jg;
private System.Windows.Forms.Button equal;
private System.Windows.Forms.TextBox box2;
private System.Windows.Forms.Button clear;
private System.Windows.Forms.ComboBox choice;
private System.Windows.Forms.Label tishi;

}
}