Commit dc26623f by raliis

Replace keskmineHinne.c

parent 6e5c591a
Showing with 80 additions and 64 deletions
......@@ -8,38 +8,56 @@
* 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
#include <stdio.h> // print, scan
#include <string.h> // strcpy, strcmp
#include "struktuur.h" // holds the structs
// counts the rows in the file
int numberofRows ();
// scans in the data from the file
void readStudents (Stud *to_be_graded);
// filters out students with unapplicable grades
int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW);
// 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)
{
char DVW;
char DVW; // var to hold the answer
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');
printf ("\n\n");
int n;
n = numberofRows();
n = numberofRows(); // counts how many rows the file contains
Stud to_be_graded[n];
Filtered applicable[n];
Avg student_list[n];
int applicable_amount;
int student_amount;
Stud to_be_graded[n]; // var to hold first scan results
Filtered applicable[n]; // var to hold applicable
Avg student_list[n]; // var to hold each student and their avg grade
int applicable_amount; // nr of applicable elements
int student_amount; // nr of students
readStudents(to_be_graded);
printf ("\n\n");
......@@ -64,16 +82,17 @@ int main (void)
int numberofRows()
{
FILE *data;
data = fopen ("andmed.txt", "r");
data = fopen ("andmed.txt", "r"); // opens the file
int read = 0;
int ch = 0;
if (data == NULL)
if (data == NULL) // if file doesn't exist, returns 0
{
return 0;
}
// scans every character, if the char is '\n' then the counter counts up
while ((ch = fgetc (data)) != EOF)
{
if (ch == '\n')
......@@ -81,24 +100,26 @@ int numberofRows()
read++;
}
}
fclose (data);
fclose (data); // closes the file
return read;
return read; // returns the nr of rows
}
void readStudents (Stud *to_be_graded)
{
FILE *data;
data = fopen ("andmed.txt", "r");
data = fopen ("andmed.txt", "r"); // opens the file
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)
{
// 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);
i++;
}
fclose (data);
fclose (data); // closes the file
}
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 j = 0;
// if the grade is between 0 and 5, then copy row to applicable
for (i = 0; i < n; i++)
{
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)
}
}
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++)
{
......@@ -125,7 +148,7 @@ int sieve (Stud *to_be_graded, Filtered *applicable, int n, char DVW)
printf ("\n\n");
}
return j;
return j; // returns nr of applicable rows
}
void sortbyName (Filtered *applicable, int applicable_amount, char DVW)
......@@ -134,6 +157,7 @@ void sortbyName (Filtered *applicable, int applicable_amount, char DVW)
int j;
Filtered temp;
// sorts applicable alphabetically
for (i = 0; i < applicable_amount; i++)
{
for (j = 1; j < applicable_amount; j++)
......@@ -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++)
{
......@@ -166,6 +191,7 @@ int list (Filtered *applicable, int applicable_amount, Avg *student_list, char D
int i;
int j = 0;
// makes a list where each name appears once
for (i = 0; i < applicable_amount; i++)
{
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
}
}
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++)
{
......@@ -184,7 +211,7 @@ int list (Filtered *applicable, int applicable_amount, Avg *student_list, char D
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)
......@@ -193,6 +220,7 @@ void average (Avg *student_list, Filtered *applicable, int applicable_amount, in
int j;
int counter = 0;
// calculates the average of each student
for (i = 0; i < student_amount; i++)
{
for (j = 0; j < applicable_amount; j++)
......@@ -207,7 +235,8 @@ void average (Avg *student_list, Filtered *applicable, int applicable_amount, in
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++)
{
......@@ -223,6 +252,7 @@ void sortbyAverage (Avg *student_list, int student_amount, char DVW)
int i, j;
Avg temp;
// sorts list by average grade
for (i = 0; i < student_amount; i++)
{
for (j = 1; j < student_amount; j++)
......@@ -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++)
{
......@@ -252,6 +283,7 @@ void money (Avg *student_list, int student_amount)
int i;
int counter = 0;
// prints the first 3 names
printf ("The money goes to: \n");
for (i = 0; i < 3; i++)
{
......@@ -259,6 +291,8 @@ void money (Avg *student_list, int student_amount)
}
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
{
if (student_list[counter].average == student_list[2].average)
......@@ -268,63 +302,45 @@ void money (Avg *student_list, int student_amount)
}
else
{
break;
break;// stops the loop, if there is a difference in the average grade
};
} 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 counter = student_amount - 4;
int i;
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");
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);
}
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
{
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);
counter--;
}
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);
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