I'm developing a calculator, it will give all of the functionality of calc.exe; however it will have a few of my own adjustments that I use constantly and have to do on paper or with different methods on calc.
I need help with subtraction, multiplication, and division the most.
I'm using custom enums, arrays, methods, and other ways of customizing my source making it easier to manage.
public enum NumericFormat
{
Binary,
Octal,
Decimal,
Hexadecimal
}
public enum Precision
{
_64Bit,
_32Bit,
_16Bit,
_8Bit
}
private int[] Input = new int[100];
private string[] Operators = new string[99];
private int Index = 0;
private int Current = 0;
private int Results = 0;
private string HexadecimalResults = "";
private string OctalResults = "";
private string BinaryResults = "";
private NumericFormat CurrentFormat = NumericFormat.Decimal;
private NumericFormat PreviousFormat = NumericFormat.Decimal;
private Precision CurrentPrecision = Precision._64Bit;
private bool OperatorClicked = false;
Those are my main variables, accessed throughout the source code. My main problem with subtraction is that it's backwards. If I subtract 3 from 15 it returns -12.
My problem with multiplication and division is that it's just returning the final value in my array.
// Source for adding, subtracting, multiplying, and dividing.
private void Equals_Click(object sender, EventArgs e)
{
for (int i = 0; i < Index; i++)
{
if (Operators[i] == "+")
Results += Input[i];
else if (Operators[i] == "-")
Results -= Input[i];
else if (Operators[i] == "*")
Results *= Input[i];
else if (Operators[i] == "/")
Results /= Input[i];
}
if (Current != 0)
{
if (Operators[Index] == "+")
Results += Current;
else if (Operators[Index] == "-")
Results -= Current;
else if (Operators[Index] == "*")
Results *= Current;
else if (Operators[Index] == "/")
Results /= Current;
}
// The following two lines have not been tested, but are replacing a for loop that goes through and resets all elements in these arrays to 0 and empty.
Operators = new string[99];
Input = new int[100];
Index = 0;
ConvertFormat();
UpdateOutput();
}
private void UpdateOutput()
{
InputDisplay1.Text = "0";
InputDisplay2.Text = Results.ToString();
}
private void Add_Click(object sender, EventArgs e)
{
ConvertToDecimal(InputDisplay1.Text, CurrentFormat);
Input[Index] = Current;
Operators[Index] = "+";
Index++;
OperatorClicked = true;
}
private void Subtract_Click(object sender, EventArgs e)
{
ConvertToDecimal(InputDisplay1.Text, CurrentFormat);
Input[Index] = Current;
Operators[Index] = "-";
Index++;
OperatorClicked = true;
}
private void Multiply_Click(object sender, EventArgs e)
{
ConvertToDecimal(InputDisplay1.Text, CurrentFormat);
Input[Index] = Current;
Operators[Index] = "*";
Index++;
OperatorClicked = true;
}
private void Divide_Click(object sender, EventArgs e)
{
ConvertToDecimal(InputDisplay1.Text, CurrentFormat);
Input[Index] = Current;
Operators[Index] = "/";
Index++;
OperatorClicked = true;
}
My other problem is with scientific notation, I've developed a method that only sort of works, so I will research more and fix that, however I'm trying to figure out how to convert double values to hex, binary, and octal and then outputing them in scientific notation. For example with hex:
12D687 in notation should be c.something * a^5;
However it constantly outputs zero. I event tried converting the number to decimal first, performing the division or multiplication by 10, and then converting it back. Any help with these topics is greatly appreciated. I don't have my entire source code available so I can't give the other methods I use in my calc but those are the basics. I believe the logic error should be located in the Equals_Click method. All the ConvertToDecimal method does is convert the input value to decimal in order to store it for arithmatic. Then I am outputing it converted back to the currently selected format.