Docstoc

kumpulan struktur

Document Sample
kumpulan struktur Powered By Docstoc
					/* contoh program penggunaan struktur
====================================*/
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
void main() {
     struct {
     int npm;
      char nama[30];
      char asalkota[15];
      float ipk;
   } mahasiswa;

    cout   <<   "Ketik   NPM           : "; cin >> mahasiswa.npm;
    cout   <<   "Ketik   Nama Mahasiswa: "; cin >> mahasiswa.nama;
    cout   <<   "Ketik   Asal kota     : "; cin >> mahasiswa.asalkota;
    cout   <<   "Ketik   IPK : "; cin >> mahasiswa.ipk;

    cout << endl;
    cout << "NPM :       "   <<   mahasiswa.npm << endl;
    cout << "Nama:       "   <<   mahasiswa.nama << endl;
    cout << "Asal:       "   <<   mahasiswa.asalkota << endl;
    cout << "IPK :       "   <<   mahasiswa.ipk << endl;
    getch();
}


//contoh program struktur dalam struktur
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main() {
      struct data_mhs {
      char nim[10]; //mak 9 digit 1 digit NULL
       char nama[20];
   };

    struct data_nil {
       float nil_uts;
        float nil_uas;
    };

    struct {
      struct data_mhs mhs;
       struct data_nil nilai;
    } gabung;

    cout   <<   "Ketik   NIM            : "; cin >> gabung.mhs.nim;
    cout   <<   "Ketik   Nama           : "; cin >> gabung.mhs.nama;
    cout   <<   "Ketik   nilai UTS: "; cin >> gabung.nilai.nil_uts;
    cout   <<   "Ketik   nilai UAS: "; cin >> gabung.nilai.nil_uas;

    cout << endl;
    cout << "NIM Anda    : " << gabung.mhs.nim << endl;
    cout << "Nama Mahasiswa: " << gabung.mhs.nama << endl;
    cout << "Nilai UTS     : " << gabung.nilai.nil_uts << endl;
    cout << "Nilai UAS     : " << gabung.nilai.nil_uas << endl;
    cout << endl;
    getch();
}


// contoh program fungsi pada struktur
//===================================
#include   <stdio.h>
#include   <conio.h>
#include   <iostream.h>
#include   <iomanip.h>

char grade(float n); //ini fungsi grade() pada structure
void main() {
   int i, j=1, k=1;

    struct {
       char nim[10];
       char nama[20];
       float nilai;
    } mhs[3];

    for (i=0; i<2; i++) {
             cout << "Data ke- " << j++ << endl;
       cout << "Ketik NIM        : "; cin >> mhs[i].nim;
       cout << "Ketik Nama Mhs   : "; cin >> mhs[i].nama;
       cout << "Ketik Nilai Akhir: "; cin >> mhs[i].nilai;
       cout << endl;
       }

    cout << "\n\nData Yang Diinputkan Adalah\n\n";
    for (i=0; i<2; i++) {
       cout << "Data ke- " << k++ << endl;
       cout << "NIM         : " << mhs[i].nim << endl;
       cout << "Nama Mhs: " << mhs[i].nama << endl;
       cout << "Nilai   : " << mhs[i].nilai << endl;
             cout << "Grade       : "; cout << grade(mhs[i].nilai) << endl;
       cout << endl;
       }
       getch();
}

//fungsi grade() untuk menentukan grid/kriteria
char grade(float n) {   // tipe data dalam kurung bisa int/float/double
      if (n > 80) return 'A';
   else if (n > 70) return 'B';
   else if (n > 60) return 'C';
   else if (n > 40) return 'D';
   else return 'E';
      }

// contoh program array pada struktur
//===================================
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
//char grade(float n);
void main() {
   int i, j=1, k=1;

    struct {
      char nim[10];
       char nama[20];
       float nilai;
    } mhs[3];

    for (i=0; i<2; i++) {
            cout << "Data ke- " <<   j++ <<   endl;
       cout << "Ketik NIM        :   "; cin   >> mhs[i].nim;
       cout << "Ketik Nama Mhs   :   "; cin   >> mhs[i].nama;
       cout << "Ketik Nilai Akhir:   "; cin   >> mhs[i].nilai;
       cout << endl;
       }

    cout << "\n\nData Yang Diinputkan Adalah\n\n";
    for (i=0; i<2; i++) {
      cout << "Data ke- " << k++ << endl;
       cout << "NIM      : " << mhs[i].nim << endl;
       cout << "Nama Mhs: " << mhs[i].nama << endl;
       cout << "Nilai   : " << mhs[i].nilai << endl;
       }
      getch();
}

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:21
posted:6/4/2012
language:
pages:3