#include
#include
class Akar {
friend ostream& operator<<(ostream&, Akar&);
friend istream& operator>>(istream&, Akar&);
public:
Akar();
int disk() { return B*B-4*A*C; }
float akar1() {return -B+sqrt(disk())/(2*A); }
float akar2() {return -B-sqrt(disk())/(2*A); }
void hitung_akar();
void cetak_disk() { cout << "diskriminan = " << disk() << endl; }
void cetak_akar() {
cout << "x1 = " << akar1() << endl;
cout << "x2 = " << akar2() << endl;
}
void cetak_imaginer(){
cout << -B/(2*A)<<" + "<< sqrt(abs(disk()))/(2*A)<<" I ";
}
private:
float A, B, C,x1, x2;
};
ostream& operator<<(ostream& out, Akar& keluaran) {
keluaran.cetak_disk();
if (keluaran.disk() >= 0) keluaran.cetak_akar();
else keluaran.cetak_imaginer();
return out;
}
istream& operator>>(istream& in, Akar& masukan) {
cout << "Koefisien pangkat 2 : "; in >> masukan.A;
cout << "Koefisien pangkat 1 : "; in >> masukan.B;
cout << "Koefisien pangkat 0 : "; in >> masukan.C;
return in;
}
Akar::Akar() {
cout << "Menghitung akar persamaan kuadrat\n";
}
void Akar::hitung_akar() {
if (A == 0) {
cout << "bukan pers. kuadrat.\n";
cout << "Harga akar = " << -C/B; } else {
if (disk() > 0) {
x1 = akar1();
x2 = akar2();
} else if (disk() == 0) {
x1 = akar1();
x2 = x1;
}
}
}
main() {
Akar kasus;
cin >> kasus;
kasus.hitung_akar();
cout << kasus;
getch();
return 0;
}
0 comments:
Post a Comment