Selamat Datang Di Blogg Saya

Jumat, 20 Januari 2012

program penerimaan siswa dengan c++

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



struct dftr
{
char nodftr[5];
char nama[20],alamat[25];
float nilai,admns;
char del;
};

FILE *fp;

long get_loc(const char *key);

long filesize(FILE *stream)
{
long length,cur;
cur = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream,cur,SEEK_SET);
return length;
}



int add_data(struct dftr value)
{
long found, length;

length = filesize(fp);
found = get_loc(value.nodftr);
if(found > -1)
return -1;
value.del = '0';

fseek(fp, length, SEEK_SET);
fwrite(&value, sizeof(value), 1, fp);

return 0;

}

int del_data(const char *key)
{
FILE *tf;
dftr temp;

tf = fopen("d:\\file.bak", "w+");
long length = filesize(fp) / sizeof(dftr);
long i, j, k;
for(i = j = k = 0, rewind(fp), rewind(tf); i <= length - 1; i++)
{
fread(&temp, sizeof(dftr), 1, fp); j++;
if(!strcmp(temp.nodftr, key)) continue;
fwrite(&temp, sizeof(dftr), 1, tf); k++;
}
fclose(fp);
remove("d:\\file.dat");
fclose(tf);
rename("d:\\file.bak", "d:\\file.dat");
fp = fopen("d:\\file.dat", "r+");

if(j == k)
return -1;
else
return 0;

}

long get_loc(const char *key)
{
struct dftr temp;
long found = -1, pos = 0,
length = filesize(fp) / sizeof(dftr);

rewind(fp);

for(; pos <= length - 1; pos++)
{
fread(&temp, sizeof(temp), 1, fp);
if(!strcmp(temp.nodftr, key))
{
found = pos;
break;
}
}
if(temp.del=='0')
return found;
else
return -1;
}

void add_menu()
{
struct dftr temp;
char nodftr[8];
char nama[20],alamat[25];
float nilai,admns;
long pos, i, j;
long length;

length = filesize(fp) / sizeof(temp);

//clrscr();

for(pos = 0, i = 0, j = 1; i <= length - 1;
i++, pos += sizeof(temp))
{

fseek(fp, pos, SEEK_SET);
fread(&temp, sizeof(temp), 1, fp);
if(temp.del=='1') continue;

j++;
}
j;
if (j == 40)
{
cout << "\n\n\n Pendaftaran Siawa Ditutup";
cout << "\n\nJumlah Siswa Sudah Memenuhi Target";
getch();
}
else
{
cout<<"Masukkan Data Pendaftar >> \n";
cout<<"No Pendaftaran : "; cin >> nodftr;
strcpy(temp.nodftr, nodftr);
cout<<"Nama Siswa : "; cin >> nama;
strcpy(temp.nama, nama);
cout<<"Alamat Siswa : "; cin >> alamat;
strcpy(temp.alamat, alamat);
cout<<"Jumlah Nilai : "; cin >> nilai;
if (nilai >= 85)
{
admns = 100000;
}
else
if ((nilai >= 70)&&(nilai<85))
{
admns = 200000;
}
else
{
admns = 350000;
}
temp.nilai = nilai;
temp.admns = admns;

if(add_data(temp) == 0)
{
cout << "\n\nData Berhasil Masuk";
getch();
}
else
{
cout << "\n\n Maaf Data Sudah Ada";
getch();
}
}
}

void delete_menu()
{
//clrscr();
char nodftr[8];
struct dftr temp;
long pos, i,j;
long length;

length = filesize(fp) / sizeof(temp);
//clrscr();

cout <<"---------------------\n";
cout <<"| Tabel Siswa Yang Sudah Mendaftar |\n";
cout <<"---------------------\n";
cout <<"No Nodftr Nama Alamat Nilai Biaya_Adm \n";

for(pos = 0, i = 0, j = 1; i <= length - 1;
i++, pos += sizeof(temp))
{

fseek(fp, pos, SEEK_SET);
fread(&temp, sizeof(temp), 1, fp);
if(temp.del=='1') continue;
cout << j << ". (" << temp.nodftr << ") "" ("<< temp.nama <<") "" (" << temp.alamat <<") "" ("<< temp.nilai <<") "" ("<< temp.admns <<")""\n";
j++;
}

j;
cout <<"---------------------\n";
cout<<"\nMasukkan Data Yang Akan Dihapus >>\n";
cout<<"No Daftar: "; cin >> nodftr;
if(!del_data(nodftr))
{
cout << "\n\nData Berhasil Dihapus";
getch();
}
else
{
cout << "\n\nMaaf Tidak Ada";
cout << "\nMohon Anda Cek Kembali";
getch();
}
}

void print_all()
{
//clrscr();
struct dftr temp;
long pos, i, j;
long length;

length = filesize(fp) / sizeof(temp);
//clrscr();

cout <<"---------------------\n";
cout <<"| Menu Siswa Yang Sudah Terdaftar |\n";
cout <<"---------------------\n";
cout << "No Nodftr Nama Alamat Nilai Biaya_Adm \n";

for(pos = 0, i = 0, j = 1; i <= length - 1;
i++, pos += sizeof(temp))
{

fseek(fp, pos, SEEK_SET);
fread(&temp, sizeof(temp), 1, fp);
if(temp.del=='1') continue;
cout << j << ". (" << temp.nodftr << ") "" ("<< temp.nama <<") "" (" << temp.alamat <<") "" ("<< temp.nilai <<") "" ("<< temp.admns <<")""\n";
j++;
}
j;
cout <<"---------------------\n";
cout << "\n\nJumlah Siswa Yang Terdaftar = " << j << " Siswa ";
cout << "\nKapasitas Kelas = 40 Siswa";
getch();
}

int main()
{
//clrscr();
if((fp = fopen("d:\\file.dat", "r+")) == NULL)
{
if((fp = fopen("d:\\file.dat", "w+")) == NULL)
{
cout << "Tidak Dapat Membuka d:\\file.txt";
getch();
return 0;
}
}
int pilih = 0;
do
{
//clrscr();
cout <<"-----------------\n";
cout <<"| Program Penerimaan Siswa |\n";
cout <<"-----------------\n";
cout <<"| 0 - Exit Program |\n";
cout <<"| 1 - Penerimaan Siswa |\n";
cout <<"| 2 - Hapus Pendaftar |\n";
cout <<"| 3 - Siswa Yang Sudah Terdaftar |\n";
cout <<"-----------------\n";
cout <<"\nMasukkan Pilihan Anda >> "; cin >> pilih;
switch(pilih)
{
case 1: add_menu(); break;
case 2: delete_menu(); break;
case 3: print_all();
}

} while(pilih);
fclose(fp);
return 0;
}




output program

1 komentar: