#include <stdafx.h>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::fixed;
using std::setprecision;
#include <cmath>
int main()
{
float a, b, c, D;
float a, b, c, D;
cout << "請輸入一元二次方程式係數a : ";
cin >> a;
cout << "請輸入一元二次方程式係數b : ";
cin >> b;
cout << "請輸入一元二次方程式係數c : ";
cin >> c;
cout << endl;
if ( a==0 )
{
if ( b==0 )
{
if ( c==0 )
cout << "x無限多解" << endl;
else
cout << "x無解" << endl;
}
else
cout << fixed << setprecision( 4 )
<< "x = " << -c / b << endl;
}
else
{
D = b * b - 4 * a * c;
if ( D > 0 )
cout << fixed << setprecision( 4 )
<< "x = " << -b / ( 2 * a ) + sqrt( D )
<< " , " << -b / ( 2 * a ) - sqrt( D ) << endl;
else if ( D == 0 )
cout << fixed << setprecision( 4 )
<< "x = " << -b / ( 2 * a )
<< " , " << -b / ( 2 * a ) << endl;
else
cout << fixed << setprecision( 4 )
<< "x = " << -b / ( 2 * a ) << "+"
<< sqrt( -D ) / ( 2 * a ) << "i , "
<<-b / ( 2 * a ) << "-"
<< sqrt( -D ) / ( 2 * a ) << "i" << endl;
}
cout << endl;
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.174.164.157