简体   繁体   English

x^y 计算器按钮 - 在 windows forms C#

[英]x^y calculator Button - in windows forms C#

   `String calcHistory = "";
    String SavedCalcHistory = "";
    String result = "";
    String equation = "";
    String baseNum = "";
    double num;
    Boolean exponentFlag = false;`

private void digits_Click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        equation += b.Text;
        textBox1.Text += b.Text;
    }

private void ExponentInput_Click(object sender, EventArgs e)
    {
        baseNum = textBox1.Text;
        textBox1.Text = "";
        exponentFlag = true;
    }

private void equals_Click(object sender, EventArgs e)
    {
        result = equation;
        result = new DataTable().Compute(result, null).ToString();
        calcHistory += equation + " = " + result + "\n";
        textBox1.Text = result;
        if(exponentFlag == true)
        {
            num = Convert.ToDouble(baseNum);
            double expo = Convert.ToDouble(textBox1.Text);
            textBox1.Text = Math.Pow(num, expo).ToString();
        }
        exponentFlag = false;
    }

Digit_click is referenced on the number buttons. Digit_click 在数字按钮上引用。

Exponent_Click is referenced on x^y button. Exponent_Click 在 x^y 按钮上被引用。 User input a number, clicks x^y button, inputs number (exponent)用户输入一个数字,点击 x^y 按钮,输入数字(指数)

on hitting = button, if Exponent flag in equals_click is true, convert first input and second input into double and pass to Math.Pow().在点击 = 按钮时,如果 equals_click 中的 Exponent 标志为真,则将第一个输入和第二个输入转换为双精度并传递给 Math.Pow()。 convert result to String and display in textbox.将结果转换为字符串并显示在文本框中。

Only one text field.只有一个文本字段。

2^2 is giving me 419,000. 2^2 给我 419,000。

The check for the exponent flag needs to be first - before you calculate the result.需要先检查指数标志 - 在计算结果之前。 Also, calcHistory probably needs to be updated correctly as well.此外, calcHistory可能也需要正确更新。

private void equals_Click(object sender, EventArgs e)
{
    if (exponentFlag == true)
    {
        num = Convert.ToDouble(baseNum);
        double expo = Convert.ToDouble(textBox1.Text);
        result = Math.Pow(num, expo).ToString();
        exponentFlag = false;
        equation = "" + baseNum + "^" + expo;
    }
    else
    {
        result = equation;
        result = new DataTable().Compute(result, null).ToString();
    }
    textBox1.Text = result;
    calcHistory += equation + " = " + result + "\n";
}

Other notes:其他注意事项:

  • you really want to learnhow to debug small programs , especially how to use a debugger and set breakpoints so that you can see step by step what is happening.你真的很想学习如何调试小程序,尤其是如何使用调试器和设置断点,这样你就可以一步一步地看到发生了什么。
  • use string instead of String使用string而不是String
  • use bool instead of Boolean使用bool而不是Boolean
  • if (exponentFlag == true) is the same as if (exponentFlag) if (exponentFlag == true)if (exponentFlag)相同
  • things like result = exponent is confusing, because the exponent is clearly not the result. result = exponent之类的东西令人困惑,因为指数显然不是结果。
  • num could be a local variable num可以是局部变量

Let's say you have textBox1 for integer1 and textBox2 for integer2 .假设您有integer1textBox1integer2textBox2 You have also " x^y " button and " = " button.您还有x^y按钮和 = 按钮。 There is also textBox3 for your result.您的结果还有textBox3

Now what you have to do is make sure if provided numbers are correct, by checking if they aren't equal to string.empty, etc.现在你要做的是通过检查它们是否不等于 string.empty 等来确保提供的数字是否正确。

If you're sure, you can use simple:如果你确定,你可以使用简单的:

int integer1 = Convert.ToInt32(textBox1.Text);
int integer2 = Convert.ToInt32(textBox2.Text);

textBox3.Text = Convert.ToInt32(Math.Pow(integer1 , integer2 )).ToString();

It works for me, check it.它对我有用,检查一下。

BUT

Update:更新:

If your calculator only has one textBox, you can do the same:如果你的计算器只有一个文本框,你也可以这样做:

int indexOfPower = textBox3.Text.IndexOf("^");
int integer1 = Convert.ToInt32(textBox3.Text.Substring(0, indexOfPower));
int integer2 = Convert.ToInt32(textBox3.Text.Substring(indexOfPower+1));

textBox3.Text = Convert.ToInt32(Math.Pow(integer1, integer2)).ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM