The_Law

Untitled

Oct 15th, 2018
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <errno.h>
  5.  
  6. enum
  7. {
  8.     EMPTY_STRING = 0x0bad1dea,
  9.     WRONG_FORMAT = 0xbedabeda,
  10.     NEG = -1,
  11.     BASE = 10
  12. };
  13.  
  14. char *getline2(FILE *f);
  15.  
  16. int
  17. main(void)
  18. {
  19.     char *strt_string;
  20.     int cnt_string = 0;
  21.     while ((strt_string = getline2(stdin)) != NULL) {
  22.         ++cnt_string;
  23.         char *it = strt_string;
  24.         while(isspace(*it)) {
  25.             ++it;
  26.         }
  27.         if (*it == 0) {
  28.             printf("%d\n", EMPTY_STRING + cnt_string);
  29.         }
  30.         else {
  31.             _Bool wrong_string = 0;
  32.             _Bool was_sign = 0;
  33.             unsigned int cnt_number = 1;
  34.             int sgn = 1;
  35.             unsigned int sum = 0;
  36.             while (*it != 0) {
  37.                 if (isspace(*it)) {
  38.                     if (was_sign) {
  39.                         printf("%d\n", (int) WRONG_FORMAT + cnt_string);
  40.                         wrong_string = 1;
  41.                         break;
  42.                     }
  43.                     ++it;
  44.                     continue;
  45.                 }
  46.                 if ((*it < '0' || '9' < *it) && (*it != '+') && (*it != '-')) {
  47.                     printf("%d\n", (int) WRONG_FORMAT + cnt_string);
  48.                     wrong_string = 1;
  49.                     break;
  50.                 }
  51.                 if ((*it == '+' || *it == '-') && was_sign) {
  52.                     printf("%d\n", (int) WRONG_FORMAT + cnt_string);
  53.                     wrong_string = 1;
  54.                     break;
  55.                 }
  56.                 if (*it == '+') {
  57.                     was_sign = 1;
  58.                     sgn = 1;
  59.                 }
  60.                 if (*it == '-'){
  61.                     was_sign = 1;
  62.                     sgn = NEG;
  63.                 }
  64.                 if ('0' <= *it && *it <= '9') {
  65.                     char *end;
  66.                     int curr = strtol(it, &end, BASE);
  67.                     if (errno == ERANGE) {
  68.                         sum += sgn * cnt_number;
  69.                         errno = 0;
  70.                     } else {
  71.                         sum += curr * sgn;
  72.                     }
  73.                     was_sign = 0;
  74.                     sgn = 1;
  75.                     ++cnt_number;
  76.                     it = end - 1;
  77.                 }
  78.                 ++it;
  79.             }
  80.             if (!wrong_string) {
  81.                 printf("%d\n", (int) sum);
  82.             }
  83.         }
  84.     }
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment