【实例简介】

【实例截图】

【核心代码】

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 Calculator

{

public partial class Form1 : Form

{

string str, opp, opp1;

double num1, num2, result;

public Form1()

{

InitializeComponent();

}

private void number(object sender, EventArgs e)

{

Button b = (Button)(sender);

str = b.Text;

if (txtOutput.Text == "0")

{

txtOutput.Text = str;

}

else

txtOutput.Text = txtOutput.Text str;

}

private void operator1(object sender, EventArgs e)

{

Button b = (Button)(sender);

if (b.Text == " ")

{

num1 = double.Parse(txtOutput.Text);

txtOutput.Text = "";

opp = " ";

opp1 = "";

}

else if (b.Text == "-")

{

num1 = double.Parse(txtOutput.Text);

txtOutput.Text = "";

opp = "-";

opp1 = "";

}

else if (b.Text == "*")

{

num1 = double.Parse(txtOutput.Text);

txtOutput.Text = "";

opp = "*";

opp1 = "";

}

else if (b.Text == "/")

{

num1 = double.Parse(txtOutput.Text);

txtOutput.Text = "";

opp = "/";

opp1 = "";

}

else if (b.Text == "=")

{

if (opp1 != "=")

{

num2 = double.Parse(txtOutput.Text);

}

if (opp == " ")

{

num1 = num1 num2;

txtOutput.Text = "" num1.ToString();

}

else if (opp == "-")

{

num1 = num1 - num2;

txtOutput.Text = "" num1.ToString();

}

else if (opp == "*")

{

num1 = num1 * num2;

txtOutput.Text = "" num1.ToString();

}

else if (opp == "/")

{

if (num2 == 0)

{

txtOutput.Text = "除数不能为零";

}

else

{

num1 = num1 / num2;

txtOutput.Text = "" num1.ToString();

}

}

opp1 = "=";

}

}

private void operator2(object sender, EventArgs e)

{

Button b = (Button)(sender);

if (b.Text == ".")

{

str = txtOutput.Text;

int index = str.IndexOf(".");

if (index == -1)

{

txtOutput.Text = str ".";

}

}

else if (b.Text == "退格

{

if (txtOutput.Text != "")

{

str = txtOutput.Text;

str = str.Substring(0, str.Length - 1);

txtOutput.Text = str;

}

}

else if (b.Text == "CE")

{

txtOutput.Text = "0";

}

else if (b.Text == "C")

{

result = num1 = num2 = 0;

str = null;

opp = null;

txtOutput.Text = "0";

}

else if (b.Text == "sqrt")

{

num1 = double.Parse(txtOutput.Text);

result = Math.Sqrt(num1);

txtOutput.Text = result.ToString();

}

else if (b.Text == "1/x")

{

num1 = double.Parse(txtOutput.Text);

result = 1 / num1;

txtOutput.Text = result.ToString();

}

else if (b.Text == "%")

{

num1 = double.Parse(txtOutput.Text);

result = num1 / 100;

txtOutput.Text = result.ToString();

}

opp1 = "";

}

}

}

更多推荐