piątek, 3 marca 2017

My class which random and sorting another value in Visual C++

sorting_random.h

#include <stdlib.h>
#include <time.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;


#pragma once

ref class sorting_random
{
private:
System::Int16 my_size;
System::Int16 h_Many;
array<System::Single>^ table_value;
array<System::Single>^ temp_table;

public:
sorting_random(int size_array, int how_many);
void random_another_Value();
void Sorting();
void Write(ListBox^ LB);
};

sorting_random.cpp

#include "StdAfx.h"
#include "sorting_random.h"

sorting_random::sorting_random(int size_array, int how_many)
{
srand(time(NULL));
my_size=size_array;
h_Many=how_many;
table_value=gcnew array<System::Single>(h_Many);
temp_table=gcnew array<System::Single>(my_size);

}
void sorting_random::random_another_Value()
{
System::Int16 i;
System::Int16 j;
System::Int16 value;
for(i=0;i<my_size;i++)
temp_table[i]=0;
i=0;
do
{
do
{
value=rand()%my_size;
}while(temp_table[value]!=0);
 temp_table[value]=1;
 table_value[i]=value;
 ++i;
}while(i<h_Many);
}
void sorting_random::Sorting()
{
System::Int16 i;
System::Int16 j;
System::Int16 temp;
for(i=1;i<h_Many;i++)
for(j=h_Many-1;j>=i;j--)
if(table_value[j]<table_value[j-1])
{
temp=table_value[j-1];
table_value[j-1]=table_value[j];
table_value[j]=temp;
}
}
void sorting_random::Write(ListBox^ LB)
{
System::Int16 i;
for(i=0;i<h_Many;i++)
LB->Items->Add(table_value[i].ToString());
}

And example:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
sorting_random table_random(30,10);
table_random.random_another_Value();
table_random.Sorting();
table_random.Write(listBox1);
sorting_random smile_table(6,3);
smile_table.random_another_Value();
smile_table.Sorting();
smile_table.Write(listBox2);
sorting_random next_table(100,85);
next_table.random_another_Value();
next_table.Sorting();
next_table.Write(listBox3);
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
};


Brak komentarzy:

Prześlij komentarz