/*
  CGS 3460 Computer Programming Using C
  Homework 6
  Problem 2
  Author: Cem Boyaci
  
  For questions send email to: cboyaci@cise.ufl.edu

*/

#include<stdio.h>

/* structure definitions */ 

enum student_type {REGULAR, PASS_FAIL};

union finalgrade
{
  float score; /* for a REGULAR student */
  char pf;   /* 'p' if pass, 'f' if fail, for PASS_FAIL type  */
};

struct Student
{

  enum student_type type;

struct
{
  char name[50];
  long phone;
}personal;

  float h[8];  /* Homework scores */
  float e[3];  /* Exam scores */
  float proj;  /* Project score */

  union finalgrade final;

};



union finalgrade compute_finalgrade(struct Student s)
{
  float total=0;
  union finalgrade f;
  int i;

  total+=0.15*s.proj;

  for(i=0;i<8; i++)
    total+=0.25/8*s.h[i];

  for(i=0;i<3; i++)
    total+=0.20*s.e[i];

  if(s.type==REGULAR)
    f.score=total;
  else
    {
      if(total>20)
	f.pf='p';
      else
	f.pf='f';

    }

  return f;

}


int main()
{
  struct Student s[80];

  int i, n, j,option;
  char yn, tmp;

  // prompt user
  printf("\nPlease selects an option:\n"); 
  printf("[1] Stores student records in a file\n");
  printf("[2] Displays student records\n");\
  scanf("%d",&option);
    
  if(option == 1){

    // get the user preference
    printf("\nEnter number of students: ");
    scanf("%d", &n);
    
    for(i=0; i<n; i++)
      {
        printf("\nIs the student a regular student?<y/n>: ");
        scanf("%c%c%c", &yn, &yn, &tmp);
        if(yn=='y')
          s[i].type=REGULAR;
        else
          s[i].type=PASS_FAIL;
  
        printf("\nEnter student name: ");
        fgets(s[i].personal.name, 50, stdin);
        printf("\nEnter phone number: ");
        scanf("%li", &s[i].personal.phone);
  
        for(j=0; j<8; j++)
          {
            printf("\nEnter Homework %d score: ", j+1);
            scanf("%f", &s[i].h[j]);
          }
        for(j=0; j<3; j++)
          {
            printf("\nEnter Exam %d score: ", j+1);
            scanf("%f", &s[i].e[j]);
          }

        printf("\nEnter project score: ");
        scanf("%f", &s[i].proj);
  
        s[i].final= compute_finalgrade(s[i]);
  
        if(s[i].type==REGULAR)
          printf("\nTotal score is %f\n", s[i].final.score);
        else
          if(s[i].final.pf=='p')
            printf("\nStudent passed\n");
          else
            printf("\nStudent failed\n"); 
      }
        
      // prompt for filename
      char filename[30];
      printf("Please enter a filename to save student records:\n");
      scanf("%s",filename);

      // open binary file for writing
      FILE* fp = fopen(filename,"wb");
      // insert the number of students as the very first bytes of this file  
      fwrite(&n,sizeof(int),1,fp);
      // now write student records
      fwrite(s,sizeof(struct Student),n,fp);
      // close the file
      fclose(fp);

      // let user know what has happened
      printf("-------------------\n");
      printf("Records are saved to %s:\n",filename);
      printf("-------------------\n");  

    }
   else if(option==2){
      
      char filename[30];
      printf("Please enter a filename to locate records:\n");
      scanf("%s",filename);

      int count;
      FILE* fp = fopen(filename,"rb"); 
      fread(&count, sizeof(int),1,fp);
      printf("\n%s has %d records\n-------------\n\n",filename,count);
      struct Student students[count];
      fread(students,sizeof(struct Student),count,fp);
      fclose(fp);
  
      for(i = 0;i<count; i++){
        printf("Student Record [%d]\n",(i+1));  
        printf("Student Name: %s",students[i].personal.name);
        printf("Phone: %ld\n",students[i].personal.phone);
        printf("-------------\n");      
      
      }
  
  }
  else{
    printf("Unrecognized option\n");
    printf("Exiting...\n");
  }

  return 0;
}



