Last active
August 29, 2015 14:26
-
-
Save TARA24Kevin/73cd037b7ae23b2d49a7 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 clean(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 curata_teava(void){ | |
int c; | |
while((c = getchar()) != 0 && c != '\n'); | |
} | |
int citesteNumar(int min, int max){ | |
int numar; | |
char c; | |
do{ | |
printf("\n\n"); | |
printf(KCYN"Alege o Optiune :\t"RESET); | |
if (scanf("%d%c", &numar, &c) == 0 || c != '\n'){ | |
curata_teava(); | |
printf(KRED"\n\n\t\t\tTasteza un numar intre "RESET KYEL"%d"RESET KRED" si "RESET KYEL"%d\n\n"RESET,min, max); | |
}else if(numar < min || numar > max){ | |
printf("\n\n\t\t\tTasteza un numar intre %d-%d\n\n",min, max); | |
}else{ | |
break; | |
} | |
}while(TRUE); | |
return numar; | |
} | |
void citesteString(char nume[], int optiune){ | |
long int verificaNume; | |
do{ | |
if(optiune == 1){ | |
printf(KCYN"\nIntrodu Numele:\t"RESET); | |
if(scanf("%s",nume) != EOF) | |
curata_teava(); | |
} | |
if(optiune == 2){ | |
printf(KCYN"\nIntrodu Prenumele:\t"RESET); | |
if(scanf("%s",nume) != EOF) | |
curata_teava(); | |
} | |
if(optiune == 3){ | |
printf(KCYN"\nIntrodu Numele Strazii:\t"RESET); | |
if(scanf("%s",nume) != EOF) | |
curata_teava(); | |
} | |
if(optiune == 4){ | |
printf(KCYN"\nIntrodu Numarul de Telefon:\t"RESET); | |
if(scanf("%s",nume) != EOF) | |
curata_teava(); | |
} | |
if ((verificaNume = strtol(nume, NULL, 10))){ | |
printf(KRED "\n\n\t\tTe rog sa tastezi nu Nume Real\n" RESET "\n"); | |
} | |
}while(verificaNume != 0); | |
} | |
int meniuPrincipal(void){ | |
int optiune; | |
printf(KGRN"\n\n\n\t\tProgram creere Baze de Date"RESET); | |
printf("\n\n"); | |
printf(KCYN"1) - Creaaza un Record nou in Baza de Date\n"RESET); | |
printf(KCYN"2) - Cauta un record in Baza de Date\n"RESET); | |
printf(KCYN"3) - Inchide Baza de Date\n"RESET); | |
optiune = citesteNumar(1,3); | |
return optiune; | |
} | |
int meniuSecundar(void){ | |
int optiune; | |
clean(); | |
printf(KGRN"\n\n\t\tCautare record in Baza de Date\n"RESET); | |
printf(KCYN"1) - Cauta un record in baza de date (Nume,Telefon,Adresa)\n"RESET); | |
printf(KCYN"2) - Inapoi la Meniul Principal\n"RESET); | |
printf(KCYN"3) - Inchide Baza de Date\n"RESET); | |
printf("\n\n"); | |
optiune = citesteNumar(1,3); | |
return optiune; | |
} | |
void seteazaRecord(void){ | |
char *dataBase = "databasee.txt"; | |
FILE* fisier = fopen(dataBase, "r"); | |
clean(); | |
printf(KGRN"\n\n\t\tAi ales creaza nou record\n"RESET); | |
printf("\n\n"); | |
citesteString(db.firstName,1); | |
citesteString(db.lastName,2); | |
printf(KCYN"\nIntrodu anul Nasterii:\t"RESET); | |
if(scanf("%d",&db.born) != EOF) | |
printf(KCYN"\nIntrodu numarul de Telefon:\t"RESET); | |
if(scanf("%s",db.phoneNumber) != EOF) | |
citesteString(db.streetName,3); | |
printf(KCYN"\nIntrodu numarul Strazii:\t"RESET); | |
if(scanf("%d",&db.streetNumber) != EOF) | |
fisier=fopen("database.txt","a+"); | |
fprintf(fisier,"\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(fisier); | |
printf(KYEL"\n\n\n\t\t\tRecordul sa adaugat cu Succes\n"RESET); | |
sleep(3); | |
} | |
void arataRecord(void){ | |
printf(KNRM"\n\t\tRecord\t%d\n\n"RESET,db.record++); | |
printf(KGRN"Nume:"RESET KYEL"\t\t\t%s\n"RESET, db.firstName); | |
printf(KGRN"Prenume:"RESET KYEL"\t\t%s\n"RESET, db.lastName); | |
printf(KGRN"Nascut:"RESET KYEL"\t\t\t%d\n", db.born); | |
printf("\n"); | |
printf(KGRN"Telefon:"RESET KYEL"\t\t%s\n"RESET, db.phoneNumber); | |
printf("\n"); | |
printf(KGRN"Strada:"RESET KYEL"\t\t\t%s\n"RESET, db.streetName); | |
printf(KGRN"Numar:"RESET KYEL"\t\t\t%d\n"RESET, db.streetNumber); | |
printf("---------------------------------------\n\n"); | |
} | |
void niciUnRecordGasit(char record[]){ | |
printf(KRED "\n\t\t\tNu exista nici un record cu numele\t"RESET KYEL "%s\n"RESET , record); | |
} | |
void cautaRecord(char string[]){ | |
char *numeFisier = "database.txt"; | |
int check = 0; | |
FILE *fisier; | |
sleep(2); | |
if ((fisier = fopen(numeFisier, "r")) == NULL) { | |
printf(KRED "\n\t\t\tEroare: fisierul"RESET KYEL "\t%s\t"RESET KRED"nu exista\n"RESET , numeFisier); | |
exit(1); | |
} | |
while( ( fscanf(fisier," %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){ | |
arataRecord(); | |
check = TRUE; | |
}else if(strcasecmp(string,db.lastName)==0){ | |
arataRecord(); | |
check = TRUE; | |
}else if(strcasecmp(string,db.streetName)==0){ | |
arataRecord(); | |
check = TRUE; | |
}else if(strcasecmp(string,db.phoneNumber)==0){ | |
arataRecord(); | |
check = TRUE; | |
} | |
} | |
if(check == FALSE) { | |
niciUnRecordGasit(string); | |
} | |
fclose(fisier); | |
} | |
void verificaRecord(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) | |
cautaRecord(name); | |
} | |
int main(void){ | |
int optiune; | |
do{ | |
optiune = meniuPrincipal(); | |
switch(optiune){ | |
case 1:{ | |
seteazaRecord(); | |
break; | |
} | |
case 2:{ | |
optiune = meniuSecundar(); | |
switch(optiune){ | |
case 1:{ | |
verificaRecord(); | |
break; | |
} | |
case 2:{ | |
break; | |
} | |
case 3:{ | |
printf("\n\n\n\t\t\tLa revedere\n\n\n"); | |
break; | |
} | |
} | |
break; | |
} | |
case 3:{ | |
printf("\n\n\n\t\t\tLa revedere\n\n\n"); | |
break; | |
} | |
} | |
}while(optiune != 3); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment