Calculator shows an error: 'Input string was not in a correct format.'

Calculator shows an error: 'Input string was not in a correct format.'

我正在尝试制作一个计算器,但我有点卡在 mod 和 exp 操作上,每当我尝试使用它们时,我都会遇到以下错误:

System.FormatException: 'Input string was not in a correct format.'

这是发生错误的事件:

private void Equal_Click(object sender, EventArgs e)
        {
            switch (operationPerf)
            {
                case "+":
                    TB.Text = (result + Double.Parse(TB.Text)).ToString();
                    CO.Text = "";
                    break;
                case "-":
                    TB.Text = (result - Double.Parse(TB.Text)).ToString();
                    CO.Text = "";
                    break;
                case "*":
                    TB.Text = (result * Double.Parse(TB.Text)).ToString();
                    CO.Text = "";
                    break;
                case "/":
                    TB.Text = (result / Double.Parse(TB.Text)).ToString();
                    CO.Text = "";
                    break;
                case "Mod":
                    TB.Text = (result % Double.Parse(TB.Text)).ToString();
                    CO.Text = "";
                    break;
                case "Exp":
                    TB.Text = Math.Exp(Double.Parse(TB.Text) * Math.Log((result) * 4)).ToString();
                    CO.Text = "";
                    break;
                default:
                    break;
            }
            result = Double.Parse(TB.Text); // Here is where the error happens
            CO.Text = "";
        }

可能 operationPerf 是预期运算符的 none,因此执行默认情况并且文本框仍然包含原始表达式(例如 "23Exp3"),当然,无法转换为 double.

您真的应该将计算器逻辑与输入和输出分开。在单独的代码文件中创建 Calculator class (Calculator.cs)

public class Calculator
{
    public double Result { get; set; }

    public void Calculate(string operation, double operand)
    {
        switch (operation)
        {
            case "+":
                Result = Result + operand;
                break;
            case "-":
                Result = Result - operand;
                break;
            case "*":
                Result = Result * operand;
                break;
            case "/":
                Result = Result / operand;
                break;
            case "Mod":
                Result = Result % operand;
                break;
            case "Exp":
                Result = Math.Exp(operand * Math.Log(Result * 4));

                // Did you want this instead?
                // Result = Math.Pow(Result, operand);
                break;
            default:
                break;
        }
    }
}

它更容易理解,因为它只包含纯数学,不需要任何解析或格式化。

在表单字段中(在表单顶部 class)然后您可以声明

public partial class Form1 : Form
{
    private Calculator _calculator = new Calculator();

    ...
}

事件处理程序也更容易

private void Equal_Click(object sender, EventArgs e)
{
    if (Double.TryParse(TB.Text, out double operand)) {
        _calculator.Calculate(operationPerf, operand);
        TB.Text = _calculator.Result.ToString();
        CO.Text = "";
    } else {
        MsgBox.Show("The TextBox does not contain a number");
    }
}

如果文本框仍然包含 "23Exp3",您将无法将其转换为 double。使用不同的文本框输入运算、运算符(数字)和结果,或者将字符串分成三部分(2 个数字和运算)。

我真的不明白Math.Exp(operand * Math.Log(Result * 4))背后的逻辑。由于我不知道预期的行为,因此无法给您任何建议。您想改用 Math.Pow 吗?

public static double Pow(double x,  double y)

Returns a specified number raised to the specified power.

参见:Math.Pow Method (Double, Double)

您要求用户输入如下内容:10Exp3。你希望你的程序计算 10 的 3 次方,然后显示结果。所以在这种情况下你期望 1000。但你只是这样做:

case "Exp":
    TB.Text = Math.Exp(Double.Parse(TB.Text) * Math.Log((result) * 4)).ToString();

您的代码的第一个问题是,您正在使用 Exp,因为这是 Exp 的文档:

Returns e raised to the specified power.

你想e升到指定的幂吗?你不可以。所以你不能使用那个方法。但是,即使您可以并且想要(也许您确实想要 e),计算机也无法将 10Exp3 转换为数字——您必须告诉它该做什么。

方法如下(阅读我的内联评论以了解更多信息):

var input = "10Exp3";
// Let's split the numbers using <Exp>
var splits = input.Split(new[] { "Exp" }, StringSplitOptions.None);

// Let's try and convert the first part into a double and see if it works
double numberBase = 0;
if (!double.TryParse(splits[0], out numberBase))
{
    // The part before the word Exp is
    // not a number, perhaps show an error to the user in a message box
}

// Now let's try and convert the second part 
double exponent = 0;
if (!double.TryParse(splits[1], out exponent))
{
    // The part after the word Exp is
    // not a number, perhaps show an error to the user in a message box
}

// Now we will call the Pow method or use Math.Exp if you want e
double result = Math.Pow(numberBase, exponent);