Jesus Christ! I made it work. But please tell me there is a easy and shorter way than this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct student {
int who;
float paid;
} student;
/* global variables */
student firstStudent, secondStudent, thirdStudent;
void readStudent(student *stud) {
scanf("%d %f", &stud->who, &stud->paid);
}
void printNameOfStudent(student stud) {
printf("%s", (stud.who == 0 ? "John" : (stud.who == 1 ? "Peter" : "William")));
}
float paidByStudent(student stud) {
return stud.paid;
}
void swapStudents(student *a, student *b) {
student h = *a;
*a = *b;
*b = h;
}
void readInput() {
readStudent(&firstStudent);
readStudent(&secondStudent);
readStudent(&thirdStudent);
}
int main(int argc, char *argv[]) {
float fairShare, difference;
readInput();
/* first swap the students and then calculate*/
if (paidByStudent(firstStudent) > paidByStudent(secondStudent) || paidByStudent(firstStudent) > paidByStudent(thirdStudent)) {
if (paidByStudent(firstStudent) > paidByStudent(secondStudent) && paidByStudent(firstStudent) > paidByStudent(thirdStudent)) {
if (paidByStudent(thirdStudent) > paidByStudent(secondStudent)) {
swapStudents(&firstStudent, &thirdStudent);
swapStudents(&thirdStudent, &secondStudent);
}
if (paidByStudent(thirdStudent) == paidByStudent(secondStudent)) {
swapStudents(&secondStudent, &firstStudent);
swapStudents(&thirdStudent, &secondStudent);
}
if (paidByStudent(secondStudent) > paidByStudent(thirdStudent)) {
swapStudents(&firstStudent, &thirdStudent);
}
}
if (paidByStudent(firstStudent) > paidByStudent(secondStudent) && paidByStudent(thirdStudent) > paidByStudent(firstStudent)) {
swapStudents(&firstStudent, &secondStudent);
}
if (paidByStudent(firstStudent) > paidByStudent(thirdStudent) && paidByStudent(secondStudent) > paidByStudent(firstStudent)) {
swapStudents(&firstStudent, &thirdStudent);
swapStudents(&secondStudent, &thirdStudent);
}
}
if (paidByStudent(secondStudent) > paidByStudent(firstStudent) && paidByStudent(secondStudent) > paidByStudent(thirdStudent)) {
if (paidByStudent(firstStudent) > paidByStudent(thirdStudent)) {
swapStudents(&thirdStudent, &secondStudent);
swapStudents(&firstStudent, &secondStudent);
}
if (paidByStudent(thirdStudent) > paidByStudent(firstStudent)) {
swapStudents(&secondStudent, &thirdStudent);
}
if (paidByStudent(firstStudent) == paidByStudent(thirdStudent)) {
swapStudents(&secondStudent, &thirdStudent);
}
}
fairShare = ((paidByStudent(firstStudent) + paidByStudent(secondStudent) + paidByStudent(thirdStudent))/3);
if(fairShare > paidByStudent(firstStudent)){
difference = fairShare - paidByStudent(firstStudent);
printNameOfStudent(firstStudent);
printf(" pays %3.2f\n", difference);
}
if(fairShare > paidByStudent(secondStudent)){
difference = fairShare - paidByStudent(secondStudent);
printNameOfStudent(secondStudent);
printf(" pays %3.2f\n", difference);
}
if(fairShare > paidByStudent(thirdStudent)){
difference = fairShare - paidByStudent(thirdStudent);
printNameOfStudent(thirdStudent);
printf(" pays %3.2f\n", difference);
}
if(fairShare == paidByStudent(firstStudent)){
printNameOfStudent(firstStudent);
printf(" receives 0.00\n");
}
if(fairShare == paidByStudent(secondStudent)){
printNameOfStudent(secondStudent);
printf(" receives 0.00\n");
}
if(fairShare == paidByStudent(thirdStudent)){
printNameOfStudent(thirdStudent);
printf(" receives 0.00\n");
}
if(fairShare < paidByStudent(firstStudent)){
difference = paidByStudent(firstStudent) - fairShare;
printNameOfStudent(firstStudent);
printf(" receives %3.2f\n", difference);
}
if(fairShare < paidByStudent(secondStudent)){
difference = paidByStudent(secondStudent) - fairShare;
printNameOfStudent(secondStudent);
printf(" receives %3.2f\n", difference);
}
if(fairShare < paidByStudent(thirdStudent)){
difference = paidByStudent(thirdStudent) - fairShare;
printNameOfStudent(thirdStudent);
printf(" receives %3.2f\n", difference);
}
return 0;
}