using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtHidden1.Text = "";
txtHidden1.Visible = false;
txtHidden2.Text = "";
txtHidden2.Visible = false;//txtHidden1和txtHidden2的功能都是临时存放操作符
txtTemp.Text = "";//txtTemp的功能是存放临时结果
txtTemp.Visible = false;
txtResult.Text = "0";
txtResult.ReadOnly = true;//txtResult的功能是结果的显示以及数字的输入
}
private void btn0_Click(object sender, EventArgs e)//按钮0
{
PressNumBtn(sender);
}
private void btn1_Click(object sender, EventArgs e)//按钮1
{
PressNumBtn(sender);
}
private void btn2_Click(object sender, EventArgs e)//按钮2
{
PressNumBtn(sender);
}
private void btn3_Click(object sender, EventArgs e)//按钮3
{
PressNumBtn(sender);
}
private void btn4_Click(object sender, EventArgs e)//按钮4
{
PressNumBtn(sender);
}
private void btn5_Click(object sender, EventArgs e)//按钮5
{
PressNumBtn(sender);
}
private void btn6_Click(object sender, EventArgs e)//按钮6
{
PressNumBtn(sender);
}
private void btn7_Click(object sender, EventArgs e)//按钮7
{
PressNumBtn(sender);
}
private void btn8_Click(object sender, EventArgs e)//按钮8
{
PressNumBtn(sender);
}
private void btn9_Click(object sender, EventArgs e)//按钮9
{
PressNumBtn(sender);
}
private void btnDian_Click(object sender, EventArgs e)//按钮小数点
{
if (txtResult.Text == "")
{
return;
}
else if (txtResult.Text.Contains("."))
{
return;
}
else
{
txtResult.Text = txtResult.Text + ".";
}
}
private void BtnJia_Click(object sender, EventArgs e)//按钮+
{
PressOperBtn(sender);
}
private void btnJian_Click(object sender, EventArgs e)//按钮-
{
PressOperBtn(sender);
}
private void btnCheng_Click(object sender, EventArgs e)//按钮*
{
PressOperBtn(sender);
}
private void btnChu_Click(object sender, EventArgs e)//按钮除
{
PressOperBtn(sender);
}
private void btnEqual_Click(object sender, EventArgs e)//按钮=
{
if (txtHidden2.Text != "" && txtTemp.Text != "")
{
txtResult.Text = WorkOut();
}
txtHidden1.Text = "";
txtHidden2.Text = "";
txtTemp.Text = "";
}
private void btnCE_Click(object sender, EventArgs e)//按钮清0
{
txtResult.Text = "0";
txtHidden1.Text = "";
txtHidden2.Text = "";
txtTemp.Text = "";
}
private void PressNumBtn(object sender)//操作数字键从0到9
{
Button btn = sender as Button;
string tag = btn.Text;
if (txtHidden1.Text == "")
{
if (txtResult.Text == "0")
{
txtResult.Text = tag;
}
else
{
txtResult.Text = txtResult.Text + tag;
}
}
else
{
txtHidden2.Text = txtHidden1.Text;
txtHidden1.Text = "";
txtResult.Text = tag;
}
}
private void PressOperBtn(object sender)//操作加、减、乘、除
{
Button btn = sender as Button;
string tag = btn.Text;
if (txtHidden2.Text != "" && txtTemp.Text != "")
{
txtResult.Text = WorkOut();
}
txtHidden1.Text = tag;
txtTemp.Text = txtResult.Text;
}
private string WorkOut()//核心功能:计算
{
string oper = txtHidden2.Text;
double num1 = Convert.ToDouble(txtTemp.Text);
double num2 = Convert.ToDouble(txtResult.Text);
double result=0;
switch (oper)
{
case "+": result = num1 + num2; break;
case "-": result = num1 - num2; break;
case "*": result = num1 * num2; break;
case "/": result = num1 / num2; break;
default: break;
}
string Result = result.ToString();
return Result;
}
}
}