fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 30
  4. #define MAX_PLAYERS 1000
  5.  
  6. // The struct that stores either a boss or a clerk.
  7. struct bbplayer {
  8. char name[SIZE];
  9. char pos[2];
  10. double pwrrank;
  11. };
  12.  
  13.  
  14. // Reads the information from the file pointed to by ifp into list
  15. // and returns the number of records found in the file, which must be 1000
  16. // or less.
  17. int readfile(FILE *ifp, struct bbplayer list[]);
  18.  
  19. // Swaps bbplayers in list in indexes index1 and index2.
  20. void swap(struct bbplayer list[], int index1, int index2);
  21.  
  22. // Sorts the array list that stores length employees based on
  23. // their power ranking.
  24. void sort(struct bbplayer list[], int length);
  25.  
  26. // Writes out the information in workers to the file pointed to by ofp
  27. // in the order the records are stored in workers. length must be the
  28. // total number of records stored.
  29. void writefile(FILE *ofp, struct bbplayer list[], int length);
  30.  
  31. // Assumes that list is sorted by decreasing order of power rankings.
  32. // Prints out to the screen the num best players that have the position
  33. // pos.
  34. void printbest(struct bbplayer list[], char pos[], int num);
  35.  
  36. int main(void) {
  37.  
  38. FILE *ifp, *ofp;
  39. char infile[SIZE], outfile[SIZE];
  40. struct bbplayer NBA[MAX_PLAYERS];
  41. int len, i;
  42.  
  43. // Get the input filename.
  44. printf("Please enter the input file with player data.\n");
  45. scanf("%s", infile);
  46. ifp = fopen(infile,"r");
  47.  
  48. // File not found.
  49. if (ifp == NULL)
  50. printf("Sorry, the file does not exist.\n");
  51.  
  52. // Open and read from the file.
  53. else {
  54. len = readfile(ifp, NBA);
  55. fclose(ifp);
  56.  
  57. sort(NBA, len); // Sort the employees in order.
  58.  
  59. // Get the file to write the output to.
  60. printf("Please enter the name of the file you want to store the sorted player data.\n");
  61. scanf("%s", outfile);
  62. ofp = fopen(outfile,"w");
  63.  
  64. writefile(ofp, NBA, len); // Write the output file.
  65. fclose(ofp);
  66. printf("Your outputfile has successfully been stored.\n");
  67.  
  68. // Write out all-star team.
  69. printf("Here is your All-Star team:\n\n");
  70. printbest(NBA, "C", 1);
  71. printbest(NBA, "F", 2);
  72. printbest(NBA, "G", 2);
  73.  
  74. }
  75.  
  76. return 0;
  77. }
  78.  
  79. // Reads the information from the file pointed to by ifp into list
  80. // and returns the number of records found in the file, which must be 1000
  81. // or less.
  82. int readfile(FILE *ifp, struct bbplayer list[]) {
  83.  
  84. int num, index;
  85. int pts, reb, ast, stl, blk;
  86.  
  87. // Read in the number of records.
  88. fscanf(ifp, "%d", &num);
  89.  
  90. // Read in each record one by one.
  91. for (index=0; index<num; index++) {
  92.  
  93. // Read in the player's name and position.
  94. fscanf(ifp, "%s", list[index].name);
  95. fscanf(ifp, "%s", list[index].pos);
  96.  
  97. fscanf(ifp, "%d", &pts);
  98. fscanf(ifp, "%d", &reb);
  99. fscanf(ifp, "%d", &ast);
  100. fscanf(ifp, "%d", &stl);
  101. fscanf(ifp, "%d", &blk);
  102.  
  103. list[index].pwrrank = (pts+2*reb+3*ast+5*stl+5*blk)/100.0;
  104. }
  105.  
  106. return num; // Returns the number of records stored.
  107. }
  108.  
  109. // Swaps bbplayers in list in indexes index1 and index2.
  110. void swap(struct bbplayer list[], int index1, int index2) {
  111.  
  112. struct bbplayer temp;
  113.  
  114. // Copy the employee in index1 into temp.
  115. strcpy(temp.name, list[index1].name);
  116. strcpy(temp.pos, list[index1].pos);
  117. temp.pwrrank = list[index1].pwrrank;
  118.  
  119. // Copy the bbplayer in index2 into index1.
  120. strcpy(list[index1].name, list[index2].name);
  121. strcpy(list[index1].pos, list[index2].pos);
  122. list[index1].pwrrank = list[index2].pwrrank;
  123.  
  124. // Copy the original bbplayer in index1 into index2.
  125. strcpy(list[index2].name, temp.name);
  126. strcpy(list[index2].pos, temp.pos);
  127. list[index2].pwrrank = temp.pwrrank;
  128.  
  129. }
  130.  
  131.  
  132. // Sorts the array list that stores length employees based on
  133. // their power ranking.
  134. void sort(struct bbplayer list[], int length) {
  135.  
  136. int i,j;
  137.  
  138. // Run a bubble sort.
  139.  
  140. // Run length-1 iterations.
  141. for (i=0; i<length-1; i++) {
  142.  
  143. // For each iteration, swap successive elements if they are out of
  144. // order. At the end of each iteration, the value in the index
  145. // length-1-i will be the maximum of the remaining candidates.
  146. for (j=0; j<length-1-i; j++) {
  147.  
  148. // Only swap employees if they are out of place.
  149. if (list[j].pwrrank < list[j+1].pwrrank) {
  150. swap(list, j, j+1);
  151. } // end if
  152.  
  153. } // end for j
  154. } // end for i
  155. }
  156.  
  157. // Writes out the information in workers to the file pointed to by ofp
  158. // in the order the records are stored in workers. length must be the
  159. // total number of records stored.
  160. void writefile(FILE *ofp, struct bbplayer list[], int length) {
  161.  
  162. int index;
  163.  
  164. // Print out the number of lines in the file.
  165. fprintf(ofp, "%d\n", length);
  166.  
  167. // Loop through each employee.
  168. for (index=0; index<length; index++) {
  169.  
  170. // Print out the employee's name.
  171. fprintf(ofp,"%s %s %.2lf\n", list[index].name, list[index].pos,
  172. list[index].pwrrank);
  173. }
  174. }
  175.  
  176.  
  177. // Assumes that list is sorted by decreasing order of power rankings.
  178. // Also assumes that there are num or more players of position pos in
  179. // list.
  180. // Prints out to the screen the num best players that have the position
  181. // pos.
  182. void printbest(struct bbplayer list[], char pos[], int num) {
  183.  
  184. int cnt = 0, index=0;
  185.  
  186. while (cnt < num) {
  187.  
  188. if (strcmp(list[index].pos,pos) == 0) {
  189. printf("%s %s %.2lf\n",pos, list[index].name, list[index].pwrrank);
  190. cnt++;
  191. }
  192. index++;
  193. }
  194.  
  195. }
  196.  
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
Please enter the input file with player data.
Sorry, the file does not exist.