gpt4 book ai didi

C#语言,计算器

转载 作者:行者123 更新时间:2023-12-01 08:15:53 29 4
gpt4 key购买 nike

大家好,感谢您帮助我。

我用 C# 制作了这个计算器,但遇到了一个问题。
当我添加像 5+5+5 这样的东西时,它给了我正确的结果,但是当我想减去两个以上的数字并且还想除或乘以两个以上的数字时,我没有得到正确的结果。

你知道我做错了什么吗

非常感谢!

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 Calculator : Form
{
public Calculator()
{
InitializeComponent();
}

private void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnOne.Text;
//txtDisplay.Text = btnOne.Text;
}

private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
}

private void btnThree_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnThree.Text;
}

private void btnFour_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFour.Text;
}

private void btnFive_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFive.Text;
}

private void btnSix_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSix.Text;
}

private void btnSeven_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
}

private void btnEight_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnEight.Text;
}

private void btnNine_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnNine.Text;
}

private void btnZero_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnZero.Text;
}

private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}

private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + ",";
}


double total1 = 0;
double total2 = 0;

bool plusButtonClicked = false;
bool minusButtonClicked = false;
bool divideButtonClicked = false;
bool multiplyButtonClicked = false;

private void btnPlus_Click(object sender, EventArgs e)
{
plusButtonClicked = true;
minusButtonClicked = false;
divideButtonClicked = false;
multiplyButtonClicked = false;

total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
}

private void btnMinus_Click(object sender, EventArgs e)
{
plusButtonClicked = false;
minusButtonClicked = true;
divideButtonClicked = false;
multiplyButtonClicked = false;

total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
}



private void btnDivide_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();

plusButtonClicked = false;
minusButtonClicked = false;
divideButtonClicked = true;
multiplyButtonClicked = false;
}

private void btnMultiply_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();

plusButtonClicked = false;
minusButtonClicked = false;
divideButtonClicked = false;
multiplyButtonClicked = true;
}



private void btnEquals_Click(object sender, EventArgs e)
{

if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(txtDisplay.Text);
}

else if (minusButtonClicked == true)
{
total2 = total1 - double.Parse(txtDisplay.Text);
}

else if (divideButtonClicked == true)
{
total2 = total1 / double.Parse(txtDisplay.Text);
}

else if (multiplyButtonClicked == true)
{
total2 = total1 * double.Parse(txtDisplay.Text);
}


txtDisplay.Text = total2.ToString();
total1 = 0;
}




}
}

最佳答案

此代码尚未经过彻底测试。为什么不尝试以下操作:

using System;
using System.Windows.Forms;

namespace Calculator
{
public enum Operator
{
None,
Add,
Minus,
Divide,
Multiply
}

public partial class Calculator : Form
{
private double total = 0;
private double currentValue = 0;
private Operator currentOperator;

public Calculator()
{
InitializeComponent();
}

private void btnOne_Click(object sender, EventArgs e)
{
ShowInput(btnOne.Text);
}

private void btnTwo_Click(object sender, EventArgs e)
{
ShowInput(btnTwo.Text);
}

private void btnThree_Click(object sender, EventArgs e)
{
ShowInput(btnThree.Text);
}

private void btnFour_Click(object sender, EventArgs e)
{
ShowInput(btnFour.Text);
}

private void btnFive_Click(object sender, EventArgs e)
{
ShowInput(btnFive.Text);
}

private void btnSix_Click(object sender, EventArgs e)
{
ShowInput(btnSix.Text);
}

private void btnSeven_Click(object sender, EventArgs e)
{
ShowInput(btnSeven.Text);
}

private void btnEight_Click(object sender, EventArgs e)
{
ShowInput(btnEight.Text);
}

private void btnNine_Click(object sender, EventArgs e)
{
ShowInput(btnNine.Text);
}

private void btnZero_Click(object sender, EventArgs e)
{
ShowInput(btnZero.Text);
}

private void btnClear_Click(object sender, EventArgs e)
{
currentOperator = Operator.None;
txtDisplay.Clear();
total = 0;
}

private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + '.';
}

private void btnPlus_Click(object sender, EventArgs e)
{
ApplyOperator(Operator.Add);
}

private void btnMinus_Click(object sender, EventArgs e)
{
ApplyOperator(Operator.Minus);
}

private void btnDivide_Click(object sender, EventArgs e)
{
ApplyOperator(Operator.Divide);
}

private void btnMultiply_Click(object sender, EventArgs e)
{
ApplyOperator(Operator.Multiply);
}

private void btnEquals_Click(object sender, EventArgs e)
{
Evaluate();
txtDisplay.Text = Convert.ToString(total);
}

private void Evaluate()
{
switch (currentOperator)
{
case Operator.Add:
total += currentValue;
break;
case Operator.Minus:
total -= currentValue;
break;
case Operator.Divide:
total /= currentValue;
break;
case Operator.Multiply:
total *= currentValue;
break;
case Operator.None:
break;
}
currentValue = 0;
currentOperator = Operator.None;
}

private void ApplyOperator(Operator op)
{
if (currentOperator != Operator.None)
{
Evaluate();
}
else
{
total = double.Parse(txtDisplay.Text);
}
txtDisplay.Clear();
currentOperator = op;
}

private void ShowInput(String n)
{
txtDisplay.Text = txtDisplay.Text + n;
currentValue = double.Parse(txtDisplay.Text);
}
}
}

我仍然建议您最终制作某种形式的运算符解析器。看看 here或自己查看“Shunting Yard”算法。

关于C#语言,计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10451647/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com