Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Mergesort C Windows Form Application
- #pragma once
- #include <iostream>
- #include <time.h>
- #include <msclrmarshal.h>
- #include <msclrmarshal_cppstd.h>
- namespace prog1 {
- using namespace std;
- 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 msclr::interop;
- /// <summary>
- /// Summary for Form1
- /// </summary>
- public ref class Form1 : public System::Windows::Forms::Form
- {
- public:
- Form1(void)
- {
- InitializeComponent();
- //
- //TODO: Add the constructor code here
- //
- }
- static void randomize(int* a, int size){
- srand ( time(NULL) );
- for(unsigned i = 0; i < size; i++){
- a[i] = rand()%45 + 1;
- }
- for(unsigned i = 0; i < size; i++){
- cout << a[i] << " ";
- }
- }
- protected:
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- ~Form1()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::Label^ label1;
- protected:
- private: System::Windows::Forms::TextBox^ textBox1;
- private: System::Windows::Forms::Button^ randButton;
- private: System::Windows::Forms::Button^ increaseButton;
- private: System::Windows::Forms::Button^ decreaseButton;
- private: System::Windows::Forms::Label^ label2;
- private: System::Windows::Forms::RichTextBox^ richTextBox1;
- private: System::Windows::Forms::Button^ clearButton;
- private:
- /// <summary>
- /// Required designer variable.
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- void InitializeComponent(void)
- {
- this->label1 = (gcnew System::Windows::Forms::Label());
- this->textBox1 = (gcnew System::Windows::Forms::TextBox());
- this->randButton = (gcnew System::Windows::Forms::Button());
- this->increaseButton = (gcnew System::Windows::Forms::Button());
- this->decreaseButton = (gcnew System::Windows::Forms::Button());
- this->label2 = (gcnew System::Windows::Forms::Label());
- this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
- this->clearButton = (gcnew System::Windows::Forms::Button());
- this->SuspendLayout();
- //
- // label1
- //
- this->label1->AutoSize = true;
- this->label1->Location = System::Drawing::Point(13, 13);
- this->label1->Name = L"label1";
- this->label1->Size = System::Drawing::Size(65, 13);
- this->label1->TabIndex = 0;
- this->label1->Text = L"Enter a size:";
- //
- // textBox1
- //
- this->textBox1->Location = System::Drawing::Point(84, 13);
- this->textBox1->Name = L"textBox1";
- this->textBox1->Size = System::Drawing::Size(27, 20);
- this->textBox1->TabIndex = 1;
- //
- // randButton
- //
- this->randButton->Location = System::Drawing::Point(118, 9);
- this->randButton->Name = L"randButton";
- this->randButton->Size = System::Drawing::Size(75, 23);
- this->randButton->TabIndex = 2;
- this->randButton->Text = L"Random";
- this->randButton->UseVisualStyleBackColor = true;
- this->randButton->Click += gcnew System::EventHandler(this, &Form1::randButton_Click);
- //
- // increaseButton
- //
- this->increaseButton->Location = System::Drawing::Point(200, 9);
- this->increaseButton->Name = L"increaseButton";
- this->increaseButton->Size = System::Drawing::Size(75, 23);
- this->increaseButton->TabIndex = 3;
- this->increaseButton->Text = L"Increasing";
- this->increaseButton->UseVisualStyleBackColor = true;
- //
- // decreaseButton
- //
- this->decreaseButton->Location = System::Drawing::Point(282, 9);
- this->decreaseButton->Name = L"decreaseButton";
- this->decreaseButton->Size = System::Drawing::Size(75, 23);
- this->decreaseButton->TabIndex = 4;
- this->decreaseButton->Text = L"Decreasing";
- this->decreaseButton->UseVisualStyleBackColor = true;
- //
- // label2
- //
- this->label2->AutoSize = true;
- this->label2->Location = System::Drawing::Point(363, 14);
- this->label2->Name = L"label2";
- this->label2->Size = System::Drawing::Size(91, 13);
- this->label2->TabIndex = 5;
- this->label2->Text = L"# of comparisons:";
- //
- // richTextBox1
- //
- this->richTextBox1->Location = System::Drawing::Point(16, 44);
- this->richTextBox1->Name = L"richTextBox1";
- this->richTextBox1->Size = System::Drawing::Size(473, 238);
- this->richTextBox1->TabIndex = 6;
- this->richTextBox1->Text = L"";
- //
- // clearButton
- //
- this->clearButton->Location = System::Drawing::Point(411, 289);
- this->clearButton->Name = L"clearButton";
- this->clearButton->Size = System::Drawing::Size(75, 23);
- this->clearButton->TabIndex = 7;
- this->clearButton->Text = L"Clear";
- this->clearButton->UseVisualStyleBackColor = true;
- //
- // Form1
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(501, 319);
- this->Controls->Add(this->clearButton);
- this->Controls->Add(this->richTextBox1);
- this->Controls->Add(this->label2);
- this->Controls->Add(this->decreaseButton);
- this->Controls->Add(this->increaseButton);
- this->Controls->Add(this->randButton);
- this->Controls->Add(this->textBox1);
- this->Controls->Add(this->label1);
- this->Name = L"Form1";
- this->Text = L"CS4413 MergeSort";
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragma endregion
- static void print_arr(int* arr, int size){
- richTextBox1->Text = "[ ";
- for(int i = 0; i < size; i++){
- richTextBox1->Text = arr[i] + ", ";
- }
- richTextBox1->Text = " ]";
- }
- static void merge_arrays(int h, int m, int arr[], int arrA[], int arrB[]){
- int i = 0,j=0,k=0;
- while(i < h && j < m){
- if(arrA[i] < arrB[j]){
- arr[k] = arrA[i];
- i++;
- }else{
- arr[k] = arrB[j];
- j++;
- }
- k++;
- }
- if(i > h){
- for(int x = j; x < m; x++){
- arr[k] = arrB[x];
- k++;
- }
- }else{
- for(int x = i; x < h; x++){
- arr[k] = arrA[x];
- k++;
- }
- }
- }
- static int* merge_sort(int* arr, const int size){
- if ( size == 1 )
- return arr;
- int h = size/2;
- int m = size/2;
- int arrayAHsize = h;
- int arrayBMsize = size - m;
- //cout << "h: " << h << "arr[h]: " << arr[h] << "m: " << m << " arraryBMsize" <<
- //arrayBMsize<< endl;
- int *arrA = (int*)malloc(h);
- int *arrB = (int*)malloc(arrayBMsize);
- int* pa;
- int* pb;
- for(int i = 0; i < h; i++){
- arrA[i] = arr[i];
- }
- for(int i = 0; i < arrayBMsize; i++){
- arrB[i] = arr[i + h];
- }
- cout << endl;
- print_arr(arrA, size/2);
- cout << "----";
- print_arr(arrB, arrayBMsize);
- cout << endl;
- //l1 = mergesort( l1 )
- pa = merge_sort(arrA,h);
- //l2 = mergesort( l2 )
- pb = merge_sort(arrB, arrayBMsize);
- merge_arrays( h, arrayBMsize,arr, arrA, arrB);
- }
- private: System::Void randButton_Click(System::Object^ sender, System::EventArgs^ e) {
- String^ s = textBox1->Text;
- string Ssize = marshal_as<std::string>(s);
- const int size = atoi(Ssize.c_str());
- //int a[atoi(Ssize.c_str())];
- int *a = (int*)malloc(size);
- int* pa = a;
- //int* pa;
- randomize(a, size);
- richTextBox1->Text += "Your set: n";
- for(int i = 0; i < size; i++){
- richTextBox1->Text += a[i] + ", ";
- }
- pa = merge_sort(a,size);
- }
- };
- }
- static void print_arr(int* arr, int size){
- richTextBox1->Text = "[ ";
- for(int i = 0; i < size; i++){
- richTextBox1->Text = arr[i] + ", ";
- }
- richTextBox1->Text = " ]";
- }
Add Comment
Please, Sign In to add comment