Senin, 11 April 2011

Pemrograman Berorientasi Objek Menggunakan C++

Program yang saya buat ini tidak hanya menggunakan berkas kelas, di dalamnya juga berisikan beberapa struktur kontrol perulangan seperti FOR, WHILE, dan DO_WHILE, dan juga struktur kontrol kondisi /pilihan seperti SWITCH CASE. Di dalamnya juga ada aplikasi dari ARRAY. Langsung saja tanpa basa-basi. nih source code_nya:

//Author : Efrat 
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class menu
{
      private:
      public:
            
void pilihan()
{
     clrscr();
     cout<<"                +-------------------+\n";
     cout<<"                |     Menu utama    |\n";
     cout<<"                +-------------------+\n";
     cout<<endl;
     cout<<"1.Ke Menu Perulanagan "<<endl;
     cout<<"2.Program Aplikasi Array"<<endl;
     cout<<"3.Untuk Keluar Dari Program"<<endl;
     cout<<"***************************************\n";
     cout<<"Pilihan anda : ";
     }
};

class tekan
{
      private:
      public:
            
void kembali()
{
     cout<<"\n";
     cout<<"Tekan 1 Untuk Ke Menu Utama"<<endl;
     cout<<"Tekan 2 Untuk Kembali"<<endl;
     cout<<"Tekan 3 Untuk Keluar Dari Program"<<endl;
     }
};

class perulangan
{
      private:
              int i,j,z;
      public:
             void For();
             void While();
             void DoWhile();
            
};

void perulangan::For()
{
     clrscr();
     cout<<"                   +-------------------------------+\n";
     cout<<"                   |    Program Perulangan For     |\n";
     cout<<"                   +-------------------------------+\n";
     cout<<""<<endl;
     cout<<"masukan bilangan : ";
     cin>>z;
     cout<<"====================\n";
     for(int i=0;i<=z;i++){
     for(int j=1;j<=i;j++){
     cout<<j;
     }
     cout<<""<<endl;
     }
     for(int i=z; i>=1; i--){
     for(int j=1; j<i; j++){
     cout<<j;
     }
     cout<<endl;
}
}

void perulangan::While()
{
     clrscr();
     cout<<"                   +-------------------------------+\n";
     cout<<"                   |    Program Perulangan While   |\n";
     cout<<"                   +-------------------------------+\n";
     cout<<""<<endl;
     i=0;
     cout<<"input bilangan bulat : ";
     cin>>z;
     cout<<"===========================\n";
     while(i<=z){
     j=1;
     while(j<=i){
     cout<<j;
     j++;
     }
     cout<<endl;
     i++;
     }
     i=z;
     while(i>=1){
     j=1;
     while(j<i){
     cout<<j;
     j++;
     }
     cout<<endl;
     i--;
     }
     cout<<endl;
}

void perulangan::DoWhile()
{
     clrscr();
     cout<<"                   +-------------------------------+\n";
     cout<<"                   |  Program Perulangan Do While  |\n";
     cout<<"                   +-------------------------------+\n";
     cout<<""<<endl;
     cout<<"input bilangan bulat : ";
     cin>>z;
     cout<<"===========================\n";
     i=0;
     do{
       cout<<endl;
       i++;
       j=1;
         do{
           cout<<j;
           j++;
     }
     while(j<=i);
   }
   while(i<z);

     i=z;
     do{
       cout<<endl;
       i--;
       j=1;
     do{
       cout<<j;
       j++;
     }
   while(j<=i);
   }
     while(i>1);
     cout<<""<<endl;
}

class array
{
    private:
            int i,s;
            char nama[100][100];
            float nilai1[5];
            float nilai2[5];
            double hasil[5];
    public:
           void AppArray();
};

void array::AppArray()
{
    cout<<"Masukkan jumlah siswa: ";
    cin>>s;
    clrscr();
    for(i=1;i<=s;i++)
    {
    cout<<"Data Ke - "<<i<<endl;
    cout<<"Nama Siswa : "; cin>>nama[i];
    cout<<"Nilai Teori : "; cin>>nilai1[i];
    cout<<"Nilai Praktek : "; cin>>nilai2[i];
    hasil[i] = (nilai1[i] * 0.70)+ (nilai2[i] * 0.30);
    cout<<endl;
    }
    clrscr();
    cout<<"+--------------------------------------------------+"<<endl;
    cout<<"|              | Mid Tes FInal Ujian |             |"<<endl;
    cout<<"|--------------------------------------------------|"<<endl;
    cout<<"|No. Nama  Siswa         |Nilai | Nilai| Hasil     |"<<endl;
    cout<<"+------------------------+------+------+-----------+"<<endl;

    for(i=1;i<=s;i++)
    {
    cout<<setiosflags(ios::left)<<setw(2)<<"|"<<i<<". ";
    cout<<setiosflags(ios::left)<<setw(20)<<nama[i];
    cout<<setprecision(2)<<"| "<<nilai1[i]<<"";
    cout<<setprecision(6)<<"   | "<<nilai2[i]<<"";
    cout<<setprecision(10)<<"   | "<<hasil[i]<<""<<"      |"<<endl;
    }
    cout<<"+--------------------------------------------------+";
}
int main()
{
    char pilih;
    char x;
    char nomor;
    char user[30];
    perulangan ulang;
    array ray;
    tekan kem;
    menu men;
    cout<<"Masukan nama anda : ";cin.getline(user, 200);
    label:
    men.pilihan();
    cin>>pilih;
    cout<<"\n";
    switch(pilih)
    {
                 case '1':ulang:clrscr();
                      cout<<"                 Selamat Datang "<<user<< " di \"Program Perulangan\""<<endl;
                      cout<<"                        ******Menu Perulangan******"<<endl;
                      cout<<"1.Perulangan For"<<endl;
                      cout<<"2.Perulangan While"<<endl;
                      cout<<"3.Perulangan Do_While"<<endl;
                      cout<<"4.Exit"<<endl;
                      cout<<"Pilihan Anda : ";
                      cin>>nomor;
                      cout<<"\n";
                      switch(nomor){
                                    case'1':ulang.For();
                                    kem.kembali();
                                    balik1:
                                    cin>>x;
                                    switch(x){
                                              case '1':goto label;
                                              case '2':goto ulang;
                                              case '3':break;
                                              default:
                                              cout<<"Nomor Yang Anda Masukan Salah, Tolong Ulangi Lagi!"<<endl;
                                              goto balik1;
                                    }
                                    break;
                                    case '2':ulang.While();
                                    kem.kembali();
                                    balik2:
                                    cin>>x;
                                    switch(x){
                                              case '1':goto label;
                                              case '2':goto ulang;
                                              case '3':break;
                                              default:
                                              cout<<"Nomor Yang Anda Masukan Salah, Tolong Ulangi Lagi!"<<endl;
                                              goto balik2;
                                              }
                                    break;
                                    case '3':ulang.DoWhile();
                                    kem.kembali();
                                    balik3:
                                    cin>>x;
                                    switch(x){
                                              case '1':goto label;
                                              case '2':goto ulang;
                                              case '3':break;
                                              default:
                                              cout<<"Nomor Yang Anda Masukan Salah, Tolong Ulangi Lagi!"<<endl;
                                              goto balik3;
                                    }
                                    break;
                                    case '4':break;
                                    default:
                                    cout<<"Nomor Yang Anda Masukan Salah, Tolong Ulangi Lagi!"<<endl;
                                    goto ulang;
                      }

                      case '2': matrik:clrscr();
                                cout<<"                 Selamat Datang "<<user<< " di \"Program Array\""<<endl;
                                 cout<<"                        ******Menu Array******"<<endl;

                              ray.AppArray();
                              kem.kembali();
                              balik:
                              cin>>x;
                              switch(x){
                                              case '1':goto label;
                                              case '2':goto matrik;
                                              case '3':break;
                                              default:
                                              cout<<"Nomor Yang Anda Masukan Salah, Tolong Ulangi Lagi!"<<endl;
                                              goto balik;
                                    }break ;
                     
                      case '3':
                      default:
                      cout<<"Nomor Yang Anda Masukan Salah, Tolong Ulangi Lagi!"<<endl;
                      goto label;
                      }
        getch();
 
Silahkan mencoba

Tidak ada komentar:

Posting Komentar