Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- /* type definition of student */
- typedef struct student {
- int who; /* 0=John, 1=Peter, 2=William */
- float paid; /* amount that student paid */
- } student;
- /* global variables */
- student firstStudent, secondStudent, thirdStudent;
- float totalPaid;
- /************************************************************************
- * ABSTRACT DATA TYPE: the following functions form the ADT student.
- * Do not change the ADT part of this program !
- */
- /* read the info of a student from the keyboard and return it. */
- void readStudent(student *stud) {
- scanf("%d %f", &stud->who, &stud->paid);
- }
- /* print the name of a student. */
- void printNameOfStudent(student stud) {
- printf("%s", (stud.who == 0 ? "John" : (stud.who == 1 ? "Peter" : "William")));
- }
- /* returns the amount that a student paid. */
- float paidByStudent(student stud) {
- return stud.paid;
- }
- /* swaps two students */
- void swapStudents(student *a, student *b) {
- student h = *a;
- *a = *b;
- *b = h;
- }
- void orderStudents(){
- /* Order the students by the amount of money
- they need to pay or receive. */
- if(paidByStudent(firstStudent) > paidByStudent(secondStudent)) swapStudents(&firstStudent, &secondStudent);
- if(paidByStudent(secondStudent) > paidByStudent(thirdStudent)) swapStudents(&secondStudent, &thirdStudent);
- if(paidByStudent(firstStudent) > paidByStudent(secondStudent)) swapStudents(&firstStudent, &secondStudent);
- }
- float absoluteFloat(float k)
- {
- return (k < 0) ? (k * -1) : (k);
- }
- void printResult(student *stud)
- {
- printNameOfStudent(*stud);
- /* If price to pay is lower or equal to zero student will receveive money
- Converts price to pay to an absolute value */
- printf(" %s %3.2f\n",((totalPaid / 3 - stud->paid > 0) ? ("pays") : ("receives")),absoluteFloat(totalPaid / 3 - stud->paid));
- }
- void readInput() {
- readStudent(&firstStudent);
- readStudent(&secondStudent);
- readStudent(&thirdStudent);
- }
- int main(int argc, char *argv[]) {
- readInput();
- /*Total of amound spent by student */
- totalPaid = paidByStudent(firstStudent) + paidByStudent(secondStudent) + paidByStudent(thirdStudent);
- /*Place students in the correct order */
- orderStudents();
- /*Print Result */
- printResult(&firstStudent);
- printResult(&secondStudent);
- printResult(&thirdStudent);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement