c#开发的计算器,第一次运算得出结果后,在等号之后继续运算会出错,如:1+2=3,不...
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 Calculator
{
public partial class Form1 : Form
{
public enum CalcuType
{
None,
/// <summary>
/// 加法
/// </summary>
Addition,
/// <summary>
/// 减法
/// </summary>
Substraction,
/// <summary>
/// 乘法
/// </summary>
Multiplication,
/// <summary>
/// 除法
/// </summary>
Division,
/// <summary>
/// 乘方
/// </summary>
Involution,
/// <summary>
/// 开方
/// </summary>
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", "0.", "-0", "-0." }.Contains(numberStr))
{
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._ValueL = 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.Multiplication:
this.txtValue.Text = (_ValueF * _ValueL).ToString();
break;
case CalcuType.Division:
if (_ValueL != 0)
{
this.txtValue.Text = (_ValueF / _ValueL).ToString();
}
else
{
MessageBox.Show("除数不能为0");
}
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);
}
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.Multiplication;
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 (this._IsNew)
{
this._ValueL = this._StoredValue;
}
else
{
this._ValueF = this._StoredValue;
}
this.txtValue.Text = this._StoredValue.ToString();
this._IsNew = true;
}
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)
{
}
}
private void btnDot_Click(object sender, EventArgs e)
{
if(txtValue.Text=="")
txtValue.Text="0.";
else
txtValue.Text=txtValue.Text+".";
}
}
}
c#开发的计算器,第一次运算得出结果后,在等号之后继续运算会出错,如:1+2=3,不清除,再点乘号就变成5了,再点3,点等号就等于15了,本来正确结果应该是9,谁帮忙看下怎么改。详细点感激不尽啊 我来了...周一给解决没上班 休息时间.. 在点乘之前你没有把CalcuType先更改成 Multiplication,所以你代码的运行顺序是1+2+2*3;还有你+、-*、/的运算不应该这样写,你这样写每一次都会冲突。你的想法是把输入框里面的数字先存到Value,但你没有考虑到进行一次运算后要进行对Value的清空重新赋值。
(下次能直接截图?这样真的行不好了解你的代码{:3_54:})
看完了你的_ValueL 没有清空
页:
[1]