1 //++++++++++++++++++++++++++
2 // QuadraticEqu.cpp
3 //++++++++++++++++++++++++++
4
5 ///termino
6 ///#include head file
7 //#include <stdio.h> // C
8 #include <iostream>
9 #include <cmath>
10
11
12 ///USING
13 using namespace std;
14
15
16 ///main_entry
17 int main()
18 {
19
20
21 ///DECLARE
22 float a, b, c, x1, x2, iDiscx,iRealRoot,iImaRoot;
23
24
25 /// Prompt & keyin
26 cout << "Enter a=";
27 cin >> a;
28 cout << "Enter b=";
29 cin >> b;
30 cout << "Enter c=";
31 cin >>c;
32
33
34 ///calculate b2-4ac
35 iDiscx = b*b - 4*a*c;
36
37
38
39 ///iDiscx > 0
40 if (iDiscx > 0)
41 {
42
43
44 ///different real root
45 x1 = (-b + sqrt(iDiscx)) / (2*a);
46 x2 = (-b - sqrt(iDiscx)) / (2*a);
47 cout << "Two different real roots." << endl;
48 cout << "x1 = " << x1 << endl;
49 cout << "x2 = " << x2 << endl;
50
51
52 /// +
53 }//end if
54
55
56 ///iDiscx = 0
57 else if (iDiscx == 0)
58 {
59
60
61 ///same real root
62 cout << "Roots are same." << endl;
63 x1 = (-b + sqrt(iDiscx)) / (2*a);
64 cout << "x1 = x2 =" << x1 << endl;
65
66
67 /// +
68 }//end else if
69
70
71 ///iDiscx < 0
72 else
73 {
74
75
76 ///complex conjugate
77 iRealRoot = -b/(2*a);
78 iImaRoot =sqrt(-iDiscx)/(2*a);
79 cout << "Roots are complex conjugate." << endl;
80 cout << "x1 = " <<iRealRoot << "+" <<iImaRoot << "i" << endl;
81 cout << "x2 = " <<iRealRoot << "-" <<iImaRoot << "i" << endl;
82
83
84 /// +
85 }//end else
86
87
88 ///main_end
89 return 0;
90 } //main_end
91
92
This Blog is dedicated to Sir Johnson Lin
flow chart of C++ language for Quadratic Equation
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言