The_Law

Untitled

Oct 9th, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 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(int* 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.     int cnt[MAX_DIGITS];
  23.     memset(cnt, 0, MAX_DIGITS * sizeof(int));
  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.         return 0;
  46.     }
  47.  
  48.     int curr;
  49.  
  50.     while((curr = getc_unlocked(inpt)) != EOF) {
  51.         if ('0' <= curr && curr <= '9') {
  52.             ++cnt[curr - '0'];
  53.         }
  54.     }
  55.  
  56.     outpt(cnt);
  57.  
  58.     pclose(inpt);
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment