Commit d05c21e1 by raliis

Upload New File

parent 92d12318
Showing with 330 additions and 0 deletions
/**
* File: keskmineHinne.c
* Author: Rainer Liis
* Created: 19.02.2018
* Last edit: 20.02.2018
*
* Description: The program scans in the students and their grades,
* finds the best and the worst.
*/
#include <stdio.h>
#include <string.h>
#include "struktuur.h"
int numberofRows (); // counts the rows in the file
void readStudents (Stud *to_be_graded); // scans in the data from the file
int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW); // filters out students with unapplicable grades
void sortbyName (Filtered *applicable, int applicable_amount, char DVW); // sorts by name
int list (Filtered *applicable, int , Avg *student_list, char DVW); // makes a sorted list
void average (Avg *student_list, Filtered *applicable, int applicable_amount, int student_amount, char DVW); // calculates each student's average
void sortbyAverage (Avg *student_list, int student_amount, char DVW); // sorts by grade
void money (Avg *student_list, int student_amount); // determines who gets the money and who gets the bill
void bill (Avg *student_list, int student_amount); // outputs bills into file
int main (void)
{
char DVW;
printf ("Print all or silent? [p/s]");
do
{
scanf ("%c", &DVW);
} while (DVW != 'p' && DVW != 's');
printf ("\n\n");
int n;
n = numberofRows();
Stud to_be_graded[n];
Filtered applicable[n];
Avg student_list[n];
int applicable_amount;
int student_amount;
readStudents(to_be_graded);
printf ("\n\n");
applicable_amount = sieve (to_be_graded, applicable, n, DVW);
sortbyName (applicable, applicable_amount, DVW);
student_amount = list (applicable, applicable_amount, student_list, DVW);
average (student_list, applicable, applicable_amount, student_amount, DVW);
sortbyAverage (student_list, student_amount, DVW);
money (student_list, student_amount);
bill (student_list, student_amount);
return 0;
}
int numberofRows()
{
FILE *data;
data = fopen ("andmed.txt", "r");
int read = 0;
int ch = 0;
if (data == NULL)
{
return 0;
}
while ((ch = fgetc (data)) != EOF)
{
if (ch == '\n')
{
read++;
}
}
fclose (data);
return read;
}
void readStudents (Stud *to_be_graded)
{
FILE *data;
data = fopen ("andmed.txt", "r");
int i = 0;
while (fscanf (data, "%s %s %d", to_be_graded[i].name, to_be_graded[i].subj, &to_be_graded[i].grade) != EOF)
{
printf ("%d. %s, %s, %d\n", i+1, to_be_graded[i].name, to_be_graded[i].subj, to_be_graded[i].grade);
i++;
}
fclose (data);
}
int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW)
{
int i;
int j = 0;
for (i = 0; i < n; i++)
{
if (to_be_graded[i].grade >= 0 && to_be_graded[i].grade <= 5)
{
strcpy (applicable[j].name, to_be_graded[i].name);
applicable[j].grade = to_be_graded[i].grade;
j++;
}
}
if (DVW == 80 || DVW == 112)
{
for (i = 0; i < j; i++)
{
printf ("%d, %s, %d\n", i+1, applicable[i].name, applicable[i].grade);
}
printf ("\n\n");
}
return j;
}
void sortbyName (Filtered *applicable, int applicable_amount, char DVW)
{
int i;
int j;
Filtered temp;
for (i = 0; i < applicable_amount; i++)
{
for (j = 1; j < applicable_amount; j++)
{
if (strcmp (applicable[j-1].name, applicable[j].name) > 0)
{
temp.grade = applicable[j-1].grade;
strcpy (temp.name, applicable[j-1].name);
applicable[j-1].grade = applicable[j].grade;
strcpy (applicable[j-1].name, applicable[j].name);
applicable[j].grade = temp.grade;
strcpy (applicable[j].name, temp.name);
}
}
}
if (DVW == 80 || DVW == 112)
{
for (i = 0; i < applicable_amount; i++)
{
printf ("%d, %s, %d\n", i+1, applicable[i].name, applicable[i].grade);
}
printf ("\n\n");
}
}
int list (Filtered *applicable, int applicable_amount, Avg *student_list, char DVW)
{
int i;
int j = 0;
for (i = 0; i < applicable_amount; i++)
{
if (strcmp (applicable[i-1].name, applicable[i].name) != 0)
{
strcpy (student_list[j].name, applicable[i].name);
j++;
}
}
if (DVW == 80 || DVW == 112)
{
for (i = 0; i < j; i++)
{
printf ("%d, %s\n", i+1, student_list[i].name);
}
printf ("\n\n");
}
return j;
}
void average (Avg *student_list, Filtered *applicable, int applicable_amount, int student_amount, char DVW)
{
int i;
int j;
int counter = 0;
for (i = 0; i < student_amount; i++)
{
for (j = 0; j < applicable_amount; j++)
{
if (strcmp (student_list[i].name, applicable[j].name) == 0)
{
student_list[i].grade += applicable[j].grade;
counter++;
}
}
student_list[i].average = (float)student_list[i].grade / (float) counter;
counter = 0;
}
if (DVW == 80 || DVW == 112)
{
for (i = 0; i < student_amount; i++)
{
printf ("%d, %s, %d, %f\n", i+1, student_list[i].name, student_list[i].grade, student_list[i].average);
}
printf ("\n\n");
}
}
void sortbyAverage (Avg *student_list, int student_amount, char DVW)
{
int i, j;
Avg temp;
for (i = 0; i < student_amount; i++)
{
for (j = 1; j < student_amount; j++)
{
if (student_list[j-1].average < student_list[j].average)
{
temp = student_list[j-1];
student_list[j-1] = student_list[j];
student_list[j] = temp;
}
}
}
if (DVW == 80 || DVW == 112)
{
for (i = 0; i < student_amount; i++)
{
printf ("%d, %s, %f\n", i+1, student_list[i].name, student_list[i].average);
}
printf ("\n\n");
}
}
void money (Avg *student_list, int student_amount)
{
int i;
int counter = 0;
printf ("The money goes to: \n");
for (i = 0; i < 3; i++)
{
printf ("%s, with an average of: %f\n", student_list[i].name, student_list[i].average);
}
counter = i;
do
{
if (student_list[counter].average == student_list[2].average)
{
printf ("%s, with an average of: %f\n", student_list[counter].name, student_list[counter].average);
counter++;
}
else
{
break;
};
} while (student_list[i].average == student_list[2].average);
printf ("\n\n");
printf ("The bills go to: \n");
for (i = student_amount - 3; i < student_amount; i++)
{
printf ("%s, with an average of: %f\n", student_list[i].name, student_list[i].average);
}
counter = student_amount - 4;
do
{
if (student_list[counter].average == student_list[i - 3].average)
{
printf ("%s, with an average of: %f\n", student_list[counter].name, student_list[counter].average);
counter--;
}
else
{
break;
};
} while (student_list[counter].average == student_list[i - 3].average);
}
void bill (Avg *student_list, int student_amount)
{
int counter;
int i;
FILE *output;
output = fopen ("bill.txt", "w");
fprintf (output, "The bills go to: \n");
for (i = student_amount - 3; i < student_amount; i++)
{
fprintf (output, "%s, with an average of: %.2f\n", student_list[i].name, student_list[i].average);
}
counter = student_amount - 4;
do
{
if (student_list[counter].average == student_list[i - 3].average)
{
fprintf (output, "%s, with an average of: %f\n", student_list[counter].name, student_list[counter].average);
counter--;
}
else
{
break;
};
} while (student_list[counter].average == student_list[i - 3].average);
fclose (output);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment