///////////////////////////////////////
//Tugas 1 Algoritma dan Struktur Data//
// Database Mahasiswa Sederhana //
// NELFRITS CHRISTOPHER //
////////// 682005021 ///////////
///////////////////////////////////////
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<errno.h>
#include<limits.h>
#define MAX 10
#define IP_MAX 4.00
#define IP_MIN 0.00
struct TanggalLahir{
char tempat[25];
int tgl;
int bulan;
int tahun;
};
struct Mahasiswa{
char nama[25];
char nim[10];
TanggalLahir tglLahir;
float IP;
};
void errorMessage();
void errorMessageDate();
void init(Mahasiswa mhs[MAX]);
void extrakData(int field,Mahasiswa mhs[MAX]);
//global variabel
int limitArray;
int tglInt,blnInt,thnInt;
float InPre;
int find[MAX];
int counterData=0;
int valid = 0;
void GUI(){
clrscr();
cout<<"\n\n\n\t\t\t\tDatabase Mahasiswa"<<endl<<endl;
cout<<" \t\t\t1. Input Data"<<endl;
cout<<" \t\t\t2. Tampilkan Data"<<endl;
cout<<" \t\t\t3. Edit Data"<<endl;
cout<<" \t\t\t4. Cari Data"<<endl;
cout<<" \t\t\t5. Exit"<<endl;
cout<<"\n \t\t\tPilihan? ";
}
void delay(int time){
for(int Aa=0;Aa<=time*900;Aa++){
for(int Bb=0;Bb<=time*900;Bb++){
}
}
}
void validateInt(char value[25],int code,Mahasiswa mhs[MAX]){
/*15-Jul-2002
(C) 1999 by Jack Klein
All rights reserved*/
char *end_ptr;
long long_var;
for( ; ; ){
fflush(stdout);
if (value [0] == '\n')
{
break;
}
errno = 0;
long_var = strtol(value, &end_ptr, 0);
if (ERANGE == errno)
{
//exitMessage();
errorMessage();
init(mhs);
}
else if (long_var > INT_MAX)
{
//exitMessage();
errorMessage();
init(mhs);
}
else if (long_var < INT_MIN)
{
//exitMessage();
errorMessage();
init(mhs);
}
else if (end_ptr == value)
{
//exitMessage();
errorMessage();
init(mhs);
}
/*else if ('' != *end_ptr)
{
//printf("extra characters on input line\n");
exitMessage();
} */
else
{
if(code==1)
tglInt = (int)long_var;
else if(code==2)
blnInt = (int)long_var;
else if(code==3)
thnInt = (int)long_var;
break;
}
}
}
void validateIP(char IP[10],Mahasiswa mhs[MAX]){
char *end_ptr;
double var;
for( ; ; ){
fflush(stdout);
if (IP[0] == '\n')
{
break;
}
errno = 0;
var = strtod(IP,&end_ptr);
if (ERANGE == errno)
{
//exitMessage();
errorMessage();
init(mhs);
}
else if (var > IP_MAX)
{
//exitMessage();
errorMessage();
init(mhs);
}
else if (var < IP_MIN)
{
//exitMessage();
errorMessage();
init(mhs);
}
else if (end_ptr == IP)
{
//exitMessage();
errorMessage();
init(mhs);
}
/*else if ('' != *end_ptr)
{
//printf("extra characters on input line\n");
exitMessage();
} */
else
{
InPre = (float)var;
break;
}
}
}
void validateDate(int tgl, int bln, int tahun,Mahasiswa mhs[MAX]){
int mod;
if(tgl<1 || tgl > 31){
errorMessageDate();
init(mhs);
}
if(bln<1 || bln>12){
errorMessageDate();init(mhs);}
if(tahun<1900 || tahun>2020){
errorMessageDate();init(mhs);}
mod = tahun%4;
if(bln==2 && mod==0 && tgl>29){
errorMessageDate();init(mhs);}
if(bln==2 && tgl>28){
errorMessageDate();init(mhs);}
if((bln==2||bln==4||bln==6||bln==9||bln==11) && tgl>30){
errorMessageDate();init(mhs);
}
}
void inputData(Mahasiswa mhs[MAX]){
clrscr();
char YN;
char name[45];
char place[45];
char NIM[20];
char tmpTgl[45];
char tmpBln[45];
char tmpTahun[45];
char IPre[10];
for(int a=limitArray;a<=MAX;a++){
clrscr();
if(a==MAX) {
clrscr();
cout<<"Storage Full, Unable to Input More Data(s)";
getch();
break;
}
cout<<"Nama: ";
gets(name);
strcpy(mhs[a].nama,name);
cout<<"NIM: ";
cin>>NIM;
strcpy(mhs[a].nim,NIM);
cout<<"Tempat Lahir: ";
gets(place);
strcpy(mhs[a].tglLahir.tempat,place);
cout<<"Tanggal: ";
fgets(tmpTgl, sizeof tmpTgl, stdin);
validateInt(tmpTgl,1,mhs);
mhs[a].tglLahir.tgl = tglInt;
cout<<"Bulan: ";
fgets(tmpBln, sizeof tmpBln, stdin);
validateInt(tmpBln,2,mhs);
mhs[a].tglLahir.bulan = blnInt;
cout<<"Tahun: ";
fgets(tmpTahun, sizeof tmpTahun, stdin);
validateInt(tmpTahun,3,mhs);
mhs[a].tglLahir.tahun = thnInt;
validateDate(tglInt,blnInt,thnInt,mhs);
cout<<"IP: ";
fgets(IPre, sizeof IPre, stdin);
validateIP(IPre,mhs);
mhs[a].IP = InPre;
cout<<"Masukkan Data?(Y/N)";
cin>>YN;
if(YN=='Y'||YN=='y'){
limitArray++;
continue;
}else{
limitArray++;
break;
}
}
}
void cetakData(Mahasiswa mhs[MAX]){
clrscr();
cout<<"----------------------------------------------------------------\n";
for(int a=0;a<limitArray;a++){
cout<<"No. :"<<(a+1)<<endl;
cout<<"Nama:"<<mhs[a].nama<<endl;
cout<<"NIM :"<<mhs[a].nim<<endl;
cout<<"TTL :"<<mhs[a].tglLahir.tempat<<", "<<mhs[a].tglLahir.tgl<<"\\"<<mhs[a].tglLahir.bulan<<"\\"<<mhs[a].tglLahir.tahun<<endl;;
cout<<"IP : "<<mhs[a].IP<<endl;
cout<<"----------------------------------------------------------------\n";
}
getch();
}
void editData(Mahasiswa mhs[MAX]){
int noRecord;
int field;
int a;
char name[45];
char place[45];
char NIM[20];
char tmpTgl[45];
char tmpBln[45];
char tmpTahun[45];
char IPre[10];
while(1){
cetakData(mhs);
cout<<"\nPilih No.ReCord/Keluar=0: ";
cin>>noRecord;
if(noRecord==0){
init(mhs);
}else if((noRecord-1)>limitArray){
init(mhs);
}
a = noRecord-1;
cout<<"Pilih Field(Nama=1,Nim=2,Tempat=3,Tanggal=4,Bulan=5,Tahun=6,IP=7): ";
cin>>field;
cout<<"\n";
if(field==1){
cout<<"Nama: ";
gets(name);
strcpy(mhs[a].nama,name);
editData(mhs);
}
else if(field==2){
cout<<"NIM: ";
cin>>NIM;
strcpy(mhs[a].nim,NIM);
editData(mhs);
}
else if(field==3){
cout<<"Tempat Lahir: ";
gets(place);
strcpy(mhs[a].tglLahir.tempat,place);
editData(mhs);
}
else if(field==4){
cout<<"Tanggal: ";
fgets(tmpTgl, sizeof tmpTgl, stdin);
validateInt(tmpTgl,1,mhs);
mhs[a].tglLahir.tgl = tglInt;
editData(mhs);
}
else if(field==5){
cout<<"Bulan: ";
fgets(tmpBln, sizeof tmpBln, stdin);
validateInt(tmpBln,2,mhs);
mhs[a].tglLahir.bulan = blnInt;
editData(mhs);
}
else if(field==6){
cout<<"Tahun: ";
fgets(tmpTahun, sizeof tmpTahun, stdin);
validateInt(tmpTahun,3,mhs);
mhs[a].tglLahir.tahun = thnInt;
editData(mhs);
}
else if(field==7){
cout<<"IP: ";
fgets(IPre, sizeof IPre, stdin);
validateIP(IPre,mhs);
mhs[a].IP = InPre;
editData(mhs);
}
else if(field==8)
init(mhs);
else{
cout<<"Wrong Input!";
editData(mhs);
}
}
}
void cariData(Mahasiswa mhs[MAX]){
int opt;
int indeks=0;
clrscr();
cout<<"Pilih Kriteria Pencarian\n";
cout<<"[1]Nama\n";
cout<<"[2]TempatLahir\n";
cout<<"[3]NIM\n";
cout<<"Pilihan?";
cin>>opt;
extrakData(opt,mhs);
while(indeks<counterData){
cout<<"----------------------------------------------------------------\n";
cout<<"Nama:"<<mhs[find[indeks]].nama<<endl;
cout<<"NIM :"<<mhs[find[indeks]].nim<<endl;
cout<<"TTL :"<<mhs[find[indeks]].tglLahir.tempat<<", "<<mhs[find[indeks]].tglLahir.tgl<<"\\"<<mhs[find[indeks]].tglLahir.bulan<<"\\"<<mhs[find[indeks]].tglLahir.tahun<<endl;;
cout<<"IP : "<<mhs[find[indeks]].IP<<endl;
cout<<"----------------------------------------------------------------\n";
indeks++;
}
counterData=0; //reset global variabel
getch();
}
void init(Mahasiswa mhs[MAX]){
char opt[1];
int counter=limitArray;
while(1){
GUI();
//opt = getch();
gets(opt);
switch(opt[0]){
case '1' :
inputData(mhs);
valid=1;
break;
case '2' :
if(valid==0)
init(mhs);
cetakData(mhs);
break;
case '3' :
if(valid==0)
init(mhs);
editData(mhs);
break;
case '4' :
if(valid==0)
init(mhs);
cariData(mhs);
break;
case '5' :
_exit(0);
break;
default :
_exit(0);
break;
}
counter++;
}
}
void main(){
Mahasiswa mhs[MAX];
init(mhs);
}
void errorMessage(){
clrscr();
cout<<"Wrong Input";
delay(100);
}
void errorMessageDate(){
clrscr();
cout<<"Error Date Input";
delay(100);
}
void extrakData(int field,Mahasiswa mhs[MAX]){
//int value[10];
char input[45];
char *output;
if(field==1){
clrscr();
cout<<"Nama?";
gets(input);
for(int i=0;i<limitArray;i++){
output = strstr(mhs[i].nama,input);
if(output!=NULL){
find[counterData] = i;
counterData++;
}else
continue;
}
}
else if(field==2){
clrscr();
cout<<"Tempat Lahir?";
gets(input);
for(int i=0;i<limitArray;i++){
output = strstr(mhs[i].tglLahir.tempat,input);
if(output!=NULL){
find[counterData] = i;
counterData++;
}else
continue;
}
}
else if(field==3){
clrscr();
cout<<"NIM?";
gets(input);
for(int i=0;i<limitArray;i++){
output = strstr(mhs[i].nim,input);
if(output!=NULL){
find[counterData] = i;
counterData++;
}else
continue;
}
}
else{
cariData(mhs);
}
}