Penjelasan
:
“Structure
digunakan untuk mengelompokkan sejumlah data yang mempunyai tipe data yang
mempunyai tipe data yang berbeda. Variable-variable yang membentuk sebuah
struktur dinamakan elemen struktur”
Struct
nama_tipe_structure
{
Elemen_structure;
}
nama_tipe_structure;
Contoh:
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
char ket(float n);
main()
{
int i,j;
char k;
struct
{
char nim[12];
char nama[15];
float nilai;
}
mhs[5];
clrscr();
cout<<"Jumlah Data Yang Ingin di
Input :"; cin>>j;
for (i=1; i<=j; i++)
{
cout<<"Data Ke-"<<i<<endl;
cout<<"Masukkan Nim :"; cin>>mhs[i].nim;
cout<<"Masukkan Nama:";cin>>mhs[i].nama;
cout<<"Masukkan Nilai Akhir :"; cin>>mhs[i].nilai;
cout<<endl<<endl;
}
clrscr();
cout<<"No NIM
Nama Nilai Akhir Keterangan"<<endl;
cout<<"--------------------------------------------------"<<endl;
for
(i=1; i<=j; i++)
{
cout<<setiosflags(ios::left)<<setw(4)<<i;
cout<<setiosflags(ios::left)<<setw(10)<<mhs[i].nim;
cout<<setiosflags(ios::left)<<setw(17)<<mhs[i].nama;
cout<<setprecision(2)<<" "<<mhs[i].nilai;
cout<<" ";
k=ket(mhs[i].nilai);
cout<<setiosflags(ios::left)<<setw(17)<<k<<endl;
cout<<endl;
}
getch();
}
char ket (float n)
{
if
(n>65)
return 'L';
else
return 'G';
}
Latihan
Mandiri 1 Structure
#include <conio>
#include <stdio>
#include <iostream>
#include <string>
#include <iomanip>
garis()
{cout<<"---------------------------------------------------------------------------"<<endl;}
double potongan( char askes, long int biaya)
{ if (toupper(askes)=='Y')
return
(biaya*0.15) ;
else
return
0 ;
}
main()
{
char lagi;
int jp,i;
long int grand=0;
struct
{ char
nama[10],waktu[5],*dokter,*jam,spesialis[5], askes;
long
int biaya, total;
} rs[2];
awal:
clrscr();
cout<<"\tRUMAH SAKIT \"SEMOGA
WARAS\""<<endl;
garis();
cout<<"\tForm
Pendaftaran"<<endl<<endl;
cout<<"Masukkan Jumlah
Pasien:";cin>>jp;
cout<<endl<<endl;
for (i=1;i<=jp;i++)
{
cout<<"Data
Ke-"<<i<<endl;
cout<<"Nama Pasien:";cin>>rs[i].nama;
cout<<"Waktu
Berobat[Pagi|Sore]:";cin>>rs[i].waktu;
cout<<"Pilih
Spesialis[THT|Anak]:";cin>>rs[i].spesialis;
cout<<"Askes[Y|T]:";cin>>rs[i].askes;
if
(!strcmp(rs[i].waktu,"PAGI")||!strcmp(rs[i].waktu,"pagi"))
{
if
(!strcmp(rs[i].spesialis,"THT")||!strcmp(rs[i].spesialis,"tht"))
{
rs[i].dokter="dr. Yuza";
rs[i].jam="07.45-15.15";
rs[i].biaya=65000;
}
else if
(!strcmp(rs[i].spesialis,"Anak")||!strcmp(rs[i].spesialis,"anak"))
{
rs[i].dokter="dr. Hilda";
rs[i].jam="07.00-15.00";
rs[i].biaya=80000;
}
}
else if
(!strcmp(rs[i].waktu,"SORE")||!strcmp(rs[i].waktu,"sore"))
{
if
(!strcmp(rs[i].spesialis,"THT")||!strcmp(rs[i].spesialis,"tht"))
{
rs[i].dokter="dr. Deanda";
rs[i].jam="18.30-21.30";
rs[i].biaya=75000;
}
else if
(!strcmp(rs[i].spesialis,"Anak")||!strcmp(rs[i].spesialis,"anak"))
{
rs[i].dokter="dr. Andini";
rs[i].jam="17.25-22.00";
rs[i].biaya=90000;
}
}
rs[i].total=rs[i].biaya-potongan(rs[i].askes,rs[i].biaya);
grand=grand+rs[i].total;
}
clrscr();
cout<<"Bukti
Pendaftaran"<<endl;
cout<<"RUMAH SAKIT \"SEMOGA
WARAS\""<<endl<<endl;
garis();
cout<<"|No | Nama
| Nama |
Jam | Biaya
| Potongan | Total |"<<endl;
cout<<"| | Pasien
| Dokter |
| Dokter |
| |"<<endl;
garis();
cout<<endl;
for (i=1;i<=jp;i++)
{
cout<<setiosflags(ios::left)<<setw(6)<<i<<"|";
cout<<setiosflags(ios::left)<<setw(9)<<rs[i].nama<<"|";
cout<<setiosflags(ios::left)<<setw(10)<<rs[i].dokter<<"|";
cout<<setiosflags(ios::left)<<setw(14)<<rs[i].jam<<"|";
cout<<setprecision(14)<<"
"<<rs[i].biaya<<"|";
cout<<setprecision(14)<<" "<<potongan(rs[i].askes,
rs[i].biaya)<<"|";
cout<<setprecision(14)<<"
"<<rs[i].total<<"|";
cout<<endl;
}
garis();
cout<<" Grand
Total=Rp."<<grand;
cout<<endl<<endl;
garis();
cout<<"Input Data Lagi [Y/T]:";cin>>lagi;
if (toupper(lagi)=='Y')
goto
awal;
else
goto
akhir;
akhir:
getch();
}
Tidak ada komentar:
Posting Komentar