Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <windows.h>
  5. #include <ctime>
  6. #include <vector>
  7. using namespace std;
  8. ofstream fout("out.txt");
  9. HANDLE hMutex;
  10. int **stat;
  11. long TaskCount;
  12. long ThreadCount;
  13. long Tasks;
  14. //-------------------------------
  15. struct Matrix {
  16. double matrix[5][2];
  17. bool check;
  18. int doneBy;
  19. int answer;
  20. int solved;
  21. double time;
  22. Matrix(){
  23. check = false;
  24. doneBy = -1;
  25. answer = -1;
  26. solved = -1;
  27. time = 0;
  28. }
  29. Matrix(double m[5][2]){
  30. check = false;
  31. check = false;
  32. doneBy = -1;
  33. answer = -1;
  34. solved = -1;
  35. time = 0;
  36. for(int i = 0;i < 5;i++){
  37. for(int j = 0;j < 2;j++){
  38. matrix[i][j] = m[i][j];
  39. }
  40. }
  41. }
  42. };
  43. struct Arg{
  44. Matrix *matr;
  45. int id;
  46. };
  47. void generator(Matrix *m){
  48. srand(time(NULL));
  49. double matrixTask[5][2];
  50. for(int i = 0;i < TaskCount;i++){
  51. matrixTask[0][0] = rand() % 6;
  52. matrixTask[0][1] = rand() % 6;
  53. matrixTask[1][0] = 5 + rand() % 6;
  54. matrixTask[1][1] = rand() % 3;
  55. matrixTask[2][0] = 5 + rand() % 11;
  56. matrixTask[2][1] = rand() % 11;
  57. matrixTask[3][0] = 5 + rand() % 6;
  58. matrixTask[3][1] = 7 + rand() % 4;
  59. matrixTask[4][0] = rand() % 6;
  60. matrixTask[4][1] = 5 + rand() % 6;
  61. Matrix m1(matrixTask);
  62. m[i] = m1;
  63. }
  64. }
  65. void showMatr(Matrix *m){
  66. for(int i = 0;i < TaskCount;i++){
  67. for(int k = 0;k < 5;k++){
  68. for(int j = 0;j < 2;j++){
  69. cout << m[i].matrix[k][j] << " ";
  70. }
  71. cout << endl;
  72. }
  73. cout << endl << endl;
  74.  
  75. }
  76. }
  77.  
  78. bool hasTasks(Matrix *m){
  79. for(int i = 0;i < TaskCount;i++){
  80. if(m[i].check == false){
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. int checker(double c[5][2]){
  87. double matrix[5][2];
  88. double res[5];
  89. for(int i = 0; i < 5;i++){
  90. if(i == 4){
  91. matrix[i][0] = c[0][0] - c[i][0];
  92. matrix[i][1] = c[0][1] - c[i][1];
  93. }
  94. else{
  95. matrix[i][0] = c[i + 1][0] - c[i][0];
  96. matrix[i][1] = c[i + 1][1] - c[i][1];
  97. }
  98. }
  99. //---------------------
  100. double T = matrix[4][0] * matrix[0][1] - matrix[4][1] * matrix[0][0];
  101. T = T/abs(T);
  102. double P = 1;
  103. for(int i = 0;i < 5;i++){
  104. double R = matrix[i][0] * matrix[i + 1][1] - matrix[i][1] * matrix[i + 1][0];
  105. P = T * P * R/abs(R);
  106. if(P < 0)
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. DWORD WINAPI myThread(void *arg){
  112. Arg *a = ((Arg*)arg);
  113. int curr = a->id;
  114. int taskNum = 0;
  115. double task[5][2];
  116. while(hasTasks(a->matr)){
  117. WaitForSingleObject( hMutex, INFINITE );
  118. for(int i = 0; i < TaskCount;i++){
  119. if(a->matr[i].check == false){
  120. a->matr[i].check = true;
  121. taskNum = i;
  122. for(int k = 0;k < 5;k++){
  123. for(int j = 0; j < 2;j++){
  124. task[k][j] = a->matr[i].matrix[k][j];
  125. }
  126. }
  127. a->matr[i].doneBy = curr;
  128. break;
  129. }
  130. }
  131. ReleaseMutex( hMutex );
  132.  
  133. int answer,solved;
  134.  
  135. LARGE_INTEGER l1, l2, l3;
  136. QueryPerformanceCounter(&l1);
  137. QueryPerformanceFrequency(&l3);
  138. if(checker(task) == 1){
  139. answer = 1;
  140. solved = 1;
  141. }
  142. else if(checker(task) == 0){
  143. answer = 0;
  144. solved = 1;
  145. }
  146. QueryPerformanceCounter(&l2);
  147. double totalTime = ((double)l2.QuadPart / (double)l3.QuadPart) - ((double)l1.QuadPart / (double)l3.QuadPart);//итоговое время
  148.  
  149. WaitForSingleObject( hMutex, INFINITE );
  150. stat[taskNum][0] = curr;
  151. stat[taskNum][1] = answer;
  152. a->matr[taskNum].answer = answer;
  153. a->matr[taskNum].solved = solved;
  154. a->matr[taskNum].time = totalTime;
  155. ReleaseMutex( hMutex );
  156. }
  157. return 0;
  158. }
  159. void solvedN(Matrix *m){
  160. int counter = 0;
  161. for(int i = 0;i < TaskCount;i++){
  162. if(m[i].answer == 0) counter++;
  163. }
  164. cout << "Tasks that havent solutions = " << counter << endl;
  165. }
  166. void threads(Matrix *m){
  167. int totalTasks = 0;
  168. for(int i = 0;i < ThreadCount;i++){
  169. int counter = 0;
  170. double time = 0;
  171. for(int j = 0; j < TaskCount;j++){
  172. if(i == m[j].doneBy){
  173. counter++;
  174. time += m[j].time;
  175. }
  176. }
  177. cout << "Thread with ID = " << i << " solved = " << counter << " tasks. Total time = " << time << endl;
  178. totalTasks += counter;
  179. }
  180. cout << "Total Solved Tasks = " << totalTasks << endl;
  181. }
  182. void minMax(Matrix *m){
  183. double min = 10;
  184. double max = 0;
  185. for(int i = 0; i < TaskCount;i++){
  186. if(m[i].time < min && m[i].time != 0){
  187. min = m[i].time;
  188. }
  189. if(m[i].time > max){
  190. max = m[i].time;
  191. }
  192. }
  193. if(min == 10)
  194. min = max;
  195. cout << "Max time for solving = " << max << endl << "Min time for solving = " << min << endl;
  196. }
  197.  
  198. int main(){
  199. Tasks = 0;
  200. hMutex=CreateMutex( NULL, FALSE, NULL );
  201. TaskCount = 0;
  202. cout << "Enter Task Count" << endl;
  203. while(TaskCount <= 0)
  204. cin >> TaskCount;
  205. stat = new int*[TaskCount];
  206. for(int i = 0 ; i < TaskCount;i++){
  207. stat[i] = new int[2];
  208. }
  209. //----------------------------------
  210. cout << "Enter ThreadCount" << endl;
  211. ThreadCount = 0;
  212. while(ThreadCount <= 0)
  213. cin >> ThreadCount;
  214. Arg args;
  215. Matrix *m = new Matrix[TaskCount];
  216. generator(m);
  217. args.matr = m;
  218. HANDLE *hThread = new HANDLE[ThreadCount];
  219. DWORD *IDThread = new DWORD[ThreadCount];
  220.  
  221. for(int i = 0;i < ThreadCount;i++){
  222. args.id = i;
  223. hThread[i] = CreateThread(NULL, 0, myThread, &args, 0, &IDThread[i]);
  224. Sleep(0.5);
  225. if (hThread[i] == NULL){
  226. cout<<"Thread doesnt exist"<<endl;
  227. return GetLastError();
  228. }
  229. }
  230. if (WaitForMultipleObjects(ThreadCount, hThread, TRUE, INFINITE)) {
  231. cout << "Wait for multiple objects failed" << endl;
  232. }
  233. for(int i = 0;i < ThreadCount;i++){
  234. CloseHandle(hThread[i]);
  235. }
  236. //--------------------------
  237. double writeTimeBeg = clock();
  238. for(int k = 0;k < TaskCount;k++){
  239. fout <<"Answer = " << args.matr[k].answer << endl;
  240. for(int i = 0;i < 5;i++){
  241. for(int j = 0;j < 2;j++){
  242. fout << args.matr[k].matrix[i][j] << " ";
  243. }
  244. fout << endl;
  245. }
  246. fout << "---------------" <<endl;
  247. }
  248. double writeTimeEnd = clock();
  249. //--------------------------
  250.  
  251. solvedN(args.matr);
  252. threads(args.matr);
  253. minMax(args.matr);
  254. cout << "Time for writting data = " << ((writeTimeEnd - writeTimeBeg) / CLOCKS_PER_SEC) << endl;
  255. delete[] m;
  256. system("pause");
  257. return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement