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 bit53.calculator {
public partial class Form1 : Form {
public enum CalcuType {
None,
/// /// 加法///
Addition,
/// /// 减法///
Substraction, /// /// 乘法///
Multlplication, /// /// 除法///
Division,
/// /// 乘方///
Involution, /// /// 开方
///
Square, }
private double? _valueF = null; private double? _valueL = null;
private CalcuType _CalculateType = CalcuType.None; private bool _IsNew = false;
private double? _StoredValue = null;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
btnval0.Click += new EventHandler(btnval_click); btnval1.Click += new EventHandler(btnval_click); btnval2.Click += new EventHandler(btnval_click); btnval3.Click += new EventHandler(btnval_click); btnval4.Click += new EventHandler(btnval_click); btnval5.Click += new EventHandler(btnval_click); btnval6.Click += new EventHandler(btnval_click); btnval7.Click += new EventHandler(btnval_click); btnval8.Click += new EventHandler(btnval_click); btnval9.Click += new EventHandler(btnval_click); }
private void btnval_click(object sender, EventArgs e) {
Button btn = (Button)sender;
string numberstr = this.txtValue.Text; if (this._IsNew) {
numberstr = btn.Text;
this._valueL = double.Parse(numberstr); } else {
if (new string[] { \"0\ {
numberstr = \"\"; }
numberstr += btn.Text;
this._valueF = double.Parse(numberstr); }
this.txtValue.Text = numberstr; this._IsNew = false; }
private void btnPI_Click(object sender, EventArgs e) {
if (this._IsNew) {
this._valueF = Math.PI; } else {
this._valueF = Math.PI; }
this.txtValue.Text = Math.PI.ToString(); this._IsNew = true; }
private void btnResult_Click(object sender, EventArgs e) {
switch (_CalculateType) {
case CalcuType.Addition:
this.txtValue.Text = (_valueF + _valueL).ToString(); break;
case CalcuType.Substraction:
this.txtValue.Text = (_valueF - _valueL).ToString(); break;
case CalcuType.Multlplication:
this.txtValue.Text = (_valueF * _valueL).ToString(); break;
case CalcuType.Division:
this.txtValue.Text = (_valueF / _valueL).ToString(); break;
case CalcuType.Involution:
this.txtValue.Text = Math.Pow((double)_valueF, (double)_valueL).ToString(); break;
case CalcuType.Square:
this.txtValue.Text = Math.Pow((double)_valueF, 1 / (double)_valueL).ToString(); break; }
this._valueF = double.Parse(this.txtValue.Text); this._IsNew = true; }
private void btnclears_Click(object sender, EventArgs e) {
this._valueL = null; this._valueF = null;
this._CalculateType = CalcuType.None; this.txtValue.Text = \"0\"; }
private void btnAddition_Click(object sender, EventArgs e) {
this.btnResult_Click(sender, e);
this._CalculateType = CalcuType.Addition; this._IsNew = true; }
private void btnSubstraction_Click(object sender, EventArgs e) {
this.btnResult_Click(sender, e);
this._CalculateType = CalcuType.Substraction; this._IsNew = true; }
private void btnMultiplication_Click(object sender, EventArgs e) {
this.btnResult_Click(sender, e);
this._CalculateType = CalcuType.Multlplication; this._IsNew = true;
}
private void btnDivision_Click(object sender, EventArgs e) {
this.btnResult_Click(sender, e);
this._CalculateType = CalcuType.Division; this._IsNew = true; }
private void btnsquare_Click(object sender, EventArgs e) {
this.btnResult_Click(sender, e);
this._CalculateType = CalcuType.Square; this._IsNew = true; }
private void btnInvolution_Click(object sender, EventArgs e) {
this.btnResult_Click(sender, e);
this._CalculateType = CalcuType.Involution; this._IsNew = true; }
private void btnBackspace_Click(object sender, EventArgs e) {
if (this.txtValue.Text.Length == 1) {
this.txtValue.Text = \"0\"; } else {
this.txtValue.Text = txtValue.Text.Substring(0, txtValue.Text.Length - 1); } }
private void btnMC_Click(object sender, EventArgs e) {
this._StoredValue = null; this.lblm.Text = \"\"; }
private void btnMR_Click(object sender, EventArgs e) {
if (_StoredValue == null) {
return; }
if (this._IsNew) {
this._valueF = this._StoredValue; } else {
this._valueF = this._StoredValue;
}
this.txtValue.Text = this._StoredValue.ToString(); this._IsNew = true;
this.lblm.Text = \"M\"; }
private void btnMS_Click(object sender, EventArgs e) {
try {
this._StoredValue = double.Parse(this.txtValue.Text); this.lblm.Text = \"M\"; }
catch (Exception) { }
}
private void btnMPlus_Click(object sender, EventArgs e) {
try {
if (this._StoredValue == null) {
this._StoredValue = double.Parse(this.txtValue.Text);
} else {
this._StoredValue += double.Parse(this.txtValue.Text); } }
catch (Exception) { } }
} }