Guest User

Untitled

a guest
Aug 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. Mergesort C Windows Form Application
  2. #pragma once
  3. #include <iostream>
  4. #include <time.h>
  5. #include <msclrmarshal.h>
  6. #include <msclrmarshal_cppstd.h>
  7.  
  8. namespace prog1 {
  9.  
  10. using namespace std;
  11. using namespace System;
  12. using namespace System::ComponentModel;
  13. using namespace System::Collections;
  14. using namespace System::Windows::Forms;
  15. using namespace System::Data;
  16. using namespace System::Drawing;
  17. using namespace msclr::interop;
  18.  
  19. /// <summary>
  20. /// Summary for Form1
  21. /// </summary>
  22. public ref class Form1 : public System::Windows::Forms::Form
  23. {
  24. public:
  25. Form1(void)
  26. {
  27. InitializeComponent();
  28. //
  29. //TODO: Add the constructor code here
  30. //
  31. }
  32.  
  33. static void randomize(int* a, int size){
  34. srand ( time(NULL) );
  35. for(unsigned i = 0; i < size; i++){
  36. a[i] = rand()%45 + 1;
  37. }
  38. for(unsigned i = 0; i < size; i++){
  39. cout << a[i] << " ";
  40. }
  41. }
  42.  
  43. protected:
  44. /// <summary>
  45. /// Clean up any resources being used.
  46. /// </summary>
  47. ~Form1()
  48. {
  49. if (components)
  50. {
  51. delete components;
  52. }
  53. }
  54. private: System::Windows::Forms::Label^ label1;
  55. protected:
  56. private: System::Windows::Forms::TextBox^ textBox1;
  57. private: System::Windows::Forms::Button^ randButton;
  58. private: System::Windows::Forms::Button^ increaseButton;
  59. private: System::Windows::Forms::Button^ decreaseButton;
  60. private: System::Windows::Forms::Label^ label2;
  61. private: System::Windows::Forms::RichTextBox^ richTextBox1;
  62. private: System::Windows::Forms::Button^ clearButton;
  63.  
  64. private:
  65. /// <summary>
  66. /// Required designer variable.
  67. /// </summary>
  68. System::ComponentModel::Container ^components;
  69.  
  70. #pragma region Windows Form Designer generated code
  71. /// <summary>
  72. /// Required method for Designer support - do not modify
  73. /// the contents of this method with the code editor.
  74. /// </summary>
  75. void InitializeComponent(void)
  76. {
  77. this->label1 = (gcnew System::Windows::Forms::Label());
  78. this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  79. this->randButton = (gcnew System::Windows::Forms::Button());
  80. this->increaseButton = (gcnew System::Windows::Forms::Button());
  81. this->decreaseButton = (gcnew System::Windows::Forms::Button());
  82. this->label2 = (gcnew System::Windows::Forms::Label());
  83. this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
  84. this->clearButton = (gcnew System::Windows::Forms::Button());
  85. this->SuspendLayout();
  86. //
  87. // label1
  88. //
  89. this->label1->AutoSize = true;
  90. this->label1->Location = System::Drawing::Point(13, 13);
  91. this->label1->Name = L"label1";
  92. this->label1->Size = System::Drawing::Size(65, 13);
  93. this->label1->TabIndex = 0;
  94. this->label1->Text = L"Enter a size:";
  95. //
  96. // textBox1
  97. //
  98. this->textBox1->Location = System::Drawing::Point(84, 13);
  99. this->textBox1->Name = L"textBox1";
  100. this->textBox1->Size = System::Drawing::Size(27, 20);
  101. this->textBox1->TabIndex = 1;
  102. //
  103. // randButton
  104. //
  105. this->randButton->Location = System::Drawing::Point(118, 9);
  106. this->randButton->Name = L"randButton";
  107. this->randButton->Size = System::Drawing::Size(75, 23);
  108. this->randButton->TabIndex = 2;
  109. this->randButton->Text = L"Random";
  110. this->randButton->UseVisualStyleBackColor = true;
  111. this->randButton->Click += gcnew System::EventHandler(this, &Form1::randButton_Click);
  112. //
  113. // increaseButton
  114. //
  115. this->increaseButton->Location = System::Drawing::Point(200, 9);
  116. this->increaseButton->Name = L"increaseButton";
  117. this->increaseButton->Size = System::Drawing::Size(75, 23);
  118. this->increaseButton->TabIndex = 3;
  119. this->increaseButton->Text = L"Increasing";
  120. this->increaseButton->UseVisualStyleBackColor = true;
  121. //
  122. // decreaseButton
  123. //
  124. this->decreaseButton->Location = System::Drawing::Point(282, 9);
  125. this->decreaseButton->Name = L"decreaseButton";
  126. this->decreaseButton->Size = System::Drawing::Size(75, 23);
  127. this->decreaseButton->TabIndex = 4;
  128. this->decreaseButton->Text = L"Decreasing";
  129. this->decreaseButton->UseVisualStyleBackColor = true;
  130. //
  131. // label2
  132. //
  133. this->label2->AutoSize = true;
  134. this->label2->Location = System::Drawing::Point(363, 14);
  135. this->label2->Name = L"label2";
  136. this->label2->Size = System::Drawing::Size(91, 13);
  137. this->label2->TabIndex = 5;
  138. this->label2->Text = L"# of comparisons:";
  139. //
  140. // richTextBox1
  141. //
  142. this->richTextBox1->Location = System::Drawing::Point(16, 44);
  143. this->richTextBox1->Name = L"richTextBox1";
  144. this->richTextBox1->Size = System::Drawing::Size(473, 238);
  145. this->richTextBox1->TabIndex = 6;
  146. this->richTextBox1->Text = L"";
  147. //
  148. // clearButton
  149. //
  150. this->clearButton->Location = System::Drawing::Point(411, 289);
  151. this->clearButton->Name = L"clearButton";
  152. this->clearButton->Size = System::Drawing::Size(75, 23);
  153. this->clearButton->TabIndex = 7;
  154. this->clearButton->Text = L"Clear";
  155. this->clearButton->UseVisualStyleBackColor = true;
  156. //
  157. // Form1
  158. //
  159. this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  160. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  161. this->ClientSize = System::Drawing::Size(501, 319);
  162. this->Controls->Add(this->clearButton);
  163. this->Controls->Add(this->richTextBox1);
  164. this->Controls->Add(this->label2);
  165. this->Controls->Add(this->decreaseButton);
  166. this->Controls->Add(this->increaseButton);
  167. this->Controls->Add(this->randButton);
  168. this->Controls->Add(this->textBox1);
  169. this->Controls->Add(this->label1);
  170. this->Name = L"Form1";
  171. this->Text = L"CS4413 MergeSort";
  172. this->ResumeLayout(false);
  173. this->PerformLayout();
  174.  
  175. }
  176. #pragma endregion
  177.  
  178. static void print_arr(int* arr, int size){
  179. richTextBox1->Text = "[ ";
  180. for(int i = 0; i < size; i++){
  181. richTextBox1->Text = arr[i] + ", ";
  182. }
  183. richTextBox1->Text = " ]";
  184. }
  185.  
  186. static void merge_arrays(int h, int m, int arr[], int arrA[], int arrB[]){
  187. int i = 0,j=0,k=0;
  188. while(i < h && j < m){
  189. if(arrA[i] < arrB[j]){
  190. arr[k] = arrA[i];
  191. i++;
  192. }else{
  193. arr[k] = arrB[j];
  194. j++;
  195. }
  196. k++;
  197. }
  198. if(i > h){
  199. for(int x = j; x < m; x++){
  200. arr[k] = arrB[x];
  201. k++;
  202. }
  203. }else{
  204. for(int x = i; x < h; x++){
  205. arr[k] = arrA[x];
  206. k++;
  207. }
  208. }
  209. }
  210.  
  211. static int* merge_sort(int* arr, const int size){
  212. if ( size == 1 )
  213. return arr;
  214. int h = size/2;
  215. int m = size/2;
  216. int arrayAHsize = h;
  217. int arrayBMsize = size - m;
  218. //cout << "h: " << h << "arr[h]: " << arr[h] << "m: " << m << " arraryBMsize" <<
  219. //arrayBMsize<< endl;
  220. int *arrA = (int*)malloc(h);
  221. int *arrB = (int*)malloc(arrayBMsize);
  222. int* pa;
  223. int* pb;
  224. for(int i = 0; i < h; i++){
  225. arrA[i] = arr[i];
  226. }
  227. for(int i = 0; i < arrayBMsize; i++){
  228. arrB[i] = arr[i + h];
  229. }
  230. cout << endl;
  231. print_arr(arrA, size/2);
  232. cout << "----";
  233. print_arr(arrB, arrayBMsize);
  234. cout << endl;
  235. //l1 = mergesort( l1 )
  236. pa = merge_sort(arrA,h);
  237. //l2 = mergesort( l2 )
  238. pb = merge_sort(arrB, arrayBMsize);
  239. merge_arrays( h, arrayBMsize,arr, arrA, arrB);
  240. }
  241. private: System::Void randButton_Click(System::Object^ sender, System::EventArgs^ e) {
  242. String^ s = textBox1->Text;
  243. string Ssize = marshal_as<std::string>(s);
  244. const int size = atoi(Ssize.c_str());
  245. //int a[atoi(Ssize.c_str())];
  246. int *a = (int*)malloc(size);
  247. int* pa = a;
  248. //int* pa;
  249. randomize(a, size);
  250. richTextBox1->Text += "Your set: n";
  251. for(int i = 0; i < size; i++){
  252. richTextBox1->Text += a[i] + ", ";
  253. }
  254. pa = merge_sort(a,size);
  255.  
  256.  
  257.  
  258. }
  259. };
  260. }
  261.  
  262. static void print_arr(int* arr, int size){
  263. richTextBox1->Text = "[ ";
  264. for(int i = 0; i < size; i++){
  265. richTextBox1->Text = arr[i] + ", ";
  266. }
  267. richTextBox1->Text = " ]";
  268. }
Add Comment
Please, Sign In to add comment