Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <fstream>
  4. #include <vector>
  5. #include <time.h>
  6. #include < sstream>
  7. using namespace std;
  8.  
  9. struct myData {
  10. int a;
  11. int b;
  12. double eps;
  13. double delta;
  14. };
  15.  
  16. int allThreadsTasks = 0;
  17. int noSolutions = 0;
  18. int mystakeTasks = 0;
  19. vector <myData> parametrs;
  20. vector <pair<myData, string>> results;
  21. CRITICAL_SECTION cs1;
  22. CRITICAL_SECTION cs2;
  23. CRITICAL_SECTION cs3;
  24.  
  25. double f(double a, double b, double x) {
  26. return a*cos(x) / (b*x*x);
  27. }
  28. double df(double a, double b, double x) {
  29. return -(cos(x)*a * 2 / (b * x*x*x) + a*sin(x) / (b*x*x));
  30. }
  31. double d2f(double a, double b, double x) {
  32. return a*(-cos(x) / b + 4 * sin(x) / (3 * x) + 2 * cos(x) / (x*x)) / (x*x);
  33. }
  34. bool CheckSolutions(double a, double b, double i1, double i2) {
  35. if (f(a, b, i1)*f(a, b, i2) > 0) {
  36. if (abs(i1 - i2) < 0.001) {
  37. return false;
  38. }
  39. double c = (i1 + i2) / 2;
  40. CheckSolutions(a, b, i1, c);
  41. CheckSolutions(a, b, c, i2);
  42. }
  43. else {
  44. return true;
  45. }
  46. }
  47.  
  48. class Thread {
  49. public:
  50. HANDLE tr;
  51. int ID=0;
  52. int tasks = 0;
  53. double maxTime=0;
  54. double minTime=100;
  55. double allTime=0;
  56. Thread() {}
  57.  
  58. int rez(myData data) {
  59. double start = clock();
  60. double i1 = 0, i2 = 100;
  61. double xk = data.delta;
  62. double xk_1 = 0;
  63. string str;
  64. pair <myData, string> p;
  65. p.first = data;
  66. bool flag = true;
  67.  
  68. if (data.b == 0 || data.delta == 0 ) {
  69. noSolutions++;
  70. p.second = "No solutions";
  71. flag = false;
  72. }
  73.  
  74. if (!CheckSolutions(data.a, data.b, i1, i2) && flag) {
  75. flag = false;
  76. p.second = "No solutions";
  77. noSolutions++;
  78. }
  79. while (abs(xk - xk_1) > data.eps && flag) {
  80. xk_1 = xk;
  81. if (df(data.a, data.b, xk_1) == 0) {
  82. p.second = "No solutions";
  83. noSolutions++;
  84. flag = false;
  85. break;
  86. }
  87. xk = xk_1 - (f(data.a, data.b, xk_1) / df(data.a, data.b, xk_1));
  88. }
  89. if (flag) {
  90. stringstream ss;
  91. ss << xk;
  92. ss >> p.second;
  93. EnterCriticalSection(&cs3);
  94. allThreadsTasks++;
  95. LeaveCriticalSection(&cs3);
  96. tasks++;
  97. }
  98. double end = clock();
  99. double time = (double)((end - start) / (double)CLOCKS_PER_SEC);
  100. if (maxTime < time) {
  101. maxTime = time;
  102. }
  103. if (minTime > time) {
  104. minTime = time;
  105. }
  106. allTime += time;
  107. EnterCriticalSection(&cs2);
  108. results.push_back(p);
  109. LeaveCriticalSection(&cs2);
  110. return 0;
  111. }
  112. };
  113.  
  114. vector <Thread> threads;
  115.  
  116. DWORD WINAPI resolve(LPVOID i) {
  117.  
  118. while (true) {
  119. bool flag = false;
  120. myData data;
  121. if (parametrs.size() == 0) {
  122. break;
  123. }
  124. EnterCriticalSection(&cs1);
  125. if (parametrs.size() != 0) {
  126. data = parametrs[0];
  127. parametrs.erase(parametrs.begin());
  128. flag = true;
  129. }
  130. LeaveCriticalSection(&cs1);
  131. if (flag) {
  132. threads[(int)i].rez(data);
  133. }
  134. }
  135.  
  136. return 0;
  137. }
  138.  
  139. int main() {
  140. srand(time(NULL));
  141. setlocale(LC_ALL, ".1251");
  142. InitializeCriticalSection(&cs1);
  143. InitializeCriticalSection(&cs2);
  144. InitializeCriticalSection(&cs3);
  145. int TestCount, ThreadCount;
  146. cout << "TestCount = ";
  147. cin >> TestCount;
  148. cout << endl<<"ThreadCount = ";
  149. cin >> ThreadCount;
  150. cout << endl;
  151.  
  152. DWORD IDThread;
  153.  
  154. for (int i = 0; i < TestCount; i++) {
  155. double a = rand() % 100;
  156. double b = rand() % 100;
  157. double eps = (rand() % 100) / 100000.0;
  158. double delta = (rand() % 10000) / 100;
  159. myData data;
  160. data.a = a;
  161. data.b = b;
  162. data.eps = eps;
  163. data.delta = delta;
  164. parametrs.push_back(data);
  165. }
  166. for (int i = 0; i < ThreadCount; i++) {
  167. Thread a;
  168. threads.push_back(a);
  169. threads[i].ID = i;
  170. }
  171.  
  172. HANDLE *handles = new HANDLE[ThreadCount];
  173. int i = 0;
  174. while (i<ThreadCount ) {
  175. handles[i] = threads[i].tr = CreateThread(NULL, 0, resolve, (void*)threads[i].ID, 0, &IDThread);
  176. i++;
  177. }
  178.  
  179. WaitForMultipleObjects(ThreadCount, handles, TRUE, INFINITE);
  180. DeleteCriticalSection(&cs1);
  181. DeleteCriticalSection(&cs2);
  182. DeleteCriticalSection(&cs3);
  183. cout << "Всего решено задач: " << allThreadsTasks << endl;
  184. cout << "Не решилось из-за ошибки в процессе вычисления: " << mystakeTasks << endl;
  185. cout << "Не имеют решений: " << noSolutions << endl;
  186. cout << "-------------------------------------------------------------------------------------------------------------------------------------------" << endl;
  187. for (int i = 0; i < ThreadCount; i++) {
  188. cout << "Поток номер " << threads[i].ID << " | Kол-во решенных задач: " << threads[i].tasks << " \t| Время решения всех задач: ";
  189. cout << threads[i].allTime << " | Макс. время решения: " << threads[i].maxTime << " мил-сек | Мин. время решения задачи: " << threads[i].minTime <<" мил-сек"<< endl;
  190. }
  191. ofstream out("output.txt");
  192. double start = clock();
  193. for (int i = 0; i < results.size(); i++){
  194. out << "a=" << results[i].first.a;
  195. out << " b=" << results[i].first.b;
  196. out << " eps=" << results[i].first.eps;
  197. out << " rez=" << results[i].second.c_str() << endl;
  198. }
  199. double end = clock();
  200. cout << "Время затраченное на запись данных в файл " << (double)((end - start)/(double)CLOCKS_PER_SEC) << " миллисекунд" << endl;
  201. system("pause");
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement