Sempurnakan
algoritma 3.3. dengan akar imajiner berbentuk : a + bi dengan a dan b
bilangan real dan bila harga
Buatlah
algoritma dan class beserta method untuk mengecek apakah pemakai memasukkan
bilangan bulat atau bilangan real.
Berikut adalah kode C++:
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
class imajiner{
private:
float xRiil;
float xImajiner;
public:
float x;
float y;
float z;
float n;
float a;
float a1;
float a2;
void nilai(float p, float q){
xRiil=p;
xImajiner=q;}
float hasil();};
float imajiner::hasil(){
cout <<"\nMasukkan Koefisien a: "; cin>>x;
cout <<"Masukkan Koefisien b: "; cin>>y;
cout <<"Masukkan Koefisien c: "; cin>>z;
n = y * y - 4 * x * z;
cout << "\nDiskriminan= (" << y
<< " * " << y << " - 4 * " << x
<< " + " << z <<") = " << n
<< endl;
cout << "Akar persamaan " << x
<<"x^2 + " << y << "x + " << z
<<" = 0" << endl;
if (n > 0)
{
a1 = (-y+sqrt(n))/(2*x);
a2 = (-y-sqrt(n))/(2*x);
cout << "x1 = " << a1 <<
endl;
cout << "x2 = " << a2 <<
endl;
}
else if (n == 0)
{
a = -y/(2*x);
cout << "x1 = x2 = " << a <<
endl;
}
else
{
xRiil = -y/(2*x);
xImajiner = sqrt(-n)/(2*x);
cout << "\nImajiner: " << endl;
cout << "x1 = " << xRiil <<
" + " << xImajiner << " * i "<< endl;
cout << "x1 = " << xRiil <<
" - " << xImajiner << " * i "<< endl;
}
}
int main(int argc, char *argv[])
{
imajiner satu;
float p,q;
satu.nilai(p,q);
satu.hasil();
cout<<"\nOleh iful";
return 0;
}
Tidak ada komentar:
Posting Komentar