Advertisement
_takumi

process-args-3

Jan 20th, 2023 (edited)
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <errno.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int isnum(char *s) {
  9.     for (size_t i = 0; s[i] != 0; ++i) {
  10.         if (s[i] != '+' && s[i] != '-' && !isdigit(s[i])) {
  11.             return 0;
  12.         }
  13.     }
  14.     return 1;
  15. }
  16.  
  17. int solve(int64_t a) {
  18.     int8_t i8 = (int8_t)a;
  19.     if (i8 == a) {
  20.         return 1;
  21.     }
  22.     int16_t i16 = (int16_t)a;
  23.     if (i16 == a) {
  24.         return 2;
  25.     }
  26.     int32_t i32 = (int32_t)a;
  27.     if (i32 == a) {
  28.         return 4;
  29.     }
  30.     return -1;
  31. }
  32.  
  33. int main(int argc, char *argv[]) {
  34.     int64_t a;
  35.     char *end;
  36.     for (int i = 1; i < argc; ++i) {
  37.         if (argv[i][0] == 0) {
  38.             puts("-1");
  39.             continue;
  40.         }
  41.         errno = 0;
  42.         a = strtol(argv[i], &end, 10);
  43.         if (errno == 0) {
  44.             if (!isnum(argv[i]) || (*end != 0)) {
  45.                 puts("-1");
  46.             } else {
  47.                 printf("%d\n", solve(a));
  48.             }
  49.         } else {
  50.             puts("-1");
  51.         }
  52.     }
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement