Advertisement
xladomaz

selection sort dadaya

Jun 19th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <fstream>
  4. #include <msclr\marshal_cppstd.h>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <sstream>
  9. #include <atlstr.h>
  10. #include <filesystem>
  11.  
  12. namespace BruteForceSort {
  13.  
  14. using namespace std;
  15. using namespace System;
  16. using namespace System::ComponentModel;
  17. using namespace System::Collections;
  18. using namespace System::Windows::Forms;
  19. using namespace System::Data;
  20. using namespace System::Drawing;
  21.  
  22. /// <summary>
  23. /// Сводка для MyForm
  24. /// </summary>
  25. public ref class MyForm : public System::Windows::Forms::Form
  26. {
  27. public:
  28. MyForm(void)
  29. {
  30. InitializeComponent();
  31. //
  32. //TODO: добавьте код конструктора
  33. //
  34. }
  35.  
  36. protected:
  37. /// <summary>
  38. /// Освободить все используемые ресурсы.
  39. /// </summary>
  40. ~MyForm()
  41. {
  42. if (components)
  43. {
  44. delete components;
  45. }
  46. }
  47.  
  48. protected:
  49.  
  50. private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
  51. private: System::Windows::Forms::Button^ button2;
  52.  
  53. private: System::Windows::Forms::TabControl^ tabControl1;
  54. private: System::Windows::Forms::TabPage^ tabPage1;
  55. private: System::Windows::Forms::TabPage^ tabPage2;
  56. private: System::Windows::Forms::WebBrowser^ webBrowser1;
  57. private: System::Windows::Forms::Label^ label1;
  58.  
  59.  
  60. private: System::Windows::Forms::Button^ button1;
  61. private: System::Windows::Forms::Button^ button3;
  62. private: System::Windows::Forms::RichTextBox^ input_array;
  63.  
  64.  
  65.  
  66.  
  67. private:
  68. /// <summary>
  69. /// Обязательная переменная конструктора.
  70. /// </summary>
  71. System::ComponentModel::Container^ components;
  72.  
  73. #pragma region Windows Form Designer generated code
  74. /// <summary>
  75. /// Требуемый метод для поддержки конструктора — не изменяйте
  76. /// содержимое этого метода с помощью редактора кода.
  77. /// </summary>
  78. void InitializeComponent(void)
  79. {
  80. this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
  81. this->button2 = (gcnew System::Windows::Forms::Button());
  82. this->tabControl1 = (gcnew System::Windows::Forms::TabControl());
  83. this->tabPage1 = (gcnew System::Windows::Forms::TabPage());
  84. this->button3 = (gcnew System::Windows::Forms::Button());
  85. this->button1 = (gcnew System::Windows::Forms::Button());
  86. this->label1 = (gcnew System::Windows::Forms::Label());
  87. this->tabPage2 = (gcnew System::Windows::Forms::TabPage());
  88. this->webBrowser1 = (gcnew System::Windows::Forms::WebBrowser());
  89. this->input_array = (gcnew System::Windows::Forms::RichTextBox());
  90. this->tabControl1->SuspendLayout();
  91. this->tabPage1->SuspendLayout();
  92. this->tabPage2->SuspendLayout();
  93. this->SuspendLayout();
  94. //
  95. // button2
  96. //
  97. this->button2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  98. static_cast<System::Byte>(204)));
  99. this->button2->Location = System::Drawing::Point(14, 159);
  100. this->button2->Name = L"button2";
  101. this->button2->Size = System::Drawing::Size(389, 30);
  102. this->button2->TabIndex = 2;
  103. this->button2->Text = L"Из формы";
  104. this->button2->UseVisualStyleBackColor = true;
  105. this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
  106. //
  107. // tabControl1
  108. //
  109. this->tabControl1->Controls->Add(this->tabPage1);
  110. this->tabControl1->Controls->Add(this->tabPage2);
  111. this->tabControl1->Location = System::Drawing::Point(7, 12);
  112. this->tabControl1->Name = L"tabControl1";
  113. this->tabControl1->SelectedIndex = 0;
  114. this->tabControl1->Size = System::Drawing::Size(419, 295);
  115. this->tabControl1->TabIndex = 4;
  116. //
  117. // tabPage1
  118. //
  119. this->tabPage1->BackColor = System::Drawing::Color::Snow;
  120. this->tabPage1->Controls->Add(this->input_array);
  121. this->tabPage1->Controls->Add(this->button3);
  122. this->tabPage1->Controls->Add(this->button1);
  123. this->tabPage1->Controls->Add(this->label1);
  124. this->tabPage1->Controls->Add(this->button2);
  125. this->tabPage1->Location = System::Drawing::Point(4, 22);
  126. this->tabPage1->Name = L"tabPage1";
  127. this->tabPage1->Padding = System::Windows::Forms::Padding(3);
  128. this->tabPage1->Size = System::Drawing::Size(411, 269);
  129. this->tabPage1->TabIndex = 0;
  130. this->tabPage1->Text = L"Ввод данных";
  131. //
  132. // button3
  133. //
  134. this->button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  135. static_cast<System::Byte>(204)));
  136. this->button3->Location = System::Drawing::Point(14, 231);
  137. this->button3->Name = L"button3";
  138. this->button3->Size = System::Drawing::Size(389, 30);
  139. this->button3->TabIndex = 8;
  140. this->button3->Text = L"Выход";
  141. this->button3->UseVisualStyleBackColor = true;
  142. this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click);
  143. //
  144. // button1
  145. //
  146. this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  147. static_cast<System::Byte>(204)));
  148. this->button1->Location = System::Drawing::Point(14, 195);
  149. this->button1->Name = L"button1";
  150. this->button1->Size = System::Drawing::Size(389, 30);
  151. this->button1->TabIndex = 7;
  152. this->button1->Text = L"Из файла";
  153. this->button1->UseVisualStyleBackColor = true;
  154. this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click_1);
  155. //
  156. // label1
  157. //
  158. this->label1->AutoSize = true;
  159. this->label1->Font = (gcnew System::Drawing::Font(L"Candara", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  160. static_cast<System::Byte>(0)));
  161. this->label1->Location = System::Drawing::Point(135, 3);
  162. this->label1->Name = L"label1";
  163. this->label1->Size = System::Drawing::Size(124, 23);
  164. this->label1->TabIndex = 5;
  165. this->label1->Text = L"Ввод массива";
  166. //
  167. // tabPage2
  168. //
  169. this->tabPage2->Controls->Add(this->webBrowser1);
  170. this->tabPage2->Location = System::Drawing::Point(4, 22);
  171. this->tabPage2->Name = L"tabPage2";
  172. this->tabPage2->Padding = System::Windows::Forms::Padding(3);
  173. this->tabPage2->Size = System::Drawing::Size(411, 269);
  174. this->tabPage2->TabIndex = 1;
  175. this->tabPage2->Text = L"Веб-браузер";
  176. this->tabPage2->UseVisualStyleBackColor = true;
  177. //
  178. // webBrowser1
  179. //
  180. this->webBrowser1->Location = System::Drawing::Point(8, 6);
  181. this->webBrowser1->MinimumSize = System::Drawing::Size(20, 20);
  182. this->webBrowser1->Name = L"webBrowser1";
  183. this->webBrowser1->Size = System::Drawing::Size(401, 271);
  184. this->webBrowser1->TabIndex = 1;
  185. //
  186. // input_array
  187. //
  188. this->input_array->Location = System::Drawing::Point(42, 29);
  189. this->input_array->Name = L"input_array";
  190. this->input_array->Size = System::Drawing::Size(323, 96);
  191. this->input_array->TabIndex = 9;
  192. this->input_array->Text = L"";
  193. //
  194. // MyForm
  195. //
  196. this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  197. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  198. this->ClientSize = System::Drawing::Size(432, 315);
  199. this->Controls->Add(this->tabControl1);
  200. this->Name = L"MyForm";
  201. this->Text = L"Сортировка методом перебора";
  202. this->tabControl1->ResumeLayout(false);
  203. this->tabPage1->ResumeLayout(false);
  204. this->tabPage1->PerformLayout();
  205. this->tabPage2->ResumeLayout(false);
  206. this->ResumeLayout(false);
  207.  
  208. }
  209. #pragma endregion
  210. private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
  211. OpenFileDialog^ opn = gcnew OpenFileDialog();
  212. opn->Filter = "Html files (*.html)|*.html|All files (*.*)|*.*";
  213. if (opn->ShowDialog() == Windows::Forms::DialogResult::OK) {
  214. webBrowser1->Navigate(opn->FileName);
  215. }
  216.  
  217. }
  218.  
  219. vector<std::string> split_by_delimiter(std::string input, char delimiter) {
  220. size_t pos = 0;
  221. std::string token;
  222. std::vector<std::string> output;
  223. while ((pos = input.find(delimiter)) != std::string::npos) {
  224. token = input.substr(0, pos);
  225. output.push_back(token);
  226. input.erase(0, pos + 1);
  227. }
  228. output.push_back(input);
  229. return output;
  230. }
  231.  
  232. void controller(std::string str) {
  233.  
  234. if (str == "") {
  235. MessageBox::Show("Введи данные в правильном формате!", "Ошибка ввода данных", MessageBoxButtons::OK, MessageBoxIcon::Error);
  236. return;
  237. }
  238.  
  239.  
  240. std::string file_name = "report.html";
  241. auto current_path = std::filesystem::current_path().string() + (string)"\\" + file_name;
  242.  
  243. ofstream myfile;
  244.  
  245. myfile.open(current_path);
  246. myfile << "<!DOCTYPE html><html><head></head><body>";
  247. myfile << "Сортировка массива методом перебора.<br>Массив до сортировки: <br>";
  248.  
  249. vector<int> vec;
  250. int column_size = 0;
  251. vector<std::string> rows = split_by_delimiter(str, '\n');
  252. for (auto row : rows) {
  253. vector<std::string> columns = split_by_delimiter(row, ' ');
  254. column_size = columns.size();
  255. for (auto column : columns) {
  256. vec.push_back(std::stoi(column, nullptr, 10));
  257. myfile << column + " ";
  258. }
  259. myfile << "<br>";
  260. }
  261.  
  262. int vecsize = vec.size();
  263. for (int j = 0; j < vecsize - 1; ++j) {
  264.  
  265. int min = j;
  266. for (int i = j + 1; i < vecsize; ++i) {
  267. if (vec.at(min) > vec.at(i)) {
  268. min = i;
  269. }
  270.  
  271. }
  272. if (min != j)
  273. swap(vec.at(j), vec.at(min));
  274. }
  275.  
  276. myfile << "Массив после сортировки: <br> ";
  277. for (int i = 0; i < vec.size();) {
  278. myfile << std::to_string(vec[i]) + " ";
  279. if (!(++i % column_size)) myfile << "<br>";
  280. }
  281.  
  282. myfile << "</body></html>" << std::endl;
  283. myfile.close();
  284.  
  285. webBrowser1->Navigate(gcnew String(current_path.c_str()));
  286.  
  287. this->tabControl1->SelectedIndex = 1;
  288. }
  289.  
  290. public: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
  291. controller(msclr::interop::marshal_as<std::string>(input_array->Text));
  292.  
  293. }
  294.  
  295. private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) {
  296. openFileDialog1->Title = "Открыть файл";
  297. openFileDialog1->Filter = "Text Files(*.txt)|*.txt|All files (*.*)|*.*";
  298.  
  299. if (openFileDialog1->ShowDialog() == Windows::Forms::DialogResult::OK) {
  300. std::ifstream input(CString(openFileDialog1->FileName));
  301. std::stringstream sstr;
  302. while (input >> sstr.rdbuf());
  303.  
  304. controller(sstr.str());
  305.  
  306. input.close();
  307. }
  308. }
  309.  
  310. private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
  311. exit(0);
  312. }
  313. private: System::Void input_array_TextChanged(System::Object^ sender, System::EventArgs^ e) {
  314. }
  315. };
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement