Solving a quadratic equation using C ++

Source: Internet
Author: User

Question: solving the quadratic equation of one element: the solution of ax ² + bx + c = 0, where a = 1, B =-3, C = 2.

Analysis: We all know that there are three solutions to the quadratic equation of a single element, that is, considering the arithmetic square root of △= (b²-4ac). When △> 0, the equation has two unequal real root X1 = (-B + SQRT (D)/(2a); x2 = (-B-SQRT (D)/(2a ); when △= 0, the equation has two equal solid roots X1 = x2 = (-B)/(2a); When △< 0, the equation has two unequal virtual root X1 = (-B)/(2a), X2 = SQRT (-d)/(2a ).

The above is our idea for solving mathematical problems. But if we use C ++ to solve the problem of solving the quadratic equations of one element, how can we solve it? First, we need to create a findroot class and declare four float data members, namely A, B, C, and D. D represents delta, and the root X1 and X2 of the equation are declared as double, in order to easily declare constructor at the same time, the main function of the program prepares parameters, solves and outputs them. The findroot UML diagram and OBJ object UML diagram are shown below:

The following describes the implementation steps and results of this program in Visual C ++ 6.0:

Steps:

1. Create a project named equation, that is, select the "Win32 console application" option in the project menu bar.

2. Add a header file equation. h for the project, declare the findroot class, and include the required header file.

Code:

# If! Defined (equation_h)
# Define equation_h
# Include <iostream>
# Include <cmath>
Using namespace STD;

// Declare the findroot class

Class findroot
{
PRIVATE:
Float a, B, c, d;
Double x1, x2;
Public:
Findroot (float X, float y, float Z );
Void find ();
Void display ();
};
# Endif

3. Add the equation. cpp file for the project and implement findroot in this file.

 

Code:

 

# Include "equation. H"
// Implement the findroot class

Findroot: findroot (float X, float y, float Z) // Constructor
{
A = X;
B = y;
C = z;
D = B * B-4 * a * C;
}
Void findroot: Find () // implement the member function find
{
If (D> 0)
{
X1 = (-B + SQRT (D)/(2 * );
X2 = (-B-SQRT (D)/(2 * );
Return;
}
Else if (D = 0)
{
X1 = x2 = (-B)/(2 * );
Return;
}
Else
{
X1 = (-B)/(2 * );
X2 = SQRT (-d)/(2 * );
}
}
Void findroot: Display () // implement the member function display
{
If (D> 0)
{
Cout <"X1 =" <X1 <"\ nx2 =" <X2 <Endl;
Return;
}
Else if (D = 0)
{
Cout <"X1 = x2 =" <X1 <Endl;
Return;
}
Else
{
Cout <"X1 =" <X1 <"+" <X2 <"I" <Endl;
Cout <"X2 =" <X1 <"-" <X2 <"I" <Endl;
}
}

4. Add the find. cpp file for the project, and design the main function in the file to solve the equation.

 

Code:

 

# Include "equation. H"
Void read (float &, float &, float &); // The parameter uses the object reference method.
Void main ()
{
Float a, B, c;
Cout <"is a program for finding the root of the equation ax2 + bx + c = 0. "<Endl;
For (;) // cyclically solving
{
Read (A, B, C); // preparation Coefficient
If (A = 0) return; // determines whether to end the For Loop Based on the input coefficient.
Findroot OBJ (A, B, C); // create the object OBJ
OBJ. Find (); // solve
OBJ. Display (); // output the calculation result
}
}
Void read (float & A, float & B, float & C) // preparation Coefficient
{
Cout <"input equation coefficient :";
Cin>;
If (A = 0) // exit the READ function if the coefficient is zero.
{
Getchar (); // eliminates the influence of carriage return.
Return;
};
Cout <"input equation coefficient B :";
Cin> B;
Cout <"input equation coefficient C :";
Cin> C;
}

5. Compile and run the program.

 

Running result:

The results are the same as the calculation results. With such a program, we will not worry about solving the unary quadratic equation in mathematics. We only need to convert the equation into a general formula, then, you can quickly get the answer by entering the corresponding parameters in the program. Although you can get the answer quickly, you are still not encouraged to use the program to solve the equation, because you are learning how to solve the problem and how to solve the problem, only in this way can we realize and improve.

Of course, this program is not particularly well designed, but for the specific design project ideas, it is more complicated. Thank you for your understanding. I hope it will help you. By the way, I wish you a good mood.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.