No.1 Buatlah program yang
meminta user memasukkan jam, menit dan detik kemudian
menampilkannya dengan format
jam:menit:detik. Deklarasikan jam dan menit berupa
integer, sedangkan menit
berupa floating point. Buat masukan dan keluaran
menggunakan operator overloading <<
dan >> sesuai yang dikehendaki.
#include
<iostream>
#include
<cstdlib>
/* run this program
using the console pauser or add your own getch, system("pause") or
input loop */
using namespace
std;
class Konversi{
friend istream&
operator>>(istream&, Konversi&);
friend ostream&
operator<<(ostream&, Konversi&);
public:
Konversi(){};
int j();
int m();
int d();
private:
int jam;
int menit;
int detik;
int sekon;
};
int Konversi :: j(){
jam=sekon/3600;
return jam;
}
int Konversi :: m(){
menit=(sekon-(3600*(sekon/3600)))/60;
return menit;
}
int Konversi :: d(){
detik=((sekon-(3600*(sekon/3600)))-(((sekon-(3600*(sekon/3600)))/60)*(60)));
return detik;
}
istream&
operator>>(istream& in,Konversi& masukan){
cout<<"\t\tPROGRAM KONVERSI
DENGAN FORMAT : JAM.MENIT.DETIK"<<endl;
cout<<endl;
cout<<"Masukkan Waktu Dalam
Detik : ";
in>>masukan.sekon;
cout<<endl;
return in;
}
ostream&
operator<<(ostream& out,Konversi& keluaran){
out<<keluaran.j()<<"
Jam - ";
out<<keluaran.m()<<"
Menit - ";
out<<keluaran.d()<<"
Detik";
return out;
}
int main(int argc,
char** argv) {
Konversi X;
cin>>X;
cout<<X;
cout<<endl;
cout<<endl;
system("PAUSE");
system("cls");
return 0;
}
Saat compile
No.2 soal selanjutnya ulangi
langkah-langkah sebagaimana soal nomor 1.
Buatlah program yang meminta user memasukkan
bagian penyebut dan pembilang dari
sebuah bilangan rasional
berbentuk p/q. Setiap memasukkan bilangan rasional outputnya
berbentuk p/q. Misalnya, masukan 1 dan 2
maka tampilan outputnya ½.
#include
<iostream>
#include
<conio.h>
using namespace
std;
class bil{
friend ostream&
operator<<(ostream&, bil&);
friend istream&
operator>>(istream&, bil&);
public:
bil();
private:
int
a,b;
};
bil::bil(){
cout<<"----------------------------------------"<<endl;
cout<<"Program Bilangan
Rasional "<<endl;
cout<<"-----------------------------------------"<<endl;
}
istream& operator>>(istream&
in, bil& mlebu){
cout<<"masukan Bilangan Ke 1
= ";
in>>mlebu.a;
cout<<"masukan Bilangan ke 2
= ";
in>>mlebu.b;
return in;
}
ostream& operator<<(ostream&
out, bil& metu){
cout<<"Keluaran = ";
out<<metu.a<<" /
"<<metu.b;;
return out;
}
/* run this program
using the console pauser or add your own getch, system("pause") or
input loop */
main(int argc, char** argv){
bil x;
cin>>x;
cout<<x;
getch();
return 0;
}
Saat compile
No.3 Buatlah program yang
meminta user memasukkan bagian ribuan, ratusan, puluhan dan
satuan. Misalnya : 1000, 200, 30, 4.
Tampilan yang dikehendaki adalah 1234.
#include
<iostream>
#include
<cstdlib>
/* run this program
using the console pauser or add your own getch, system("pause") or
input loop */
using namespace
std;
void first (int n) {
switch (n) {
case 1: cout<<"Satu "; break;
case 2: cout<<"Dua "; break;
case 3: cout<<"Tiga "; break;
case 4: cout<<"Empat "; break;
case 5: cout<<"Lima "; break;
case 6: cout<<"Enam "; break;
case 7: cout<<"Tujuh "; break;
case 8: cout<<"Delapan ";
break;
case 9: cout<<"Sembilan ";
break;
case 10: cout<<"Sepuluh ";
break;
case 11: cout<<"Sebelas ";
break;
default: break;
}
}
void second (int n) {
int bul, sisa;
bul = n / 10;
sisa = n % 10;
if (bul == 0)
first (sisa);
else if (bul == 1) {
if (sisa <= 1)
first (n);
else {
first (sisa);
cout<<"Belas ";
}
}
else {
first (bul);
cout<<"Puluh ";
first (sisa);
}
}
void third (int n) {
int bul, sisa;
bul = n / 100;
sisa = n % 100;
if (bul == 0)
second (sisa);
else if (bul == 1) {
if (sisa == 0)
cout<<"Seratus ";
else {
cout<<"Seratus ";
second (sisa);
}
}
else {
first (bul);
cout<<"Ratus ";
second (sisa);
}
}
void fourth (int n) {
int bul, sisa;
bul = n / 1000;
sisa = n % 1000;
if (bul == 0)
third (sisa);
else if (bul == 1) {
if (sisa == 0)
cout<<"Seribu ";
else {
cout<<"Seribu ";
third (sisa);
}
}
else {
third (bul);
cout<<"Ribu ";
third (sisa);
}
}
void fifth (int n) {
int bul, sisa;
bul = n / 1000000;
sisa = n % 1000000;
if (bul == 0)
fourth (sisa);
else if (bul == 1) {
if (sisa == 0)
cout<<"Satu Juta ";
else {
cout<<"Satu juta ";
fourth (sisa);
}
}
else {
third (bul);
cout<<"Juta ";
fourth (sisa);
}
}
int main(int argc,
char** argv) {
int n = 1;
while (n == 1) {
int num1;
do {
cout<< "";
cin>>num1;
} while (num1<1);
fifth (num1);
cout<<"\n";
}
return 0;
}
Saat compile
No.4 Buatlah program untuk merepresentasikan
operasi-operasi aritmatika : penjumlahan,
pengurangan, perkalian dan
pembagian. User diminta memasukkan 2 buah bilangan bulat,
kemudian menampilkan sajian
lengkapnya dari semua operasi. Sebagai contoh, user
memasukkan 1 dan 2 maka
tampilan outputnya :
1 + 2 = 2
1 – 2 = -1
1 * 2 = 2
1 : 2 = ½
Class hitung {
}
Berikut program nya
#include <iostream>
using namespace std;
class hitung{
public:
void
masukan();
void proses();
void
keluaran();
private:
float
x,y,hasil1,hasil2,hasil3,hasil4;
};
void hitung::masukan(){
cout<<"\t\t --->>>MEREPRESENTASIKAN
OPERASI-OPERASI<<<--- \n";
cout<<"\n\t\t\t\t -->>ARITMATIKA<<--\n";
cout<<"\nMasukkan Nilai Ke- 1 : ";cin>>x;
cout<<"Masukkan Nilai ke- 2 : ";cin>>y;
cout<<endl;
}
void hitung::proses(){
hasil1=x+y;
hasil2=x-y;
hasil3=x*y;
hasil4=x/y;
}
void hitung::keluaran(){
cout<<"Hasil : "<<x<<" +
"<<y<<"= "<<hasil1<<endl;
cout<<"Hasil : "<<x<<" -
"<<y<<"= "<<hasil2<<endl;
cout<<"Hasil : "<<x<<" *
"<<y<<"= "<<hasil3<<endl;
cout<<"Hasil : "<<x<<" :
"<<y<<"= "<<hasil4<<endl;
}
/* run this program using the console pauser or add
your own getch, system("pause") or input loop */
int main(){
hitung
bab2;
char opsi;
do{
bab2.masukan();
bab2.proses();
bab2.keluaran();
cout<<"\n\t-->>ULANGI LAGI
y/n<<--\n";cin>>opsi;
}
while(opsi=='y');
cout<<"\n\t--->>>TERIMAKASIH<<<---";
return 0;
}
Saat compile
No.5 Buatlah program untuk
merepresentasikan bilangan bulat menjadi bilangan scientific
number berbentuk 1.23E1 untuk
menyatakan bilangan 12.3. Masukan bilangan bulat 4
digit, keluaran berbentuk aEb,
dengan a adalah bilangan antara 0 dan 10, sedangkan b
dari 0 sampai 4.
#include
<iostream>
#include
<cstdlib>
/* run this program
using the console pauser or add your own getch, system("pause") or
input loop */
using namespace
std;
int main(int argc,
char** argv) {
double a,b,c;
a = 12.3141592654;
b = 2004.0;
c = 1.23E1;
cout.precision(4);
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
cout<<fixed<<a<<"\t"<<b<<"\t"<<c<<endl;
cout<<scientific<<a<<"\t"<<b<<"\t"<<c<<endl;
return 0;
}
Saat compile





Tidak ada komentar:
Posting Komentar