Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include <locale.h>
  6.  
  7. typedef struct Record
  8. {
  9.     char *data; //содержимое строки
  10.     int hitcount; //счетчик обращений
  11.  
  12. } Record ;
  13.  
  14. typedef struct Caсhe
  15. {
  16.     Record *records; //состоит из массива строк
  17.     int size; //размер кэша
  18. } Caсhe;
  19.  
  20. void int_size(Caсhe *q) {
  21.     setlocale(LC_ALL, "Rus");
  22.     printf("Введите размер кэша: ");
  23.     scanf("%d", &(*q).size);
  24. }
  25.  
  26. Record* fillCache(Caсhe *q) //Функция, при помощи которой заполняем кэш
  27. {
  28.     int_size(q);
  29.     int size = (*q).size;
  30.     printf("Заполните Кэш: ");
  31.     (*q).records = (Record*)malloc(size*sizeof(Record));
  32.     for (int i = 0; i < size; i++) {
  33.         (*q).records[i].data = (char*)malloc(20 * sizeof(char));
  34.         scanf("%s", &(*q).records[i].data);
  35.     }
  36.     return &(*q).records;
  37. }
  38.  
  39. void addRecord(Caсhe *q) //функция добавления строки в кэш
  40. {
  41.     char* records = fillCache(q);
  42.     int size = (*q).size;
  43.     printf("Введите строку: ");
  44.     while (1)
  45.     {
  46.         char *str = (char*)malloc(sizeof(char) * 20);
  47.         scanf("%s", str, 20);
  48.         for (int i = 0; i < size; i++)
  49.         {
  50.             if (strcmp(str, (*q).records[i].data) == 0)
  51.             {
  52.                 (*q).records[i].hitcount++;
  53.             }
  54.             if (strcmp(str, (*q).records[i].data) != 0)
  55.             {
  56.                 for (i = 0; i < size; i++)
  57.                 {
  58.                     int min = (*q).records[i].data;
  59.                     if ((*q).records[i].hitcount < min)
  60.                     {
  61.                         min = (*q).records[i].hitcount; //нашли элемент с минимальным числом обращений И заменили его на новую строку
  62.                         (*q).records[i].data = str;
  63.                         (*q).records[i].hitcount = 1;
  64.                     }
  65.                 }
  66.             }
  67.             if (strcmp(str, "STOP") == 0); break;
  68.         }
  69.     }
  70. }
  71. Record* getRecord(Caсhe *q)
  72. {
  73.     char* records = fillCache(q);
  74.     int size = (*q).size;
  75.     char *str1 = (char*)malloc(sizeof(char) * 20);
  76.     scanf("%s", str1, 20);
  77.     for (int i = 0; i < size; i++)
  78.     {
  79.         if (strcmp(str1, (*q).records[i].data) == 0)
  80.         {
  81.             printf("OKEY");
  82.             (*q).records[i].hitcount++;
  83.             getchar();
  84.             return (*q).records[i].data;
  85.         }
  86.         if (strcmp(str1, (*q).records[i].data) != 0)
  87.         {
  88.             printf("LOSE");
  89.             getchar();
  90.             return 0;
  91.         }
  92.     }
  93. }
  94. int main() {
  95.     Caсhe q;
  96.     addRecord(&q);
  97.     getRecord(&q);
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement