#include"iostream" using namespace std; void save1(int a, int b, int c); void save2(int a, int b, int c); void save3(int a, int b, int c); int main() { cout << "求解Ax^2+Bx+C=0的解" << endl; cout << "please input A B C" << endl; cout << "輸入一個參數(shù)后,,請按回車鍵" << endl; cout << "input A:"; int A; cin >> A; cout << endl; cout << "input B:"; int B; cin >> B; cout << endl; cout << "input C:"; int C; cin >> C; cout << endl; int temp = B * B - 4 * A*C; if (temp < 0) { save1(A,B,C); } if (temp ==0) { save2(A, B, C); } if (temp > 0) { save3(A, B, C); } system("pause"); return 0; } void save3(int a, int b, int c) { double temp = b * b - 4 * a*c; double tenp1 = sqrt(temp); double x = (-b + tenp1) / 2 * a; double x1 = -x; cout << "第一個解" << x << endl; cout << "第二個解" << x1 << endl; } void save2(int a, int b, int c) { double temp = b * b - 4 * a*c; double tenp1 = sqrt(temp); double x = (-b + tenp1) / 2 * a; cout << "一個解" << x << endl; } void save1(int a, int b, int c) { double temp, real1, real2, image1, image2; temp = -(b*b - 4 * a * c); real1 = -b / (2 * a *1.0); real2 = real1; image1 = sqrt(temp); image2 = -image1; cout << "兩個虛根" << endl; cout << "x1 = " << real1 << " + " << image1 << "j" << endl; cout << "x2 = " << real2 << image2 << "j" << endl;
}
|