Last active
June 29, 2018 12:15
-
-
Save TARA24Kevin/c21f25bf4dac2cbf1a7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<strings.h> | |
#include<string.h> | |
#include<stdlib.h> | |
#include<unistd.h> | |
#define KCYN "\x1B[36m" | |
#define KGRN "\x1B[32m" | |
#define KNRM "\x1B[0m" | |
#define KRED "\x1B[31m" | |
#define KYEL "\x1B[33m" | |
#define RESET "\033[0m" | |
#define TRUE 1 | |
#define FALSE 0 | |
struct DataBase{ | |
char firstName[20]; | |
char lastName[20]; | |
int born; | |
char phoneNumber[13]; | |
char streetName[20]; | |
int streetNumber; | |
int record; | |
}db; | |
void cleanSCR(void); | |
void cleanSTD(void); | |
int getNumb(int min, int max); | |
void getSTR(char *name, int option ); | |
int mainMenu(void); | |
int secondMenu(void); | |
void setRecord(void); | |
void showRecord(void); | |
void noRecordFound(char record[]); | |
void searchRecord(char *string); | |
void checkRecord(void); | |
int main(void){ | |
int option; | |
do{ | |
option = mainMenu(); | |
switch(option){ | |
case 1:{ | |
setRecord(); | |
break; | |
} | |
case 2:{ | |
option = secondMenu(); | |
switch(option){ | |
case 1:{ | |
checkRecord(); | |
break; | |
} | |
case 2:{ | |
break; | |
} | |
case 3:{ | |
printf("\n\n\n\t\t\tGood Bye\n\n\n"); | |
break; | |
} | |
} | |
break; | |
} | |
case 3:{ | |
printf("\n\n\n\t\t\tGood Bye\n\n\n"); | |
break; | |
} | |
} | |
}while(option != 3); | |
return 0; | |
} | |
int mainMenu(void){ | |
int option; | |
printf(KGRN"\n\n\n\t\tCreate a DataBase"RESET); | |
printf("\n\n"); | |
printf(KCYN"1) - Create new Record\n"RESET); | |
printf(KCYN"2) - Seearch a Record\n"RESET); | |
printf(KCYN"3) - Exit Database\n"RESET); | |
option = getNumb(1,3); | |
return option; | |
} | |
int secondMenu(void){ | |
int option; | |
cleanSCR(); | |
printf(KGRN"\n\n\t\tSearch record in Database\n"RESET); | |
printf(KCYN"1) - Search Record (Name,Phone,Adress)\n"RESET); | |
printf(KCYN"2) - Back to main manu\n"RESET); | |
printf(KCYN"3) - Exit Database\n"RESET); | |
printf("\n\n"); | |
option = getNumb(1,3); | |
return option; | |
} | |
void cleanSCR(void){ | |
int x; | |
for ( x = 0; x < 10; x++ ){ | |
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); | |
} | |
} | |
void cleanSTD(void){ | |
int c; | |
while((c = getchar()) != 0 && c != '\n'); | |
} | |
int getNumb(int min, int max){ | |
int num; | |
char c; | |
do{ | |
printf("\n\n"); | |
printf(KCYN"Choose an Option :\t"RESET); | |
if (scanf("%d%c", &num, &c) == 0 || c != '\n'){ | |
cleanSTD( ); | |
printf(KRED"\n\n\t\t\tType a Number between "RESET KYEL"%d"RESET KRED" si "RESET KYEL"%d\n\n"RESET,min, max); | |
}else if(num < min || num > max){ | |
printf("\n\n\t\t\tType a Number between %d-%d\n\n",min, max); | |
}else{ | |
break; | |
} | |
}while(TRUE); | |
return num; | |
} | |
void getSTR(char *name, int option){ | |
long int checkName; | |
do{ | |
if(option == 1){ | |
printf(KCYN"\nGive firstname:\t"RESET); | |
if(scanf("%s",name) != EOF) | |
cleanSTD(); | |
} | |
if(option == 2){ | |
printf(KCYN"\nGive lastname:\t"RESET); | |
if(scanf("%s",name) != EOF) | |
cleanSTD(); | |
} | |
if(option == 3){ | |
printf(KCYN"\nGive street:\t"RESET); | |
if(scanf("%s",name) != EOF) | |
cleanSTD(); | |
} | |
if(option == 4){ | |
printf(KCYN"\nIGive phone:\t"RESET); | |
if(scanf("%s",name) != EOF) | |
cleanSTD(); | |
} | |
if ((checkName = strtol(name, NULL, 10))){ | |
printf(KRED "\n\n\t\tPlease type a real Number\n" RESET "\n"); | |
} | |
}while(checkName != 0); | |
} | |
void showRecord(void){ | |
printf(KNRM"\n\t\tRecord\t%d\n\n"RESET,db.record++); | |
printf(KGRN"Firstname:"RESET KYEL"\t\t\t%s\n"RESET, db.firstName); | |
printf(KGRN"Lastname:"RESET KYEL"\t\t%s\n"RESET, db.lastName); | |
printf(KGRN"Born:"RESET KYEL"\t\t\t%d\n", db.born); | |
printf("\n"); | |
printf(KGRN"Phone:"RESET KYEL"\t\t%s\n"RESET, db.phoneNumber); | |
printf("\n"); | |
printf(KGRN"Street:"RESET KYEL"\t\t\t%s\n"RESET, db.streetName); | |
printf(KGRN"Number:"RESET KYEL"\t\t\t%d\n"RESET, db.streetNumber); | |
printf("---------------------------------------\n\n"); | |
} | |
void setRecord(void){ | |
const char *dataBase = "databasee.txt"; | |
FILE *fileF = fopen("scores.dat", "ab+"); | |
if ( fileF == NULL ) { | |
printf(KRED "\n\t\t\tError: the file"RESET KYEL "\t%s\t"RESET KRED"does not exist\n"RESET , dataBase); | |
exit(1); | |
} | |
cleanSCR(); | |
printf(KGRN"\n\n\t\tYou choose create Database\n"RESET); | |
printf("\n\n"); | |
getSTR(db.firstName,1); | |
getSTR(db.lastName,2); | |
printf(KCYN"\nType Birthday:\t"RESET); | |
if( scanf("%d",&db.born) != 1 ){ | |
printf("Error, scanf()\n"); | |
exit( 1 ); | |
} | |
printf(KCYN"\nType phone number:\t"RESET); | |
if(scanf("%s",db.phoneNumber) != 1){ | |
printf("Error, scanf()\n"); | |
exit( 1 ); | |
} | |
getSTR(db.streetName,3); | |
printf(KCYN"\nType Number:\t"RESET); | |
if(scanf("%d",&db.streetNumber) != 1){ | |
printf("Error, scanf()\n"); | |
exit( 1 ); | |
} | |
fileF=fopen("database.txt","a+"); | |
fprintf(fileF,"\n%s\t%s\t%d\t%s\t%s\t%d",db.firstName, db.lastName, db.born, db.phoneNumber, db.streetName, db.streetNumber); | |
fclose(fileF); | |
printf(KYEL"\n\n\n\t\t\tThe new Record was succesfully Added\n"RESET); | |
sleep(3); | |
} | |
void noRecordFound(char record[]){ | |
printf(KRED "\n\t\t\tNo Record with name\t"RESET KYEL "%s was Found.\n"RESET , record); | |
} | |
void searchRecord(char *string){ | |
const char *fileName = "database.txt"; | |
int check = 0; | |
FILE *fileF; | |
sleep(2); | |
if ((fileF = fopen(fileName, "r")) == NULL) { | |
printf(KRED "\n\t\t\tError: the file"RESET KYEL "\t%s\t"RESET KRED"does not exist\n"RESET , fileName); | |
exit(1); | |
} | |
while( ( fscanf(fileF," %19s %19s %d %19s %19s %d",db.firstName,db.lastName,&db.born,db.phoneNumber,db.streetName,&db.streetNumber)) == 6) { | |
if(strcasecmp(string,db.firstName)==0){ | |
showRecord(); | |
check = TRUE; | |
}else if(strcasecmp(string,db.lastName)==0){ | |
showRecord(); | |
check = TRUE; | |
}else if(strcasecmp(string,db.streetName)==0){ | |
showRecord(); | |
check = TRUE; | |
}else if(strcasecmp(string,db.phoneNumber)==0){ | |
showRecord(); | |
check = TRUE; | |
} | |
} | |
if(check == FALSE) { | |
noRecordFound(string); | |
} | |
fclose(fileF); | |
} | |
void checkRecord(void){ | |
char name[20]; | |
db.record=1; | |
printf(KCYN"\n\n\n\tTasteaza un record ("RESET KYEL"Nume, Strada, Telefon"RESET KCYN") pentru a cauta:\t"RESET); | |
if(scanf("%19s", name) != EOF) | |
searchRecord(name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment