`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.
Exponent_Click is referenced on x^y button. User input a number, clicks x^y button, inputs number (exponent)
on hitting = button, if Exponent flag in equals_click is true, convert first input and second input into double and pass to Math.Pow(). convert result to String and display in textbox.
Only one text field.
2^2 is giving me 419,000.
CodePudding user response:
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.
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 learn how 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 ofString
- use
bool
instead ofBoolean
if (exponentFlag == true)
is the same asif (exponentFlag)
- things like
result = exponent
is confusing, because the exponent is clearly not the result. num
could be a local variable
CodePudding user response:
Let's say you have textBox1 for integer1 and textBox2 for integer2. You have also "x^y" button and "=" button. There is also textBox3 for your result.
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.
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();