/**************************************************************************
This program read inputs of children playgroup between age 2 and 4 and
stores it in an array of record then print a table containing the inportant
information of each children.
**************************************************************************/

#include <stdio.h>
#include <string.h>
#include <conio.h>

#define max 15

/**************************************************************************
Type defining the components needed to present names.
**************************************************************************/

typedef struct
{
  char  first[max];
  char  middle[max];
  char  last[max];
}Name;

/**************************************************************************
Type defining the components needed to present date birth and start playgroup.
**************************************************************************/

typedef struct
{
  int   day;
  char  month[max];
  long   year;
}Date,Start;

/**************************************************************************
Type defining the components needed to present condition of child in age 2
**************************************************************************/

typedef struct
{
  char  diapers;
  char  eat_alone;
}age2;

/**************************************************************************
Type defining the components needed to present condition of child in age 3.
**************************************************************************/

typedef struct
{
  char  dress;
}age3;

/**************************************************************************
type defining the components needed to present condition of child in age 4
**************************************************************************/

typedef struct
{
  char  siblings;
}age4;

/**************************************************************************
Type defining the components needed to present to store value of conditions.
**************************************************************************/

typedef union
{
  age2  a[max];
  age3  b[max];
  age4  c[max];
}cond;

/**************************************************************************
Type defining the components needed to present data of each child.
**************************************************************************/

typedef struct
{
  Name   name[max];
  Date   date[max];
  Start  start[max];
  int    age;
  cond   y[max];
  float  price;
}data;

void ReadArrayRec    (data x[max],int*);
void Delete          (data x[max],int*);
void Compution       (data x[max],int);
void PrintArrayRec   (data x[max],int);

int main()

{
  data x[max],z[max];
  int n=0;

  ReadArrayRec(x,&n);
  Delete(x,&n);
  Compution(x,n);
  PrintArrayRec(x,n);

  return 0;

}

/**************************************************************************
This function Create array of structures to store data of each child.
***************************************************************************/

void ReadArrayRec (data x[max],int *n)
{
  int i;

  clrscr();

  printf("\nPlzzz enter size of array : \n");
  scanf("%d",&*n);

  for (i=0;i<*n;i++)
  {

    /* Enter names of child */
    printf("\nEnter names of child # %d \n",i+1);
    printf("=========================\n");
    printf("Enter first name : \n");
    scanf("%s",x[i].name[i].first);
    printf("Enter middle name : \n");
    scanf("%s",x[i].name[i].middle);
    printf("Enter last name : \n");
    scanf("%s",x[i].name[i].last);

    /* Enter date of birth */

    printf("\nEnter date of birth name : \n");
    printf("=========================\n");
    printf("Enter day : \n");
    scanf("%d",&x[i].date[i].day);
    printf("Enter month : \n");
    scanf("%s",x[i].date[i].month);
    printf("Enter year: \n");
    scanf("%ld",&x[i].date[i].year);

    /* Enter playgroup start date */

    printf("\nEnter playgroup start date : \n");
    printf("=========================\n");
    printf("Enter day : \n");
    scanf("%d",&x[i].start[i].day);
    printf("Enter month : \n");
    scanf("%s",x[i].start[i].month);
    printf("Enter year: \n");
    scanf("%ld",&x[i].start[i].year);

    /* Enter age */

    printf("\nEnter age : \n");
    printf("=========================\n");
    scanf("%d",&x[i].age);

    if (x[i].age==2)
    {
      printf("Is child still diapers or not ('y' for yes && 'n' for no) : \n");
      scanf(" %c",&x[i].y[i].a[i].diapers);
      printf("Is child eat alone or not ('y' for yes && 'n' for no) : \n");
      scanf(" %c",&x[i].y[i].a[i].eat_alone);
    }
    else
    if (x[i].age==3)
    {
      printf("Is child dress themselves or not  ('y' for yes && 'n' for no) : \n");
      scanf(" %c",&x[i].y[i].b[i].dress);
    }
    else
    if (x[i].age==4)
    {
      printf("Is child has siblings enrolled or not  ('y' for yes && 'n' for no) : \n");
      scanf(" %c",&x[i].y[i].c[i].siblings);
    }
   x[i].price=80;
  }
  printf("\n");
}

/**************************************************************************
This function delete structures that not in age 2 and 4.
***************************************************************************/

void Delete (data x[max],int *n)
{
  data k[max];
  int i,j,t=0;

  for (i=0;i<*n;i++)
  {
    if (x[i].age!=2 && x[i].age!=3 && x[i].age!=4)
    {
      if (i==0)
      {
	 printf("\nDelete structers not in 2..4 : \n");
	 printf("================================== \n");
      }
      if (x[i].age<2)
	printf("%s %s will not be accepted in the playgroup due to being under the age range \n",x[i].name[i].first,x[i].name[i].last);
      else
	printf("%s %s will not be accepted in the playgroup due to being over the age range \n",x[i].name[i].first,x[i].name[i].last);
    }
    else   /* Copy to another array with out element age not in 2..4 */
    {
     strcpy(k[t].name[t].first,x[i].name[i].first);
     strcpy(k[t].name[t].middle,x[i].name[i].middle);
     strcpy(k[t].name[t].last,x[i].name[i].last);
     k[t].date[t].day=x[i].date[i].day;
     strcpy(k[t].date[i].month,x[i].date[i].month);
     k[t].date[t].year=x[i].date[i].year;
     k[t].start[t].year=x[i].start[i].day;
     strcpy(k[t].start[t].month,x[i].start[i].month);
     k[t].start[t].year=x[i].start[i].year;
     k[t].y[t].a[t].diapers=x[i].y[i].a[i].diapers;
     k[t].y[t].a[t].eat_alone=x[i].y[i].a[i].eat_alone;
     k[t].y[t].b[t].dress=x[i].y[i].b[i].dress;
     k[t].y[t].c[t].siblings=x[i].y[i].c[i].siblings;
     k[t].age=x[i].age;
     k[t].price=x[i].price;
     t++;
    }
  }           /* END FOR LOOP */
  *n=t;
  for (i=0;i<*n;i++)    /* return to original array with out age over or down 2..4*/
  {
     strcpy(x[i].name[i].first,k[i].name[i].first);
     strcpy(x[i].name[i].middle,k[i].name[i].middle);
     strcpy(x[i].name[i].last,k[i].name[i].last);
     x[i].date[i].day=k[i].date[i].day;
     strcpy(x[i].date[i].month,k[i].date[i].month);
     x[i].date[i].year=k[i].date[i].year;
     x[i].start[i].year=k[i].start[i].day;
     strcpy(x[i].start[i].month,k[i].start[i].month);
     x[i].start[i].year=k[i].start[i].year;
     x[i].y[i].a[i].diapers=k[i].y[i].a[i].diapers;
     x[i].y[i].a[i].eat_alone=k[i].y[i].a[i].eat_alone;
     x[i].y[i].b[i].dress=k[i].y[i].b[i].dress;
     x[i].y[i].c[i].siblings=k[i].y[i].c[i].siblings;
     x[i].age=k[i].age;
     x[i].price=k[i].price;
   }
}

/**************************************************************************
This function compute monthly charge of each student.
***************************************************************************/

void Compution (data x[max],int n)
{
  int i;

  for (i=0;i<n;i++)
  {
    if (x[i].age==2)
    {
      if (x[i].y[i].a[i].diapers=='y')           /* is diapers */
	x[i].price=x[i].price+5;
     if (x[i].y[i].a[i].eat_alone=='n')         /* is not eat alone */
	x[i].price=x[i].price+5;
    }
    else
    if (x[i].age==3)
    {
      if (x[i].y[i].b[i].dress=='n')        /* not dress themselves */
	x[i].price=x[i].price+3;
    }
    else
    if (x[i].age==4)
    {
      if (x[i].y[i].c[i].siblings=='y')     /* has siblings */
	x[i].price=x[i].price-(x[i].price*0.2);
    }
  }
}

/**************************************************************************
This function print important information for each child.
***************************************************************************/

void PrintArrayRec (data x[max],int n)
{
  int i;

  printf("\n");
  printf("last |   first  |    year    |    playgroup     |       |  total cost  \n");
  printf("name |   name   |  of birth  |  starting month  |  age  |   per month   \n");
  printf("---------------------------------------------------------------------\n");

  for (i=0;i<n;i++)
  printf("%s    %5s        %2ld            %5s          %d        %2.1f\n",x[i].name[i].last,x[i].name[i].first,x[i].date[i].year,x[i].start[i].month,x[i].age,x[i].price);

}































