Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <fstream>
- using namespace std;
- void DelMas(int* mas)
- {
- delete[] mas;
- }
- int* ReadFile(char* FileName)
- {
- int maxSize,countOperation, maxSizeBlock, id, sizeBlock;
- char operation;
- fstream fin = fstream(FileName, ios::in);
- if (fin.good())
- {
- fin>>maxSize>>countOperation>>maxSizeBlock;
- int* mas = new int[maxSize];
- for (int i = 0; i < maxSize; i++)
- mas[i] = 0;
- for (int i = 0; i < maxSize; i++)
- cout << i<<" "<<mas[i] << endl;
- for (int i = 0; i < countOperation; i++)
- {
- fin >> id >> operation >> sizeBlock;
- if (sizeBlock > maxSizeBlock)
- {
- cout << "Size block bigger than Max Size Block";
- exit(1);
- }
- bool f = false;
- switch (operation)
- {
- case 'c':
- while (!f)
- {
- bool l = false;
- int i = rand(), k;
- i %= maxSize - 1;
- k = sizeBlock;
- if (i + k >= maxSize - 1)
- continue;
- for (int z = 0; z < k; z++)
- {
- if (mas[i + z] != 0)
- {
- l = true;
- break;
- }
- }
- if (l == false)
- {
- f = true;
- for (int z = 0; z < k; z++)
- {
- mas[i + z] = id;
- }
- }
- }
- break;
- case 'd':
- for (int i = 0; i < maxSize; i++)
- if (mas[i] == id)
- {
- mas[i] = 0;
- }
- break;
- case 'a':
- while (!f)
- {
- bool l = false;
- int i = rand(), k;
- i %= maxSize - 1;
- k = sizeBlock;
- if (i + k >= maxSize - 1)
- continue;
- for (int z = 0; z < k; z++)
- {
- if (mas[i + z] != 0)
- {
- l = true;
- break;
- }
- }
- if (l == false)
- {
- f = true;
- for (int z = 0; z < k; z++)
- {
- mas[i + z] = id;
- }
- }
- }
- break;
- default:
- cout << "Incorrect command input\n";
- exit(1);
- break;
- }
- }
- for (int i = 0; i < maxSize; i++)
- cout << i << " " << mas[i] << endl;
- fin.close();
- return mas;
- DelMas(mas);
- }
- else
- {
- cout << "All bad\n";
- exit(1);
- }
- }
- void Study(char* FileName)
- {
- int maxSize, countOperation, maxSizeBlock, id, sizeBlock;
- char operation;
- fstream fin = fstream(FileName, ios::in);
- if (fin.good())
- {
- }
- else
- {
- cout << "All bad\n";
- exit(1);
- }
- }
- void CreateData(char* FileName)
- {
- int MaxSize=1000, countOperation=100, maxSizeBlock=20, id, sizeBlock,idCount=20;
- char operation;
- int* mas = new int[idCount];
- for (int i = 0; i < idCount; i++)
- {
- mas[i] = -1;
- }
- fstream fout = fstream(FileName, ios::out);
- if (fout.good())
- {
- fout << MaxSize << " " << countOperation << " " << maxSizeBlock << endl;
- for (int i = 0; i < countOperation; i++)
- {
- bool flag = false; // Проверка на существующий уже create
- int oper = rand() % 3;
- id = rand() % idCount;
- sizeBlock = rand() % maxSizeBlock;
- switch (oper)
- {
- case 0:
- for (int z = 0; z < idCount; z++)
- if (mas[z] == id)
- flag = true;
- if (flag == true)
- {
- operation = 'a';
- while (sizeBlock == maxSizeBlock)
- sizeBlock = rand() % maxSizeBlock;
- fout << id << " " << operation << " " << sizeBlock << endl;
- }
- else
- {
- operation = 'c';
- for (int z = 0; z < idCount; z++)
- if (mas[z] == -1)
- mas[z] = id;
- fout << id << " " << operation << " " << sizeBlock << endl;
- }
- break;
- case 1:operation = 'd';
- break;
- case 2:
- break;
- default:
- break;
- }
- }
- }
- else
- {
- cout << "All bad\n";
- exit(1);
- }
- }
- int main()
- {
- srand((int)time(0));
- int maxSize = 0;
- char FileName[] = "input.txt",FileName1[]="input1.txt";
- int* mas;
- CreateData(FileName1);
- mas = ReadFile(FileName);
- DelMas(mas);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement