Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // Function Declarations
  4. int getStu (FILE* spStu,
  5. int* stuID, int* exam1,
  6. int* exam2, int* final);
  7. int writeStu (FILE* spGrades,
  8. int stuID, int avrg, char grade);
  9. void calcGrade (int exam1, int exam2, int final,
  10. int* avrg, char* grade);
  11.  
  12. int main (void)
  13. {
  14. // Local Declarations
  15. FILE* spStu;
  16. FILE* spGrades;
  17.  
  18. int stuID;
  19. int exam1;
  20. int exam2;
  21. int final;
  22. int avrg;
  23.  
  24. char grade;
  25.  
  26. // Statements
  27. printf("Begin student grading\n");
  28. if (!(spStu = fopen ("stu_grades.txt", "r")))
  29. {
  30. printf("\aError opening student file\n");
  31. return 100;
  32. } // if open input
  33.  
  34. if (!(spGrades = fopen ("P07-06Gr.DAT", "w")))
  35. {
  36. printf("\aError opening grades file\n");
  37. return 102;
  38. } // if open output
  39.  
  40. while (getStu (spStu, &stuID, &exam1, &exam2, &final))
  41. {
  42. calcGrade (exam1, exam2, final, &avrg, &grade);
  43. writeStu (spGrades, stuID, avrg, grade);
  44. } // while
  45.  
  46. fclose (spStu);
  47. fclose (spGrades);
  48.  
  49. printf("End student grading\n");
  50. return 0;
  51. } // main
  52.  
  53. /* ======================== getStu =======================
  54. Reads data from student file.
  55. Pre spStu is an open file.
  56. stuID, exam1, exam2, final pass by address
  57. Post reads student ID and exam scores
  58. if data read --returns 1
  59. if EOF or error--returns 0
  60. */
  61. int getStu (FILE* spStu, int* stuID, int* exam1,
  62. int* exam2, int* final)
  63. {
  64. // Local Declarations
  65. int ioResult;
  66.  
  67. // Statements
  68. ioResult = fscanf(spStu, "%d%d%d%d", stuID,
  69. exam1, exam2, final);
  70. if (ioResult == EOF)
  71. return 0;
  72. else if (ioResult != 4)
  73. {
  74. printf("\aError reading data\n");
  75. return 0;
  76. } // if
  77. else
  78. return 1;
  79. } // getStu
  80.  
  81. /* ==================== calcGrade ===================
  82. Determine student grade based on absolute scale.
  83. Pre exam1, exam2, and final contain scores
  84. avrg and grade are addresses of variables
  85. Post Average and grade copied to addresses
  86. */
  87. void calcGrade (int exam1, int exam2, int final,
  88. int* avrg, char* grade)
  89. {
  90. // Statements
  91. *avrg = (exam1 + exam2 + final) / 3;
  92. if (*avrg >= 90)
  93. *grade = 'A';
  94. else if (*avrg >= 80)
  95. *grade = 'B';
  96. else if (*avrg >= 70)
  97. *grade = 'C';
  98. else if (*avrg >= 60)
  99. *grade = 'D';
  100. else
  101. *grade = 'F';
  102. return;
  103. } // calcGrade
  104.  
  105. /* ===================== writeStu ====================
  106. Writes student grade data to output file.
  107. Pre spGrades is an open file
  108. stuID, avrg, and grade have values to write
  109. Post Data written to file
  110. */
  111. int writeStu (FILE* spGrades, int stuID,
  112. int avrg, char grade)
  113. {
  114. // Statements
  115. fprintf(spGrades, "%04d %d %c\n",
  116. stuID, avrg, grade);
  117. return 0;
  118. } // writeStu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement