Advertisement
STANAANDREY

lab 13 bin5

Jan 24th, 2023
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #define MAX 1000
  6. #define IDS_NR 3
  7. int ids[IDS_NR] = {1, 2, 3};
  8.  
  9. typedef struct {
  10.   uint16_t year;
  11.   uint8_t month, day, idx;
  12.   int16_t val;
  13. } SensorReg;
  14.  
  15. int randBetween(int low, int high) {
  16.   return (rand() % (high - low + 1)) + low;
  17. }
  18.  
  19. FILE *safeOpenFile(const char *const path, const char *const mode) {
  20.     FILE *file = NULL;
  21.     file = fopen(path, mode);
  22.     if (file == NULL) {
  23.         perror("opening error!");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.     return file;
  27. }
  28.  
  29. void safeCloseFile(FILE *file) {
  30.     if (fclose(file) == EOF) {
  31.         perror(NULL);
  32.     }
  33. }
  34.  
  35. void printSensorReg(const SensorReg *const sr) {
  36.   printf("%hu %hhu %hhu %hhu %hd\n", sr->year, sr->month, sr->day, sr->idx, sr->val);
  37. }
  38.  
  39. void genTest() {
  40.   SensorReg sr;
  41.   FILE *f = safeOpenFile("data.bin", "wb");
  42.   //defect
  43.   for (int i = 0; i < 10; i++) {
  44.     sr.year = randBetween(1990, 2023);
  45.     sr.month = randBetween(1, 12);
  46.     sr.day = randBetween(1, 30);
  47.     sr.idx = ids[0];
  48.     sr.val = randBetween(MAX + 1, MAX * 10);
  49.     if (rand() & 1) {
  50.       //sr.val *= -1;
  51.     }
  52.     fwrite(&sr, sizeof(sr), 1, f);
  53.     //printSensorReg(&sr);
  54.   }
  55.   //probleme
  56.   for (int i = 0; i < 10; i++) {
  57.     sr.year = randBetween(1990, 2023);
  58.     sr.month = randBetween(1, 12);
  59.     sr.day = randBetween(1, 30);
  60.     sr.idx = ids[1];
  61.     if (i & 1) sr.val = randBetween(MAX + 1, MAX * 10);
  62.     else sr.val = randBetween(0, MAX);
  63.     if (rand() & 1) {
  64.       sr.val *= -1;
  65.     }
  66.     fwrite(&sr, sizeof(sr), 1, f);
  67.     //printSensorReg(&sr);
  68.   }
  69.   //ok
  70.   for (int i = 0; i < 10; i++) {
  71.     sr.year = randBetween(1990, 2023);
  72.     sr.month = randBetween(1, 12);
  73.     sr.day = randBetween(1, 30);
  74.     sr.idx = ids[2];
  75.     sr.val = randBetween(0, MAX);
  76.     if (rand() & 1) {
  77.       sr.val *= -1;
  78.     }
  79.     fwrite(&sr, sizeof(sr), 1, f);
  80.     //printSensorReg(&sr);
  81.   }
  82.   safeCloseFile(f);
  83. }
  84.  
  85. int checkOk(uint8_t idx) {
  86.   SensorReg sr;
  87.   FILE *f = safeOpenFile("data.bin", "rb");
  88.   while (fread(&sr, sizeof(sr), 1, f) == 1) {
  89.     if (sr.idx == idx) {
  90.       //printf("%d", sr.val);
  91.       if (-MAX > sr.val || sr.val > MAX) {
  92.     return 0;
  93.       }
  94.     }
  95.   }
  96.   safeCloseFile(f);
  97.   return 1;
  98. }
  99.  
  100. int checkBroke(uint8_t idx) {
  101.   SensorReg sr;
  102.   FILE *f = safeOpenFile("data.bin", "rb");
  103.   while (fread(&sr, sizeof(sr), 1, f) == 1) {
  104.     if (sr.idx == idx) {
  105.       if (-MAX <= sr.val && sr.val <= MAX) {
  106.     return 0;
  107.       }
  108.     }
  109.   }
  110.   safeCloseFile(f);
  111.   return 1;
  112. }
  113.  
  114. void solve() {
  115.   int sum = 0;
  116.   for (int i = 0; i < 3; i++) {
  117.     if (checkOk(ids[i])) {
  118.       printf("good sensor: %d\n", ids[i]);
  119.       sum += i;
  120.       break;
  121.     }
  122.   }
  123.   for (int i = 0; i < 3; i++) {
  124.     if (checkBroke(ids[i])) {
  125.       printf("bad sensor: %d\n", ids[i]);
  126.       sum -= i;
  127.       break;
  128.     }
  129.   }
  130.   printf("broke sensor: %d\n", ids[IDS_NR - sum]);
  131. }
  132.  
  133. void solve2() {
  134.   uint16_t year;
  135.   uint8_t month;
  136.   scanf("%hu %hhu", &year, &month);
  137.   FILE *f = safeOpenFile("data.bin", "rb");
  138.   SensorReg sr;
  139.   while (fread(&sr, sizeof(sr), 1, f) == 1) {
  140.     if (sr.year == year && sr.month == month) {
  141.       printSensorReg(&sr);
  142.     }
  143.   }
  144.   safeCloseFile(f);
  145. }
  146.  
  147. int main(void) {
  148.   srand(time(NULL));
  149.   genTest();
  150.   solve();
  151.   solve2();
  152.   return 0;
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement