Program Array of Record C - Array of Record C - Array - Record - data base - Operasi File
===================================================================
INi tampilannya bro :
Setelah ISI DATA:
======================================================================
#include <stdio.h>
#include <stdlib.h>
#define max 20
typedef struct recdata
{
char nama[30];
char jabatan[30];
char favorit[50];
}dt;
int n;
dt dat[max];
void data(int n)
{
FILE *PF;
int j;
PF=fopen("flatfile.txt","a+");
for(j=1;j<=n;j++)
{
if(PF != NULL)
{
fprintf(PF,"%s # ",dat[j].nama);
fprintf(PF,"%s # ",dat[j].jabatan);
fprintf(PF,"%s\n",dat[j].favorit);
}
}
fclose(PF);
}
int tambah_data(dt dat[],int n)
{
printf("Masukkan data : \n");
printf("-------------\n\n");
printf("Nama : "); fflush(stdin);gets(dat[n].nama);
printf("Posisi : "); fflush(stdin);gets(dat[n].jabatan);
printf("Favorit : "); fflush(stdin);gets(dat[n].favorit);
printf("\n");
system("PAUSE");
return 0;
}
void tampil_data()
{
FILE *PF;
char kar;
PF=fopen("flatfile.txt","a+");
if ((PF=fopen("flatfile.txt","r+")) == NULL)
{
printf("Data tidak ada\n");
}
else
{
while((kar=fgetc(PF))!=EOF)
putchar(kar);
}
fclose(PF);
}
void edit(dt dat[],int n)
{
int index;
printf("Edit Data\n============\n");
printf("Masukkan index data yg akan diganti : ");scanf("%d",&index);
printf("\n");
printf("Nama : %s\n",dat[index].nama);
printf("Posisi : %s\n",dat[index].jabatan);
printf("Favorit : %s\n",dat[index].favorit);
printf("\n");
printf("===============================\n");
printf("Silahkan masukan data yang baru :\n");
printf("===============================\n");
printf("Nama : "); fflush(stdin);gets(dat[index].nama);
printf("Posisi : "); fflush(stdin);gets(dat[index].jabatan);
printf("Favorit : "); fflush(stdin);gets(dat[index].favorit);
system ("PAUSE");
}
void cari(dt dat[],int n)
{
FILE *PF;
char kar;
int cari,j;
PF=fopen("flatfile.txt","a+");
printf("\nCari Data Karakter\n");
printf("====================\n\n");
if((PF=fopen("flatfile.txt","a")) == NULL)
{
printf("Data Masih Kosong\n");
}
else
{
printf("Masukkan Nomor Karakter : "); scanf("%i",&cari);
j=1;
while((j<=n)&&(j!= cari))
{
j++;
}
if(j == cari)
{
printf("=======================\n");
printf("Nama : %s\n\n",dat[j].nama);
printf("Posisi : %s\n\n",dat[j].jabatan);
printf("Favorit : %s\n\n",dat[j].favorit);
}
else
{
printf("Data Tidak Ditemukan \a\n");
}
}
fclose(PF);
system("PAUSE");
}
int hapus_data(dt dat[], int n)
{
int cari,i,j;
char pilih;
printf("\nHapus Data karakter\n");
printf("=====================\n\n");
printf("Masukkan Nomor Urut : "); scanf("%i",&cari);
i=1;
while((i<=n)&&(i != cari))
{
i++;
}
if(i == cari)
{
printf("\n---------------------\n");
printf("Nama : %s\n",dat[i].nama);
printf("Posisi : %s\n",dat[i].jabatan);
printf("Favorit : %s\n\n",dat[i].favorit);
printf("Hapus Data Penghuni Yang Dipilih ? [Y/T] : "); scanf("%s",&pilih);
if((pilih=='Y')||(pilih=='y'))
{
printf("\n\n %i\n",i);
for(j=i; j<=n-1; j++)
{
dat[j]=dat[j+1];
}
n=n-1;
}
}
else
{
printf("\n\nData Tidak Ada\n");
}
system("PAUSE");
return n;
}
int main(int argc, char *argv[])
{
int menu;
do
{
system("CLS");
printf("==============================================================\n");
printf(" Data Anggota Bajak Laut Topi Jerami\n");
printf(" -----------------------------------\n\n");
data(n);
tampil_data();
printf("\n==============================================================\n");
printf(" 1.Tambah 2.Ubah 3.Cari 4.Hapus 0.Keluar\n");
printf("==============================================================\n");
printf(" Pilih Menu (0-4) : ");scanf("%i",&menu);
printf("==============================================================\n");
switch(menu)
{
case 1 :
{
n=n+1;
system("CLS");
tambah_data(dat,n);
break;
}
case 2 :
{
system("CLS");
edit(dat,n);
break;
}
case 3 :
{
system("CLS");
cari(dat,n);
break;
}
case 4 :
{
system("CLS");
n=hapus_data(dat,n);
break;
}
case 0 :
{
system("CLS");
printf("Keluar\n");
break;
}
default :
{
printf("Salah Memasukkan Menu!\n");
getch();
break;
}
}//endcase
} //endwhile
while(menu!=0);
system("PAUSE");
return 0;
}
========================================================================
No comments:
Post a Comment