All pastes #1825071 Raw Edit

baza.cpp

public cpp v1 · immutable
#1825071 ·published 2010-03-06 12:05 UTC
rendered paste body
//#include <windows.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h>#include <unistd.h>//#define DEF_FNAME "studenci.bin"static char _Fname[32] = "studenci.bin";static int _SortOrder = 1;struct STUD{	int  nr;	int  unused;	char nazw[32];	char imie[32];	short wiek;	short dur_r;	short dur_m;	short dur_d;	double forsa;};int WyliczWiek(int rok){	struct tm *local;	time_t today;	today = (time(0)/60 /60 /24 /365) + 1970;	return today - rok;}void GetStud(STUD* p){	if( !p ) return;	memset(p, 0, sizeof(STUD));	//nullujemy	char buf[1024];	printf("Nazwisko: ");	scanf("%s", buf);	buf[31] = 0;	//obcinamy przy 32 bicie	strcpy(p->nazw, buf);	//to co w buforze do p->nazw	printf("Imie: ");	scanf("%s", buf);	buf[31] = 0;	strcpy(p->imie, buf);	printf("Data urodzenia (rrrr-mm-dd): ");	scanf("%d-%d-%d", &(p->dur_r), &(p->dur_m), &(p->dur_d));	printf("Forsa: ");	scanf("%lf", &(p->forsa));}void DisplayStud(STUD* p, char* buf)	{	if( !p ) return;	p->wiek = WyliczWiek(p->dur_r);	if( buf ) sprintf(buf, "%d. %s %s, %d, %04d-%02d-%02d, %.2lf", p->nr, p->nazw, p->imie, p->wiek, p->dur_r, p->dur_m, p->dur_d, p->forsa);	else printf("%d. %s %s, %d, %04d-%02d-%02d, %.2lf", p->nr, p->nazw, p->imie, p->wiek, p->dur_r, p->dur_m, p->dur_d, p->forsa);}int Add(STUD* p){	if( !p ) return 1;	FILE* f = fopen(_Fname, "a+b");	if(!f) return 1;	fseek(f, 0, SEEK_END);	//0B do SEEK_END	int pos = ftell(f);	//printf("pos = %d\n", pos);	//return 0;	p->nr = 1 + (pos/sizeof(STUD));	int n = fwrite(p, sizeof(STUD), 1, f);	fclose(f);	return (n==1);}int Get(STUD* p, int nr){	if( !p || nr <= 0 ) return 1;	FILE* f = fopen(_Fname, "rb");	if(!f) return 1;	fseek(f, (nr-1)*sizeof(STUD), SEEK_SET);	//tyle B wzgledem poczatku (SEEK_SET)	unsigned int pos = ftell(f);	if( pos != (nr-1)*sizeof(STUD))	{		fclose(f);		return 1;	}	int n = fread(p, sizeof(STUD), 1, f);	fclose(f);	return (n==1);}int Put(STUD* p, int nr){	if( !p || nr <= 0 ) return 1;	FILE* f = fopen(_Fname, "r+b");	if(!f) return 1;	fseek(f, (nr-1)*sizeof(STUD), SEEK_SET);	unsigned int pos = ftell(f);	if( pos != (nr-1)*sizeof(STUD) )	{		fclose(f);		return 1;	}	p->nr = nr;	int n = fwrite(p, sizeof(STUD), 1, f);	fclose(f);	return (n==1);}int Load(STUD** pp){	if( !pp ) return 1;	*pp = 0;	FILE* f = fopen(_Fname, "rb");	if(!f) return 1;	fseek(f, 0, SEEK_END);	unsigned int nb = ftell(f);	fseek(f, 0, SEEK_SET);	if(nb % sizeof(STUD))	{		printf("Wadliwy rozmiar bazy ...");		fclose(f);		return 1;	}	int n = nb / sizeof(STUD);	if( n == 0 )	{		fclose(f);		return 1;	}	*pp = (STUD*)malloc(nb);	if( *pp == 0 )	{		fclose(f);		return 1;	}	int nn = fread(*pp, sizeof(STUD), n, f);	fclose(f);	if( nn != n )	{		free(*pp);		*pp = 0;		return 1;	}	return n;}int fpor(const void* p1, const void* p2){	STUD* s1 = (STUD*)p1;	STUD* s2 = (STUD*)p2;	s1->wiek = WyliczWiek(s1->dur_r);	s2->wiek = WyliczWiek(s2->dur_r);if (_SortOrder == 1)        {		if (s1->nr > s2->nr) return 0;		else if (s1->nr < s2->nr) return -1;		else return 1;        }else if (_SortOrder == 2) return strcmp(s1->nazw, s2 -> nazw);else if (_SortOrder == 3) return strcmp(s1->imie, s2 -> imie);else if (_SortOrder == 4)        {		if (s1->wiek > s2->wiek) return 0;        	else if (s1->wiek < s2->wiek) return -1;		else return 1;        }else if (_SortOrder == 5)        {	if((s1-> dur_r > s2->dur_r) ||		(s1->dur_r == s2->dur_r && s1-> dur_m > s2-> dur_m) ||		(s1->dur_r == s2->dur_r && s1 ->dur_m == s2->dur_m && s1->dur_d > s2->dur_d)) return 0;	if((s2-> dur_r > s1->dur_r) ||		(s2->dur_r == s1->dur_r && s2-> dur_m > s1-> dur_m) ||		(s2->dur_r == s1->dur_r && s1 ->dur_m == s2->dur_m && s2->dur_d > s1->dur_d)) return -1;		else return 1;        }else if (_SortOrder == 6)        {		if (s1->forsa > s2->forsa) return 0;		else if (s1->forsa < s2->forsa) return -1;		else return 1;	}else return 1;}void Sort (STUD* p, int n){	if (!p || !n) return;	qsort (p,n, sizeof(STUD), fpor);}int main(){	printf("Baza studentow.\n");	int Opcja = 0;	while(1)	{		printf("\nDostepne opcje: \n"		"1 - Dodaj,\t\t2 - Odczyt/Popraw,\t3 - Porzadek sortowania,\n"		"4 - Wyswietl na ekran,\t5 - Do pliku,\t\t6 - Wyczysc,\t7 - Koniec\n");		printf("\nWybierz opcje: ");		scanf("%d", &Opcja);		if( Opcja < 1 || Opcja > 7 ) { printf("Wadliwa opcja ..."); continue; }		if( Opcja == 1 )		{			printf("Dopisanie do bazy\n\n");			STUD s;			GetStud(&s);			Add(&s);		}		else if( Opcja == 2 )		{			printf("Odczyt / Zmiana danych\n\n");			printf("Podaj numer: ");			int nr;			scanf("%d", &nr);			STUD s;			int ret = Get(&s, nr);			if( !ret )			{				printf("Brak studenta o nr = %d ...", nr);				continue;			}			DisplayStud(&s, 0);			printf("\nZmiana danych ? (t/n): ");			char bb[32] = "";			scanf("%s", bb);			if( bb[0] != 't' ) continue;			GetStud(&s);			Put(&s, nr);		}		else if( Opcja == 3 )		{			printf("Porzadek sortowania\n\n");			printf("Wybierz (1-nr, 2-nazw, 3-imie, 4-wiek, 5-data_ur, 6-forsa): ");			scanf("%d", &_SortOrder);			if (_SortOrder < 1 || _SortOrder > 6) _SortOrder = 1;		}		else if( Opcja == 4 )		{			printf("Wyswietlenie bazy na ekran\n\n");			STUD* p = 0;			int n = Load(&p);			if( !n || !p ) continue;			Sort (p,n);			for(int i=0; i<n; i++)			{				DisplayStud(p+i, 0);				putchar('\n');			}			if( p ) free(p); //wystarczyloby samo free(p), if(p) zbedny		}		else if( Opcja == 5 )		{			printf("Zapis bazy do pliku tekstowego\n\n");			printf("Nazwa pliku: ");			char fname[100] = "";			scanf("%s", fname);			FILE* f = fopen (fname, "wt");			if (!f) { printf ("Nie mozna utworzyc pliku: %s...", fname); continue; }			STUD* p = 0;			int n = Load(&p);			if( !n || !p ) continue;			Sort (p,n);			for(int i=0; i<n; i++)			{				char buf[1024];				DisplayStud(p+i, buf);				fprintf(f,"%s\n",buf);			}			if( p ) free(p);			fclose (f);		}		else if( Opcja == 6 )		{			printf("Wyczyszczenie bazy\n\n");			printf("Baza zostanie usunieta. Jestes pewien ? (t/n): ");			char bb[32] = "";			scanf("%s", bb);			if( bb[0] != 't' ) continue;			unlink(_Fname);		}		else if( Opcja == 7 ) break;	}}