The_Law

Untitled

Oct 9th, 2018
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <string.h>
  5.  
  6. enum
  7. {
  8.     MAX_DIGITS = 10
  9. };
  10.  
  11. void
  12. outpt(long long *ans)
  13. {
  14.     for (int i = 0; i < MAX_DIGITS; ++i) {
  15.         printf("%d %d\n", i, ans[i]);
  16.     }
  17. }
  18.  
  19. int
  20. main(void)
  21. {
  22.     long long cnt[MAX_DIGITS];
  23.     memset(cnt, 0, MAX_DIGITS * sizeof(cnt[0]));
  24.  
  25.     char raw[PATH_MAX + 1];
  26.     if (fgets(raw, PATH_MAX, stdin) == NULL) {
  27.         outpt(cnt);
  28.         return 0;
  29.     }
  30.  
  31.     int it = PATH_MAX;
  32.     while (it >= 0 && raw[it] != '\n') {
  33.         --it;
  34.     }
  35.  
  36.     for (int i = 0; i < 3; ++i, --it) {
  37.         if (it >= 0 && (raw[it] == '\r' || raw[it] == '\n')) {
  38.             raw[it] = 0;
  39.         }
  40.     }
  41.  
  42.     FILE *inpt = fopen(raw, "r");
  43.     if (inpt == NULL) {
  44.         outpt(cnt);
  45.         pclose(inpt);
  46.         return 0;
  47.     }
  48.  
  49.     int curr;
  50.  
  51.     while((curr = getc_unlocked(inpt)) != EOF) {
  52.         if ('0' <= curr && curr <= '9') {
  53.             ++cnt[curr - '0'];
  54.         }
  55.     }
  56.  
  57.     outpt(cnt);
  58.  
  59.     pclose(inpt);
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment