Novahq.net Forum

Novahq.net Forum (https://novahq.net/forum/index.php)
-   Web design and Programming (https://novahq.net/forum/forumdisplay.php?f=32)
-   -   C# Quadratic Formula Calculator (Beginner) (https://novahq.net/forum/showthread.php?t=45255)

--BulletMagnet-- 08-01-2010 03:17 PM

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.

VooDoo- 08-19-2010 07:19 PM

i did this on my ti92 .. not the exact code but similar ..

great time saver lol

--BulletMagnet-- 08-21-2010 09:26 AM

True, it is an awesome time saver. I'm still having trouble with C# displaying imaginary numbers... that's the huge downside to the program now.

atholon 08-21-2010 01:53 PM

Looks good.

VooDoo- 08-23-2010 09:27 AM

well if you get an imaginary number as a result ... why do you care what it is? lol

atholon 08-23-2010 11:23 AM

I think you also need to be handling exceptions that Int32.Parse throws. :|

--BulletMagnet-- 08-23-2010 07:46 PM

Quote:

Originally Posted by atholon (Post 367645)
i think you also need to be handling exceptions that int32.parse throws. :|

Quote:

Originally Posted by --bulletmagnet--
i'm working on some validation in the textbox...

:)

--BulletMagnet-- 08-26-2010 11:58 PM

Quote:

Originally Posted by VooDoo- (Post 367641)
well if you get an imaginary number as a result ... why do you care what it is? lol

Because the professor cares... lol.

VooDoo- 09-04-2010 12:39 AM

then i don't even want to know what the hell kind of math you do lol

ShadowZ 09-10-2010 09:41 AM

Quote:

Originally Posted by --BulletMagnet-- (Post 367606)
True, it is an awesome time saver. I'm still having trouble with C# displaying imaginary numbers... that's the huge downside to the program now.

If it is not in the math library, then you might have to program a short section of code that says "sqrt(-1) = j"

--BulletMagnet-- 09-13-2010 12:11 AM

Quote:

Originally Posted by ShadowZ (Post 367993)
If it is not in the math library, then you might have to program a short section of code that says "sqrt(-1) = j"

Yea that might be a necessity. Are you a programmer yourself?

ShadowZ 09-13-2010 11:19 PM

Quote:

Originally Posted by --BulletMagnet-- (Post 368060)
Yea that might be a necessity. Are you a programmer yourself?

A little bit. I major in EEEN but I do have some basic C++ knowledge.

Most of the coding I do nowadays is either modding the .def files for DFx2 or assembly language for the HCS9S12.

Also got a bit of experiance with the Ti-83 :headbang:


All times are GMT -5. The time now is 10:52 AM.

Powered by vBulletin®