Advertisement
seba101

[PRIR] Liczba pierwsza watki

Nov 20th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <string>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include "pch.h"
  6. #include <iostream>
  7. #include <Windows.h>
  8.  
  9. using namespace std;
  10.  
  11. struct Date {
  12. int min, max;
  13. char filename[10];
  14. };
  15.  
  16. DWORD WINAPI Function(LPVOID lpParam) {
  17. bool p;
  18. Date *data = (Date*)lpParam;
  19. int i = data->min;
  20. int n = data->max;
  21. char* plik = data->filename;
  22. strcat(plik, ".txt");
  23. FILE * file = fopen(plik, "a");
  24. for (i ; i < n; i++)
  25. {
  26. p = true;
  27.  
  28. for (int j = 2; j < i; j++)
  29. if (i % j == 0)
  30. p = false;
  31.  
  32. if (p == true)
  33. fprintf(file,"%d\n", i);
  34. }
  35. fclose(file);
  36. return 0;
  37.  
  38. }
  39.  
  40. int main()
  41. {
  42. int iww = 0, od = 0, pdo = 0, r = 0, tmp = 0, tod = 0, tpdo = 0;
  43. std::cin >> iww;
  44. std::cin >> od;
  45. std::cin >> pdo;
  46.  
  47. HANDLE *h = new HANDLE[iww];
  48. DWORD *TID = new DWORD[iww];
  49. Date *dd = new Date[iww];
  50.  
  51. tmp = (pdo - od) / iww;
  52. r = tmp;
  53. tod = od;
  54. for (int i = 0; i < iww; i++)
  55. {
  56. if (tmp >= pdo) tmp = pdo;
  57. char c = i + '0';
  58. dd[i] = {tod,r,c };
  59. h[i] = CreateThread(NULL, 0, Function, &dd[i], 0, &TID[i]);
  60. tod = r + 1;
  61. r = r + tmp;
  62. }
  63.  
  64.  
  65. WaitForMultipleObjects(iww, h, TRUE, INFINITE);
  66.  
  67. for (int i = 0; i < iww; i++)
  68. {
  69.  
  70. char filen[10];
  71. sprintf(filen, "%d.txt", i);
  72. FILE * file = fopen(filen,"r");
  73.  
  74. while (!feof(file))
  75. {
  76. int s = 0;
  77. fscanf(file, "%d", &s);
  78. if(s != 0) cout << s << "\n";
  79. }
  80. fclose(file);
  81. }
  82.  
  83. delete[] h;
  84. delete[] TID;
  85. delete[] dd;
  86.  
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement