sobota, 10 marca 2018

Sorting data types with different techniques. (C++ Builder)




FILE - date.h

#include <vcl.h>
#include <string.h>

struct Persons
{
char name[40];
int age;
float income;
};



 bool is_Min (int i,int j) { return (i<j); }


int comp_char(const void *function1,const void *function2)
{
return(strcmp((char*)function1,(char*)function2));
}
void float_Sort(float *table, int size)
{
int i,j;
float temp_float;
for(i=1;i<size;i++)
{
j=i;
temp_float=table[j];
while((j>0) && (table[j-1]>temp_float))
{
table[j]=table[j-1];
j--;
}
table[j]=temp_float;
}
}

The main file:

#include <vcl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include "date.h"

__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
 srand(time(NULL));
}
//---------------------------------------------------------------------------






void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ListBox1->Items->Clear();
 ListBox2->Items->Clear();
 ListBox3->Items->Clear();
 ListBox4->Items->Clear();
 ListBox5->Items->Clear();
 ListBox6->Items->Clear();
 struct Persons *PP=new struct Persons[10];
 int i,j;
 int *ages=new int[10];
 float *incomes=new float[10];
 char p_date[10][20]={"John Connor","Bob Dylan","Steve Jobs","Michael Jordan","Barack Obama","John Rambo","Ronald Regan",
 "Don Johnson","Mike Tyson","Bruce Lee"};
 for(i=0;i<10;i++)
 {
ages[i]=40+rand()%40;
incomes[i]=1.+rand()%100/1.+rand()%100/1000.;
 }

 for(i=0;i<10;i++)
 {
   strcpy(PP[i].name,p_date[i]);
   PP[i].age=ages[i];
   PP[i].income=incomes[i];
 }
 for(i=0;i<10;i++)
 {
  ListBox1->Items->Add(PP[i].name);
  ListBox2->Items->Add(IntToStr(PP[i].age));
  ListBox3->Items->Add(FloatToStr(PP[i].income)+" mln$");
 }

 float_Sort(incomes,10);
 qsort((void*)p_date,10,sizeof(p_date[0]),comp_char);
 std::vector<int> int_vector(ages,ages+10);
 std::sort (int_vector.begin(), int_vector.end(), is_Min);
 std::vector<int>::iterator temp_iterator;

 for(std::vector<int>::iterator temp_iterator=int_vector.begin();temp_iterator!=int_vector.end();++temp_iterator)
  ListBox5->Items->Add(*temp_iterator);




 for(i=0;i<10;i++)
 {
   strcpy(PP[i].name,p_date[i]);
   PP[i].income=incomes[i];

 }



 for(i=0;i<10;i++)
 {
  ListBox4->Items->Add(PP[i].name);
  ListBox6->Items->Add(FloatToStr(PP[i].income)+" mln$");
 }





}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}



Brak komentarzy:

Prześlij komentarz