#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

struct CD
    {
    char imie[20];
    char nazwisko[20];
    char data[10];
    char adres[30];
    int numer;
    };

main()
{

CD t[5],c;
char pow;
int i,j,g,cos,level,a;
int ile = 0;
do
{
clrscr();
printf("\n1 - Dodaj rekord");
printf("\n2 - Wyszukaj rekord");
printf("\n3 - Wyswietl wedlug nazwiska");
printf("\n4 - Wyswietl wszystkie rekordy");
printf("\n5 - Usun rekord");
printf("\n6 - Usun wszystkie rekordy\n");
scanf("%d",&cos);
if (cos == 1)
 level = '1';
else if (cos == 2)
 level = '2';
else if (cos == 3)
 level = '3';
else if (cos == 4)
 level = '4';
else if (cos == 5)
 level = '5';
else if (cos == 6)
 level = '6';
switch(level)
{
case '1':
 printf("Ile rekordow chcesz dodac: ");
 scanf("%d",&g);
 g = g+ile;
 for (i=ile; i<g; i++)
     {
      printf("\nPodaj imie ");
      scanf("%s",& t[i].imie);
      printf("\nPodaj nazwisko ");
      scanf("%s",& t[i].nazwisko);
      printf("\nPodaj data ");
      scanf("%s",&t[i].data);
      printf("\nPodaj adres ");
      scanf("%s",& t[i].adres);
      t[i].numer = ile+1;
      ile = ile +1;
     }
     break;
case '2':

if(g==0)
 printf("Brak rekordow w bazie!");
else
{
printf("\n\nKtory rekord wyswietlic: ");
scanf("%d",&i);
i = i-1;
printf("\nImie: %s",t[i].imie);
printf("\nNazwisko: %s",t[i].nazwisko);
printf("\nWiek: %s",t[i].data);
printf("\nTytul: %s",t[i].adres);
printf("\nNumer:%d",t[i].numer);
}
break;
case'3':
for(i=0; i<ile; i++)
{
 for(j=0; j<ile-1; j++)
 {
if(strncmp(t[i].nazwisko,t[i+1].nazwisko,1)>0)
 {
strcpy(c.nazwisko,t[i].nazwisko);
strcpy(t[i].nazwisko,t[i+1].nazwisko);
strcpy(t[i+1].nazwisko,c.nazwisko);

strcpy(c.imie,t[i].imie);
strcpy(t[i].imie,t[i+1].imie);
strcpy(t[i+1].imie,c.imie);

strcpy(c.data,t[i].data);
strcpy(t[i].data,t[i+1].data);
strcpy(t[i+1].data,c.data);

strcpy(c.adres,t[i].adres);
strcpy(t[i].adres,t[i+1].adres);
strcpy(t[i+1].adres,c.adres);
 }
}
for(i=0; i<ile; i++)
{
printf("\n%s",t[i].nazwisko);
}
}
   break;
case'4':
       if(ile == 0)
       {
       printf("Brak rekordow w bazie");
       }
       else
       {
       for(i=0; i<ile; i++)
       {
	printf("\nImie: %s",t[i].imie);
	printf("\nNazwisko: %s",t[i].nazwisko);
	printf("\nData: %s",t[i].data);
	printf("\nAdres: %s",t[i].adres);
       }
       }
	break;
case'5':
	printf("\nNumer rekordu do usuniecia: ");
	scanf("%d",&a);
	for(a=a; a<ile; a++)
        {
	strcpy(t[a].imie,t[a+1].imie);
	strcpy(t[a].nazwisko,t[a+1].nazwisko);
	strcpy(t[a].adres,t[a+1].adres);
	strcpy(t[a].data,t[a+1].data);
	t[a+1].imie[0] = 0;
	t[a+1].nazwisko[0] = 0;
	t[a+1].adres[0] = 0;
	t[a+1].data[0] = 0;
	}

	break;
case'6':
	printf("\nCzy napewno chcesz usunac wszystkie dane?");
	pow = getche();
	if (pow == 'Y' || pow == 'y')
	{
	for(a=0; a<ile; a++)
	{
	t[a].imie[0] = 0;
	t[a].nazwisko[0] = 0;
	t[a].adres[0] = 0;
	t[a].data[0] = 0;
        }
	}
	ile = 0;
	break;
default: printf("\nWprowadzono zle dane");
}
printf("\n\nCzy chcesz kontynuowac?");
pow = getche();
}
while(pow == 'Y'|| pow == 'y');
getch();
}

