#include <stdio.h>

#include <malloc.h>

#include <string.h>

 

typedef struct {

  int ID;

  char fname[30], lname[30];

  int hwrk[20], exams[20], fExam;

} studentrectype;

 

typedef struct {

  char cname[40];

  int numS, numH, numE;

  float wghtH, wghtE, wghtF;

} classtype;

 

studentrectype *students;

classtype section;

 

void getrec(FILE *f, studentrectype *s);

 

main()

{

  FILE *f;

  char c;

  int i, j;

  float score;

 

  f = fopen("egr155.dat", "rt");

  fscanf(f, "%s\n", section.cname);

  fscanf(f, "%d\n", &section.numS);

  fscanf(f, "%d %d\n", &section.numH, &section.numE);

  fscanf(f, "%f %f %f\n", &section.wghtH, &section.wghtE, &section.wghtF);

  students = (studentrectype *)malloc(section.numS * sizeof(studentrectype));

  for (i=0; i < section.numS; i++) {

    getrec(f, students+i);

    score = 0;

    for (j=0; j < section.numH; j++) score += section.wghtH * students[i].hwrk[j];

    for (j=0; j < section.numE; j++) score += section.wghtE * students[i].exams[j];

    score += section.wghtF * students[i].fExam;

    if (score >= 90) c = 'A';

    else if (score >= 80) c = 'B';

    else if (score >= 70) c = 'C';

    else if (score >= 60) c= 'D';

    else c = 'F';

    printf("The grade for %s %s is:\t%6.2f %c\n", students[i].fname, students[i].lname, score, c);

  }

  return 0;

}

 

void getrec(FILE *f, studentrectype *s)

{

  char buf[2048], *ss;

  int *ip;

 

  fgets(buf, 2048, f);

  ss = strtok(buf, "\t\n");

  s->ID = atoi(ss);

  ss = strtok(NULL, "\t\n");

  strcpy(s->fname, ss);

  ss = strtok(NULL, "\t\n");

  strcpy(s->lname, ss);

  fgets(buf, 2048, f);

  ip = s->hwrk;

  ss = strtok(buf, "\t\n");

  do {

    *ip++ = atoi(ss);

  } while ((ss = strtok(NULL, "\t\n")) != NULL); 

  fgets(buf, 2048, f);

  ip = s->exams;

  ss = strtok(buf, "\t\n");

  do {

    *ip++ = atoi(ss);

  } while ((ss = strtok(NULL, "\t\n")) != NULL); 

  fscanf(f, "%d\n", &s->fExam);

}

EGR155

3

4 2

1.0 1.0 0.0

1     Tim   Adair

7     8     6     16

20    25

0

2     Tom   Bair

7     7     8     16

15    20

0

3     Jim   Dahl

6     6     6     16

22    27

0