.

Minggu, 11 November 2012

C++ Menghitung Nilai Kelulusan Mahasiswa


Berikut ini merupakan kode C++ untuk menghitung Nilai Kelulusan Mahasiswa

#include <iostream>                                                                                                                                                                                                                                                                                                      /* header C++ yang menyatakan penggunaan library iostream untuk menampilkan dan memasukan data dengan metode cout dan cin*/
using namespace std;                                                                                                                                                                                                                                                                                    /* menggunakan namespace bernama std yang berfungsi untuk menampilkan informasi kelayar melalui cout*/  
void case1();                                                                                                                                                                                                                                                                                                                      /* fungsi dengan nama case1 yang tidak memiliki nilai balik*/
void case2();                                                                                                                                                                                                                                                                                                                      /* fungsi dengan nama case2 yang tidak memiliki nilai balik*/
namespace dhanu                                                                                                                                                                                                                                                                                                           /* membuat namespace dengan nama dhanu*/
{                                                                                                                                                                                                                                                                                                                                                                              /* awal dari badan namespace dengan nama dhanu*/
struct idm                                                                                                                                                                                                                                                                                                                                            /* idm(identitas data mahasiswa) nama struktur*/
                {                                                                                                                                                                                                                                                                                                                                                              /* awal dari badan struktur yang bernama idm*/
                char nama[100];                                                              
                                                                                                                                                                                                                                                 /* variable nama memiliki tipe data char yang memiliki panjang array 100*/
                char e[254];                                                                                                                                                                                                                                                                                                        /* variable e memiliki tipe data char yang memiliki panjang array 254*/
                double a[10];                                                                                                                                                                                                                                                                                                     /* variable a, memiliki tipe data double yang memiliki panjang array 10*/
                double b,c,d,f;                                                                                                                                                                                                                                                                                                  /* variable b,c,d,f memiliki tipe data double*/
}                                                                                                                                                                                                                                                                                                                                                                              /* akhir dari badan struktur yang bernama idm*/
idmh;                                                                                                                                                                                                                                                                                                                                                    /* membuat variable idmh dengan tipe data idm dari data struktur*/
}                                                                                                                                                                                                                                                                                                                                                                              /* akhir dari badan namespace dengan nama dhanu*/
int main()                                                                                                                                                                                                                                                                                                                                            /* merupakan fungsi minimal yang harus ada*/
{                                                                                                                                                                                                                                                                                                                                                                              /* awal badan dari fungsi main*/
                using  namespace dhanu;                                                                                                                                                                                                                                                             /* menggunakan isi dari namespace yang bernama dhanu */
                char *p[100] ;                                                                                                                                                                                                                                                                                                    /* p merupakan pointer ke char dengan array 100 */
                p[0] ="||================Menghitung Nilai Akhir=================||\n";                                                                          /* mendeklarasikan p urutan ke 0 dengan nilai ||================Menghitung Nilai Akhir=================||dan disertai dengan new line */
                p[1] ="||=======================================================||\n";                                                                              /* mendeklarasikan p urutan ke 1 dengan nilai ||=======================================================||dan disertai dengan new line */
                cout <<p[0];                                                                                                                                                                                                                                                                                                       /* menampilkan isi dari p urutan ke 0 yang telah dideklarasikan sebelumnya*/
                cout <<"\n";                                                                                                                                                                                                                                                                                                       /* menambahkan new line*/
                cout << " Masukan Nama Anda                                 : ";                                                                                                                                                                                          /* menampilkan "Masukan Nama Anda"*/
                cin.getline(idmh.nama,100);                                                                                                                                                                                                                                                       /* membaca masukan dari "Masukan nama Anda" dan menempatkannya ke variable idmh.nama dengan panjang 100 karakter*/
               
                cout << " Masukan NIM Anda                    : ";                                                                                                                                                                                                          /* menampilkan "Masukan NIM Anda"*/
                cin.getline(idmh.e, 254);                                                                                                                                                                                                                                                               /* membaca masukan dari "Masukan NIM Anda " dan menempatkannya ke variable idmh.e dengan panjang 254 karakter*/
               
                cout <<" Masukan Nilai UAS        (1..100): ";                                                                                                                                                                                           /* menampilkan "Masukan Nilai UAS   (1..100)"*/
                cin >> idmh.a[0] ;                                                                                                                                                                                                                                                                                             /* membaca masukan dari "Masukan nilai UAS   (1..100)" dan menempatkannya ke variable idmh.a dengan array 0*/
               
                cout << " Masukan Nilai UTS       (1..100): ";                                                                                                                                                                                           /* menampilkan "Masukan Nilai UTS   (1..100)"*/
                cin>> idmh.b ;                                                                                                                                                                                                                                                                                                   /* membaca masukan dari "Masukan nilai UTS   (1..100)" dan menempatkannya ke variable idmh.b*/
               
                cout << " Masukan Nilai Tugas    (1..100): ";                                                                                                                                                                           /* menampilkan "Masukan Nilai Tugas   (1..100)"*/
                cin >> idmh.c ;                                                                                                                                                                                                                                                                                   /* membaca masukan dari "Masukan nilai Tugas   (1..100)" dan menempatkannya ke variable idmh.c*/
               
                cout << " Masukan nilai Quiz       (1..100): ";                                                                                                                                                                           /* menampilkan "Masukan nilai Quiz   (1..100)"*/
                cin >> idmh.f ;                                                                                                                                                                                                                                                                                                   /* membaca masukan dari "Masukan nilai Quiz   (1..100)" dan menempatkannya ke variable idmh.f*/
               
                idmh.d = (0.35*idmh.a[0]) + (0.25*idmh.b ) + (0.2*idmh.c) + (0.2*idmh.f) ;                                                          /* operasi penghitung jumlah nilai,dengan rumus jumlah nilai = ((0.35*nilai dari variable idmh.a dengan array 0) + (0.25* nilai dari varible imdh.b) + (0.2* nilai dari varibale idmh.c) + (0.2*nilai dari variable idmh.f))dan menempatkan ke variabel idmh.d, */
int kode;                                                                                                                                                                                                                                                                                                                                              /* variable kode memiliki tipe data integer*/
if (idmh.d>=80)                                                                                                                                                                                                                                                                                                                 /* mendeklarasikan kondisi dimana jika variable idmh.d lebih besar  sama dengan 80 maka statement ini dibuatkan kode 1*/
kode=1;                                                                                                                                                                                                                                                                                                                                                               /* mendeklarasikan kondisi sebelumnya bernilai 1 yang ditempatkan dalam variable kode*/
else if (idmh.d>=65)                                                                                                                                                                                                                                                                                       /* mendeklarasikan kondisi dimana jika variable idmh.d lebih besar  sama dengan 65 maka statement ini dibuatkan kode 2*/
kode=2;                                                                                                                                                                                                                                                                                                                                                               /* mendeklarasikan kondisi sebelumnya bernilai 2 yang ditempatkan dalam variable kode*/
else if (idmh.d>=55)                                                                                                                                                                                                                                                                                       /* mendeklarasikan kondisi dimana jika variable idmh.d lebih besar atau sama dengan 55 maka statement ini dibuatkan kode 3*/
kode=3;                                                                                                                                                                                                                                                                                                                                                               /* mendeklarasikan kondisi sebelumnya bernilai 3 yang ditempatkan dalam variable kode*/
else if (idmh.d>=45)                                                                                                                                                                                                                                                                                       /* mendeklarasikan kondisi dimana jika variable idmh.d lebih besar atau sama dengan 45 maka statement ini dibuatkan kode 4*/
kode=4;                                                                                                                                                                                                                                                                                                                                                               /* mendeklarasikan kondisi sebelumnya bernilai 4 yang ditempatkan dalam variable kode*/
                                                                                                                                                                                                                                                                                               

                switch (kode)                                                                                                                                                                                                                                                                                                    /* mendeklarasikan switch dengan pilihan kode */
                {                                                                                                                                                                                                                                                                                                                                                              /* awal dari badan switch*/
                case 1: cout << "\n";                                                                                                                                                                                                                                                                       /* menambahkan new line*/
                                                cout << " "<<idmh.nama <<","<< "\n";                                                                                                                                                                  /* menampilkan nilai dari variable idmh.nama , dan disertai new line*/
                                                cout << " "<<idmh.e<< "\n";                                                                                                                                                                                                                       /* menampilkan nilai dari variable idmh.e , dan disertai new line*/
                                                cout << " Anda mendapatkan nilai A, dengan jumlah nilai " << idmh.d  << "\n";   /* menampilkan "Anda mendapatkan nilai A, dengan jumlah nilai",dan nilai dari variable idmh.d dan disertai new line*/
                                                case1();                                                                                                                                                                                                                                                                                                /* menampilkan isi fungsi dari case1*/
                                                break;                                                                                                                                                                                                                                                                                                   /* mendefinisikan suatu perintah case 1 harus berhenti dan keluar dari siwtch lalu melanjutkan perintah di luar switch */
                               
                case 2: cout << "\n";                                                                                                                                                                                                                                                                       /* menambahkan new line*/
                                                cout <<" "<< idmh.nama <<","<< "\n";                                                                                                                                                                  /* menampilkan nilai dari variable idmh.nama, dan disertai new line*/
                                                cout << " " <<idmh.e<< "\n";                                                                                                                                                                                                      /* menampilkan nilai dari variable idmh.e , dan disertai new line*/
                                                cout << " Anda mendapatkan nilai B , dengan jumlah nilai " << idmh.d  << "\n"; /* menampilkan "Anda mendapatkan nilai E, dengan jumlah nilai",dan nilai dari variable idmh.d dan disertai new line*/
                                                case1();                                                                                                                                                                                                                                                                                                /* menampilkan isi fungsi dari case1*/ 
                                                break;                                                                                                                                                                                                                                                                                                   /* mendefinisikan suatu perintah case 2 harus berhenti dan keluar dari siwtch lalu melanjutkan perintah di luar switch */
                                               
                case 3:  cout << "\n";                                                                                                                                                                                                                                                                      /* menambahkan new line*/
                                                cout << " "<<idmh.nama <<","<< "\n";                                                                                                                                                                  /* menampilkan nilai dari variable idmh.nama, dan disertai new line*/
                                                cout << " "<<idmh.e<< "\n";                                                                                                                                                                                                                       /* menampilkan nilai dari variable idmh.e , dan disertai new line*/
                                                cout << " Anda mendapatkan nilai C,  dengan jumlah nilai " << idmh.d  << "\n";  /* menampilkan "Anda mendapatkan nilai C, dengan jumlah nilai,dan nilai dari variable idmh.d dan disertai new line*/
                                                case1();                                                                                                                                                                                                                                                                                                /* menampilkan isi fungsi dari case1*/
                                                break;                                                                                                                                                                                                                                                                                                   /* mendefinisikan suatu perintah case 3 harus berhenti dan keluar dari siwtch lalu melanjutkan perintah di luar switch */
                                               
                case 4:  cout << "\n";                                                                                                                                                                                                                                                                      /* menambahkan new line*/
                                                cout << " " << idmh.nama <<","<< "\n";                                                                                                                                                                                /* menampilkan "Selamat" dan nilai dari variable idmh.nama , dan disertai new line*/
                                                cout << " " <<idmh.e<< "\n";                                                                                                                                                                                                      /* menampilkan nilai dari variable idmh.e , dan disertai new line*/
                                                cout << " Anda mendapatkan nilai D, dengan jumlah nilai " << idmh.d  << "\n";   /* menampilkan "Anda mendapatkan nilai D, dengan jumlah nilai,dan nilai dari variable idmh.d dan disertai new line*/
                                                case2();                                                                                                                                                                                                                                                                                                /* menampilkan isi fungsi dari case2*/
                                                break;                                                                                                                                                                                                                                                                                                   /* mendefinisikan suatu perintah case 4 harus berhenti dan keluar dari siwtch lalu melanjutkan perintah di luar switch */
                                                                               
                default:cout << "\n";                                                                                                                                                                                                                                                                      /* menambahkan new line*/
                                                cout << " "<< idmh.nama <<","<< "\n";                                                                                                                                                                 /* menampilkan nilai dari variable idmh.nama , dan disertai new line*/ 
                                                cout << " " <<idmh.e<< "\n";                                                                                                                                                                                                      /* menampilkan nilai dari variable idmh.e , dan disertai new line*/
                                                cout << " Anda mendapatkan nilai E, dengan jumlah nilai " << idmh.d  << "\n";    /* menampilkan "Anda mendapatkan nilai E, dengan jumlah nilai,dan nilai dari variable idmh.d dan disertai new line*/
                                                case2();                                                                                                                                                                                                                                                                                                /* menampilkan isi fungsi dari case2*/
                                                break;                                                                                                                                                                                                                                                                                                   /* mendefinisikan suatu perintah case 5 harus berhenti dan keluar dari siwtch lalu melanjutkan perintah di luar switch */
                }                                                                                                                                                                                                                                                                                                                                                              /* akhir dari  badan switch*/
               
                                cout << "\n";                                                                                                                                                                                                                                                                                      /* menambahkan break line*/
                                cout <<p[1];                                                                                                                                                                                                                                                                                       /* menampilkan isi dari p urutan ke 1 yang telah dideklarasikan sebelumnya*/
                                cin.get();                                                                                                                                                                                                                                                                                                              /* membaca newline agar program terhenti sampai newline berupa enter diberikan user kepada program*/ 
                                return 0;                                                                                                                                                                                                                                                                                                              /* menghentikan fungsi int main() */
                }                                                                                                                                                                                                                                                                                                                                                              /* akhir dari badan fungsi main*/


void case1()                                                                                                                                                                                                                                                                                                                        /* mendeklarasikan isi dari fungsi case1*/
 {                                                                                                                                                                                                                                                                                                                                                                             /* awal dari badan fungsi case1*/
                                cout << " Anda Lulus\n";                                                                                                                                                                                                                                               /* menampilkan "Anda Lulus", dan disertai new line*/
                                cout << " Selamat anda bisa melanjutkan ke semester selanjutnya \n";                                                  /* menampilkan "Anda bisa melanjutkan ke semester selanjutnya", dan disertai new line.*/
}                                                                                                                                                                                                                                                                                                                                                                              /* akhir dari badan fungsi case1*/
void case2()                                                                                                                                                                                                                                                                                                                        /* mendeklarasikan isi dari fungsi case2*/
                {                                                                                                                                                                                                                                                                                                                                                              /* awal dari badan fungsi case2*/
                cout << " Anda Tidak Lulus\n";                                                                                                                                                                                                                   /* menampilkan "Anda Tidak Lulus", dan disertai new line*/
                cout << " Silakan mengulanginya tahun depan \n";                                                                                                                                                           /* menampilkan "Silakan mengulanginya tahun depan", dan disertai new line.*/
}                                                                                                                                                                                                                                                                                                                                                                              /* akhir dari badan fungsi case2*/





4 komentar: