Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <float.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <math.h>
  7. #include <inttypes.h>
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11. int real_exist = 0;
  12. int int_exist = 0;
  13. double min_double = DBL_MAX;
  14. int32_t max_int32 = INT32_MIN;
  15. FILE *file;
  16. if (argc == 1) {
  17. file = stdin;
  18. } else if (argc == 2) {
  19. file = fopen(argv[1], "r");
  20. } else {
  21. fprintf(stderr, "%s", "Too many arguments!\n");
  22. return(1);
  23. }
  24. if (!file) {
  25. fprintf(stderr, "%s", "There is no such a file in directory!\n");
  26. return(1);
  27. }
  28. char arr[65];
  29. char *buf = arr;
  30. int working = 1;
  31. while(1) {
  32. int cur;
  33. char *buf_cur = buf;
  34. cur = fgetc(file);
  35. while (cur == ' ' || cur == '\t' || cur == '\n' || cur == '\r') {
  36. cur = fgetc(file);
  37. }
  38. *buf = cur;
  39. if ((((0 <= cur) && (cur <= 31)) || cur == 127) && cur != ' '
  40. && cur != '\t' && cur != '\n' && cur != '\r') {
  41. fprintf(stderr, "%s", "Unexpected character!\n");
  42. return(1);
  43. }
  44. int ind = 0;
  45. cur = fgetc(file);
  46. while (ind < 63 && !(cur == ' ' || cur == '\t' || cur == '\n' || cur == '\r')
  47. && cur != EOF) {
  48. if ((((0 <= cur) && (cur <= 31)) || cur == 127) && cur != ' '
  49. && cur != '\t' && cur != '\n' && cur != '\r') {
  50. fprintf(stderr, "%s", "Unexpected character!\n");
  51. return(1);
  52. }
  53. buf++;
  54. *buf = cur;
  55. ind++;
  56. cur = fgetc(file);
  57. }
  58. buf++;
  59. *buf = 0;
  60. if (cur == EOF) {
  61. working = 0;
  62. }
  63. int i = 0;
  64. char *iter = buf_cur;
  65. while (*(iter + i)) {
  66. if ((((0 <= *(iter+i)) && (*(iter+i) <= 31)) || *(iter+i) == 127)
  67. && *(iter+i) != ' ' && *(iter+i) != '\t' && *(iter+i) != '\n'
  68. && *(iter+i) != '\r') {
  69. fprintf(stderr, "%s", "Unexpected character!\n");
  70. return(1);
  71. } else {
  72. if (i > 62) {
  73. fprintf(stderr, "%s", "The word is too long!\n");
  74. return(1);
  75. }
  76. }
  77. ++i;
  78. }
  79. errno = 0;
  80. char *eptr;
  81. int64_t numb_int = strtoll(buf_cur, &eptr, 10);
  82. if (*eptr) {
  83. errno = 0;
  84. char *eptr_second;
  85. double numb_double = strtod(buf_cur, &eptr_second);
  86. if (errno == ERANGE) {
  87. fprintf(stderr, "%s", "The real number is too long!\n");
  88. return(1);
  89. }
  90. if (!*eptr_second) {
  91. if (isnan(numb_double)) {
  92. min_double = NAN;
  93. real_exist = 1;
  94. }
  95. if (min_double > numb_double) {
  96. min_double = numb_double;
  97. real_exist = 1;
  98. }
  99. }
  100. } else if (!errno && (int32_t) numb_int == numb_int
  101. && errno != ERANGE) {
  102. if (max_int32 < numb_int) {
  103. max_int32 = numb_int;
  104. int_exist = 1;
  105. }
  106. }
  107. if (!working) {
  108. break;
  109. }
  110. }
  111. if (argc != 1) {
  112. fclose(file);
  113. }
  114. if (!int_exist) {
  115. fprintf(stderr, "%s", "No int found!\n");
  116. return(1);
  117. } else if (!real_exist) {
  118. fprintf(stderr, "%s", "No real found!\n");
  119. return(1);
  120. }
  121. fprintf(stdout, "%"PRId32" %.10g\n", max_int32, min_double);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement