Tuesday, 1 August 2017

Contoh Program Sederhana Dengan Structure Program C++ :


Contoh 1:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>

main()
{
struct
{
char nim[5];
char nama[15];
float nilai;
} mahasiswa;

clrscr();
cout<<"masukan NIM = ";
cin>>mahasiswa.nim;
cout<<"masukan Nama = ";
cin>>mahasiswa.nama;
cout<<"masukan Nilai Akhir = ";
cin>>mahasiswa.nilai;

clrscr();

cout<<"NIM = "<<mahasiswa.nim<<endl;
cout<<"Nama = "<<mahasiswa.nama<<endl;
cout<<"Nilai Akhir = "<<mahasiswa.nilai<<endl;

getch();
}

Contoh 2: Nested Structure


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
main()
{
struct dtmhs
{
char nim[5];
char nama[15];
};

struct dtnil
{
float nil1;
float nil2;
};

struct
{
struct dtmhs mhs;
struct dtnil nil;
} nilai;

clrscr();

//-> masukan data
cout<<"masukan NIM = "; cin>>nilai.mhs.nim;
cout<<"masukan Nama = "; cin>>nilai.mhs.nama;
cout<<"masukan Nilai UTS = "; cin>>nilai.nil.nil1;
cout<<"masukan Nilai UAS = "; cin>>nilai.nil.nil2;
cout<<endl;

//-> menampilkan hasil masukan
cout<<"NIM = "<<nilai.mhs.nim<<endl;
cout<<"Nama = "<<nilai.mhs.nama<<endl;
cout<<"Nilai UTS = "<<nilai.nil.nil1<<endl;
cout<<"Nilai UAS = "<<nilai.nil.nil2<<endl;
cout<<endl;

getch();
return(0);
}

Contoh 3: Structure dengan Array
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
main()
{
int i;
struct
{
char nim[5];
char nama[15];
float nilai;
} mhs[5];

clrscr();
for(i=1; i<=2; i++)
{
cout<<"masukan NIM = "; cin>>mhs[i].nim;
cout<<"masukan Nama = "; cin>>mhs[i].nama;
cout<<"masukan Nilai Akhir = "; cin>>mhs[i].nilai;
}
cout<<endl;
cout<<"data Mahasiswa"<<endl;
for(i=1; i<=2; i++)
{
cout<<"Data Ke - "<<i<<endl;
cout<<"NIM = "<<mhs[i].nim<<endl;
cout<<"Nama = "<<mhs[i].nama<<endl;
cout<<"Nilai Akhir = "<<mhs[i].nilai<<endl;
cout<<endl;
}

getch();
}

Contoh 4: Structure dengan Function

#include<stdio.h>
#include<conio.h>
#include<iostream.h>

char* ket(float n);

main()
{
int i, j=1, k=1;
struct
{
char nim[5];
char nama[15];
float nilai;
} mhs[5];

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

clrscr();
for(i=0; i<2; i++)
{
cout<<"Data Ke - "<<k++<<endl;
cout<<"NIM = "<<mhs[i].nim<<endl;
cout<<"Nama = "<<mhs[i].nama<<endl;
cout<<"Nilai Akhir = "<<mhs[i].nilai<<endl;
cout<<"Keterangan yang didapat = ";
cout<<ket(mhs[i].nilai)<<endl;
cout<<endl;
}

getch();
}

char* ket(float n)
{
if(n > 65)
cout<< "Lulus";
else
cout<< "Gagal";
}

1 comment: