#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
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;
}
private:
int A,B,C;
float x1,x2;
};
ostream& operator<<(ostream&out,Akar& keluaran){
keluaran.cetak_disk();
if(keluaran.disk()>=0)keluaran.cetak_akar();
else out<<"Akar imajiner";
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;
}
}
}
int main(int argc, char** argv) {
Akar kasus;
cin>>kasus;
kasus.hitung_akar();
cout<<kasus;
return 0;
}
Tidak ada komentar:
Posting Komentar