The_Law

Untitled

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