C#计算器(窗体设计及其所有功能代码)
玛丽莲梦兔
797次浏览
2020年07月31日 08:56
最佳经验
本文由作者推荐
badboy歌词-嫣怎么读
using c;
using entModel;
using
using g;
using
using
using
namespace 计算器
{
public partial class Calculator_Form : Form
{
//当前字符串
public string mNow;
public int OperationType;//适用于计算的类型,应用数字去控制算法
//定义二个操作数
public double mNumber1, mNumber2;
//判断是否是连=
Boolean bRe = false;
//临时变量用于连=时存储第二个操作数
double tmp = 0;
public Calculator_Form()
{
InitializeComponent();
}
//数字0-9
private void Calculator_Form_Click(object sender, EventArgs e)
{
//获得触发的按钮
Button lButton = sender as Button;
string lString = Number_;
if (lString == "0" && == 1)
{
Number_ = ng();
}
else
{
Number_ = ng(lString + );
}
bRe = false;
}
//.
private void Dot_button_Click(object sender, EventArgs e)
{
if (Number_f(".") != -1)
{
(this, "操作错误");
}
else
{
Number_ = Number_ + ".";
}
bRe = false;
}
//Backspace
private void Backspace_button_Click(object sender, EventArgs e)
{
string lString = Number_;
lString = ing(0, - 1);
Number_ = lString;
bRe = false;
}
//CE
private void CE_button_Click(object sender, EventArgs e)
{
Number_ = "";
bRe = false;
}
//C
private void C_button_Click(object sender, EventArgs e)
{
mNow = "";
Number_ = "0";
bRe = false;
}
//+
private void Add_button_Click(object sender, EventArgs e)
{
if (Number_ngth > 0)
{
mNumber1 = le(Number_);
mNow = "";
OperationType = 1;
Number_ = "";
Number_();
}
bRe = false;
}
//-
private void Substract_button_Click(object sender, EventArgs e)
{
if (Number_ngth > 0)
{
mNumber1 = le(Numb
er_);
mNow = "";
OperationType = 2;
Number_ = "";
Number_();
}
bRe = false;
}
//*
private void Multiply_button_Click(object sender, EventArgs e)
{
if (Number_ngth > 0)
{
mNumber1 = le(Number_);
mNow = "";
OperationType = 3;
Number_ = "";
Number_();
}
bRe = false;
}
// /
private void Divide_button_Click(object sender, EventArgs e)
{
if (Number_ngth > 0)
{
mNumber1 = le(Number_);
mNow = "";
OperationType = 4;
Number_ = "";
Number_();
}
bRe = false;
}
//求相反数
private void OppositeNumber_button_Click(object sender, EventArgs e)
{
mNumber1 = le(Number_);
Number_ = ng(0 - mNumber1);
bRe = false;
}
//√
private void RadicalSignbutton_Click(object sender, EventArgs e)
{
if (le(Number_) < 0)
{
_ = "error";
}
else
{
mNumber2 = le(Number_);
mNow = "";
Number_ = ng((mNumber2));//写成了一段代码
Number_();
}
bRe = false;
}
//%
private void Remainder_button_Click(object sender, EventArgs e)
{
mNow = ng(le(Number_) / 100);
Number_ = mNow;
mNow = "";
bRe = false;
}
//求倒数
private void Reciprocal_button_Click(object sender, EventArgs e)
{
if (le(Number_) == 0)
{
_ = "error";
}
else
{
mNumber2 = le(Number_);
mNow = "";
Number_ = ng(1 / mNumber2);
}
bRe = false;
}
//=
private void Equal_button_Click(object sender, EventArgs e)
{
switch (OperationType)
{
case 1:
//看是不是连=
if (bRe)
{
Num
ber_ = ng(le(Number_) + tmp);
break;
}
mNumber2 = le(Number_);
tmp = mNumber2;
double add = mNumber1 + mNumber2;
Number_ = ng(add);
break;
case 2:
if (bRe)
{
Number_ = ng(le(Number_) - tmp);
break;
}
mNumber2 = le(Number_);
tmp = mNumber2;
double Sub = mNumber1 - mNumber2;
Number_ = ng(Sub);
break;
case 3:
if (bRe)
{
Number_ = ng(le(Number_) * tmp);
break;
}
mNumber2 = le(Number_);
tmp = mNumber2;
double Mul = mNumber1 * mNumber2;
Number_ = ng(Mul);
break;
case 4:
if (bRe)
{
Number_ = ng(le(Number_) / tmp);
break;
}
mNumber2 = le(Number_);
tmp = mNumber2;
double Div = mNumber1 / mNumber2;
Number_ = ng(Div);
break;
}
mNow = "";
bRe = true;
}
}
}