Kumpulan Program C++ dengan Strukture
Contoh 1:
Contoh 2: Nested Structure
Contoh 3: Structure dengan Array
Contoh 4: Structure dengan Function
Contoh 1:
#include
#include
#include
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 = "< cout<<"Nama = "< cout<<"Nilai Akhir = "<
getch();
}
#include
#include
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 = "<
getch();
}
Contoh 2: Nested Structure
#include
#include
#include
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<
//-> menampilkan hasil masukan
cout<<"NIM = "< cout<<"Nama = "< cout<<"Nilai UTS = "< cout<<"Nilai UAS = "< cout<
getch();
return(0);
}
#include
#include
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<
//-> menampilkan hasil masukan
cout<<"NIM = "<
getch();
return(0);
}
Contoh 3: Structure dengan Array
#include
#include
#include
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< cout<<"data Mahasiswa"< for(i=1; i<=2; i++)
{
cout<<"Data Ke - "< cout<<"NIM = "< cout<<"Nama = "< cout<<"Nilai Akhir = "< cout< }
getch();
}
#include
#include
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<
{
cout<<"Data Ke - "< cout<<"NIM = "<
getch();
}
Contoh 4: Structure dengan Function
#include
#include
#include
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 br="" i=""> {
cout<<"Data Ke - "< cout<<"masukan NIM = "; cin>>mhs[i].nim;
cout<<"masukan Nama = "; cin>>mhs[i].nama;
cout<<"masukan Nilai Akhir = "; cin>>mhs[i].nilai;
cout< }
clrscr();
for(i=0; i<2 br="" i=""> {
cout<<"Data Ke - "< cout<<"NIM = "< cout<<"Nama = "< cout<<"Nilai Akhir = "< cout<<"Keterangan yang didapat = ";
cout< cout< }
getch();
}
char* ket(float n)
{
if(n > 65)
cout<< "Lulus";
else
cout<< "Gagal";
}
2> 2>
#include
#include
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 br="" i=""> {
cout<<"Data Ke - "<
cout<<"masukan Nama = "; cin>>mhs[i].nama;
cout<<"masukan Nilai Akhir = "; cin>>mhs[i].nilai;
cout<
clrscr();
for(i=0; i<2 br="" i=""> {
cout<<"Data Ke - "<
cout<
getch();
}
char* ket(float n)
{
if(n > 65)
cout<< "Lulus";
else
cout<< "Gagal";
}