Advertisement
Guest User

Test1

a guest
Oct 14th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define FEEL_LEN 6
  6. #define STRUCT_MAX 20
  7. #define DAY_LEN 8
  8.  
  9. typedef struct day{
  10. char date[DAY_LEN];
  11. char feeling[FEEL_LEN];
  12. int run;
  13. int walk;
  14. int stand;
  15. int sit;
  16. int lay;
  17. }day;
  18.  
  19. int ReadFile(day List[],int max);
  20. void mCommon(day List[],int dCount);
  21. void output1(day List[],int dCount);
  22.  
  23. int main(void){
  24. day days[STRUCT_MAX];
  25. int daysCount = ReadFile(days, STRUCT_MAX);
  26. mCommon(days, daysCount);
  27. output1(days, daysCount);
  28. return 0;
  29. }
  30.  
  31. int ReadFile(day List[], int max){
  32. FILE *fp = fopen("001.txt", "r");
  33.  
  34. if (fp == NULL){
  35. exit(2);
  36. }
  37. int i = 0;
  38. while(fscanf(fp, "%s %s %d %d %d %d %d", List[i].date,
  39. List[i].feeling, &List[i].run, &List[i].walk, &List[i].stand,
  40. &List[i].sit, &List[i].lay) == 7){
  41. i++;
  42. if(i == max){
  43. break;
  44. }
  45. }
  46. fclose(fp);
  47. return i;
  48. }
  49.  
  50. void mCommon(day List[],int dCount){
  51. int count[4] = {0};
  52. int max, maxIndex;
  53. for(int i = 0; i < dCount; i++){
  54. if(strcmp(List[i].feeling, "great") == 0){
  55. count[0] += 1;
  56. }else if(strcmp(List[i].feeling, "bad") == 0){
  57. count[1] += 1;
  58. }else if(strcmp(List[i].feeling, "good") == 0){
  59. count[2] += 1;
  60. }else if(strcmp(List[i].feeling, "awesome") == 0){
  61. count[3] += 1;
  62. }
  63. }
  64. max = count[0];
  65. for(int i = 1; i < 4; i++){
  66. if(count[i] > max){
  67. max = count[i];
  68. maxIndex = i;
  69. }
  70. }
  71. if(maxIndex == 0){
  72. printf("The most common feeling is great appears %d\n", max);
  73. }else if(maxIndex == 1){
  74. printf("The most common feeling is bad appears %d\n", max);
  75. }else if(maxIndex == 2){
  76. printf("The most common feeling is good appears %d\n", max);
  77. }else if(maxIndex == 3){
  78. printf("The most common feeling is awesome appears %d\n", max);
  79. }
  80. }
  81.  
  82. void output1(day List[], int dCount){
  83. FILE *outf = fopen("out.txt", "w");
  84. int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0;
  85. float per1, per2, per3;
  86. for(int i = 0; i < dCount; i++){
  87. sum1 += List[i].run;
  88. sum2 += List[i].walk;
  89. sum3 += List[i].stand;
  90. sum4 += List[i].sit;
  91. sum5 += List[i].lay;
  92. per1 = List[i].run * 1.37;
  93. per2 = List[i].walk * 0.59;
  94. per3 = List[i].stand * 0.18;
  95. float sum = per1 + per2 + per3;
  96. printf("On day %.*s/%.*s/%.*s person was got: %0.2f\n", 2,
  97. 0 + List[i].date, 2, 2 + List[i].date, 4,
  98. 4 + List[i].date, sum);
  99. printf("Person was running for %d:%d\n", List[i].run / 60,
  100. List[i].run % 60);
  101. printf("Person was walking for %d:%d\n", List[i].walk / 60,
  102. List[i].walk % 60);
  103. printf("Person was standing for %d:%d\n", List[i].stand / 60,
  104. List[i].stand % 60);
  105. printf("Person was sitting for %d:%d\n", List[i].sit / 60,
  106. List[i].sit % 60);
  107. printf("Person was laying down for %d:%d\n", List[i].lay / 60,
  108. List[i].lay % 60);
  109. fprintf(outf, "On day %.*s/%.*s/%.*s person was got: %0.2f\n",
  110. 2, 0 + List[i].date, 2, 2 + List[i].date, 4,
  111. 4 + List[i].date, sum);
  112. fprintf(outf, "Person was running for %d:%d\n",
  113. List[i].run / 60, List[i].run % 60);
  114. fprintf(outf, "Person was walking for %d:%d\n",
  115. List[i].walk / 60, List[i].walk % 60);
  116. fprintf(outf, "Person was standing for %d:%d\n",
  117. List[i].stand / 60, List[i].stand % 60);
  118. fprintf(outf, "Person was sitting for %d:%d\n",
  119. List[i].sit / 60, List[i].sit % 60);
  120. fprintf(outf, "Person was laying down for %d:%d\n",
  121. List[i].lay / 60, List[i].lay % 60);
  122. }
  123. printf("Average time running is %d:%d\n", (sum1 / dCount) / 60,
  124. (sum1 / dCount) % 60);
  125. printf("Average time running is %d:%d\n", (sum2 / dCount) / 60,
  126. (sum2 / dCount) % 60);
  127. printf("Average time running is %d:%d\n", (sum3 / dCount) / 60,
  128. (sum3 / dCount) % 60);
  129. printf("Average time running is %d:%d\n", (sum4 / dCount) / 60,
  130. (sum4 / dCount) % 60);
  131. printf("Average time running is %d:%d\n", (sum5 / dCount) / 60,
  132. (sum5 / dCount) % 60);
  133. fprintf(outf, "Average time running is %d:%d\n", (sum1 / dCount) /
  134. 60, (sum1 / dCount) % 60);
  135. fprintf(outf, "Average time running is %d:%d\n", (sum2 / dCount) /
  136. 60, (sum2 / dCount) % 60);
  137. fprintf(outf, "Average time running is %d:%d\n", (sum3 / dCount) /
  138. 60, (sum3 / dCount) % 60);
  139. fprintf(outf, "Average time running is %d:%d\n", (sum4 / dCount) /
  140. 60, (sum4 / dCount) % 60);
  141. fprintf(outf, "Average time running is %d:%d\n", (sum5 / dCount) /
  142. 60, (sum5 / dCount) % 60);
  143. fclose(outf);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement