View Single Post
  #1  
Old 08-01-2010, 03:17 PM
--BulletMagnet-- is offline --BulletMagnet--
--BulletMagnet--'s Avatar
DF2 Forever

Join Date: Jun 2005
Location: USA
Posts: 718

Send a message via MSN to --BulletMagnet--
C# Quadratic Formula Calculator (Beginner)

I got tired of doing this for my math homework all the time, and didn't find a suitable web calculator that I liked.

I consider myself no more than a novice, so this serves as both help and an opportunity for any programmers out there to offer constructive criticism. I often find that I've taken the least efficient route when I'm programming something, so feel free to tell me ways in which I could have simplified the program.

Main Function
Code:
double formula_main(double a, double b, double c, double negorpos)
        {
            double result;
            result = (-b + negorpos*(Math.Sqrt(Math.Pow(b, 2) - 4 * a * c))) / (2 * a);

            // Value To Be Returned
            return result;
        }
Calling the Function (add this to a button click event or similar event handler)
Code:
            // Declare Variables
            double a, b, c, res1, res2;

            // Execute 
            // Ensure That The Input Boxes Hold Text
            if (txtA.Text != "" & txtB.Text != "" & txtC.Text != "")
            {
                // Assign User Input To Variables
                // C# Cannot Accept The String Value Like C++, You Must Convert It
                // With the Parse method of the Integer class
                a = int.Parse(txtA.Text);
                b = int.Parse(txtB.Text);
                c = int.Parse(txtC.Text);

                // Perform Function
                res1 = formula_main(a, b, c, 1);
                res2 = formula_main(a, b, c, -1);

                // Display Results
                txtZeros.AppendText(res1.ToString());
                txtZeros.AppendText(Environment.NewLine + res2.ToString());
            }
NOTE: txtA, txtB, and txtC are the respective names of three TextBoxes I placed on the form to accept user input. txtZeros is a multiline TextBox which displays the results.

I haven't added many accessories yet, besides a simple copy button to copy the results. I'm working on some validation in the TextBox, and for now, imaginary numbers are not supported and all radicals must be simplified before inputting them.

Feel free to use.
Reply With Quote