/***************************************************************************
(c) Copyright 2002 Reshape Inc., All Rights Reserved.
www.reshape.com
****************************************************************************
File : score.c
Author : Jeremy Russell
Date : November 20, 2002
Description : 1. (Roster problem revisited)We have made a very rough
roster program in Project3, question 2. This problem is
basically the same except that you are requested to read
the input from a file and send output also to a file. The
source roster can be created with a text editor (no word
processors like MS Word please except that you know you
have to save it as a text file) with the same data as in
Project3. Save the file as roster.txt, the output file
should be called score.txt, Your program should be called
score.c. Submit all of them.
***************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STRING 60
#define SCORES 3
#define STUDENTS 3
struct student {
char name[STRING];
int scoreOne;
int scoreTwo;
int scoreThree;
float final;
};
typedef struct student StudentRecord;
// Sub routine predeclaration.
StudentRecord storeLine (char *);
double calculateTotal (StudentRecord * );
void sortByFinalTotal( StudentRecord * );
/***************************************************************************
Function : main
Author : Jeremy Russell
Date : October 17, 2002
Description : The main() function is the main part of the progrem.
Arguments : void
Returns : 1 on fail and 0 un success.
Notes :
See Also :
***************************************************************************/
int main (int argc, char *argv[])
{
char line[STRING];
struct student class[STUDENTS];
char rosterFile[] = "roster.txt";
char scoreFile[] = "score.txt";
int x;
FILE *roster, *score;
char *prog = argv[0];
double tmp;
if ((roster = fopen(rosterFile, "r")) == NULL) {
fprintf(stderr, "%s: Cannot open %s.\n", prog, rosterFile);
}
// Iterate through the files, reading each line.
for (x = 0; x < STUDENTS; x++)
{
// Get the name.
fgets( line, STRING, roster );
class[x] = storeLine(line);
}
fclose(roster);
sortByFinalTotal(class);
if ((score = fopen(scoreFile, "w")) == NULL) {
fprintf(stderr, "%s: Cannot open %s.\n", prog, scoreFile);
}
fprintf (score, "These are the scores sorted highest to lowest:\n");
for (x = 0; x < STUDENTS; x++) {
fprintf (score, "%s's score is %.2f\n", class[x].name, class[x].final);
}
fclose(score);
return 0;
}
/***************************************************************************
Function : storeLine
Author : Jeremy Russell
Date : November 20, 2002
Description : Returns a structure fully populated.
Arguments : char[STRING]
Returns : StudentRecord (struct student)
Notes :
See Also :
***************************************************************************/
StudentRecord storeLine (char line[])
{
char *name, delimiter[2] = {' ',','};
StudentRecord temp;
name = strtok(line, delimiter);
strcpy(temp.name, name);
temp.scoreOne = atoi(strtok(NULL, delimiter));
temp.scoreTwo = atoi(strtok(NULL, delimiter));
// printf("TEST A: %d\n", temp.scoreTwo);
temp.scoreThree = atoi(strtok(NULL, delimiter));
// printf("TEST B: %d\n", temp.scoreThree);
temp.final = calculateTotal(&temp);
// printf("TEST: %2f\n", temp.final);
return temp;
}
/***************************************************************************
Function : calculateTotal
Author : Jeremy Russell
Date : October 16, 2002
Description : The calculateTotal() function takes a list of numbers and an
associtaed list of how to weigh those numbers (i.e. a
percentage) and returns a double
Arguments : StudentRecord *
Returns : The calculateTotal returns a double.
Notes :
See Also :
***************************************************************************/
double calculateTotal (StudentRecord *record)
{
return (0.3 * (float) record->scoreOne) + (0.3 * (float) record->scoreTwo) + (0.4 * (float) record->scoreThree);
}
/***************************************************************************
Function : sortByFinalTotal
Author : Jeremy Russell
Date : October 17, 2002
Description : The sortByFinalTotal() funcion does a bubble sort of
the final scores. The score array has an associated
name array that must also be sarranged to match the
final score array.
Arguments : SrudentRecord [STUDENTS]
Returns : void
Notes :
See Also :
***************************************************************************/
void sortByFinalTotal( StudentRecord list[] )
{
int pass, j;
StudentRecord holder;
for ( pass = 1; pass < STUDENTS; pass++ )
{
for ( j = 0; j < STUDENTS - 1; j++ )
{
if ( list[j].final < list[j + 1].final )
{
// Switch the scores.
holder = list[j];
list[j] = list[j + 1];
list[j + 1] = holder;
}
}
}
}