Minggu, 31 Oktober 2010

antrian

#include
#include
int main(int argc, char *argv[])
{
int queue[5];
int depan = -1;
int belakang = -1;
int pilihan, data, i;
do{
cout << "menu " << endl;
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. view\n";
cout <> pilihan;
switch (pilihan)
{
case 1: //enqueue
//apakah queue belum penuh?
if (belakang < 4 )
{
cout <> data;
queue[belakang+1] = data;
belakang++;
if (belakang == 0)
depan = 0;
}
else
cout << "Queue Penuh" << endl;
break;
case 2: //dequeue
//apakah queue belum kosong?
if (depan <= belakang)
{ cout << "Data keluar = " << depan;
depan++;
}
else
cout << "queue kosong";
break;
case 3:
for(i=depan; i<=belakang; i++)
cout << queue[i];
cout << endl;
break;
}
}while (pilihan != 4);
system("PAUSE");
return EXIT_SUCCESS;
}

Rabu, 13 Oktober 2010

Praktikum III S.data ,Selasa 12 Oktober 2010 10.30-12.00

#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;
}
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;
}
int main(int argc, char *argv[])
{
cout<<”ARI LISTIYANTI\n09018256\n\n”;
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 diassign 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;
}

Rabu, 06 Oktober 2010

Praktikum II S.data,Selasa 10.30-12.00

Radip Yoga S
09018243

#include <cstdlib>
#include <iostream>
class Operasi;
using namespace std;
//template <class T>
class Kompleks
{
      friend class Operasi;
      friend ostream& operator<<(ostream&, const Kompleks&);
      friend istream& operator>>(istream&, Kompleks&);
      public:
             Kompleks(int s=0, int t=0):a(s),b(t){}
             void cetak();
             Kompleks operator-();
             Kompleks operator-(const Kompleks&);
             Kompleks operator+(const Kompleks&);
      private:
              int a;
              int b;
};
//template <class T>
    void Kompleks::cetak(){
    if(b>0) cout << "Bilangan Kompleks : " << a << "+" << b << "i";
    else cout << "Bilangan kompleks : " << a << b << "i";
    cout << endl;
    }
    //template <class T>
    Kompleks Kompleks::operator-(){
    Kompleks x;
    x.a=a;
    x.b=-b;
    return x;
    }
    //template <class T>
    Kompleks Kompleks::operator-(const Kompleks& m){
    Kompleks x;
    x.a=a-m.a;
    x.b=b-m.b;
    return x;
    }
    //template <class T>
    Kompleks Kompleks::operator+(const Kompleks& m){
    Kompleks x;
    x.a=a+m.a;
    x.b=b+m.b;
    }
    //template <class T>
    ostream& operator<<(ostream& out, const Kompleks& 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>1) out << '[' << x.a << "+" << x.b << "i" << ']';
    else if (x.b==-1) out << '[' << x.a << "-i" << ']';
    else out << '[' << x.a << x.b << ']';
    return out;
    }
    //template <class T>
    istream& operator>>(istream& in, Kompleks& x){
    cout << "Masukan bagian real     : ";
    in >> x.a;
    cout << "Masukan bagian imajiner : ";
    in >> x.b;
    return in;
    }
    //template <class T>
class Operasi
{
    public:
    void cetak();
    Kompleks jumlah(const Kompleks&, const Kompleks&);
    Kompleks kali(const Kompleks&, const Kompleks&);
    Kompleks kurang(const Kompleks&, const Kompleks&);
    private:
              int a;
              int b;
    };
void Operasi::cetak(){
     if(b>0) cout << "Bilangan Kompleks : " << a << "+" << b << "i";
    else cout << "Bilangan kompleks : " << a << b << "i";
    cout << endl;
}
    //template <class T>
    Kompleks Operasi::jumlah(const Kompleks& m, const Kompleks& n){
    Kompleks temp;
    temp.a=m.a+n.a;
    temp.b=m.b+n.b;
    return temp;
    }
    //template <class T>
    Kompleks Operasi::kurang(const Kompleks& m, const Kompleks& n){
    Kompleks temp;
    temp.a=m.a-n.a;
    temp.b=m.b-n.b;
    return temp;
    }
    //template <class T>
    Kompleks Operasi::kali(const Kompleks& m, const Kompleks& n){
    Kompleks 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(int argc, char *argv[])
{
    Kompleks x(2, 3), y(4, -4), t;
    Operasi z;
    cout << "Menggunakan cetak() : "; x.cetak();
    cout << "Menggunakan Overloading : " << x;
    cout << "Konjugat : " << -x;
    y.cetak();
    cout << "\nPenjumlahan menggunakan method : ";
    t=z.jumlah(x, y);
    t.cetak();
    cout << "Penjumlahan menggunakan operator : ";
    t=x+y;
    cout << x << "+" << y << "=" << t;
    cout << "\nPerkalian menggunakan method : ";
    t=z.kali(x, y);
    z.cetak();
    cout << "\nPerkalian menggunakan operator : ";
    //x*y;
    //ut << x << "*" << y << "=" << t;
    t-y;
    cout << "\n" << x << "-" << y << "=" << t << endl;
    Kompleks n;
    cin >> n;
    cout << n;
    system("PAUSE");
    return EXIT_SUCCESS;
}