[解答] 計程上機考Q7

看板NTUBIME96-HW作者 (柯南42真是妙)時間22年前 (2003/12/01 00:59), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
#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 + sqrt ( D ) )/ ( 2 * a ) << " , " << ( -b - sqrt ( D ) / ( 2 * a ) << 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 ※ 編輯: wendychang 來自: 218.174.158.179 (12/02 01:03)
文章代碼(AID): #_oY8FAq (NTUBIME96-HW)