cyberjab

Untitled

Apr 2nd, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.15 KB | None | 0 0
  1. #pragma once
  2.  
  3. namespace КурсачАиСД {
  4. #include "K-meansRBT.h"
  5.  
  6. using namespace System;
  7. using namespace System::ComponentModel;
  8. using namespace System::Collections;
  9. using namespace System::Windows::Forms;
  10. using namespace System::Data;
  11. using namespace System::Drawing;
  12. using namespace System::Threading;
  13. using namespace System::Timers;
  14.  
  15. /// <summary>
  16. /// Сводка для MyForm
  17. /// </summary>
  18. public ref class MyForm : public System::Windows::Forms::Form
  19. {
  20. Graphics^ g;
  21.  
  22. public:
  23. MyForm(void)
  24. {
  25. InitializeComponent();
  26. //
  27. g = panel1->CreateGraphics();
  28. //
  29. //TODO: добавьте код конструктора
  30. //
  31. }
  32.  
  33. protected:
  34. /// <summary>
  35. /// Освободить все используемые ресурсы.
  36. /// </summary>
  37. ~MyForm()
  38. {
  39. if (components)
  40. {
  41. delete components;
  42. }
  43. }
  44. private: System::Windows::Forms::Panel^ panel1;
  45. protected:
  46.  
  47. protected:
  48.  
  49. protected:
  50.  
  51. private:
  52. /// <summary>
  53. /// Обязательная переменная конструктора.
  54. /// </summary>
  55. System::ComponentModel::Container ^components;
  56.  
  57. #pragma region Windows Form Designer generated code
  58. /// <summary>
  59. /// Требуемый метод для поддержки конструктора — не изменяйте
  60. ///
  61. /// содержимое этого метода с помощью редактора кода.
  62. /// </summary>
  63. void InitializeComponent(void)
  64. {
  65. this->panel1 = (gcnew System::Windows::Forms::Panel());
  66. this->SuspendLayout();
  67. //
  68. // panel1
  69. //
  70. this->panel1->Dock = System::Windows::Forms::DockStyle::Fill;
  71. this->panel1->Location = System::Drawing::Point(0, 0);
  72. this->panel1->Name = L"panel1";
  73. this->panel1->Size = System::Drawing::Size(600, 600);
  74. this->panel1->TabIndex = 0;
  75. //
  76. // MyForm
  77. //
  78. this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
  79. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  80. this->AutoValidate = System::Windows::Forms::AutoValidate::Disable;
  81. this->CausesValidation = false;
  82. this->ClientSize = System::Drawing::Size(600, 600);
  83. this->ControlBox = false;
  84. this->Controls->Add(this->panel1);
  85. this->Enabled = false;
  86. this->ImeMode = System::Windows::Forms::ImeMode::On;
  87. this->Name = L"MyForm";
  88. this->ShowIcon = false;
  89. this->Text = L"k-means++";
  90. this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
  91. this->ResumeLayout(false);
  92.  
  93. }
  94.  
  95.  
  96. #pragma endregion
  97. public: void Mn() {
  98. MyForm::Update();
  99. Application::DoEvents();
  100. g->FillRectangle(Brushes::Black, 0, 0, 600, 600);
  101. try {
  102. vector<pdd> data;
  103. data.clear();
  104. int k;
  105. string message = "Список команд: load - загрузка данных из файла, random - создание случайных данных\n";
  106. cout << message;
  107. string cmd;
  108. cout << "Введите команду: ";
  109. cin >> cmd;
  110. if (cmd == "load") {
  111. fstream inputFile;
  112. string a;
  113. cout << "Введите название файла с данными: ";
  114. cin >> a;
  115. inputFile.open(a, ios::in);
  116. if (inputFile.is_open()) {
  117. int n;
  118. ldb p1, p2;
  119. inputFile >> n;
  120. forp(i, 0, n) {
  121. inputFile >> p1 >> p2;
  122. data.push_back(MP(p1, p2));
  123. }
  124. inputFile.close();
  125. cout << "Введите количество кластеров: ";
  126. cin >> k;
  127. if (k < 1 || k > 12) {
  128. cout << "Количество кластеров - число в диапазоне [1,12]!\n";
  129. Mn();
  130. /*Invalidate();*/
  131. }
  132. }
  133. else {
  134. cout << "Файл не найден!\n";
  135. Mn();
  136. /*Invalidate();*/
  137. }
  138. k_meansRBT alg = k_meansRBT();
  139. forp(i, 0, size(data)) {
  140. g->FillEllipse(Brushes::White, 25 + data[i].first, 25 + data[i].second, 7, 7);
  141. }
  142. clock_t start = clock();
  143. bool flag = alg.clusterisation(data, k);
  144. vector<vector<pdd>> dtd = alg.data_to_draw();
  145. clock_t end = clock();
  146. double seconds = (double)(end - start) / CLOCKS_PER_SEC;
  147. cout << "Время кластеризации: " << seconds << "s" << endl;
  148. Pen^ redPen = gcnew Pen(Color::Red, 3.0f);
  149. vector<pdd> c = alg.get_centers();
  150. forp(i, 0, size(dtd)) {
  151. forp(j, 0, size(dtd[i])) {
  152. switch (i) {
  153. case 0:
  154. g->FillEllipse(Brushes::Coral, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  155. break;
  156. case 1:
  157. g->FillEllipse(Brushes::White, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  158. break;
  159. case 2:
  160. g->FillEllipse(Brushes::Green, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  161. break;
  162. case 3:
  163. g->FillEllipse(Brushes::Blue, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  164. break;
  165. case 4:
  166. g->FillEllipse(Brushes::Orange, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  167. break;
  168. case 5:
  169. g->FillEllipse(Brushes::DarkCyan, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  170. break;
  171. case 6:
  172. g->FillEllipse(Brushes::DarkBlue, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  173. break;
  174. case 7:
  175. g->FillEllipse(Brushes::Gold, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  176. break;
  177. case 8:
  178. g->FillEllipse(Brushes::DarkViolet, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  179. break;
  180. case 9:
  181. g->FillEllipse(Brushes::DeepPink, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  182. break;
  183. case 10:
  184. g->FillEllipse(Brushes::DarkGray, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  185. break;
  186. case 11:
  187. g->FillEllipse(Brushes::Indigo, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. g->DrawEllipse(redPen, 25 + c[i].first, 25 + c[i].second, 8, 8);
  194. }
  195. fstream outputFile;
  196. string so = "";
  197. forp(i, 0, size(a) - 4) {
  198. so += a[i];
  199. }
  200. string out = so + "_" + to_string(k) + ".txt";
  201. outputFile.open(out, ios::out);
  202. vector<vector<pdd>> otpt = alg.data_to_draw();
  203. outputFile << size(otpt) << '\n';
  204. forp(i, 0, size(otpt)) {
  205. outputFile << "Cluster " << i + 1 << " - " << size(otpt[i]) << '\n';
  206. forp(j, 0, size(otpt[i])) {
  207. outputFile << otpt[i][j].first << " " << otpt[i][j].second << '\n';
  208. }
  209. }
  210. outputFile.close();
  211. cout << "Программа успешно заверишла выполнение!\n";
  212. cout << "Данные сохранены в файл " << out << '\n';
  213. cout << "Завершить программу?\nВведите yes, если да, любую строку, если нет:\n";
  214. cin >> cmd;
  215. if (cmd == "yes") {
  216. MyForm::Close();
  217. return;
  218. }
  219. else {
  220. Mn();
  221. }
  222. }
  223. elif(cmd == "random") {
  224. int x, k;
  225. cout << "Введите количество объектов: ";
  226. cin >> x;
  227. if (x <= 0) {
  228. cout << "Введите число > 0!\n";
  229. Mn();
  230. /*Invalidate();*/
  231. }
  232. cout << "Введите количество кластеров: ";
  233. cin >> k;
  234. if (k < 0 || k > 12) {
  235. cout << "Количество кластеров - число в диапазоне [1,12]!\n";
  236. Mn();
  237. /*Invalidate();*/
  238. }
  239. ldb probability = 0.675;
  240. forp(i, 0, x) {
  241. if (rand() % 100 < (probability * 100))
  242. data.push_back(MP(rand() % 551, rand() % 551));
  243. }
  244. k_meansRBT alg = k_meansRBT();
  245. forp(i, 0, size(data)) {
  246. g->FillEllipse(Brushes::White, 25 + data[i].first, 25 + data[i].second, 7, 7);
  247. }
  248. clock_t start = clock();
  249. bool flag = alg.clusterisation(data, k);
  250. vector<vector<pdd>> dtd = alg.data_to_draw();
  251. clock_t end = clock();
  252. double seconds = (double)(end - start) / CLOCKS_PER_SEC;
  253. cout << "Время кластеризации: " << seconds << "s" << endl;
  254. Pen^ redPen = gcnew Pen(Color::Red, 3.0f);
  255. vector<pdd> c = alg.get_centers();
  256. forp(i, 0, size(dtd)) {
  257. forp(j, 0, size(dtd[i])) {
  258. switch (i) {
  259. case 0:
  260. g->FillEllipse(Brushes::Coral, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  261. break;
  262. case 1:
  263. g->FillEllipse(Brushes::White, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  264. break;
  265. case 2:
  266. g->FillEllipse(Brushes::Green, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  267. break;
  268. case 3:
  269. g->FillEllipse(Brushes::Blue, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  270. break;
  271. case 4:
  272. g->FillEllipse(Brushes::Orange, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  273. break;
  274. case 5:
  275. g->FillEllipse(Brushes::DarkCyan, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  276. break;
  277. case 6:
  278. g->FillEllipse(Brushes::DarkBlue, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  279. break;
  280. case 7:
  281. g->FillEllipse(Brushes::Gold, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  282. break;
  283. case 8:
  284. g->FillEllipse(Brushes::DarkViolet, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  285. break;
  286. case 9:
  287. g->FillEllipse(Brushes::DeepPink, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  288. break;
  289. case 10:
  290. g->FillEllipse(Brushes::DarkGray, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  291. break;
  292. case 11:
  293. g->FillEllipse(Brushes::Indigo, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. g->DrawEllipse(redPen, 25 + c[i].first, 25 + c[i].second, 8, 8);
  300. }
  301. fstream outputFile;
  302. string so = "dataset" + to_string(x);
  303. string out = so + "_" + to_string(k) + ".txt";
  304. outputFile.open(out, ios::out);
  305. vector<vector<pdd>> otpt = alg.data_to_draw();
  306. outputFile << size(otpt) << '\n';
  307. forp(i, 0, size(otpt)) {
  308. outputFile << "Cluster " << i + 1 << " - " << size(otpt[i]) << '\n';
  309. forp(j, 0, size(otpt[i])) {
  310. outputFile << otpt[i][j].first << " " << otpt[i][j].second << '\n';
  311. }
  312. }
  313. outputFile.close();
  314. cout << "Программа успешно заверишла выполнение!\n";
  315. cout << "Данные сохранены в файл " << out << '\n';
  316. cout << "Завершить программу?\nВведите yes, если да, любую строку, если нет:\n";
  317. cin >> cmd;
  318. if (cmd == "yes") {
  319. MyForm::Close();
  320. return;
  321. }
  322. else {
  323. Mn();
  324. /*Invalidate();*/
  325. }
  326. }
  327. else {
  328. cout << "Неверная команда!\n";
  329. Mn();
  330. /*Invalidate();*/
  331. }
  332. }
  333. catch (...) {
  334. cout << "Ошибка!\n";
  335. Mn();
  336. /*Invalidate();*/
  337. }
  338. }
  339.  
  340. private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
  341. MyForm::Update();
  342. MyForm::BringToFront();
  343. cout << "Перед использованием программы прочитайте файл README.txt\n";
  344. Thread^ myThread;
  345. myThread = gcnew Thread(gcnew ThreadStart(this, &MyForm::Mn));
  346. myThread->Start();
  347. }
  348.  
  349. };
  350. }
  351.  
Add Comment
Please, Sign In to add comment