Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.36 KB | None | 0 0
  1.  
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. #include <fstream.h>
  6. #include <iostream.h>
  7. #include <iomanip.h>
  8. #define N 100
  9. #pragma package(smart_init)
  10. #pragma resource "*.dfm"
  11.  
  12. TForm1 *Form1;
  13. __fastcall TForm1::TForm1(TComponent* Owner)
  14.         : TForm(Owner)
  15. {
  16. }
  17. typedef struct  STR{
  18.     char fio[N];
  19.     char dolzn[N];
  20.     int stage;
  21.     int oklad;
  22.     char fack[N];
  23.     char date_create[N];
  24.     }
  25.     st;
  26.  
  27.     int n;
  28.     st sts[N];
  29.  
  30.    int counS=0; //счётчик сравнений
  31.    int counP=0; //счётчик перестановок
  32.  
  33.  
  34.    
  35. void print()
  36.  {
  37.    for (int i = 0; i < n; i++)
  38.     {
  39.  Form1->StringGrid1->Cells[0][i]=sts[i].fio;
  40.  Form1->StringGrid1->Cells[1][i]=sts[i].dolzn;
  41.  Form1->StringGrid1->Cells[2][i]=sts[i].stage;
  42.  Form1->StringGrid1->Cells[3][i]=sts[i].oklad;
  43.  Form1->StringGrid1->Cells[4][i]=sts[i].fack;
  44.  Form1->StringGrid1->Cells[5][i]=sts[i].date_create;
  45.       }
  46.    }
  47.  
  48. void print2()
  49.   {
  50.     for (int i = 0; i < n; i++)
  51.      {
  52. Form1->StringGrid2->Cells[0][i]=sts[i].fio;
  53. Form1->StringGrid2->Cells[1][i]=sts[i].dolzn;
  54. Form1->StringGrid2->Cells[2][i]=sts[i].stage;
  55. Form1->StringGrid2->Cells[3][i]=sts[i].oklad;
  56. Form1->StringGrid2->Cells[4][i]=sts[i].fack;
  57. Form1->StringGrid2->Cells[5][i]=sts[i].date_create;
  58.  
  59.  
  60.       }
  61.    }
  62.  
  63.  
  64. void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
  65. {
  66.     for (int i = 0; i < n; ++i)
  67.     {
  68.  strcpy(sts[i].fio, (StringGrid1->Cells[0][i].c_str()));
  69.  strcpy(sts[i].dolzn, (StringGrid1->Cells[1][i].c_str()));
  70.  sts[i].stage=StrToInt(StringGrid1->Cells[2][i]);
  71.  sts[i].oklad=StrToInt(StringGrid1->Cells[3][i]);
  72.  strcpy(sts[i].fack, (StringGrid1->Cells[4][i].c_str()));
  73.  strcpy(sts[i].date_create, (StringGrid1->Cells[5][i].c_str()));
  74.           }
  75.  
  76.  
  77.  
  78.  int k=0,q=0,tmp;
  79.  bool fl;
  80.  fl=1;
  81. // for (int i = 0 ; i < n ; i++)
  82.   while (fl)
  83.        {
  84.  
  85.        fl=0;
  86.        for (int j = 0 ; j < n - 1  ; j++)
  87.           {
  88.            q++;
  89.  
  90.            if (sts[j].oklad > sts[j+1].oklad )
  91.              {
  92.                fl=1;
  93.                tmp= sts[j].oklad;
  94.                sts[j].oklad = sts[j+1].oklad;
  95.                sts[j+1].oklad = tmp;
  96.                k++;
  97.              }
  98.         }
  99.     }
  100.  
  101.   print2();
  102.   Label3->Caption=k; // сравнения
  103.   Label4->Caption=q; // перестановки
  104.   ofstream out;
  105.   out.open("out.cpp");
  106.  
  107.     for (int i = 0; i < n-1; i++)
  108.       {
  109.         out<<sts[i].fio<<setw(14);
  110.     out<<sts[i].dolzn<<setw(14);
  111.     out<<sts[i].stage<<setw(14);
  112.     out<<sts[i].oklad<<setw(14);
  113.     out<<sts[i].fack<<setw(14);
  114.     out<<sts[i].date_create<<setw(14);
  115.         out<<endl;
  116.       }
  117.  
  118.      out.close();
  119. }
  120.  
  121.  
  122. void __fastcall TForm1::FormCreate(TObject *Sender)
  123. {
  124.  
  125. n=0;
  126.  
  127.  ifstream n_k;
  128.  n_k.open("in.cpp");
  129.    if (n_k.fail())
  130.       {
  131.      ShowMessage("No file");
  132.      exit(1);
  133.         }
  134.  
  135.  
  136.  while (!n_k.eof())
  137.     {
  138.       char s[N];
  139.       n_k.getline(s, sizeof(s) );
  140.       n++;
  141.       }
  142.  n_k.close();
  143.  
  144.  
  145.  
  146.     ifstream in;
  147.     in.open("in.cpp");
  148.     if (in.fail())
  149.       {
  150.      ShowMessage("No file");
  151.      exit(1);
  152.         }
  153.  
  154.    for (int i = 0; i < n; ++i)
  155.       {
  156.         in>>sts[i].fio;
  157.         in>>sts[i].dolzn;
  158.         in>>sts[i].stage;
  159.         in>>sts[i].oklad;
  160.         in>>sts[i].fack;
  161.         in>>sts[i].date_create;
  162.           }
  163.  
  164.          print();
  165.  
  166.  
  167.         in.close();
  168.  
  169. }
  170.  
  171. int  Merge(int first, int last)
  172.   {
  173.  int middle, start, final, j;
  174.  int buff[N];
  175.  middle=(first+last)/2; //делим массив
  176.  start=first; //начало левой части
  177.  final=middle+1; //начало правой части
  178.  
  179.   for(j=first; j<=last; j++) //от начала до конца
  180.    {
  181.  
  182.  if ((start<=middle) && ((final>last) || (sts[start].oklad < sts[final].oklad)))
  183.   {
  184.  
  185.   counP+=final-(middle+1);
  186.  
  187.   buff[j]=sts[start].oklad;
  188.   start++;
  189.  
  190.    }
  191.    else
  192.    {
  193.   buff[j]=sts[final].oklad;
  194.    final++;
  195.    
  196.    }
  197.      }
  198.  
  199.  //возвращение результата в список
  200.  
  201.   for (j=first; j<=last; j++)
  202.   sts[j].oklad=buff[j];
  203.   return counP;
  204.   }
  205.  
  206.  
  207. //рекурсивная процедура сортировки
  208.  void MergeSort(int first, int last)
  209.   {
  210.      
  211.     if (first<last)
  212.     {
  213.         MergeSort(first, (first+last)/2);        //сортируем левую часть
  214.         MergeSort((first+last)/2+1, last);      //сортируем правую часть
  215.         Merge(first, last);                     //сливаем две части
  216.  
  217.      }
  218.  
  219.  
  220. }
  221.  
  222.  
  223. void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
  224. {
  225.  
  226.   counP=0;
  227.   counS=0;
  228.  
  229.  
  230.  for (int i = 0; i < n; ++i)
  231.     {
  232. strcpy(sts[i].fio, (StringGrid1->Cells[0][i].c_str()));
  233. strcpy(sts[i].dolzn, (StringGrid1->Cells[1][i].c_str()));
  234. sts[i].stage=StrToInt(StringGrid1->Cells[2][i]);
  235. sts[i].oklad=StrToInt(StringGrid1->Cells[3][i]);
  236. strcpy(sts[i].fack, (StringGrid1->Cells[4][i].c_str()));
  237. strcpy(sts[i].date_create, (StringGrid1->Cells[5][i].c_str()));
  238. }
  239.  
  240.  
  241.   MergeSort(0, n-1);
  242.  
  243.   Form1->Label5->Caption=counS;
  244.   Form1->Label6->Caption=counP;
  245.  
  246.   print2();
  247.   ofstream out;
  248.   out.open("out.cpp");
  249.     for (int i = 0; i < n-1; ++i)
  250.       {
  251.         out<<sts[i].fio<<setw(8);
  252.     out<<sts[i].dolzn<<setw(8);
  253.     out<<sts[i].stage<<setw(8);
  254.     out<<sts[i].oklad<<setw(8);
  255.     out<<sts[i].fack<<setw(8);
  256.     out<<sts[i].date_create<<setw(8);
  257.         out<<endl;
  258.       }
  259.  
  260.      out.close();
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement