Menghitung 2 bilangan integer
Algoritma hasil Kali
Deklarasi
a,b : integer (input)
hasil : integer (output)
Deskripsi
Read (a,b)
Hasil = a*b
Write (hasil)
Flowchart
Source code
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class Perkalian {
friend ostream& operator<<(ostream&,const Perkalian&);
friend istream& operator>>(istream&,Perkalian&);
public:
Perkalian();
void hitung_perkalian(){hasil = (a*b);}
private:
int a,b;
int hasil;
};
Perkalian::Perkalian(){
cout << "Program mengkaliakn 2 bilangan integer\n";
cout << "semoga sukses\n";
}
istream& operator>>(istream& in, Perkalian&masukan){
cout << "Masukkan nilai a =";
in >> masukan.a;
cout << "Masukkan nilai b =";
in >> masukan.b;
return in;
}
ostream& operator<<(ostream& out,const Perkalian&keluaran){
out << "Nilai a:"<<keluaran.a<<endl;
out << "Nilai b:"<<keluaran.b<<endl;
out << "Hasil kali integer di atas: "<<keluaran.hasil << endl;
return out;
}
int main(int argc, char *argv[])
{
Perkalian X;
cin>>X;
X.hitung_perkalian();
cout << X;
system("PAUSE");
return EXIT_SUCCESS;
}
Program Konversi jarak
Algoritma
Deklarasi
M : real (input)
CM : real (output)
Inci : real (output)
Deskripsi
Read (m)
Cm = m*100
Inci = m * 100/2.54
Write (cm)
Write (inci)
Flowchart
Source code
#include <iostream>
#include <fstream>
using namespace std;
class Konversi {
friend ostream& operator<<(ostream&,const Konversi&);
friend istream& operator>>(istream&,Konversi&);
public:
Konversi();
void konversi_ke_cm(){cm = (m*100);}
void konversi_ke_inci(){inci = cm/2.54;}
private:
float m;
float cm,inci;
};
Konversi::Konversi(){
cout << "Program konversi m ke cm dan inci\n";
cout << "semoga sukses\n";
}
istream& operator>>(istream& in, Konversi& masukan){
cout << "Masukkan nilai m ";
in >> masukan.m;
masukan.konversi_ke_cm();
masukan.konversi_ke_inci();
return in;
}
ostream& operator<<(ostream& out,const Konversi&keluaran){
out << "Nilai m:"<<keluaran.m<<endl;
out << keluaran.m<<"m ="<<keluaran.cm<<"cm"<<endl;
out << keluaran.m<<"m ="<<keluaran.inci<<"inci"<<endl;
return out;
}
int main(int argc, char *argv[])
{
Konversi X;
cin>>X;
cout << X;
system("PAUSE");
return EXIT_SUCCESS;
}
Konversi Suhu
Algoritma
Deklarasi
C : integer {derajat celcius}
R : real {derajat reamur}
F : real {derajat fahrenheit}
K : real {derajat kelvin}
Deskripsi
Read (C)
R = 4/5*C {rumus R = 4/5*C}
F = 9/8 *C+32 {rumus F = 9/5*C + 32}
K = C + 273 {rumus K = C + 273}
Write (R,F,K)
Flowchart
Source code
#include <iostream>
#include <fstream>
using namespace std;
class Suhu {
friend ostream& operator<<(ostream&,Suhu&);
friend istream& operator>>(istream&,Suhu&);
public:
void konversi_keR(){r = 4/5.0*c;}
void konversi_keF(){f = 9/5.0*c+32;}
void konversi_keK(){k = c+273;}
private:
int c;
float r,f,k;
};
istream& operator>>(istream& in, Suhu& A){
cout << "Masukkan suhu derajat celsius :";
in >> A.c;
return in;
}
ostream& operator<<(ostream& out,Suhu&A){
A.konversi_keR();
A.konversi_keF();
A.konversi_keK();
out << A.c<<"celcius = "<< A.r << "reamur"<<endl;
out << A.c<<"celcius = "<< A.f << "fahrenheit"<<endl;
out << A.c<<"celcius = "<< A.k << "kelvin"<<endl;
return out;
}
int main(int argc, char *argv[])
{
Suhu X;
cin>>X;
cout << X;
system("PAUSE");
return EXIT_SUCCESS;
}
Tidak ada komentar:
Posting Komentar