Commit dc26623f by raliis

Replace keskmineHinne.c

parent 6e5c591a
Showing with 83 additions and 67 deletions
...@@ -8,40 +8,58 @@ ...@@ -8,40 +8,58 @@
* finds the best and the worst. * finds the best and the worst.
*/ */
#include <stdio.h> #include <stdio.h> // print, scan
#include <string.h> #include <string.h> // strcpy, strcmp
#include "struktuur.h" #include "struktuur.h" // holds the structs
int numberofRows (); // counts the rows in the file // counts the rows in the file
void readStudents (Stud *to_be_graded); // scans in the data from the file int numberofRows ();
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 // scans in the data from the file
int list (Filtered *applicable, int , Avg *student_list, char DVW); // makes a sorted list void readStudents (Stud *to_be_graded);
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 // filters out students with unapplicable grades
void money (Avg *student_list, int student_amount); // determines who gets the money and who gets the bill int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW);
void bill (Avg *student_list, int student_amount); // outputs bills into file
// sorts by name
void sortbyName (Filtered *applicable, int applicable_amount, char DVW);
// makes a sorted list
int list (Filtered *applicable, int , Avg *student_list, char DVW);
// calculates each student's average
void average (Avg *student_list, Filtered *applicable, int applicable_amount, int student_amount, char DVW);
// sorts by grade
void sortbyAverage (Avg *student_list, int student_amount, char DVW);
// determines who gets the money and who gets the bill
void money (Avg *student_list, int student_amount);
// outputs bills into file
void bill (Avg *student_list, int student_amount);
int main (void) int main (void)
{ {
char DVW; char DVW; // var to hold the answer
printf ("Print all or silent? [p/s]"); printf ("Print all or silent? [p/s]");
do
do // only accepts s or p
{ {
scanf ("%c", &DVW); scanf ("%c", &DVW); // determines whether functions print results or not
} while (DVW != 'p' && DVW != 's'); } while (DVW != 'p' && DVW != 's');
printf ("\n\n"); printf ("\n\n");
int n; int n;
n = numberofRows(); n = numberofRows(); // counts how many rows the file contains
Stud to_be_graded[n]; Stud to_be_graded[n]; // var to hold first scan results
Filtered applicable[n]; Filtered applicable[n]; // var to hold applicable
Avg student_list[n]; Avg student_list[n]; // var to hold each student and their avg grade
int applicable_amount; int applicable_amount; // nr of applicable elements
int student_amount; int student_amount; // nr of students
readStudents(to_be_graded); readStudents(to_be_graded);
printf ("\n\n"); printf ("\n\n");
applicable_amount = sieve (to_be_graded, applicable, n, DVW); applicable_amount = sieve (to_be_graded, applicable, n, DVW);
...@@ -64,41 +82,44 @@ int main (void) ...@@ -64,41 +82,44 @@ int main (void)
int numberofRows() int numberofRows()
{ {
FILE *data; FILE *data;
data = fopen ("andmed.txt", "r"); data = fopen ("andmed.txt", "r"); // opens the file
int read = 0; int read = 0;
int ch = 0; int ch = 0;
if (data == NULL) if (data == NULL) // if file doesn't exist, returns 0
{ {
return 0; return 0;
} }
while ((ch = fgetc (data)) != EOF) // scans every character, if the char is '\n' then the counter counts up
while ((ch = fgetc (data)) != EOF)
{ {
if (ch == '\n') if (ch == '\n')
{ {
read++; read++;
} }
} }
fclose (data); fclose (data); // closes the file
return read; return read; // returns the nr of rows
} }
void readStudents (Stud *to_be_graded) void readStudents (Stud *to_be_graded)
{ {
FILE *data; FILE *data;
data = fopen ("andmed.txt", "r"); data = fopen ("andmed.txt", "r"); // opens the file
int i = 0; int i = 0;
// reads in the data from the file
while (fscanf (data, "%s %s %d", to_be_graded[i].name, to_be_graded[i].subj, &to_be_graded[i].grade) != EOF) while (fscanf (data, "%s %s %d", to_be_graded[i].name, to_be_graded[i].subj, &to_be_graded[i].grade) != EOF)
{ {
// prints the data to the screen
printf ("%d. %s, %s, %d\n", i+1, to_be_graded[i].name, to_be_graded[i].subj, to_be_graded[i].grade); printf ("%d. %s, %s, %d\n", i+1, to_be_graded[i].name, to_be_graded[i].subj, to_be_graded[i].grade);
i++; i++;
} }
fclose (data); fclose (data); // closes the file
} }
int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW) int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW)
...@@ -106,6 +127,7 @@ int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW) ...@@ -106,6 +127,7 @@ int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW)
int i; int i;
int j = 0; int j = 0;
// if the grade is between 0 and 5, then copy row to applicable
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
if (to_be_graded[i].grade >= 0 && to_be_graded[i].grade <= 5) if (to_be_graded[i].grade >= 0 && to_be_graded[i].grade <= 5)
...@@ -116,7 +138,8 @@ int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW) ...@@ -116,7 +138,8 @@ int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW)
} }
} }
if (DVW == 80 || DVW == 112) // if the input character is p, it prints the result
if (DVW == 112) // 112 is the ascii value of 'p'
{ {
for (i = 0; i < j; i++) for (i = 0; i < j; i++)
{ {
...@@ -125,7 +148,7 @@ int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW) ...@@ -125,7 +148,7 @@ int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW)
printf ("\n\n"); printf ("\n\n");
} }
return j; return j; // returns nr of applicable rows
} }
void sortbyName (Filtered *applicable, int applicable_amount, char DVW) void sortbyName (Filtered *applicable, int applicable_amount, char DVW)
...@@ -134,6 +157,7 @@ void sortbyName (Filtered *applicable, int applicable_amount, char DVW) ...@@ -134,6 +157,7 @@ void sortbyName (Filtered *applicable, int applicable_amount, char DVW)
int j; int j;
Filtered temp; Filtered temp;
// sorts applicable alphabetically
for (i = 0; i < applicable_amount; i++) for (i = 0; i < applicable_amount; i++)
{ {
for (j = 1; j < applicable_amount; j++) for (j = 1; j < applicable_amount; j++)
...@@ -150,7 +174,8 @@ void sortbyName (Filtered *applicable, int applicable_amount, char DVW) ...@@ -150,7 +174,8 @@ void sortbyName (Filtered *applicable, int applicable_amount, char DVW)
} }
} }
if (DVW == 80 || DVW == 112) // if the input character is p, it prints the result
if (DVW == 112) // 112 is the ascii value of 'p'
{ {
for (i = 0; i < applicable_amount; i++) for (i = 0; i < applicable_amount; i++)
{ {
...@@ -166,6 +191,7 @@ int list (Filtered *applicable, int applicable_amount, Avg *student_list, char D ...@@ -166,6 +191,7 @@ int list (Filtered *applicable, int applicable_amount, Avg *student_list, char D
int i; int i;
int j = 0; int j = 0;
// makes a list where each name appears once
for (i = 0; i < applicable_amount; i++) for (i = 0; i < applicable_amount; i++)
{ {
if (strcmp (applicable[i-1].name, applicable[i].name) != 0) if (strcmp (applicable[i-1].name, applicable[i].name) != 0)
...@@ -175,7 +201,8 @@ int list (Filtered *applicable, int applicable_amount, Avg *student_list, char D ...@@ -175,7 +201,8 @@ int list (Filtered *applicable, int applicable_amount, Avg *student_list, char D
} }
} }
if (DVW == 80 || DVW == 112) // if the input character is p, it prints the result
if (DVW == 112) // 112 is the ascii value of 'p'
{ {
for (i = 0; i < j; i++) for (i = 0; i < j; i++)
{ {
...@@ -184,7 +211,7 @@ int list (Filtered *applicable, int applicable_amount, Avg *student_list, char D ...@@ -184,7 +211,7 @@ int list (Filtered *applicable, int applicable_amount, Avg *student_list, char D
printf ("\n\n"); printf ("\n\n");
} }
return j; return j; // returns the nr of students
} }
void average (Avg *student_list, Filtered *applicable, int applicable_amount, int student_amount, char DVW) void average (Avg *student_list, Filtered *applicable, int applicable_amount, int student_amount, char DVW)
...@@ -193,6 +220,7 @@ void average (Avg *student_list, Filtered *applicable, int applicable_amount, in ...@@ -193,6 +220,7 @@ void average (Avg *student_list, Filtered *applicable, int applicable_amount, in
int j; int j;
int counter = 0; int counter = 0;
// calculates the average of each student
for (i = 0; i < student_amount; i++) for (i = 0; i < student_amount; i++)
{ {
for (j = 0; j < applicable_amount; j++) for (j = 0; j < applicable_amount; j++)
...@@ -207,7 +235,8 @@ void average (Avg *student_list, Filtered *applicable, int applicable_amount, in ...@@ -207,7 +235,8 @@ void average (Avg *student_list, Filtered *applicable, int applicable_amount, in
counter = 0; counter = 0;
} }
if (DVW == 80 || DVW == 112) // if the input character is p, it prints the result
if (DVW == 112) // 112 is the ascii value of 'p'
{ {
for (i = 0; i < student_amount; i++) for (i = 0; i < student_amount; i++)
{ {
...@@ -223,6 +252,7 @@ void sortbyAverage (Avg *student_list, int student_amount, char DVW) ...@@ -223,6 +252,7 @@ void sortbyAverage (Avg *student_list, int student_amount, char DVW)
int i, j; int i, j;
Avg temp; Avg temp;
// sorts list by average grade
for (i = 0; i < student_amount; i++) for (i = 0; i < student_amount; i++)
{ {
for (j = 1; j < student_amount; j++) for (j = 1; j < student_amount; j++)
...@@ -236,7 +266,8 @@ void sortbyAverage (Avg *student_list, int student_amount, char DVW) ...@@ -236,7 +266,8 @@ void sortbyAverage (Avg *student_list, int student_amount, char DVW)
} }
} }
if (DVW == 80 || DVW == 112) // if the input character is p, it prints the result
if (DVW == 112) // 112 is the ascii value of 'p'
{ {
for (i = 0; i < student_amount; i++) for (i = 0; i < student_amount; i++)
{ {
...@@ -252,6 +283,7 @@ void money (Avg *student_list, int student_amount) ...@@ -252,6 +283,7 @@ void money (Avg *student_list, int student_amount)
int i; int i;
int counter = 0; int counter = 0;
// prints the first 3 names
printf ("The money goes to: \n"); printf ("The money goes to: \n");
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
{ {
...@@ -259,6 +291,8 @@ void money (Avg *student_list, int student_amount) ...@@ -259,6 +291,8 @@ void money (Avg *student_list, int student_amount)
} }
counter = i; counter = i;
// checks if the 4th person from the top has the same average as the 3rd,
// if that is true, also prints the 4th student's info and then the 5th and so on
do do
{ {
if (student_list[counter].average == student_list[2].average) if (student_list[counter].average == student_list[2].average)
...@@ -268,63 +302,45 @@ void money (Avg *student_list, int student_amount) ...@@ -268,63 +302,45 @@ void money (Avg *student_list, int student_amount)
} }
else else
{ {
break; break;// stops the loop, if there is a difference in the average grade
}; };
} while (student_list[i].average == student_list[2].average); } while (student_list[i].average == student_list[2].average);
printf ("\n\n"); 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) void bill (Avg *student_list, int student_amount)
{ {
int counter; int counter = student_amount - 4;
int i; int i;
FILE *output; FILE *output;
output = fopen ("bill.txt", "w"); output = fopen ("bill.txt", "w"); // opens the output file
// prints the last 3 people from student_list to the output file
printf ("The bills go to: \n");
fprintf (output, "The bills go to: \n"); fprintf (output, "The bills go to: \n");
for (i = student_amount - 3; i < student_amount; i++) 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);
fprintf (output, "%s, with an average of: %.2f\n", student_list[i].name, student_list[i].average); fprintf (output, "%s, with an average of: %.2f\n", student_list[i].name, student_list[i].average);
} }
counter = student_amount - 4; // checks if the 4th person from the bottom has the same average as the 3rd,
// if that is true, also prints the 4th student's info and then the 5th and so on
do do
{ {
if (student_list[counter].average == student_list[i - 3].average) 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);
fprintf (output, "%s, with an average of: %f\n", student_list[counter].name, student_list[counter].average); fprintf (output, "%s, with an average of: %f\n", student_list[counter].name, student_list[counter].average);
counter--; counter--;
} }
else else
{ {
break; break; // stops the loop, if there is a difference in the average grade
}; };
} while (student_list[counter].average == student_list[i - 3].average); } while (student_list[counter].average == student_list[i - 3].average);
fclose (output); fclose (output); // closes the file
} }
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