[Alpro] Flowchart & Program Mencari akar Persamaan Kuadrat

Flowchart:

Program C++ dengan class overloading

#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

class Persamaan{
friend istream& operator>>(istream&, Persamaan&);
friend ostream& operator<<(ostream&, Persamaan&);
public:
void proses();
void HitungPers1();
void HitungPers2();
void HitungPers3();
private:
float a,b,c,D,X,X1,X2,XRiil,XImajiner;

};

istream& operator>>(istream& input, Persamaan& Masukan){
cout<<" \t\t    PROGRAM MENCARI AKAR PERSAMAAN KUADRAT \n\n";
cout<<" Perhitungan akar-akar persamaan aX2+bX+c= 0\n";
cout<<"Masukkan nilai koefisien a,b,dan c\n";
cout<<"Nilai a: ";
input>>Masukan.a;
cout<<"Nilai b: ";
input>>Masukan.b;
cout<<"Nilai c: ";
input>>Masukan.c;
}

ostream& operator<<(ostream& output, Persamaan& Keluaran){
output<<"Akar Persamaan "<<Keluaran.a<<"X2 + "<<Keluaran.b<<"X + "<<Keluaran.c<<"= 0 adalah\n";
if(Keluaran.D>0){Keluaran.HitungPers1();}
else if(Keluaran.D==0){Keluaran.HitungPers2();}
else{Keluaran.HitungPers3();}
}

void Persamaan::proses(){
D = b*b-4*a*c;
cout<<"Diskriminan =\n "<<D<<endl;
}

void Persamaan::HitungPers1(){
X1 = (-b+sqrt(D))/(2*a);
X2 = (-b-sqrt(D))/(2*a);
cout<<" X1 =\n "<<X1;
cout<<" X2 =\n "<<X2;
}

void Persamaan::HitungPers2(){
X = -b/(2*a);
cout<<" X1 = X2 = \n"<<X;
}

void Persamaan::HitungPers3(){
XRiil = -b/(2*a);
XImajiner = sqrt(-D)/(2*a);
cout<<" Akar imajiner:\n ";
cout<<" X1 = "<<XRiil<<"+"<<XImajiner<<"*i\n ";
cout<<" X2 = "<<XRiil<<"-"<<XImajiner<<"*i\n ";
}


int main(int argc, char *argv[])
{
Persamaan kuadrat;
cin>>kuadrat;
kuadrat.proses();
cout<<kuadrat;

system("PAUSE");
return EXIT_SUCCESS;
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s