Selasa, 19 Oktober 2010
Posttest Praktikum Struktur Data Ke-4
#include <cstdlib>
#include <iostream>
#define maks5
using namespace std;
class Array1D{
friend ostream& operator<<(ostream&, const Array1D&);
friend istream& operator>>(istream&, Array1D&);
public:
Array1D();
void cetak();
void geser_kiri();
void geser_kanan();
void hapus_elemen();
void urut();
private:
char A[5];
};
//konstrukstor
Array1D::Array1D(){
for(int i=0;i<5;i++)
A[i]='O';
}
void Array1D::cetak(){
for(int i=0;i<5;i++)
cout<<A[i]<<" ";
}
//overloading fungsi cetak
ostream& operator<<(ostream& out, const Array1D& x){
for(int i=0;i<5;i++)
out<<x.A[i]<<" ";
out<<endl;
return out;
}
//overloading input
istream& operator>>(istream& in, Array1D& x){
for(int i=0;i<5;i++){
cout<<"masukkan nilai array ke-"<<i+1<<" : ";
in>>x.A[i];
}
return in;
}
//fungsi untuk menggeser semua nilai 1 indeks ke kanan
void Array1D::geser_kanan(){
int n=5;
int temp=A[n-1];
for(int i=n-1;i>=0;i--)
A[i+1]=A[i];
A[0]=temp;
}
//fungsi untuk menggeser semua nilai 1 indeks ke kiri
void Array1D::geser_kiri(){
int n=5;
int temp=A[0];
for(int i=0;i<n;i++)
A[i]=A[i+1];
A[n-1]=temp;
}
//menthods yang di tambahkan untuk menghapus nilai dari indeks tertentu
void Array1D::hapus_elemen(){
int posisi;
cout<<"\npilih indeks berapa yang akan di hapus :\n";
cin>>posisi;
if(posisi>0 && posisi<=5)
A[posisi-1]='O';
else cout<<"indeks hanya terdiri dari 1 - 5\n";
}
//main fingsi
int main(int argc, char *argv[])
{
Array1D x;
cout<<"Array masih kosong : "<<x; //pemanggilan overloading output
cin>>x; //penggunaan overloading input
cout<<"Isi Array saat ini : "<<x;
x.geser_kiri(); //penggunaan fingsi
cout<<"Isi Array setelah di geser kiri : "<<x;
x.geser_kanan();
cout<<"Isi Array setelah di geser kanan : "<<x;
cout<<"Urutan elemen pada indeksnya saat ini : "<<x;
x.hapus_elemen();
cout<<"Setelah dihapus menjadi : "<<x;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
#define maks5
using namespace std;
class Array1D{
friend ostream& operator<<(ostream&, const Array1D&);
friend istream& operator>>(istream&, Array1D&);
public:
Array1D();
void cetak();
void geser_kiri();
void geser_kanan();
void hapus_elemen();
void urut();
private:
char A[5];
};
//konstrukstor
Array1D::Array1D(){
for(int i=0;i<5;i++)
A[i]='O';
}
void Array1D::cetak(){
for(int i=0;i<5;i++)
cout<<A[i]<<" ";
}
//overloading fungsi cetak
ostream& operator<<(ostream& out, const Array1D& x){
for(int i=0;i<5;i++)
out<<x.A[i]<<" ";
out<<endl;
return out;
}
//overloading input
istream& operator>>(istream& in, Array1D& x){
for(int i=0;i<5;i++){
cout<<"masukkan nilai array ke-"<<i+1<<" : ";
in>>x.A[i];
}
return in;
}
//fungsi untuk menggeser semua nilai 1 indeks ke kanan
void Array1D::geser_kanan(){
int n=5;
int temp=A[n-1];
for(int i=n-1;i>=0;i--)
A[i+1]=A[i];
A[0]=temp;
}
//fungsi untuk menggeser semua nilai 1 indeks ke kiri
void Array1D::geser_kiri(){
int n=5;
int temp=A[0];
for(int i=0;i<n;i++)
A[i]=A[i+1];
A[n-1]=temp;
}
//menthods yang di tambahkan untuk menghapus nilai dari indeks tertentu
void Array1D::hapus_elemen(){
int posisi;
cout<<"\npilih indeks berapa yang akan di hapus :\n";
cin>>posisi;
if(posisi>0 && posisi<=5)
A[posisi-1]='O';
else cout<<"indeks hanya terdiri dari 1 - 5\n";
}
//main fingsi
int main(int argc, char *argv[])
{
Array1D x;
cout<<"Array masih kosong : "<<x; //pemanggilan overloading output
cin>>x; //penggunaan overloading input
cout<<"Isi Array saat ini : "<<x;
x.geser_kiri(); //penggunaan fingsi
cout<<"Isi Array setelah di geser kiri : "<<x;
x.geser_kanan();
cout<<"Isi Array setelah di geser kanan : "<<x;
cout<<"Urutan elemen pada indeksnya saat ini : "<<x;
x.hapus_elemen();
cout<<"Setelah dihapus menjadi : "<<x;
system("PAUSE");
return EXIT_SUCCESS;
}
Senin, 18 Oktober 2010
Post Test Praktikum Struktur Data Ke-3
#include <cstdlib>
#include <iostream>
using namespace std;
class Bilangan{
friend ostream& operator << (ostream&, const Bilangan&);
friend istream& operator >> (istream&, Bilangan&);
public:
Bilangan(int a0=0, float b0=0.0) : a(a0), b(b0){}
void banding_int(const Bilangan&, const Bilangan&);
Bilangan& operator=(const Bilangan&);
Bilangan operator+(const Bilangan&) const;
Bilangan operator-()const;
// protected:
int a;
float b;
};
ostream& operator << (ostream&out, const Bilangan& x){
out << “Bagian integer : ” << x.a << endl;
out << “Bagian float : ” << x.b << endl;
return out;
}
void Bilangan::banding_int(const Bilangan& x, const Bilangan& y){
if (x.a > y.a) cout << x.a << “::x lebih besar dari ” << y.a << “::y”;
else cout << x.a << “::x lebih kecil dari ” << y.a << “::y”;
}
Bilangan& Bilangan::operator=(const Bilangan&x){
a = x.a;
b = x.b;
return *this;
}
istream& operator >> (istream& in, Bilangan& x){
cout << “\nMasukkan bagian integer : “;
in >> x.a;
cout << “Masukkan bagian float : “;
in >> x.b;
return in;
}
Bilangan Bilangan::operator+(const Bilangan& x) const {
Bilangan cc;
cc.a = a+x.a;
cc.b = b+x.b;
return cc;
}
Bilangan Bilangan::operator-()const{
Bilangan x;
x.a = -a;
x.b = -b;
return x;
}
“Bil_char.h”
#include <cstdlib>
#include <iostream>
#include “bilangan.h”
using namespace std;
class Bil_char : public Bilangan{
friend ostream& operator << (ostream&, const Bil_char&);
public:
Bil_char(int a0=0, int b0=0, char ch=’x') : Bilangan(a0,b0), c(ch){}
private:
char c;
};
ostream& operator << (ostream& out, const Bil_char& x){
out << “Bagian integer : ” << x.a << endl;
out << “Bagian float : ” << x.b << endl;
out << “Bagian char : ” << x.c << endl;
return out;
}
“Turunan.cpp”
#include <cstdlib>
#include <iostream>
#include “bil_char.h”
using namespace std;
int main(int argc, char *argv[])
{
Bilangan s, t(-2,3.14), d;
cout << “Nilai awal s\n” << s;
cout << “Nilai awal t dari deklarasi\n” << t;
s = t;
cout << “Setelah s di-assign t\n”;
cout << “Nilai s\n” << s;
cout << “Masukkan nilai – nilai objek d”;
cin >> d;
cout << “Setelah d + t => \n” << d+t;
cout << “Nilai d dinegatifkan\n” << -d;
Bil_char ss;
cout << “Nilai awal ss\n” << ss;
system(“PAUSE”);
return EXIT_SUCCESS;
}
#include <iostream>
using namespace std;
class Bilangan{
friend ostream& operator << (ostream&, const Bilangan&);
friend istream& operator >> (istream&, Bilangan&);
public:
Bilangan(int a0=0, float b0=0.0) : a(a0), b(b0){}
void banding_int(const Bilangan&, const Bilangan&);
Bilangan& operator=(const Bilangan&);
Bilangan operator+(const Bilangan&) const;
Bilangan operator-()const;
// protected:
int a;
float b;
};
ostream& operator << (ostream&out, const Bilangan& x){
out << “Bagian integer : ” << x.a << endl;
out << “Bagian float : ” << x.b << endl;
return out;
}
void Bilangan::banding_int(const Bilangan& x, const Bilangan& y){
if (x.a > y.a) cout << x.a << “::x lebih besar dari ” << y.a << “::y”;
else cout << x.a << “::x lebih kecil dari ” << y.a << “::y”;
}
Bilangan& Bilangan::operator=(const Bilangan&x){
a = x.a;
b = x.b;
return *this;
}
istream& operator >> (istream& in, Bilangan& x){
cout << “\nMasukkan bagian integer : “;
in >> x.a;
cout << “Masukkan bagian float : “;
in >> x.b;
return in;
}
Bilangan Bilangan::operator+(const Bilangan& x) const {
Bilangan cc;
cc.a = a+x.a;
cc.b = b+x.b;
return cc;
}
Bilangan Bilangan::operator-()const{
Bilangan x;
x.a = -a;
x.b = -b;
return x;
}
“Bil_char.h”
#include <cstdlib>
#include <iostream>
#include “bilangan.h”
using namespace std;
class Bil_char : public Bilangan{
friend ostream& operator << (ostream&, const Bil_char&);
public:
Bil_char(int a0=0, int b0=0, char ch=’x') : Bilangan(a0,b0), c(ch){}
private:
char c;
};
ostream& operator << (ostream& out, const Bil_char& x){
out << “Bagian integer : ” << x.a << endl;
out << “Bagian float : ” << x.b << endl;
out << “Bagian char : ” << x.c << endl;
return out;
}
“Turunan.cpp”
#include <cstdlib>
#include <iostream>
#include “bil_char.h”
using namespace std;
int main(int argc, char *argv[])
{
Bilangan s, t(-2,3.14), d;
cout << “Nilai awal s\n” << s;
cout << “Nilai awal t dari deklarasi\n” << t;
s = t;
cout << “Setelah s di-assign t\n”;
cout << “Nilai s\n” << s;
cout << “Masukkan nilai – nilai objek d”;
cin >> d;
cout << “Setelah d + t => \n” << d+t;
cout << “Nilai d dinegatifkan\n” << -d;
Bil_char ss;
cout << “Nilai awal ss\n” << ss;
system(“PAUSE”);
return EXIT_SUCCESS;
}
Post Test Praktikum Struktur Data Ke-2
#include <iostream.h>
template <class T>
class kompleks {
friend class operasi;
friend ostream& operator<<(ostream&, const kompleks<T>&) ;
friend istream& operator>>(istream&, kompleks<T>&) ;
public :
kompleks(T s=0, T +=0): a(s), b(+){};
void cetak();
kompleks operator-();
kompleks operator-(const kompleks<T>&);
kompleks operator+(const kompleks<T>&);
private :
T a;
T b;
};
template <class T>
void kompleks<T>::cetak()
{
if(b>0)cout<<"bilangan kompleks : " << a<<"+"<<b<<"i";
else cout<<"bilangan kompleks : " <<a<<b<<"i";
cout<< endl;
}
template <class T>
kompleks<T>kompleks<T>::operator-()
{
kompleks x;
x.a=a;
x.b=-b;
return x;
}
template <class T>
kompleks<T>kompleks<T>::operator-(const kompleks<T>& m)
{
kompleks x;
x.a=a - m.a;
x.b=b - m.b;
return x;
}
template <class T>
kompleks<T> kompleks<T>::operator+(const kompleks<T>& m)
{
kompleks x;
x.a = a + m.a;
x.b = b + m.b;
return x;
}
template <class T>
ostream& operator<<(ostream& out, const kompleks<T>& x);
{
if(x.b == 0)out <<'['<<x.a<<']';
else if (x.a == 0 && x.b =1)out<< '['<<"i"<<']';
else if (x.a == 0 && x.b =-1)out<< '['<<"-i"<<']';
else if (x.a == 0 && x.b >1)out<< '['<<x.b<<"i"<<']';
else if (x.a == 0 && x.b <-1)out<< '['<<x.b<<"i"<<']';
else if (x.b==1)out<<'['<<x.a<<"+"<<"i"<<']';
else if (x.b>0) out<<'['<<x.a<<"+"<<x.b<<"i"<<']';
else if (x.b==-1)out<<'['<<x.a<<"-i"<<']';
else out <<'['<<x.a<<x.b<<"i"<<']';
return out;
}
template <class T>
istream& operator>>(istream& in,kompleks<T>& x)
{
cout<<"masukkan bagian real : ";
in >>x.a;
cout<<"masukkan bagian imaginer : ";
in>>x.b;
return in;
}
template <class T>
class operasi {
public :
kompleks<T>jumlah(const kompleks<T>&,const kompleks<T>&);
kompleks<T>kali(const kompleks<T>&,const kompleks<T>&);
kompleks<T>kurang(const kompleks<T>&,const kompleks<T>&);
};
template <class T>
kompleks<T> operasi<T> :: jumlah(const kompleks<T>& m, const kompleks<T>& n)
{
kompleks<T>temp;
temp.a=m.a + n.a;
temp.b=m.b + n.b;
return temp;
}
template <class T >
kompleks<T>operasi<T>::kurang(const kompleks<T>& m, const kompleks<T>& n)
{
kompleks<T>temp;
temp.a=m.a - n.a;
temp.b=m.b- n.b;
return temp;
}
template <class T >
kompleks<T>operasi<T>::kurang(const kompleks<T>& m, const kompleks<T>& n)
{
kompleks<T>temp;
temp.a=(m.a*n.a) -(m.b*n.b);
temp.b=(m.a*n.b) -(m.b*n.a);
return temp;
}
int main()
{
kompleks<int>x(2.3),y(4,-4),+;
operasi<int> z;
cout<<"menggunakan cetak():";x.cetak();
cout<<"menggunakan overloading : "<< x;
cout<<"konjugat : " << -x;
y.cetak();
cout<<"\npenjumlahan mengunakan methid ";
t=z.jumlah(x.y);
t.cetak();
cout<<"penjumlahan menggunakan operator ";
t=x+y;
cout<<x<<"+"<<y<<"="<<t;
cout<<"\perkalian menggunakan method : ";
t=z.kali(x.y);
t.cetak();
cout<<"perkalian menggunakan operator : ";
t=x*y;
cout<<x<<"*"<<y<<"="<<t<<endl;
kompleks<int>n;
cin>>n;
cout<<n;
return 0;
}
template <class T>
class kompleks {
friend class operasi;
friend ostream& operator<<(ostream&, const kompleks<T>&) ;
friend istream& operator>>(istream&, kompleks<T>&) ;
public :
kompleks(T s=0, T +=0): a(s), b(+){};
void cetak();
kompleks operator-();
kompleks operator-(const kompleks<T>&);
kompleks operator+(const kompleks<T>&);
private :
T a;
T b;
};
template <class T>
void kompleks<T>::cetak()
{
if(b>0)cout<<"bilangan kompleks : " << a<<"+"<<b<<"i";
else cout<<"bilangan kompleks : " <<a<<b<<"i";
cout<< endl;
}
template <class T>
kompleks<T>kompleks<T>::operator-()
{
kompleks x;
x.a=a;
x.b=-b;
return x;
}
template <class T>
kompleks<T>kompleks<T>::operator-(const kompleks<T>& m)
{
kompleks x;
x.a=a - m.a;
x.b=b - m.b;
return x;
}
template <class T>
kompleks<T> kompleks<T>::operator+(const kompleks<T>& m)
{
kompleks x;
x.a = a + m.a;
x.b = b + m.b;
return x;
}
template <class T>
ostream& operator<<(ostream& out, const kompleks<T>& x);
{
if(x.b == 0)out <<'['<<x.a<<']';
else if (x.a == 0 && x.b =1)out<< '['<<"i"<<']';
else if (x.a == 0 && x.b =-1)out<< '['<<"-i"<<']';
else if (x.a == 0 && x.b >1)out<< '['<<x.b<<"i"<<']';
else if (x.a == 0 && x.b <-1)out<< '['<<x.b<<"i"<<']';
else if (x.b==1)out<<'['<<x.a<<"+"<<"i"<<']';
else if (x.b>0) out<<'['<<x.a<<"+"<<x.b<<"i"<<']';
else if (x.b==-1)out<<'['<<x.a<<"-i"<<']';
else out <<'['<<x.a<<x.b<<"i"<<']';
return out;
}
template <class T>
istream& operator>>(istream& in,kompleks<T>& x)
{
cout<<"masukkan bagian real : ";
in >>x.a;
cout<<"masukkan bagian imaginer : ";
in>>x.b;
return in;
}
template <class T>
class operasi {
public :
kompleks<T>jumlah(const kompleks<T>&,const kompleks<T>&);
kompleks<T>kali(const kompleks<T>&,const kompleks<T>&);
kompleks<T>kurang(const kompleks<T>&,const kompleks<T>&);
};
template <class T>
kompleks<T> operasi<T> :: jumlah(const kompleks<T>& m, const kompleks<T>& n)
{
kompleks<T>temp;
temp.a=m.a + n.a;
temp.b=m.b + n.b;
return temp;
}
template <class T >
kompleks<T>operasi<T>::kurang(const kompleks<T>& m, const kompleks<T>& n)
{
kompleks<T>temp;
temp.a=m.a - n.a;
temp.b=m.b- n.b;
return temp;
}
template <class T >
kompleks<T>operasi<T>::kurang(const kompleks<T>& m, const kompleks<T>& n)
{
kompleks<T>temp;
temp.a=(m.a*n.a) -(m.b*n.b);
temp.b=(m.a*n.b) -(m.b*n.a);
return temp;
}
int main()
{
kompleks<int>x(2.3),y(4,-4),+;
operasi<int> z;
cout<<"menggunakan cetak():";x.cetak();
cout<<"menggunakan overloading : "<< x;
cout<<"konjugat : " << -x;
y.cetak();
cout<<"\npenjumlahan mengunakan methid ";
t=z.jumlah(x.y);
t.cetak();
cout<<"penjumlahan menggunakan operator ";
t=x+y;
cout<<x<<"+"<<y<<"="<<t;
cout<<"\perkalian menggunakan method : ";
t=z.kali(x.y);
t.cetak();
cout<<"perkalian menggunakan operator : ";
t=x*y;
cout<<x<<"*"<<y<<"="<<t<<endl;
kompleks<int>n;
cin>>n;
cout<<n;
return 0;
}
Post Test Praktikum Struktur Data ke-1
using namespace std;
class Bilangan {
friend ostream& operator<<(ostream&,const Bilangan&);
friend istream& operator>>(istream&,Bilangan&);
public:
Bilangan(int a0=0,float b0=0.0):a (a0),b(b0) {}
void banding_int(const Bilangan&,const Bilangan&);
Bilangan& operator=(const Bilangan&);
Bilangan operator+(const Bilangan&)const;
Bilangan operator-()const;
private:
int a;
float b;
};
ostream& operator<<(ostream& out, const Bilangan& x)
{ out<<”Bagian integer:”<<x.a<<endl;
out<<”Bilangan float:”<<x.b<<endl;
return out;
}
void Bilangan::banding_int(const Bilangan& x,const Bilangan& y)
{
if(x.a>y.a)cout<<x.a<<”::x lebih besar dari”<<y.a<<”::y”;
else cout<<x.a<<”::x lebih kecil dari”<<y.a<<”::y”;
}
Bilangan& Bilangan::operator=(const Bilangan& x)
{ a=x.a;
b=x.b;
return*this;
}
istream& operator>>(istream& in,Bilangan& x)
{
cout<<”\nMasukkan bagian integer :”;
in>>x.a;
cout<<”Masukkan bagian float :”;
in>>x.b;
return in;
}
Bilangan Bilangan::operator+(const Bilangan& x)const
{ Bilangan cc;
cc.a=a+x.a;
cc.b=b+x.b;
return cc;
}
Bilangan Bilangan::operator-()const
{ Bilangan x;
x.a=-a;
x.b=-b;
return x;
}
void main() {
Bilangan s,t(-2,3.14),d;
cout<<”Nilai awal s\n”<<s;
cout<<”Nilai awal t dari deklarasi\n”<<t;
s=t;
cout<<”Setelah s di-assign t\n”;
cout<<”Nilai s\n”<<s;
cout<<”Masukkan nilai-nilai objek d”;
cin>> d;
cout<<”Setelah d+t=> \n”<<d+t;
cout<<”Nilai d dinegatifkan\n”<<-d;
}
Tidak ada komentar:
Posting Komentar