c#开发的计算器,第一次运算得出结果后,在等号之后继续运算会出错
c#开发的计算器,第一次运算得出结果后,在等号之后继续运算会出错,如:1+2=3,不清除,再点乘号就变成5了,再点3,点等号就等于15了,本来正确结果应该是9,谁帮忙看下...
c#开发的计算器,第一次运算得出结果后,在等号之后继续运算会出错,如:1+2=3,不清除,再点乘号就变成5了,再点3,点等号就等于15了,本来正确结果应该是9,谁帮忙看下怎么改。详细点感激不尽啊
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:
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 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:
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;
} 展开
1个回答
展开全部
从你描述的问题来看应该是你在点击+-*/按钮时,在改变运算类型之前进行了一次运算。分析下你面熟的问题:
点击1,然后点击+,由于_valueL没有值,所以不进行运算;点击2,再点击=,进行1+2的运算,结果为3。此时的_valueL为2
点击*,进行一次+的运算:3+2,结果为5,然后才改变运算类型为*
再点击3,此时的运算类型仍然为*
点击=,进行一次*的运算:5*3,结果为15
如果是上面分析的这种情况的话,那你的修改方式应该是:只有在点击=时才进行结果的运算;点击+-*/时,只改变运算类型。
你贴出来的代码里面没有+-*/按钮的处理逻辑,而且代码没有注释,所以不确定上述分析的正确性,仅供参考了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询