sobota, 7 stycznia 2017

Binary numbers in Visual C++


#pragma endregion
private: System::Void write_Bits(System::Char x)
{
System::Int32 i;
System::Int32 temporary;
array <System::Char>^ date={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192};
System::String^ text;
text="";
for(i=8;i>=0;i--)
{
temporary=(date[i]&x);
if(temporary!=0)
text=text+"1";
else
text=text+"0";
}
listBox2->Items->Add(text);
}
private: System::Void write_left_Bits(System::Char x)
{
System::Int32 i;
System::Int32 temporary;
array <System::Char>^ date={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192};
System::String^ text;
text="";
for(i=8;i>=0;i--)
{
temporary=(date[i]&x);
if(temporary!=0)
text=text+"1";
else
text=text+"0";
}
listBox3->Items->Add(text);
}

   private: System::Void write_negation_Bits(System::Char x)
{
System::Int32 i;
System::Int32 temporary;
array <System::Char>^ date={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192};
System::String^ text;
text="";
for(i=8;i>=0;i--)
{
temporary=(date[i]&x);
if(temporary!=0)
text=text+"1";
else
text=text+"0";
}
listBox4->Items->Add(text);
}

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
;
System::Int32 i;
System::Int32 left;
System::Int32 negation;
for(i=0;i<32;i++)
{
listBox1->Items->Add(i.ToString());
write_Bits(i);
left=i<<1;
write_left_Bits(left);
negation=~i;
write_negation_Bits(negation);
 
}
 
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
};
}

piątek, 6 stycznia 2017

Operations on first numbers and tables. C++ Builder.

The algorithm primitive and time-consuming.



private: // User declarations
int __fastcall init_Random();
int __fastcall sum_Table(int *table, int size);
bool __fastcall is_First(int x);
int *a_table,*b_table,*c_table;
int *a_small_table, *b_small_table, *c_small_table;
int *a_index_table, *b_index_table, *c_index_table;
int *my_testing_table;
int *temporary_index;
int random_value;
int long_value;
void __fastcall init_Table();
void __fastcall destroy_Table();

TForm1 *Form1;
 int __fastcall TForm1::init_Random()
 {
srand(time(NULL));
 }
 int __fastcall TForm1::sum_Table(int *table, int size)
 {
int i,sum;
sum=0;
for(i=0;i<size;i++)
 sum+=table[i];
return sum;
 }
 bool __fastcall TForm1::is_First(int x)
 {
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
 if(x%i==0)
  ++sum;
if(sum==2)
 return true;
else
 return false;
 }
 void __fastcall TForm1::init_Table()
 {
a_table=new int[100];
b_table=new int[100];
c_table=new int[100];
a_small_table=new int [10];
b_small_table=new int [10];
c_small_table=new int [10];
a_index_table=new int [10];
b_index_table=new int [10];
c_index_table=new int [10];
my_testing_table=new int[30];
temporary_index=new int[30];

int i,j;
i=0;
j=1;
do
{
 if(is_First(j))
 {
 a_table[i]=b_table[i]=c_table[i]=j;
 ++i;
 ++j;
 }
 else
  ++j;

}while(i<100);

 }
 void __fastcall TForm1::destroy_Table()
 {
delete temporary_index;
delete c_table;
delete b_table;
delete a_table;
delete a_index_table;
delete b_index_table;
delete c_index_table;
delete a_small_table;
delete b_small_table;
delete c_small_table;
delete my_testing_table;
 }
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 destroy_Table();
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Random();
 init_Table();
 int i;
 i=rand()%25+75;
 random_value=a_table[i];
 long_value=3*random_value;
 Edit1->Text=IntToStr(random_value);
 Edit2->Text=IntToStr(random_value);
 Edit3->Text=IntToStr(random_value);
 Edit4->Text=IntToStr(long_value);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 ;
 int i,j,k,value;
 do
 {
 i=0;
 do
 {
   value=rand()%100;
   a_small_table[i]=a_table[value];
   a_index_table[i]=value;
   i++;
 }while(i<10);
 }while(sum_Table(a_small_table,10)!=random_value);

 do
 {
   i=0;
   do
   {
  value=rand()%100;
  b_small_table[i]=b_table[value];
  b_index_table[i]=value;
  i++;
   }while(i<10);
 }while(sum_Table(b_small_table,10)!=random_value);

 do
 {
   i=0;
   do
   {
 value=rand()%100;
 c_small_table[i]=c_table[value];
 c_index_table[i]=value;
 i++;
   }while(i<10);
 }while(sum_Table(c_small_table,10)!=random_value);
 for(i=0;i<10;i++)
 {
  ListBox1->Items->Add(IntToStr(a_small_table[i]));
  ListBox2->Items->Add(IntToStr(b_small_table[i]));
  ListBox3->Items->Add(IntToStr(c_small_table[i]));
  StringGrid1->Cells[0][i]=IntToStr(a_index_table[i]);
  StringGrid2->Cells[0][i]=IntToStr(b_index_table[i]);
  StringGrid3->Cells[0][i]=IntToStr(c_index_table[i]);
 }
 for(i=0,j=10,k=20;i<10;i++,j++,k++)
 {
my_testing_table[i]=a_small_table[i];
my_testing_table[j]=b_small_table[i];
my_testing_table[k]=c_small_table[i];
temporary_index[i]=a_index_table[i];
temporary_index[j]=b_index_table[i];
temporary_index[k]=c_index_table[i];
 }
 for(i=0;i<30;i++)
 {
StringGrid4->Cells[i][0]=IntToStr(temporary_index[i]);
StringGrid4->Cells[i][1]=IntToStr(my_testing_table[i]);
 }
}

sobota, 31 grudnia 2016

StringGrids with first numbers and random numbers in C++ Builder


private: // User declarations
bool __fastcall is_First(int x);
void __fastcall init_Random();
int *tab_first;

bool __fastcall TForm1::is_First(int x)
{
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
if(x%i==0)
 ++sum;
if(sum==2)
return true;
else
return false;
}
void __fastcall TForm1::init_Random()
{
srand(time(NULL));
}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Random();
 tab_first=new int[100];
 int i,sum;
 sum=1;
 i=0;
 do
 {
if(is_First(sum))
{
tab_first[i]=sum;
++sum;
++i;
}
else
++sum;
 }while(i<100);
 int j;
 sum=0;
 for(i=0;i<10;i++)
 {
  for(j=0;j<10;j++)
   {
StringGrid1->Cells[j][i]=IntToStr(tab_first[sum]);
++sum;
   }
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 delete tab_first;
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 int tab_sum[10][10];
 int temp_sum[10][10];
 int i,j;
  for(i=0;i<10;i++)
  {
   for(j=0;j<10;j++)
   {
tab_sum[i][j]=0;
temp_sum[j][i]=StrToInt(StringGrid1->Cells[j][i]);
   }
   }
  for(i=0;i<10;i++)
  {
 for(j=0;j<10;j++)
 {
 tab_sum[i][j]+=temp_sum[i][j];
 if(i>0)
  tab_sum[i][j]+=temp_sum[i-1][j];
 if(i>0 && j>0)
  tab_sum[i][j]+=temp_sum[i-1][j-1];
 if(j>0)
  tab_sum[i][j]+=temp_sum[i][j-1];
 if(i<9 && j>0)
  tab_sum[i][j]+=temp_sum[i+1][j-1];
 if(i<9)
  tab_sum[i][j]+=temp_sum[i+1][j];
 if(i<9 && j<9)
  tab_sum[i][j]+=temp_sum[i+1][j+1];
 if(j<9)
  tab_sum[i][j]+=temp_sum[i][j+1];
 if(i>0 && j<9)
  tab_sum[i][j]+=temp_sum[i-1][j+1];



 }
  }
  int k,sum_f,k1,sum_f1;
  k=0,k1=0;
  sum_f=0,sum_f1=0;
  for(i=0;i<10;i++)
  {
   for(j=0;j<10;j++)
   {
StringGrid2->Cells[i][j]=IntToStr(tab_sum[i][j]);
if(is_First(tab_sum[i][j]))
{
ListBox1->Items->Add(IntToStr(tab_sum[i][j]));
k++;
sum_f+=tab_sum[i][j];
}
else
{
ListBox2->Items->Add(IntToStr(tab_sum[i][j]));
k1++;
sum_f1+=tab_sum[i][j];
}
   }
  }
  Label2->Caption=IntToStr(k)+" elements";
  Edit1->Text=IntToStr(sum_f);
  Label5->Caption=IntToStr(k1)+" elements";
  Edit2->Text=IntToStr(sum_f1);
  int temp_random[100];
  int first_random[10];
  int sum_random[10];
  int sum_temp_random[100];
  int value_random[10];
  k=0;
  for(i=0;i<10;i++)
   {
for(j=0;j<10;j++)
{
  sum_temp_random[k]=tab_sum[i][j];
  k++;
}
   }
  i=0;
  for(j=0;j<100;j++)
   temp_random[j]=0;
  do
  {
do
{
k=rand()%100;
}while(temp_random[k]!=0);
temp_random[k]=1;
value_random[i]=k+1;
first_random[i]=tab_first[k];
sum_random[i]=sum_temp_random[k];
i++;
  }while(i<10);
  for(i=0;i<10;i++)
  {
 StringGrid3->Cells[i][0]=IntToStr(value_random[i]);
 StringGrid3->Cells[i][1]=IntToStr(first_random[i]);
 StringGrid3->Cells[i][2]=IntToStr(sum_random[i]);
  }

}

piątek, 30 grudnia 2016

The draw different numbers in the table, the sum of which is unknown in advance (Visual C++)


I want to draw the numbers in the table, the sum of which is equal to the sum previously proposed. The algorithm draws a different number when the sum is equal to the sum of the set.


#pragma endregion
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
srand(time(NULL));
int size_table, range,sum_table;
int sum_temporary;
int i,k;
size_table=Int32::Parse(textBox1->Text);
range=Int32::Parse(textBox2->Text);
sum_table=Int32::Parse(textBox3->Text);
dataGridView1->ColumnCount=size_table;
dataGridView1->RowCount=1;
dataGridView1->Rows[0]->Height=70;
array<System::Int32>^ temporary_value=gcnew array<System::Int32>(range);
array<System::Int32>^ result_value=gcnew array<System::Int32>(size_table);
for(;;)
                {
                 for(i=0;i<range;i++) 
            temporary_value[i]=0;
            sum_temporary=0;
            i=0;
           do
           {
            do
           {
        k=rand()%range;
       }while(temporary_value[k]!=0);
      temporary_value[k]=1;
         result_value[i]=k;
     sum_temporary+=k;
    i++;
        }while(i<size_table);
       if(sum_temporary==sum_table)
      break;
      }
          for(i=0;i<size_table;i++)
 dataGridView1->Rows[0]->Cells[i]->Value=result_value[i].ToString();
 textBox4->Text=sum_table.ToString();
}
};
}



niedziela, 25 grudnia 2016

Component PerformanceCounter in Visual C++



#pragma endregion
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
timer1->Enabled=false;
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
timer1->Enabled=true;
}
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {

label4->Text=Memory->NextValue().ToString()+"%";
label5->Text=Disc->NextValue().ToString()+"%";
label6->Text=CPU->NextValue().ToString()+"%";
System::Double proc_CPU;
System::Double proc_Memory;
System::Double proc_Disc;
proc_CPU=Double::Parse(CPU->NextValue().ToString());
if (proc_CPU<1.)
proc_CPU+=1;
proc_Memory=Double::Parse(Memory->NextValue().ToString());
if (proc_Memory<1.)
 proc_Memory+=1;

proc_Disc=Double::Parse(Disc->NextValue().ToString());
if (proc_Disc<1.)
 proc_Disc+=1;
progressBar1->Value=proc_Memory;
progressBar2->Value=proc_Disc;
progressBar3->Value=proc_CPU;

 
}
};
}



sobota, 17 grudnia 2016

Using Threat in programming Visual C++



#pragma endregion
private: Graphics^ my_graph1;
private: Graphics^ my_graph2;
private: Graphics^ my_graph3;
private: Graphics^ my_graph4;
private: Graphics^ my_pixels;
private: Graphics^ my_lines;
private: Pen^ my_pen1;
private: Pen^ my_pen2;
private: Pen^ my_pen3;
private: Pen^ my_pen4;
private: System::Void init_Random()
{
srand(time(NULL));
}
private: System::Void draw_Ellipse(Graphics^ graph,Pen^ pen)
{
int i,j;
for(i=3;i<77;i+=5)
graph->DrawEllipse(pen,0,0,i,i);
for(j=55;j<112;j+=8)
graph->DrawEllipse(pen,j,j,144,111);
}
private: System::Void draw_Lines(Graphics^ graph, Pen^ pen)
{
int i,j;
for(i=5;i<144;i+=5)
graph->DrawLine(pen,i,0,i,111);
for(j=5;j<111;j+=5)
graph->DrawLine(pen,0,j,144,j);

}

private: System::Void init_Lines()
{
my_lines=panel6->CreateGraphics();
Pen^ Pen_line1=gcnew Pen(System::Drawing::Color::Blue);
Pen^ Pen_line2=gcnew Pen(System::Drawing::Color::Yellow);
Pen^ Pen_line3=gcnew Pen(System::Drawing::Color::Green);
Pen^ Pen_line4=gcnew Pen(System::Drawing::Color::Purple);
int i,j;
Pen_line1->DashStyle=System::Drawing::Drawing2D::DashStyle::DashDot;
Pen_line2->DashStyle=System::Drawing::Drawing2D::DashStyle::Dot;
Pen_line4->DashStyle=System::Drawing::Drawing2D::DashStyle::Solid;

int margin1=5;
int margin2=15;
for(i=margin1;i<panel6->Width-margin1;i+=margin1)
my_lines->DrawLine(Pen_line1,i,margin1,i,panel6->Height-margin1);
for(j=margin1;j<panel6->Height-margin1;j+=margin2)
my_lines->DrawLine(Pen_line2,margin1,j,panel6->Width-margin1,j);
for(i=margin1;i<panel6->Width-margin1;i+=margin1)
my_lines->DrawLine(Pen_line3,i,margin1,i+margin2,panel6->Height-margin1);
for(j=margin1;j<panel6->Height-margin1;j+=margin1)
my_lines->DrawLine(Pen_line4,margin1,j,panel6->Width-margin1,j+margin2);

}
private: System::Void init_Graph()
{
my_graph1=panel1->CreateGraphics();
my_graph2=panel2->CreateGraphics();
my_graph3=panel3->CreateGraphics();
my_graph4=panel4->CreateGraphics();
my_pen1=gcnew Pen(System::Drawing::Color::Black);
my_pen2=gcnew Pen(System::Drawing::Color::Yellow);
my_pen3=gcnew Pen(System::Drawing::Color::Green);
my_pen4=gcnew Pen(System::Drawing::Color::Red);
draw_Lines(my_graph1,my_pen1);
draw_Lines(my_graph2,my_pen2);
draw_Lines(my_graph3,my_pen3);
draw_Lines(my_graph4,my_pen4);
draw_Ellipse(my_graph1,my_pen4);
draw_Ellipse(my_graph2,my_pen3);
draw_Ellipse(my_graph3,my_pen2);
draw_Ellipse(my_graph4,my_pen1);
 
}
private: System::Void init_Pixels()
{
my_pixels=panel5->CreateGraphics();
Bitmap^ my_bitmap=gcnew Bitmap(panel5->Width,panel5->Height);
int i,x,y;
System::Drawing::Color my_color;
for(i=0;i<100000;i++)
{
x=rand()%(panel5->Width-1)+1;
y=rand()%(panel5->Height-1)+1;
my_color=System::Drawing::Color::FromArgb(rand()%255,rand()%255,rand()%255);
my_bitmap->SetPixel(x,y,my_color);
}
my_pixels->DrawImage(dynamic_cast<Image^>(my_bitmap),10,10);
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Thread^ thread_graph=gcnew Thread(gcnew ThreadStart(this,&Form1::init_Graph));
thread_graph->Start();
Thread^ thread_pixels=gcnew Thread(gcnew ThreadStart(this,&Form1::init_Pixels));
thread_pixels->Start();
Thread^ thread_lines=gcnew Thread(gcnew ThreadStart(this,&Form1::init_Lines));
thread_lines->Start();
 
}
private: System::Void panel2_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
}
};
}