You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

205 lines
7.5 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #ifdef DEBUG
  6. # define D(x) x
  7. #else
  8. # define D(x)
  9. #endif
  10. struct item {
  11. char name[40] ;
  12. float size ;
  13. int amount;
  14. char mu[10] ;
  15. } ;
  16. struct rentt {
  17. char name[40] ;
  18. int day, month, year, hours, minutes, seconds ;
  19. struct item items[256] ;
  20. } ;
  21. int main(int argc, char *argv[])
  22. {
  23. FILE * inpf = fopen("input.txt", "r"),
  24. * outf = fopen("output.txt", "w") ;
  25. // output buffer
  26. char buf[100000] = "\0" ;
  27. // the actual rent
  28. struct rentt rent[500] ;
  29. // input
  30. int cur = 0 ;
  31. while(fgets(rent[cur].name, 32, inpf) != NULL) {
  32. // get rid of \n at the end
  33. int sz = strlen(rent[cur].name) ;
  34. if(rent[cur].name[sz-1] == '\n')
  35. rent[cur].name[--sz] = '\0' ;
  36. // validate name
  37. if(sz < 2 || sz > 30)
  38. return fprintf(outf, "Invalid input!"), 0 ;
  39. for(int i = 0 ; i < sz ; ++i)
  40. if(!isalpha(rent[cur].name[i]) && rent[cur].name[i] != ' ')
  41. return fprintf(outf, "Invalid input!"), 0 ;
  42. // enter date and validate input
  43. char date[100] ;
  44. fgets(date, 100, inpf) ;
  45. if(!(isdigit(date[0]) , isdigit(date[1]) , date[2] == '/'
  46. && isdigit(date[3]) && isdigit(date[4]) && date[5] == '/'
  47. && isdigit(date[6]) && isdigit(date[7])
  48. && isdigit(date[8]) && isdigit(date[9]) && date[10] == ' '
  49. && isdigit(date[11]) && isdigit(date[12]) && date[13] == ':'
  50. && isdigit(date[14]) && isdigit(date[15]) && date[16] == ':'
  51. && isdigit(date[17]) && isdigit(date[18])))
  52. return fprintf(outf, "Invalid input!"), 0 ;
  53. sscanf(date, "%d/%d/%d %d:%d:%d", &rent[cur].day, &rent[cur].month, &rent[cur].year, &rent[cur].hours, &rent[cur].minutes, &rent[cur].seconds) ;
  54. // validate date
  55. if(rent[cur].month == 1 || rent[cur].month == 3 || rent[cur].month == 5
  56. || rent[cur].month == 7 || rent[cur].month == 8
  57. || rent[cur].month == 10 || rent[cur].month == 12)
  58. {
  59. if(rent[cur].day > 31)
  60. return fprintf(outf, "Invalid input!"), 0 ;
  61. } else if( rent[cur].month == 2) {
  62. // leap year case
  63. if(rent[cur].year % 400 == 0) {
  64. if(rent[cur].day > 29)
  65. return fprintf(outf, "Invalid input!"), 0 ;
  66. } else if(rent[cur].year % 4 == 0 && rent[cur].year % 100 != 0) {
  67. if( rent[cur].day > 29)
  68. return fprintf(outf, "Invalid input!"), 0 ;
  69. } else {
  70. if(rent[cur].day > 28)
  71. return fprintf(outf, "Invalid input!"), 0 ;
  72. }
  73. } else if ( rent[cur].month >= 1 && rent[cur].month <= 12) {
  74. if(rent[cur].day > 30)
  75. return fprintf(outf, "Invalid input!"), 0 ;
  76. } else
  77. return fprintf(outf, "Invalid input!"), 0 ;
  78. // validate time
  79. if(!(rent[cur].hours >= 0 && rent[cur].hours <= 23
  80. && rent[cur].minutes >= 0 && rent[cur].minutes <= 59
  81. && rent[cur].seconds >= 0 && rent[cur].seconds <= 59))
  82. return fprintf(outf, "Invalid input!"), 0 ;
  83. sprintf(buf + strlen(buf), "%s has rented ", rent[cur].name) ;
  84. int curi = 0 ;
  85. while(1) {
  86. char temp[256] ;
  87. char lol = fgets(temp, 256, inpf) ;
  88. // check if it is the last line
  89. if(lol == NULL || temp[0] == '\n') break ;
  90. // get rid of \n at the end
  91. sz = strlen(temp) ;
  92. if(temp[sz-1] == '\n')
  93. temp[--sz] = '\0' ;
  94. if(temp[sz] != '\0')
  95. temp[++sz] = '\0' ;
  96. // enter and validate the name of an item
  97. int curp = 0 ;
  98. for(int i = 0 ; i < sz ; ++i) {
  99. if(isalpha(temp[i]) || temp[i] == ' ') {
  100. rent[cur].items[curi].name[curp++] = temp[i] ;
  101. }
  102. else {
  103. if(curp == 0)
  104. return fprintf(outf, "Invalid input!"), 0 ;
  105. if(rent[cur].items[curi].name[curp-1] == ' ')
  106. rent[cur].items[curi].name[--curp] = '\0' ;
  107. else
  108. return fprintf(outf, "Invalid input!"), 0 ;
  109. break ;
  110. }
  111. }
  112. // copy only the size, the amount and the measurement unit of an item
  113. char vals[256] ;
  114. memcpy(vals, temp+curp, strlen(temp+curp)+1) ;
  115. // format them
  116. sscanf(vals, "%f%d%s", &rent[cur].items[curi].size,
  117. &rent[cur].items[curi].amount,
  118. rent[cur].items[curi].mu) ;
  119. D(printf("vals: %s\nsz: %f\nam: %d\nmu: %s\n",
  120. temp, rent[cur].items[curi].size,
  121. rent[cur].items[curi].amount,
  122. rent[cur].items[curi].mu)) ;
  123. // validation
  124. if(!(rent[cur].items[curi].size > 0.0 && rent[cur].items[curi].size <= 200.0
  125. && rent[cur].items[curi].amount > 0 && rent[cur].items[curi].amount <= 30
  126. && (strcmp(rent[cur].items[curi].mu, "pair") == 0
  127. || strcmp(rent[cur].items[curi].mu, "pcs") == 0)))
  128. return fprintf(outf, "Invalid input!"), 0 ;
  129. // the case when there are several pairs
  130. if(strcmp(rent[cur].items[curi].mu, "pair") == 0
  131. && rent[cur].items[curi].amount > 1)
  132. sprintf(rent[cur].items[curi].mu, "pairs") ;
  133. curi ++ ;
  134. }
  135. // append items to the buffer
  136. if(curi == 1) {
  137. sprintf(buf + strlen(buf), "%d %s of %s of size %g",
  138. rent[cur].items[0].amount, rent[cur].items[0].mu,
  139. rent[cur].items[0].name, rent[cur].items[0].size) ;
  140. } else for(int i = 0 ; i < curi ; ++i)
  141. {
  142. if(i == curi-1)
  143. sprintf(buf + strlen(buf), " and %d %s of %s of size %g",
  144. rent[cur].items[i].amount, rent[cur].items[i].mu,
  145. rent[cur].items[i].name, rent[cur].items[i].size) ;
  146. else if(i == 0)
  147. sprintf(buf + strlen(buf), "%d %s of %s of size %g",
  148. rent[cur].items[i].amount, rent[cur].items[i].mu,
  149. rent[cur].items[i].name, rent[cur].items[i].size) ;
  150. else
  151. sprintf(buf + strlen(buf), ", %d %s of %s of size %g",
  152. rent[cur].items[i].amount, rent[cur].items[i].mu,
  153. rent[cur].items[i].name, rent[cur].items[i].size) ;
  154. }
  155. // if there is no any item
  156. if(curi <= 0)
  157. return fprintf(outf, "Invalid input!"), 0 ;
  158. // append the date/time to the buffer
  159. sprintf(buf + strlen(buf), " on %d%d/%d%d/%d%d%d%d at %d%d:%d%d:%d%d.\n",
  160. rent[cur].day/10, rent[cur].day%10,
  161. rent[cur].month/10, rent[cur].month%10,
  162. rent[cur].year/1000, rent[cur].year/100%10,
  163. rent[cur].year/10%10, rent[cur].year%10,
  164. rent[cur].hours/10, rent[cur].hours%10,
  165. rent[cur].minutes/10, rent[cur].minutes%10,
  166. rent[cur].seconds/10, rent[cur].seconds%10) ;
  167. cur++ ;
  168. }
  169. // print out the buffer
  170. fprintf(outf, "%s", buf) ;
  171. }