fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void crdel(char *s)
  5. {
  6. while (*s != '\n')s++;
  7. *s = '\0';
  8. }
  9.  
  10. int main(void)
  11. {
  12. char title[100], buf[100], infile[30], outfile[30], name[20];
  13. FILE *fpi, *fpo;
  14. int cnt, *total, *rank, w[5], i, j, number, min, max, tmp;
  15.  
  16. printf("input file name = ");
  17. scanf("%s", infile);
  18. fpi = fopen(infile, "r");
  19. if (fpi == NULL)
  20. {
  21. printf("input file (%s) open error\n", infile);
  22. return 0;
  23. }
  24.  
  25. printf("output file name = ");
  26. scanf("%s", outfile);
  27. fpo = fopen(outfile, "w");
  28. if (fpo == NULL)
  29. {
  30. printf("output file (%s) open error\n", outfile);
  31. return 0;
  32. }
  33.  
  34. for (cnt = 0; fgets(buf, 100, fpi) ; cnt++);
  35. cnt--;
  36. total = (int *)calloc(sizeof(int) * cnt, 0);
  37. rank = (int *)calloc(sizeof(int) * cnt, 0);
  38.  
  39. rewind(fpi);
  40. fgets(title, 100, fpi);
  41. for ( i = 0; i < cnt; ++i)
  42. {
  43. fgets(buf, 100, fpi);
  44. sscanf(buf, "%s %d %d %d %d %d %d %d", name, &number, w, w + 1, w + 2, w + 3, w + 4, w + 5);
  45. for ( j = 0; j < 5; ++j)
  46. total[i] += w[j];
  47. }
  48.  
  49. min = total[0];
  50. max = total[0];
  51. for ( i = 1; i < cnt; ++i)
  52. {
  53. if (min > total[i])min = total[i];
  54. if (max < total[i])max = total[i];
  55. }
  56.  
  57. tmp = 1;
  58. for ( i = min; i <= max; ++i)
  59. for ( j = 0; j < cnt; ++j)
  60. if (total[j] == i)
  61. rank[j] = tmp++;
  62.  
  63. rewind(fpi);
  64. fgets(title, 100, fpi);
  65. crdel(title);
  66. fprintf(fpo, "%s rank\n", title);
  67. for ( i = 0; i < cnt; ++i)
  68. {
  69. fgets(buf, 100, fpi);
  70. crdel(buf);
  71. fprintf(fpo, "%s %d\n", buf, rank[i]);
  72. }
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0.02s 1812KB
stdin
Standard input is empty
stdout
input file name = input file (��C�) open error