C#计算器(窗体设计及其所有功能代码)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
?using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
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_textBox.Text;
if (lString == "0" && lString.Length == 1)
{
Number_textBox.Text = Convert.ToString(lButton.Text);
}
else
{
Number_textBox.Text = Convert.ToString(lString + lButton.Text);
}
bRe = false;
}
//.
private void Dot_button_Click(object sender, EventArgs e)
{
if (Number_textBox.Text.IndexOf(".") != -1)
{
System.Windows.Forms.MessageBox.Show(this, "操作错误");
}
else
{
Number_textBox.Text = Number_textBox.Text + ".";
}
bRe = false;
}
//Backspace
private void Backspace_button_Click(object sender, EventArgs e)
{
string lString = Number_textBox.Text;
lString = lString.Substring(0, lString.Length - 1);
Number_textBox.Text = lString;
bRe = false;
}
//CE
private void CE_button_Click(object sender, EventArgs e)
{
Number_textBox.Text = "";
bRe = false;
}
//C
private void C_button_Click(object sender, EventArgs e)
{
mNow = "";
Number_textBox.Text = "0";
bRe = false;
}
//+
private void Add_button_Click(object sender, EventArgs e)
{
if (Number_textBox.TextLength > 0)
{
mNumber1 = Convert.ToDouble(Number_textBox.Text);
mNow = "";
OperationType = 1;
Number_textBox.Text = "";
Number_textBox.Focus();
}
bRe = false;
}
//-
private void Substract_button_Click(object sender, EventArgs e)
{
if (Number_textBox.TextLength > 0)
{
mNumber1 = Convert.ToDouble(Numb
er_textBox.Text);
mNow = "";
OperationType = 2;
Number_textBox.Text = "";
Number_textBox.Focus();
}
bRe = false;
}
//*
private void Multiply_button_Click(object sender, EventArgs e)
{
if (Number_textBox.TextLength > 0)
{
mNumber1 = Convert.ToDouble(Number_textBox.Text);
mNow = "";
OperationType = 3;
Number_textBox.Text = "";
Number_textBox.Focus();
}
bRe = false;
}
// /
private void Divide_button_Click(object sender, EventArgs e)
{
if (Number_textBox.TextLength > 0)
{
mNumber1 = Convert.ToDouble(Number_textBox.Text);
mNow = "";
OperationType = 4;
Number_textBox.Text = "";
Number_textBox.Focus();
}
bRe = false;
}
//求相反数
private void OppositeNumber_button_Click(object sender, EventArgs e)
{
mNumber1 = Convert.ToDouble(Number_textBox.Text);
Number_textBox.Text = Convert.ToString(0 - mNumber1);
bRe = false;
}
//√
private void RadicalSignbutton_Click(object sender, EventArgs e)
{
if (Convert.ToDouble(Number_textBox.Text) < 0)
{
this.Number_textBox.Text = "error";
}
else
{
mNumber2 = Convert.ToDouble(Number_textBox.Text);
mNow = "";
Number_textBox.Text = Convert.ToString(System.Math.Sqrt(mNumber2));//写成了一段代码
Number_textBox.Focus();
}
bRe = false;
}
//%
private void Remainder_button_Click(object sender, EventArgs e)
{
mNow = Convert.ToString(Convert.ToDouble(Number_textBox.Text) / 100);
Number_textBox.Text = mNow;
mNow = "";
bRe = false;
}
//求倒数
private void Reciprocal_button_Click(object sender, EventArgs e)
{
if (Convert.ToDouble(Number_textBox.Text) == 0)
{
this.Number_textBox.Text = "error";
}
else
{
mNumber2 = Convert.ToDouble(Number_textBox.Text);
mNow = "";
Number_textBox.Text = Convert.ToString(1 / mNumber2);
}
bRe = false;
}
//=
private void Equal_button_Click(object sender, EventArgs e)
{
switch (OperationType)
{
case 1:
//看是不是连=
if (bRe)
{
Num
ber_textBox.Text = Convert.ToString(Convert.ToDouble(Number_textBox.Text) + tmp);
break;
}
mNumber2 = Convert.ToDouble(Number_textBox.Text);
tmp = mNumber2;
double add = mNumber1 + mNumber2;
Number_textBox.Text = Convert.ToString(add);
break;
case 2:
if (bRe)
{
Number_textBox.Text = Convert.ToString(Convert.ToDouble(Number_textBox.Text) - tmp);
break;
}
mNumber2 = Convert.ToDouble(Number_textBox.Text);
tmp = mNumber2;
double Sub = mNumber1 - mNumber2;
Number_textBox.Text = Convert.ToString(Sub);
break;
case 3:
if (bRe)
{
Number_textBox.Text = Convert.ToString(Convert.ToDouble(Number_textBox.Text) * tmp);
break;
}
mNumber2 = Convert.ToDouble(Number_textBox.Text);
tmp = mNumber2;
double Mul = mNumber1 * mNumber2;
Number_textBox.Text = Convert.ToString(Mul);
break;
case 4:
if (bRe)
{
Number_textBox.Text = Convert.ToString(Convert.ToDouble(Number_textBox.Text) / tmp);
break;
}
mNumber2 = Convert.ToDouble(Number_textBox.Text);
tmp = mNumber2;
double Div = mNumber1 / mNumber2;
Number_textBox.Text = Convert.ToString(Div);
break;
}
mNow = "";
bRe = true;
}
}
}