Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. void DelMas(int* mas)
  7. {
  8. delete[] mas;
  9. }
  10. int* ReadFile(char* FileName)
  11. {
  12. int maxSize,countOperation, maxSizeBlock, id, sizeBlock;
  13. char operation;
  14. fstream fin = fstream(FileName, ios::in);
  15. if (fin.good())
  16. {
  17. fin>>maxSize>>countOperation>>maxSizeBlock;
  18. int* mas = new int[maxSize];
  19. for (int i = 0; i < maxSize; i++)
  20. mas[i] = 0;
  21. for (int i = 0; i < maxSize; i++)
  22. cout << i<<" "<<mas[i] << endl;
  23. for (int i = 0; i < countOperation; i++)
  24. {
  25. fin >> id >> operation >> sizeBlock;
  26. if (sizeBlock > maxSizeBlock)
  27. {
  28. cout << "Size block bigger than Max Size Block";
  29. exit(1);
  30. }
  31. bool f = false;
  32. switch (operation)
  33. {
  34. case 'c':
  35. while (!f)
  36. {
  37. bool l = false;
  38. int i = rand(), k;
  39. i %= maxSize - 1;
  40. k = sizeBlock;
  41. if (i + k >= maxSize - 1)
  42. continue;
  43. for (int z = 0; z < k; z++)
  44. {
  45. if (mas[i + z] != 0)
  46. {
  47. l = true;
  48. break;
  49. }
  50. }
  51. if (l == false)
  52. {
  53. f = true;
  54. for (int z = 0; z < k; z++)
  55. {
  56. mas[i + z] = id;
  57. }
  58. }
  59. }
  60. break;
  61. case 'd':
  62. for (int i = 0; i < maxSize; i++)
  63. if (mas[i] == id)
  64. {
  65. mas[i] = 0;
  66. }
  67. break;
  68. case 'a':
  69. while (!f)
  70. {
  71. bool l = false;
  72. int i = rand(), k;
  73. i %= maxSize - 1;
  74. k = sizeBlock;
  75. if (i + k >= maxSize - 1)
  76. continue;
  77. for (int z = 0; z < k; z++)
  78. {
  79. if (mas[i + z] != 0)
  80. {
  81. l = true;
  82. break;
  83. }
  84. }
  85. if (l == false)
  86. {
  87. f = true;
  88. for (int z = 0; z < k; z++)
  89. {
  90. mas[i + z] = id;
  91. }
  92. }
  93. }
  94. break;
  95. default:
  96. cout << "Incorrect command input\n";
  97. exit(1);
  98. break;
  99. }
  100. }
  101. for (int i = 0; i < maxSize; i++)
  102. cout << i << " " << mas[i] << endl;
  103. fin.close();
  104. return mas;
  105. DelMas(mas);
  106. }
  107. else
  108. {
  109. cout << "All bad\n";
  110. exit(1);
  111. }
  112. }
  113. void Study(char* FileName)
  114. {
  115. int maxSize, countOperation, maxSizeBlock, id, sizeBlock;
  116. char operation;
  117. fstream fin = fstream(FileName, ios::in);
  118. if (fin.good())
  119. {
  120. }
  121. else
  122. {
  123. cout << "All bad\n";
  124. exit(1);
  125. }
  126. }
  127. void CreateData(char* FileName)
  128. {
  129. int MaxSize=1000, countOperation=100, maxSizeBlock=20, id, sizeBlock,idCount=20;
  130. char operation;
  131. int* mas = new int[idCount];
  132. for (int i = 0; i < idCount; i++)
  133. {
  134. mas[i] = -1;
  135. }
  136. fstream fout = fstream(FileName, ios::out);
  137. if (fout.good())
  138. {
  139. fout << MaxSize << " " << countOperation << " " << maxSizeBlock << endl;
  140. for (int i = 0; i < countOperation; i++)
  141. {
  142. bool flag = false; // Проверка на существующий уже create
  143. int oper = rand() % 3;
  144. id = rand() % idCount;
  145. sizeBlock = rand() % maxSizeBlock;
  146. switch (oper)
  147. {
  148. case 0:
  149. for (int z = 0; z < idCount; z++)
  150. if (mas[z] == id)
  151. flag = true;
  152. if (flag == true)
  153. {
  154. operation = 'a';
  155. while (sizeBlock == maxSizeBlock)
  156. sizeBlock = rand() % maxSizeBlock;
  157. fout << id << " " << operation << " " << sizeBlock << endl;
  158. }
  159. else
  160. {
  161. operation = 'c';
  162. for (int z = 0; z < idCount; z++)
  163. if (mas[z] == -1)
  164. mas[z] = id;
  165. fout << id << " " << operation << " " << sizeBlock << endl;
  166. }
  167.  
  168. break;
  169. case 1:operation = 'd';
  170.  
  171. break;
  172. case 2:
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. }
  179. else
  180. {
  181. cout << "All bad\n";
  182. exit(1);
  183. }
  184. }
  185. int main()
  186. {
  187. srand((int)time(0));
  188. int maxSize = 0;
  189. char FileName[] = "input.txt",FileName1[]="input1.txt";
  190. int* mas;
  191. CreateData(FileName1);
  192. mas = ReadFile(FileName);
  193. DelMas(mas);
  194. return 0;
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement