using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyComputer
{
public partial class Form1 : Form
{
//HasDot表示有没有小数点,false位默认值,表示没有
bool HasDot = false;
//HasOption表示有没有选择运算符,默认是没有的false
bool HasOption = false;
string Option ;
//存储第一个运算或者存储上一次运算的结果
decimal OptionNumber = 0;
public Form1()
{
InitializeComponent();
}
private void btnZero_Click(object sender, EventArgs e)
{
OptionNumber = Convert.ToDecimal(txtResult.Text);
if (HasDot || OptionNumber!=0)
txtResult.Text = txtResult.Text + "0";
}
private void btnZero_CursorChanged(object sender, EventArgs e)
{
}
private void btnDot_Click(object sender, EventArgs e)
{
if (HasDot == false)
{
HasDot = true;
txtResult.Text = txtResult.Text + ".";
}
}
private void btnBackSpace_Click(object sender, EventArgs e)
{
MessageBox.Show("你好!");
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "2";
}
private void btnOne_Click(object sender, EventArgs e)
{
Button btnNumber = (Button)sender;
//看当前的值是不是0,还有打没打小数点
if (Convert.ToDecimal(txtResult.Text) == 0 && HasDot ==false)
{
txtResult.Text = btnNumber.Text;
}
else
txtResult.Text = txtResult.Text + btnNumber.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtResult.Text = "0";//重置文本框
HasDot = false; //重置小数点
}
private void btnAdd_Click(object sender, EventArgs e)
{
//存储第一个运算数
OptionNumber = Convert.ToDecimal(txtResult.Text);
//存储运算符
Option = ((Button)sender).Text;
//准备输入第二个运算数
txtResult.Text = "0";//重置文本框
HasDot = false; //重置小数点
}
private void btnResult_Click(object sender, EventArgs e)
{
if (Option.Equals("+"))
{
//MessageBox.Show("相等");
txtResult.Text = (OptionNumber + Convert.ToDecimal(txtResult.Text)).ToString();
}
else if (Option.Equals("-"))
{
//运算符号为-
txtResult.Text = (OptionNumber - Convert.ToDecimal(txtResult.Text)).ToString();
}
else if (Option.Equals("*"))
{
//运算符号为*
txtResult.Text = (OptionNumber * Convert.ToDecimal(txtResult.Text)).ToString();
}
else if (Option.Equals("/"))
{
//运算符号为/
//除数不能为0
if (Convert.ToDecimal(txtResult.Text).Equals(0))
{
txtResult.Text = "error!除数不能为0";
}
else
txtResult.Text = (OptionNumber / Convert.ToDecimal(txtResult.Text)).ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}