Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- namespace КурсачАиСД {
- #include "K-meansRBT.h"
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- using namespace System::Threading;
- using namespace System::Timers;
- /// <summary>
- /// Сводка для MyForm
- /// </summary>
- public ref class MyForm : public System::Windows::Forms::Form
- {
- Graphics^ g;
- public:
- MyForm(void)
- {
- InitializeComponent();
- //
- g = panel1->CreateGraphics();
- //
- //TODO: добавьте код конструктора
- //
- }
- protected:
- /// <summary>
- /// Освободить все используемые ресурсы.
- /// </summary>
- ~MyForm()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::Panel^ panel1;
- protected:
- protected:
- protected:
- private:
- /// <summary>
- /// Обязательная переменная конструктора.
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Требуемый метод для поддержки конструктора — не изменяйте
- ///
- /// содержимое этого метода с помощью редактора кода.
- /// </summary>
- void InitializeComponent(void)
- {
- this->panel1 = (gcnew System::Windows::Forms::Panel());
- this->SuspendLayout();
- //
- // panel1
- //
- this->panel1->Dock = System::Windows::Forms::DockStyle::Fill;
- this->panel1->Location = System::Drawing::Point(0, 0);
- this->panel1->Name = L"panel1";
- this->panel1->Size = System::Drawing::Size(600, 600);
- this->panel1->TabIndex = 0;
- //
- // MyForm
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->AutoValidate = System::Windows::Forms::AutoValidate::Disable;
- this->CausesValidation = false;
- this->ClientSize = System::Drawing::Size(600, 600);
- this->ControlBox = false;
- this->Controls->Add(this->panel1);
- this->Enabled = false;
- this->ImeMode = System::Windows::Forms::ImeMode::On;
- this->Name = L"MyForm";
- this->ShowIcon = false;
- this->Text = L"k-means++";
- this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
- this->ResumeLayout(false);
- }
- #pragma endregion
- public: void Mn() {
- MyForm::Update();
- Application::DoEvents();
- g->FillRectangle(Brushes::Black, 0, 0, 600, 600);
- try {
- vector<pdd> data;
- data.clear();
- int k;
- string message = "Список команд: load - загрузка данных из файла, random - создание случайных данных\n";
- cout << message;
- string cmd;
- cout << "Введите команду: ";
- cin >> cmd;
- if (cmd == "load") {
- fstream inputFile;
- string a;
- cout << "Введите название файла с данными: ";
- cin >> a;
- inputFile.open(a, ios::in);
- if (inputFile.is_open()) {
- int n;
- ldb p1, p2;
- inputFile >> n;
- forp(i, 0, n) {
- inputFile >> p1 >> p2;
- data.push_back(MP(p1, p2));
- }
- inputFile.close();
- cout << "Введите количество кластеров: ";
- cin >> k;
- if (k < 1 || k > 12) {
- cout << "Количество кластеров - число в диапазоне [1,12]!\n";
- Mn();
- /*Invalidate();*/
- }
- }
- else {
- cout << "Файл не найден!\n";
- Mn();
- /*Invalidate();*/
- }
- k_meansRBT alg = k_meansRBT();
- forp(i, 0, size(data)) {
- g->FillEllipse(Brushes::White, 25 + data[i].first, 25 + data[i].second, 7, 7);
- }
- clock_t start = clock();
- bool flag = alg.clusterisation(data, k);
- vector<vector<pdd>> dtd = alg.data_to_draw();
- clock_t end = clock();
- double seconds = (double)(end - start) / CLOCKS_PER_SEC;
- cout << "Время кластеризации: " << seconds << "s" << endl;
- Pen^ redPen = gcnew Pen(Color::Red, 3.0f);
- vector<pdd> c = alg.get_centers();
- forp(i, 0, size(dtd)) {
- forp(j, 0, size(dtd[i])) {
- switch (i) {
- case 0:
- g->FillEllipse(Brushes::Coral, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 1:
- g->FillEllipse(Brushes::White, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 2:
- g->FillEllipse(Brushes::Green, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 3:
- g->FillEllipse(Brushes::Blue, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 4:
- g->FillEllipse(Brushes::Orange, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 5:
- g->FillEllipse(Brushes::DarkCyan, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 6:
- g->FillEllipse(Brushes::DarkBlue, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 7:
- g->FillEllipse(Brushes::Gold, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 8:
- g->FillEllipse(Brushes::DarkViolet, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 9:
- g->FillEllipse(Brushes::DeepPink, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 10:
- g->FillEllipse(Brushes::DarkGray, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 11:
- g->FillEllipse(Brushes::Indigo, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- default:
- break;
- }
- }
- g->DrawEllipse(redPen, 25 + c[i].first, 25 + c[i].second, 8, 8);
- }
- fstream outputFile;
- string so = "";
- forp(i, 0, size(a) - 4) {
- so += a[i];
- }
- string out = so + "_" + to_string(k) + ".txt";
- outputFile.open(out, ios::out);
- vector<vector<pdd>> otpt = alg.data_to_draw();
- outputFile << size(otpt) << '\n';
- forp(i, 0, size(otpt)) {
- outputFile << "Cluster " << i + 1 << " - " << size(otpt[i]) << '\n';
- forp(j, 0, size(otpt[i])) {
- outputFile << otpt[i][j].first << " " << otpt[i][j].second << '\n';
- }
- }
- outputFile.close();
- cout << "Программа успешно заверишла выполнение!\n";
- cout << "Данные сохранены в файл " << out << '\n';
- cout << "Завершить программу?\nВведите yes, если да, любую строку, если нет:\n";
- cin >> cmd;
- if (cmd == "yes") {
- MyForm::Close();
- return;
- }
- else {
- Mn();
- }
- }
- elif(cmd == "random") {
- int x, k;
- cout << "Введите количество объектов: ";
- cin >> x;
- if (x <= 0) {
- cout << "Введите число > 0!\n";
- Mn();
- /*Invalidate();*/
- }
- cout << "Введите количество кластеров: ";
- cin >> k;
- if (k < 0 || k > 12) {
- cout << "Количество кластеров - число в диапазоне [1,12]!\n";
- Mn();
- /*Invalidate();*/
- }
- ldb probability = 0.675;
- forp(i, 0, x) {
- if (rand() % 100 < (probability * 100))
- data.push_back(MP(rand() % 551, rand() % 551));
- }
- k_meansRBT alg = k_meansRBT();
- forp(i, 0, size(data)) {
- g->FillEllipse(Brushes::White, 25 + data[i].first, 25 + data[i].second, 7, 7);
- }
- clock_t start = clock();
- bool flag = alg.clusterisation(data, k);
- vector<vector<pdd>> dtd = alg.data_to_draw();
- clock_t end = clock();
- double seconds = (double)(end - start) / CLOCKS_PER_SEC;
- cout << "Время кластеризации: " << seconds << "s" << endl;
- Pen^ redPen = gcnew Pen(Color::Red, 3.0f);
- vector<pdd> c = alg.get_centers();
- forp(i, 0, size(dtd)) {
- forp(j, 0, size(dtd[i])) {
- switch (i) {
- case 0:
- g->FillEllipse(Brushes::Coral, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 1:
- g->FillEllipse(Brushes::White, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 2:
- g->FillEllipse(Brushes::Green, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 3:
- g->FillEllipse(Brushes::Blue, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 4:
- g->FillEllipse(Brushes::Orange, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 5:
- g->FillEllipse(Brushes::DarkCyan, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 6:
- g->FillEllipse(Brushes::DarkBlue, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 7:
- g->FillEllipse(Brushes::Gold, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 8:
- g->FillEllipse(Brushes::DarkViolet, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 9:
- g->FillEllipse(Brushes::DeepPink, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 10:
- g->FillEllipse(Brushes::DarkGray, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- case 11:
- g->FillEllipse(Brushes::Indigo, 25 + dtd[i][j].first, 25 + dtd[i][j].second, 7, 7);
- break;
- default:
- break;
- }
- }
- g->DrawEllipse(redPen, 25 + c[i].first, 25 + c[i].second, 8, 8);
- }
- fstream outputFile;
- string so = "dataset" + to_string(x);
- string out = so + "_" + to_string(k) + ".txt";
- outputFile.open(out, ios::out);
- vector<vector<pdd>> otpt = alg.data_to_draw();
- outputFile << size(otpt) << '\n';
- forp(i, 0, size(otpt)) {
- outputFile << "Cluster " << i + 1 << " - " << size(otpt[i]) << '\n';
- forp(j, 0, size(otpt[i])) {
- outputFile << otpt[i][j].first << " " << otpt[i][j].second << '\n';
- }
- }
- outputFile.close();
- cout << "Программа успешно заверишла выполнение!\n";
- cout << "Данные сохранены в файл " << out << '\n';
- cout << "Завершить программу?\nВведите yes, если да, любую строку, если нет:\n";
- cin >> cmd;
- if (cmd == "yes") {
- MyForm::Close();
- return;
- }
- else {
- Mn();
- /*Invalidate();*/
- }
- }
- else {
- cout << "Неверная команда!\n";
- Mn();
- /*Invalidate();*/
- }
- }
- catch (...) {
- cout << "Ошибка!\n";
- Mn();
- /*Invalidate();*/
- }
- }
- private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
- MyForm::Update();
- MyForm::BringToFront();
- cout << "Перед использованием программы прочитайте файл README.txt\n";
- Thread^ myThread;
- myThread = gcnew Thread(gcnew ThreadStart(this, &MyForm::Mn));
- myThread->Start();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement