Program,Algoritma,Flowchart Akar-akar persamaan kuadrat
Algoritma :
Deklarasi
A,B,C :integer {koefisien-koefisien persamaan}
disk : longlint {nilai diskriminan}
x1,x2 : real {nilai-nilai akar untuk disk>=0}
Deskripsi
read (A,B,C)
disk B*B-4*C
if(A=0) then write (‘bukan persamaan kuadrat’)
else if disk > 0 then
x1 (-B+ sqrt (disk)/2*A
x1 (-B+ sqrt (disk)/2*Aelse if disk = 0 then
x1 (-B/2*A
x2 (x1
else write (‘Akar imajiner’)
end if
write (x1,x2)
Flow chart
Source code
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
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 << " x1 =" << akar2 () << endl;
}
private:
int A,B,C ; // input.
float x1,x2 ; // akar 1 dan akar 2.
};
ostream& operator << (ostream& out, Akar& keluaran) {
keluaran.cetak_disk ();
if (keluaran.disk () >=0) keluaran.cetak_Akar();
else cout << "akar imajiner ";
return out ;
}
istream& operator >> (istream& in, Akar& masukkan) {
cout << "kooefisien pangkal 2 : " ; in >> masukkan.A ;
cout << "kooefisien pangkal 1 : " ; in >> masukkan.B ;
cout << "kooefisien pangkal 0 : " ; in >> masukkan.C ;
return in ;
}
Akar :: Akar () {
cout << " menghitung akar persamaan kuadrat\n" ;
}
void Akar :: hitung_Akar () {
if ( A==0) {
cout << " bukan persamaan 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;
system("PAUSE");
return EXIT_SUCCESS;
}
Program Konversi nilai dalm bentuk class
Algoritma
Deklarasi
nilai : integer
nilai huruf : char
Deskripsi
read (nilai)
if(nilai>0)and (nilai<=20)then nilai_huruf =E
else if(nilai>20)and (nilai<=40)then nilai_huruf =D
else if(nilai>40)and (nilai<=60)then nilai_huruf =C
else if(nilai>60)and (nilai<=80)then nilai_huruf =B
else nilai_huruf=A
endif
write (nilai_huruf)
source code
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
class Nilai {
friend ostream& operator << (ostream&, Nilai&);
friend istream& operator >> (istream&, Nilai&);
public:
Nilai() {};
void konversikan() {
if ((nilai > 0)&&(nilai <=20))
nilai_huruf='E';
else if ((nilai > 20)&&(nilai <=40))
nilai_huruf='D';
else if ((nilai > 40)&&(nilai <=60))
nilai_huruf='C';
else if ((nilai > 60)&&(nilai <=80))
nilai_huruf='B';
else nilai_huruf ='A';
}
private:
int nilai;
char nilai_huruf;
};
istream& operator >> (istream&in, Nilai& masukkan) {
cout << " Masukkan nilai angka =";
cin >> masukkan.nilai;
return in;
};
ostream& operator<<(ostream& out,Nilai& keluaran) {
out << "nilai angka : " << keluaran.nilai << endl;
out << "nilai huruf : " << keluaran.nilai_huruf;
return out;
}
int main(int argc, char *argv[])
{
{
Nilai angka;
cin>> angka;
angka.konversikan();
cout<<angka;
}
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
#include <math.h>
using namespace std;
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 << " x1 =" << akar2 () << endl;
}
private:
int A,B,C ; // input.
float x1,x2 ; // akar 1 dan akar 2.
};
ostream& operator << (ostream& out, Akar& keluaran) {
keluaran.cetak_disk ();
if (keluaran.disk () >=0) keluaran.cetak_Akar();
else cout << "akar imajiner ";
return out ;
}
istream& operator >> (istream& in, Akar& masukkan) {
cout << "kooefisien pangkal 2 : " ; in >> masukkan.A ;
cout << "kooefisien pangkal 1 : " ; in >> masukkan.B ;
cout << "kooefisien pangkal 0 : " ; in >> masukkan.C ;
return in ;
}
Akar :: Akar () {
cout << " menghitung akar persamaan kuadrat\n" ;
}
void Akar :: hitung_Akar () {
if ( A==0) {
cout << " bukan persamaan 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;
system("PAUSE");
return EXIT_SUCCESS;
}
Program Konversi nilai dalm bentuk class
Algoritma
Deklarasi
nilai : integer
nilai huruf : char
Deskripsi
read (nilai)
if(nilai>0)and (nilai<=20)then nilai_huruf =E
else if(nilai>20)and (nilai<=40)then nilai_huruf =D
else if(nilai>40)and (nilai<=60)then nilai_huruf =C
else if(nilai>60)and (nilai<=80)then nilai_huruf =B
else nilai_huruf=A
endif
write (nilai_huruf)
source code
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
class Nilai {
friend ostream& operator << (ostream&, Nilai&);
friend istream& operator >> (istream&, Nilai&);
public:
Nilai() {};
void konversikan() {
if ((nilai > 0)&&(nilai <=20))
nilai_huruf='E';
else if ((nilai > 20)&&(nilai <=40))
nilai_huruf='D';
else if ((nilai > 40)&&(nilai <=60))
nilai_huruf='C';
else if ((nilai > 60)&&(nilai <=80))
nilai_huruf='B';
else nilai_huruf ='A';
}
private:
int nilai;
char nilai_huruf;
};
istream& operator >> (istream&in, Nilai& masukkan) {
cout << " Masukkan nilai angka =";
cin >> masukkan.nilai;
return in;
};
ostream& operator<<(ostream& out,Nilai& keluaran) {
out << "nilai angka : " << keluaran.nilai << endl;
out << "nilai huruf : " << keluaran.nilai_huruf;
return out;
}
int main(int argc, char *argv[])
{
{
Nilai angka;
cin>> angka;
angka.konversikan();
cout<<angka;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Program Konversi Hari
Algoritma
Deklarasi
Hari_ke : integer
Hari : string
Deskripsi
read(hari_ke);
case(hari_ke)of
1. : hari = "senin"
2. : hari = "selasa"
3. : hari = "rabu"
4. : hari = "kamis"
5. : hari = "jum'at"
6. : hari = "sabtu"
source code
#include <cstdlib>
#include <iostream>
#include<conio.h>
using namespace std;
class Kalender {
friend ostream& operator << (ostream&, Kalender&);
friend istream& operator >> (istream&, Kalender&);
public:
Kalender() {};
void adalah ();
private:
int hari_ke;
char*hari;
};
void Kalender::adalah () {
switch(hari_ke) {
case 1: hari="senin";break;
case 2: hari="selasa";break;
case 3: hari="rabu";break;
case 4: hari="kamis";break;
case 5: hari="jum'at";break;
case 6: hari="sabtu";break;
default:hari="minggu";break;
}
}
istream& operator >> (istream&in, Kalender& masukkan) {
cout << "masukkan hari ke=";
in >> masukkan.hari_ke;
return in;
}
ostream& operator <<(ostream&out, Kalender&keluaran) {
out << "Hari ke-"<< keluaran.hari_ke << "adalah"<<keluaran.hari<<endl;
return out;
}
int main(int argc, char *argv[])
{
{
Kalender tanggal ;
cin >> tanggal;
tanggal.adalah();
cout<<tanggal;
system("PAUSE");
return EXIT_SUCCESS;
}
}
MMMmmmmm...
BalasHapustugas melulu.
Cukup membantu buat tugas pemrograman, meskipun aku gak ngerti._.
BalasHapusDi dalam Flow chart, angka - 4 itu daridmana?
BalasHapus