Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <float.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <math.h>
- #include <inttypes.h>
- int main(int argc, char* argv[])
- {
- int real_exist = 0;
- int int_exist = 0;
- double min_double = DBL_MAX;
- int32_t max_int32 = INT32_MIN;
- FILE *file;
- if (argc == 1) {
- file = stdin;
- } else if (argc == 2) {
- file = fopen(argv[1], "r");
- } else {
- fprintf(stderr, "%s", "Too many arguments!\n");
- return(1);
- }
- if (!file) {
- fprintf(stderr, "%s", "There is no such a file in directory!\n");
- return(1);
- }
- char arr[65];
- char *buf = arr;
- int working = 1;
- while(1) {
- int cur;
- char *buf_cur = buf;
- cur = fgetc(file);
- while (cur == ' ' || cur == '\t' || cur == '\n' || cur == '\r') {
- cur = fgetc(file);
- }
- *buf = cur;
- if ((((0 <= cur) && (cur <= 31)) || cur == 127) && cur != ' '
- && cur != '\t' && cur != '\n' && cur != '\r') {
- fprintf(stderr, "%s", "Unexpected character!\n");
- return(1);
- }
- int ind = 0;
- cur = fgetc(file);
- while (ind < 63 && !(cur == ' ' || cur == '\t' || cur == '\n' || cur == '\r')
- && cur != EOF) {
- if ((((0 <= cur) && (cur <= 31)) || cur == 127) && cur != ' '
- && cur != '\t' && cur != '\n' && cur != '\r') {
- fprintf(stderr, "%s", "Unexpected character!\n");
- return(1);
- }
- buf++;
- *buf = cur;
- ind++;
- cur = fgetc(file);
- }
- buf++;
- *buf = 0;
- if (cur == EOF) {
- working = 0;
- }
- int i = 0;
- char *iter = buf_cur;
- while (*(iter + i)) {
- if ((((0 <= *(iter+i)) && (*(iter+i) <= 31)) || *(iter+i) == 127)
- && *(iter+i) != ' ' && *(iter+i) != '\t' && *(iter+i) != '\n'
- && *(iter+i) != '\r') {
- fprintf(stderr, "%s", "Unexpected character!\n");
- return(1);
- } else {
- if (i > 62) {
- fprintf(stderr, "%s", "The word is too long!\n");
- return(1);
- }
- }
- ++i;
- }
- errno = 0;
- char *eptr;
- int64_t numb_int = strtoll(buf_cur, &eptr, 10);
- if (*eptr) {
- errno = 0;
- char *eptr_second;
- double numb_double = strtod(buf_cur, &eptr_second);
- if (errno == ERANGE) {
- fprintf(stderr, "%s", "The real number is too long!\n");
- return(1);
- }
- if (!*eptr_second) {
- if (isnan(numb_double)) {
- min_double = NAN;
- real_exist = 1;
- }
- if (min_double > numb_double) {
- min_double = numb_double;
- real_exist = 1;
- }
- }
- } else if (!errno && (int32_t) numb_int == numb_int
- && errno != ERANGE) {
- if (max_int32 < numb_int) {
- max_int32 = numb_int;
- int_exist = 1;
- }
- }
- if (!working) {
- break;
- }
- }
- if (argc != 1) {
- fclose(file);
- }
- if (!int_exist) {
- fprintf(stderr, "%s", "No int found!\n");
- return(1);
- } else if (!real_exist) {
- fprintf(stderr, "%s", "No real found!\n");
- return(1);
- }
- fprintf(stdout, "%"PRId32" %.10g\n", max_int32, min_double);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement