Advertisement
Guest User

test

a guest
Sep 21st, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. /* type definition of student */
  7. typedef struct student {
  8. int who; /* 0=John, 1=Peter, 2=William */
  9. float paid; /* amount that student paid */
  10. } student;
  11.  
  12. /* global variables */
  13. student firstStudent, secondStudent, thirdStudent;
  14. float totalPaid;
  15. /************************************************************************
  16. * ABSTRACT DATA TYPE: the following functions form the ADT student.
  17. * Do not change the ADT part of this program !
  18. */
  19.  
  20. /* read the info of a student from the keyboard and return it. */
  21. void readStudent(student *stud) {
  22. scanf("%d %f", &stud->who, &stud->paid);
  23. }
  24.  
  25. /* print the name of a student. */
  26. void printNameOfStudent(student stud) {
  27. printf("%s", (stud.who == 0 ? "John" : (stud.who == 1 ? "Peter" : "William")));
  28. }
  29.  
  30. /* returns the amount that a student paid. */
  31. float paidByStudent(student stud) {
  32. return stud.paid;
  33. }
  34.  
  35. /* swaps two students */
  36. void swapStudents(student *a, student *b) {
  37. student h = *a;
  38. *a = *b;
  39. *b = h;
  40. }
  41.  
  42. void orderStudents(){
  43. /* Order the students by the amount of money
  44. they need to pay or receive. */
  45. if(paidByStudent(firstStudent) > paidByStudent(secondStudent)) swapStudents(&firstStudent, &secondStudent);
  46. if(paidByStudent(secondStudent) > paidByStudent(thirdStudent)) swapStudents(&secondStudent, &thirdStudent);
  47. if(paidByStudent(firstStudent) > paidByStudent(secondStudent)) swapStudents(&firstStudent, &secondStudent);
  48. }
  49.  
  50. float absoluteFloat(float k)
  51. {
  52. return (k < 0) ? (k * -1) : (k);
  53. }
  54.  
  55. void printResult(student *stud)
  56. {
  57. printNameOfStudent(*stud);
  58. /* If price to pay is lower or equal to zero student will receveive money
  59. Converts price to pay to an absolute value */
  60.  
  61. printf(" %s %3.2f\n",((totalPaid / 3 - stud->paid > 0) ? ("pays") : ("receives")),absoluteFloat(totalPaid / 3 - stud->paid));
  62. }
  63.  
  64.  
  65. void readInput() {
  66. readStudent(&firstStudent);
  67. readStudent(&secondStudent);
  68. readStudent(&thirdStudent);
  69. }
  70.  
  71.  
  72. int main(int argc, char *argv[]) {
  73. readInput();
  74.  
  75. /*Total of amound spent by student */
  76. totalPaid = paidByStudent(firstStudent) + paidByStudent(secondStudent) + paidByStudent(thirdStudent);
  77.  
  78. /*Place students in the correct order */
  79. orderStudents();
  80.  
  81. /*Print Result */
  82. printResult(&firstStudent);
  83. printResult(&secondStudent);
  84. printResult(&thirdStudent);
  85.  
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement