#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Person
{
char surname[50];
unsigned int age;
struct Person *descendants;
unsigned int descendantsCount;
};
void zrobPotomka(struct Person *parent, unsigned int howMany)
{
parent->descendants = (struct Person*) malloc(howMany * sizeof(struct Person));
parent->descendantsCount = howMany;
for (int offset = 0; offset < howMany; offset++)
{
struct Person *currentDescendant = parent->descendants + offset;
strcpy(currentDescendant->surname, parent->surname);
currentDescendant->age = 0;
}
}
void printPersonData(struct Person *person)
{
printf("Nazwisko: %s, Wiek: %d, ilość potomków: %d \n", person->surname, person->age, person->descendantsCount);
//printf("Wiek: %d, ilość potomków: %d \n", person->age, person->descendantsCount);
for (int cnt = 0; cnt < person->descendantsCount; cnt++)
{
printf("Potomek %d:", cnt + 1);
struct Person *descendant = person->descendants + cnt;
printPersonData(descendant);
}
}
void printPersonsData(struct Person** person,int size,int idxArray[])
{
for(int j=0; j<size; j++)
{
printf("Nazwisko: %s, Wiek: %d, ilość potomków: %d \n", person[idxArray[j]]->surname, person[idxArray[j]]->age, person[idxArray[j]]->descendantsCount);
//printf("Wiek: %d, ilość potomków: %d \n", person->age, person->descendantsCount);
for (int cnt = 0; cnt < person[idxArray[j]]->descendantsCount; cnt++)
{
printf("Potomek %d:", cnt + 1);
struct Person *descendant = person[idxArray[j]]->descendants + cnt;
printPersonData(descendant);
}
}
}
void createPersons(struct Person** persons, int idx, const char surname[25], unsigned int age, int surnameLong)
{
persons[idx] = (struct Person*)malloc(sizeof(struct Person));
persons[idx]->age = age;
memcpy(persons[idx]->surname, surname, surnameLong);
}
bool ifPersonDoesntExist(int tab[],int idx)
{
for(int i=0; i<sizeof(tab)/sizeof(tab[0]); i++)
{
if(tab[i]==idx)
{
return false;
}
}
return true;
}
int main()
{
struct Person** osoby;
char surname[100];
int age;
char w;
int size,i=0;
int idx,idxArray[size];
printf("podaj ile chcesz stworzyc osob");
scanf("%d", &size);
osoby = (struct Person*) malloc(size * sizeof(struct Person));
while (i < size)
{
printf("%d", i);
printf("podaj jaką opcje wybierasz \n");
printf("a --- utwórz strukture pod wybranym indeksem \n");
printf("p --- wypisz wszystkie dane z tablicy \n");
printf("c --- utworz potomkow na bazie osoby \n");
scanf(" %c", &w);
switch (w)
{
case 'a':
printf("podaj indeks do utowrzenia osoby ");
scanf("%d", &idx);
if (ifPersonDoesntExist(idxArray,idx))
{idxArray[i]=idx;
printf("podaj prosze nazwisko");
scanf("%s",&surname);
printf("podaj prosze oraz wiek");
scanf("%d",&age);
createPersons(osoby, idx, surname, age, sizeof(surname)/sizeof(surname[0]));
age=0;
i++;
}
break;
case 'c':
zrobPotomka(&osoby, size-1);
break;
case 'p':
printPersonsData(osoby,i,idxArray);
break;
}
i++;
}
return 0;
}