Contoh bahasa C dengan menggunakan array, struct, string strcpy dan strcmpi
#include <stdio.h>
#include <stdlib.h>
int total(int lm, int hrg, int pjk);
int main()
{
struct
{
char kode[3], jenis[20];
int harga, lama, pajak, hapus;
float diskon;
}bis[10];
char penyewa[20], tanggal[15], x;
int a, banyak, total_biaya_sewa, total_pembayaran, menu, cari;
float total_diskon;
system("cls");
printf("----------------Data yang Tersedia--------------\n\n");
printf("------------------------------------------------\n");
printf("\t\tPROGRAM PENYEWAAN MOTOR\n");
printf("------------------------------------------------\n");
printf("\n================================================\n");
printf("No\t kode\t jenis\t harga\t pajak\n");
printf("1.\t SU\t Suzuki\t 800000\t 300000\n2.\t HO\t Honda\t 900000\t 500000\n3.\t YA\t Yamaha\t 1000000 40000\n");
printf("\n================================================\n");
getch();
system("cls");
printf("------------------Masukkan Data----------------\n\n");
printf("Jumlah Sewa = ");
scanf("%d",&banyak);
printf("Nama Penyewa = ");
scanf("%s",&penyewa);
printf("Tanggal sewa = ");
scanf("%s",&tanggal);
for(a=1;a<=banyak;a++)
{
{
printf("Data penyewa Ke ");
scanf("%d",&a);
printf("\nMasukan kode jenis = ");
scanf("%s",&bis[a].kode);
printf("\nMasukan Lama Sewa = ");
scanf("%d",&bis[a].lama);
printf("Jam\n\n");
}
{
if(strcmpi(bis[a].kode,"SU")==0)
{
strcpy(bis[a].jenis,"Suzuki");
bis[a].harga=800000;
bis[a].pajak=300000;
}
else if(strcmpi(bis[a].kode,"HO")==0)
{
strcpy(bis[a].jenis,"Honda");
bis[a].harga=900000;
bis[a].pajak=500000;
}
else if(strcmpi(bis[a].kode,"YA")==0)
{
strcpy(bis[a].jenis,"Yamaha");
bis[a].harga=1000000;
bis[a].pajak=400000;
}
else
{
strcpy(bis[a].jenis,"salah kode");
bis[a].harga=0;
bis[a].pajak=0;
}
}
{
if(bis[a].lama>20)
{
bis[a].diskon=total(bis[a].lama, bis[a].harga, bis[a].pajak)*0.1;
}
else
{
bis[a].diskon=0;
}
}
}
printf("\n--------------------------------VIEW DATA---------------------------------\n");
printf("\nDaftar Penyewaan Motor\n\n");
printf("\t\t\t\tPT. SEWA MOTOR MAJU \n");
printf("\n===========================================================================\n");
printf("Nama penyewa = %s\n",penyewa);
printf("Tanggal Sewa = %s",tanggal);
printf("\n===========================================================================\n");
printf("No\t kode\t jenis\t diskon\t lama\t harga\t pajak\t total\n");
printf("\n===========================================================================\n");
for(a=1;a<=banyak;a++)
{
printf(" %d\t",a);
printf(" %s\t",bis[a].kode);
printf("%s\t",bis[a].jenis);
printf("%2.f\t",bis[a].diskon);
printf(" %d\t",bis[a].lama);
printf("%d\t",bis[a].harga);
printf("%d\t",bis[a].pajak);
printf("%d\t",total(bis[a].lama, bis[a].harga, bis[a].pajak));
printf("\n");
}
printf("\n===========================================================================\n");
total_biaya_sewa=0;
total_diskon=0;
for(a=1;a<=banyak;a++)
{
total_biaya_sewa=total(bis[a].lama, bis[a].harga, bis[a].pajak)+total_biaya_sewa;
total_diskon=bis[a].diskon+total_diskon;
}
total_pembayaran=total_biaya_sewa-total_diskon;
printf("\t\t\tTotal Biaya Sewa = Rp %d\n",total_biaya_sewa);
printf("\t\t\tDiskon = Rp %2.f\n",total_diskon);
printf("\t\t\tTotal Pembayaran = Rp %d\n",total_pembayaran);
printf("\nApakah program ingin diulang?(Y/N)\n");
printf("\nJawab : ");
scanf("%s",&x);
if(x=='Y'||x=='y')
{
return main();
}
else
{
printf("\n\t\t\tProgram Selesai\n\t\t\t Terima kasih\n\n\n");
}
return 0;
}
int total(int lm, int hrg, int pjk)
{
return (hrg*lm+pjk);
}
Comments
Post a Comment