Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include<stdio.h>
#include<string.h>
#include<strings.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 BazaDate{
char fisier[20];
int record;
}bd;
struct Agenda{
char firstName[20];
char lastName[20];
int born;
char phoneNumber[13];
char streetName[20];
int streetNumber;
}agenda;
void clean( void );
void curata_teava( void );
int citesteNumar(int min, int max);
void citesteString(char nume[], int optiune);
int meniuPrincipal( void );
int meniuSecundar( void );
void seteazaRecord( void );
void arataRecord( void );
void niciUnRecordGasit(char record[]);
void cautaRecord(char string[]);
void verificaRecord( void );
void afiseazaFisier( void );
void iesire( void );
int main( void ){
int optiune1,optiune2;
strcpy(bd.fisier, "database.txt");
do{
optiune1 = meniuPrincipal();
switch(optiune1){
case 1:{
seteazaRecord();
break;
}
case 2:{
do{
optiune2 = meniuSecundar();
switch(optiune2){
case 1:{
afiseazaFisier();
break;
}
case 2:{
verificaRecord();
break;
}
case 3:{
break;
}
case 4:{
iesire();
return 0;
}
break;
}
}while(optiune2 == 1 || optiune2 == 2);
break;
}
case 3:{
iesire();
break;
}
}
}while(optiune1 != 3);
return 0;
}
void clean( void ){
int x;
for ( x = 0; x < 1; x++ ){
printf("\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(KRED"\n\n\t\t\tTasteza un numar intre "RESET KYEL"%d"RESET KRED" si "RESET KYEL"%d\n\n"RESET,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);
scanf("%s",nume);
curata_teava();
}
if(optiune == 2){
printf(KCYN"\nIntrodu Prenumele:\t"RESET);
scanf("%s",nume);
curata_teava();
}
if(optiune == 3){
printf(KCYN"\nIntrodu Numele Strazii:\t"RESET);
scanf("%s",nume);
curata_teava();
}
if(optiune == 4){
printf(KCYN"\nIntrodu Numarul de Telefon:\t"RESET);
scanf("%s",nume);
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;
clean();
printf(KGRN"\n\n\n\t\tProgram creere Baza de Date"RESET);
printf("\n\n");
printf(KCYN"1) - Creaza o inregistrare in Baza de Date\n"RESET);
printf(KCYN"2) - Cauta o inregistrare 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;
printf(KGRN"\n\n\t\tCautare inregistrare in Baza de Date\n"RESET);
printf(KCYN"1) - Afiseaza toate Inregistrarile\n"RESET);
printf(KCYN"2) - Cauta o inregistrare in baza de date (Nume, Strada, Telefon)\n"RESET);
printf(KCYN"3) - Meniul Principal\n"RESET);
printf(KCYN"4) - Inchide Baza de Date\n"RESET);
printf("\n\n");
optiune = citesteNumar(1,4);
return optiune;
}
void seteazaRecord( void ){
FILE* fisier = fopen(bd.fisier, "r");
clean();
printf(KGRN"\n\n\t\tAi ales creaza o noua Inregistrare\n"RESET);
printf("\n\n");
citesteString(agenda.firstName,1);
citesteString(agenda.lastName,2);
printf(KCYN"\nIntrodu anul Nasterii:\t"RESET);
scanf("%d",&agenda.born);
printf(KCYN"\nIntrodu numarul de Telefon:\t"RESET);
scanf("%s",agenda.phoneNumber);
citesteString(agenda.streetName,3);
printf(KCYN"\nIntrodu numarul Strazii:\t"RESET);
scanf("%d",&agenda.streetNumber);
fisier=fopen("database.txt","a+");
fprintf(fisier,"\n%s\t%s\t%d\t%s\t%s\t%d\n",agenda.firstName, agenda.lastName, agenda.born, agenda.phoneNumber, agenda.streetName, agenda.streetNumber);
fclose(fisier);
printf(KYEL"\n\n\n\t\t\tInregistrarea sa adaugat cu Succes\n"RESET);
sleep(3);
}
void arataRecord( void ){
printf(KNRM"\n\t\tRecord\t%d\n\n"RESET,bd.record++);
printf(KGRN"Nume:"RESET KYEL"\t\t\t%s\n"RESET, agenda.firstName);
printf(KGRN"Prenume:"RESET KYEL"\t\t%s\n"RESET, agenda.lastName);
printf(KGRN"Nascut:"RESET KYEL"\t\t\t%d\n", agenda.born);
printf("\n");
printf(KGRN"Telefon:"RESET KYEL"\t\t%s\n"RESET, agenda.phoneNumber);
printf("\n");
printf(KGRN"Strada:"RESET KYEL"\t\t\t%s\n"RESET, agenda.streetName);
printf(KGRN"Numar:"RESET KYEL"\t\t\t%d\n"RESET, agenda.streetNumber);
printf("---------------------------------------\n\n");
}
void niciUnRecordGasit(char record[]){
printf(KRED "\n\t\t\tNu exista nici o Inregistrare cu numele\t"RESET KYEL "%s\n"RESET , record);
}
void cautaRecord(char string[]){
int check = 0;
FILE *fisier;
sleep(2);
if ((fisier = fopen(bd.fisier, "r")) == NULL) {
printf(KRED "\n\t\t\tEroare: fisierul"RESET KYEL "\t%s\t"RESET KRED"nu exista\n"RESET , bd.fisier);
exit(1);
}
while( ( fscanf(fisier," %19s %19s %d %19s %19s %d",agenda.firstName,agenda.lastName,&agenda.born,agenda.phoneNumber,agenda.streetName,&agenda.streetNumber)) == 6) {
if(strcasecmp(string,agenda.firstName)==0){
arataRecord();
check = TRUE;
}else if(strcasecmp(string,agenda.lastName)==0){
arataRecord();
check = TRUE;
}else if(strcasecmp(string,agenda.streetName)==0){
arataRecord();
check = TRUE;
}else if(strcasecmp(string,agenda.phoneNumber)==0){
arataRecord();
check = TRUE;
}
}
if(check == FALSE) {
niciUnRecordGasit(string);
}
fclose(fisier);
}
void verificaRecord( void ){
char name[20];
bd.record=1;
printf(KCYN"\n\n\n\tTasteaza o inregistrare ("RESET KYEL"Nume, Strada, Telefon"RESET KCYN") pentru a cauta:\t"RESET);
scanf("%19s", name);
cautaRecord(name);
}
void afiseazaFisier( void ){
long unsigned int lungime;
char *buffer;
long unsigned int rezultat;
FILE *fisier;
if ((fisier = fopen(bd.fisier, "r")) == NULL) {
printf(KRED "\n\t\t\tEroare: fisierul"RESET KYEL "\t%s\t"RESET KRED"nu exista\n"RESET , bd.fisier);
exit(1);
}
fseek (fisier , 0 , SEEK_END);
lungime = ( long unsigned int )ftell (fisier);
rewind (fisier);
buffer = malloc (sizeof(char) * lungime);
if (buffer == NULL){
fputs ("Memory error",stderr);
exit (2);
}
rezultat = fread (buffer,1,lungime,fisier);
if (rezultat != lungime){
fputs ("Reading error",stderr);
exit(3);
}
printf("%s",buffer);
fclose (fisier);
free (buffer);
}
void iesire( void ){
printf("\n\n\n\t\t\tLa revedere\n\n\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment