您的当前位置:首页正文

c#计算器代码

2020-03-13 来源:年旅网


以下控件表

控件类型 Name Text form calcForm 计算器 button button1 0 ..... button10 9

bDot .(小数点) 小数点按钮 bPlus +(加号) 加号按钮 bSub -(减号) 减号按钮 bMul *(乘号) 乘号按钮 bDiv /(除号) 除号按钮 bEqu =(等号) 等号按钮

bClr AC 清除按钮

textBox txtCalc (空值) 用来显示输入及输出结果

using System;

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

using System.Windows.Forms;

namespace calculator {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

public string a; public string b; public string num;

public string op; public string result; public int i = 0;

private void button1_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"7\"; } else { num = num + \"7\"; } result = num; show(); }

public void show() //在textBox1里显示数据 {

if (i == 0) {

tBshow.Text = num + \".\"; }

else {

tBshow.Text = num; } }

private void b1_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"1\"; } else { num = num + \"1\"; } result = num; show(); }

private void b2_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"2\"; } else { num = num + \"2\"; } result = num; show(); }

private void b3_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"3\"; } else { num = num + \"3\"; } result = num; show(); }

private void b4_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"4\"; } else { num = num + \"4\"; } result = num; show(); }

private void b5_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"5\"; } else { num = num + \"5\"; } result = num; show();

}

private void b6_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"6\"; } else { num = num + \"6\"; } result = num; show(); }

private void b8_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"8\"; } else { num = num + \"8\"; } result = num; show(); }

private void b9_Click(object sender, EventArgs e) {

if (num == \"0\") { num = null; num = num + \"9\"; } else { num = num + \"9\"; }

result = num; show(); }

private void b0_Click(object sender, EventArgs e) {

if (num != \"0\") {

num = num + \"0\";

}

else { num = \"0\"; } result = num; show(); }

private void bDot_Click(object sender, EventArgs e) {

if (i == 0) { num = num + \".\"; i = 1; } }

private void bPlus_Click(object sender, EventArgs e) {

a = result; op = \"+\"; i = 0; num = null; }

private void bSub_Click(object sender, {

a = result; op = \"-\"; i = 0; num = null; }

private void bMul_Click(object sender, {

a = result; op = \"*\"; i = 0; num = null;

EventArgs e) EventArgs e) }

private void bDiv_Click(object sender, EventArgs e) {

a = result; op = \"/\"; i = 0; num = null; }

private void result_Box() //将结果显示在textBox1中 {

tBshow.Text = result; }

private void bEqu_Click(object sender, EventArgs e) {

b = num;

double m = 0, n = 0; double r = 0;

if (num == null) { tBshow.Text = \"0\"; } //若直接按“=”,则显示textBox1值为0

else {

if (op == null) { tBshow.Text = result; num = null; } //若输入了第一个数但未选择运算符,默认textBox1的值为num(第一个数) else {

if (b == null) //若输入了运算符但未输入第二个数,默认两个数相同 {

m = double.Parse(a); n = double.Parse(a); switch (op) {

case \"+\": r = m + n; result = r.ToString(); result_Box(); break;

case \"-\": r = m - n; result = r.ToString(); result_Box(); break;

case \"*\": r = m * n; result = r.ToString(); result_Box(); break;

case \"/\": if (n == 0)

{

tBshow.Text = \"除数不能为0\";

}

else { r = m / n; result = r.ToString(); result_Box(); }

} }

{

m = n = {

r.ToString(); result_Box(); r.ToString(); result_Box(); r.ToString(); result_Box(); break; else //输入已完成,执行“=” double.Parse(a); double.Parse(b); switch (op) case \"+\": r = m + n; result = break;

case \"-\": r = m - n; result = break;

case \"*\": r = m * n; result = break;

case \"/\": if (n == 0) {

tBshow.Text = \"除数不能为0\";

}

else { r = m / n; result = r.ToString(); result_Box(); }

break; } } } }

num = \"0\"; i = 0; }

private void bBack_Click(object sender, EventArgs e) {

num = result; if (num != null) {

if (num.Substring(num.Length - 1, 1) != \".\")

{

if (num.Length > 1) {

string k; k = num;

k = k.Substring(0, num.Length - 1); num = k; result = num; result_Box(); }

else { num = \"0\"; i = 0; show(); } } else {

if (num.Length > 1) {

string k; k = num;

k = k.Substring(0, num.Length - 2); i = 0; num = k;

tBshow.Text = num;

result = num; }

else { num = \"0\"; i = 0; show(); } } }

else { tBshow.Text = \"未输入数据\"; } }

private void bClr_Click(object sender, EventArgs e) {

tBshow.Text = \"0.\"; num = null; op = null; i = 0; }

private void tBshow_TextChanged(object sender, EventArgs e) {

}

} }

因篇幅问题不能全部显示,请点此查看更多更全内容