Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include <locale.h>
- typedef struct Record
- {
- char *data; //содержимое строки
- int hitcount; //счетчик обращений
- } Record ;
- typedef struct Caсhe
- {
- Record *records; //состоит из массива строк
- int size; //размер кэша
- } Caсhe;
- void int_size(Caсhe *q) {
- setlocale(LC_ALL, "Rus");
- printf("Введите размер кэша: ");
- scanf("%d", &(*q).size);
- }
- Record* fillCache(Caсhe *q) //Функция, при помощи которой заполняем кэш
- {
- int_size(q);
- int size = (*q).size;
- printf("Заполните Кэш: ");
- (*q).records = (Record*)malloc(size*sizeof(Record));
- for (int i = 0; i < size; i++) {
- (*q).records[i].data = (char*)malloc(20 * sizeof(char));
- scanf("%s", &(*q).records[i].data);
- }
- return &(*q).records;
- }
- void addRecord(Caсhe *q) //функция добавления строки в кэш
- {
- char* records = fillCache(q);
- int size = (*q).size;
- printf("Введите строку: ");
- while (1)
- {
- char *str = (char*)malloc(sizeof(char) * 20);
- scanf("%s", str, 20);
- for (int i = 0; i < size; i++)
- {
- if (strcmp(str, (*q).records[i].data) == 0)
- {
- (*q).records[i].hitcount++;
- }
- if (strcmp(str, (*q).records[i].data) != 0)
- {
- for (i = 0; i < size; i++)
- {
- int min = (*q).records[i].data;
- if ((*q).records[i].hitcount < min)
- {
- min = (*q).records[i].hitcount; //нашли элемент с минимальным числом обращений И заменили его на новую строку
- (*q).records[i].data = str;
- (*q).records[i].hitcount = 1;
- }
- }
- }
- if (strcmp(str, "STOP") == 0); break;
- }
- }
- }
- Record* getRecord(Caсhe *q)
- {
- char* records = fillCache(q);
- int size = (*q).size;
- char *str1 = (char*)malloc(sizeof(char) * 20);
- scanf("%s", str1, 20);
- for (int i = 0; i < size; i++)
- {
- if (strcmp(str1, (*q).records[i].data) == 0)
- {
- printf("OKEY");
- (*q).records[i].hitcount++;
- getchar();
- return (*q).records[i].data;
- }
- if (strcmp(str1, (*q).records[i].data) != 0)
- {
- printf("LOSE");
- getchar();
- return 0;
- }
- }
- }
- int main() {
- Caсhe q;
- addRecord(&q);
- getRecord(&q);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement