Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit1.h"
- #include <fstream.h>
- #include <iostream.h>
- #include <iomanip.h>
- #define N 100
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- typedef struct STR{
- char fio[N];
- char dolzn[N];
- int stage;
- int oklad;
- char fack[N];
- char date_create[N];
- }
- st;
- int n;
- st sts[N];
- int counS=0; //счётчик сравнений
- int counP=0; //счётчик перестановок
- void print()
- {
- for (int i = 0; i < n; i++)
- {
- Form1->StringGrid1->Cells[0][i]=sts[i].fio;
- Form1->StringGrid1->Cells[1][i]=sts[i].dolzn;
- Form1->StringGrid1->Cells[2][i]=sts[i].stage;
- Form1->StringGrid1->Cells[3][i]=sts[i].oklad;
- Form1->StringGrid1->Cells[4][i]=sts[i].fack;
- Form1->StringGrid1->Cells[5][i]=sts[i].date_create;
- }
- }
- void print2()
- {
- for (int i = 0; i < n; i++)
- {
- Form1->StringGrid2->Cells[0][i]=sts[i].fio;
- Form1->StringGrid2->Cells[1][i]=sts[i].dolzn;
- Form1->StringGrid2->Cells[2][i]=sts[i].stage;
- Form1->StringGrid2->Cells[3][i]=sts[i].oklad;
- Form1->StringGrid2->Cells[4][i]=sts[i].fack;
- Form1->StringGrid2->Cells[5][i]=sts[i].date_create;
- }
- }
- void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
- {
- for (int i = 0; i < n; ++i)
- {
- strcpy(sts[i].fio, (StringGrid1->Cells[0][i].c_str()));
- strcpy(sts[i].dolzn, (StringGrid1->Cells[1][i].c_str()));
- sts[i].stage=StrToInt(StringGrid1->Cells[2][i]);
- sts[i].oklad=StrToInt(StringGrid1->Cells[3][i]);
- strcpy(sts[i].fack, (StringGrid1->Cells[4][i].c_str()));
- strcpy(sts[i].date_create, (StringGrid1->Cells[5][i].c_str()));
- }
- int k=0,q=0,tmp;
- bool fl;
- fl=1;
- // for (int i = 0 ; i < n ; i++)
- while (fl)
- {
- fl=0;
- for (int j = 0 ; j < n - 1 ; j++)
- {
- q++;
- if (sts[j].oklad > sts[j+1].oklad )
- {
- fl=1;
- tmp= sts[j].oklad;
- sts[j].oklad = sts[j+1].oklad;
- sts[j+1].oklad = tmp;
- k++;
- }
- }
- }
- print2();
- Label3->Caption=k; // сравнения
- Label4->Caption=q; // перестановки
- ofstream out;
- out.open("out.cpp");
- for (int i = 0; i < n-1; i++)
- {
- out<<sts[i].fio<<setw(14);
- out<<sts[i].dolzn<<setw(14);
- out<<sts[i].stage<<setw(14);
- out<<sts[i].oklad<<setw(14);
- out<<sts[i].fack<<setw(14);
- out<<sts[i].date_create<<setw(14);
- out<<endl;
- }
- out.close();
- }
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- n=0;
- ifstream n_k;
- n_k.open("in.cpp");
- if (n_k.fail())
- {
- ShowMessage("No file");
- exit(1);
- }
- while (!n_k.eof())
- {
- char s[N];
- n_k.getline(s, sizeof(s) );
- n++;
- }
- n_k.close();
- ifstream in;
- in.open("in.cpp");
- if (in.fail())
- {
- ShowMessage("No file");
- exit(1);
- }
- for (int i = 0; i < n; ++i)
- {
- in>>sts[i].fio;
- in>>sts[i].dolzn;
- in>>sts[i].stage;
- in>>sts[i].oklad;
- in>>sts[i].fack;
- in>>sts[i].date_create;
- }
- print();
- in.close();
- }
- int Merge(int first, int last)
- {
- int middle, start, final, j;
- int buff[N];
- middle=(first+last)/2; //делим массив
- start=first; //начало левой части
- final=middle+1; //начало правой части
- for(j=first; j<=last; j++) //от начала до конца
- {
- if ((start<=middle) && ((final>last) || (sts[start].oklad < sts[final].oklad)))
- {
- counP+=final-(middle+1);
- buff[j]=sts[start].oklad;
- start++;
- }
- else
- {
- buff[j]=sts[final].oklad;
- final++;
- }
- }
- //возвращение результата в список
- for (j=first; j<=last; j++)
- sts[j].oklad=buff[j];
- return counP;
- }
- //рекурсивная процедура сортировки
- void MergeSort(int first, int last)
- {
- if (first<last)
- {
- MergeSort(first, (first+last)/2); //сортируем левую часть
- MergeSort((first+last)/2+1, last); //сортируем правую часть
- Merge(first, last); //сливаем две части
- }
- }
- void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
- {
- counP=0;
- counS=0;
- for (int i = 0; i < n; ++i)
- {
- strcpy(sts[i].fio, (StringGrid1->Cells[0][i].c_str()));
- strcpy(sts[i].dolzn, (StringGrid1->Cells[1][i].c_str()));
- sts[i].stage=StrToInt(StringGrid1->Cells[2][i]);
- sts[i].oklad=StrToInt(StringGrid1->Cells[3][i]);
- strcpy(sts[i].fack, (StringGrid1->Cells[4][i].c_str()));
- strcpy(sts[i].date_create, (StringGrid1->Cells[5][i].c_str()));
- }
- MergeSort(0, n-1);
- Form1->Label5->Caption=counS;
- Form1->Label6->Caption=counP;
- print2();
- ofstream out;
- out.open("out.cpp");
- for (int i = 0; i < n-1; ++i)
- {
- out<<sts[i].fio<<setw(8);
- out<<sts[i].dolzn<<setw(8);
- out<<sts[i].stage<<setw(8);
- out<<sts[i].oklad<<setw(8);
- out<<sts[i].fack<<setw(8);
- out<<sts[i].date_create<<setw(8);
- out<<endl;
- }
- out.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement