GamerSK

Uloha 11/2

Jan 9th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TForm1 *Form1;
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm1::TForm1(TComponent* Owner)
  13.     : TForm(Owner)
  14. {
  15. }
  16. //---------------------------------------------------------------------------
  17. /*1) Generuj n random cisiel do listboxu
  18.      - moznost ulozit do suboru generuj.txt (cez dialog)
  19.   2) Nacita zo suboru generuj.txt (cez dialog) cisla do stlpcov v tabulke (stringgrid)
  20.   3) Zo stlpca v tabulke prekopirujes tieto cisla do komponentu memo tak aby boli v riadku 13 8 20 ...
  21.   4) Vypis poctu parnych, neparnych, nulovych cisiel (ShowMessage) Vypis je robeny z MEMA
  22.   5) Nacitanie tychto cisiel zo sltpca zabulky do pola
  23.   vypis cisiel z pola do druheho stlpca v tabulke
  24.   vypis zoradenych cisiel do tretieho stlpca v tabulke
  25. */
  26. void __fastcall TForm1::Button1Click(TObject *Sender) // 1)
  27. {
  28.     ListBox1->Items->Clear();
  29.     int n = StrToInt(InputBox("", "", ""));
  30.     for (int i = 0; i < n; i++) {
  31.         ListBox1->Items->Add(random(100));
  32.     }
  33.     if ( SaveDialog1->Execute() ) {
  34.         ListBox1->Items->LoadFromFile(SaveDialog1->FileName);
  35.     }
  36. }
  37. //---------------------------------------------------------------------------
  38.  
  39. void __fastcall TForm1::Button2Click(TObject *Sender) // 2)
  40. {
  41.     for (int i = 1; i < StringGrid1->RowCount; i++) {
  42.         StringGrid1->Cells[0][i] = "";
  43.         StringGrid1->Cells[1][i] = "";
  44.     }
  45.     ListBox2->Items->Clear();
  46.     StringGrid1->RowCount = 2;
  47.     if ( OpenDialog1->Execute() ) {
  48.         ListBox2->Items->LoadFromFile(OpenDialog1->FileName);
  49.     }
  50.     StringGrid1->RowCount = ListBox2->Items->Count + 1;
  51.     for (int i = 0; i < ListBox2->Items->Count; i++) {
  52.         StringGrid1->Cells[0][i+1] = i+1;
  53.         StringGrid1->Cells[1][i+1] = ListBox2->Items->Strings[i];
  54.     }
  55. }
  56. //---------------------------------------------------------------------------
  57.  
  58. void __fastcall TForm1::Button3Click(TObject *Sender) // 3)
  59. {
  60.     Memo1->Lines->Clear();
  61.     AnsiString str = "";
  62.     for (int i = 1; i < StringGrid1->RowCount; i++) {
  63.         str += StringGrid1->Cells[1][i];
  64.         if ( i-1 != StringGrid1->RowCount ) {
  65.             str += " ";
  66.         }
  67.     }
  68.     Memo1->Lines->Add(str);
  69. }
  70. //---------------------------------------------------------------------------
  71.  
  72. void __fastcall TForm1::FormCreate(TObject *Sender)
  73. {
  74.     StringGrid1->Cells[0][0] = "#";
  75.     StringGrid1->Cells[1][0] = "Hodnota";
  76. }
  77. //---------------------------------------------------------------------------
  78.  
  79. void __fastcall TForm1::Button4Click(TObject *Sender) // 4)
  80. {
  81.     int a = 0,
  82.         b = 0,
  83.         c = 0;
  84.     AnsiString str = Memo1->Lines->Strings[0],
  85.                cislo = "";
  86.     for (int i = 1; i <= str.Length(); i++) {
  87.         if ( str[i] != ' ' ) {
  88.             cislo += str[i];
  89.         } else {
  90.             if ( StrToInt(cislo) % 2 == 0 ) {
  91.                 a++;
  92.             } else {
  93.                 b++;
  94.             }
  95.             if ( StrToInt(cislo) == 0 ) {
  96.                 c++;
  97.             }
  98.             cislo = "";
  99.         }
  100.     }
  101.     ShowMessage(
  102.         "Počet párnych: "+IntToStr(a)+"\n"+
  103.         "Počet nepárnych: "+IntToStr(b)+"\n"+
  104.         "Počet nulových: "+IntToStr(c)
  105.     );
  106. }
  107. //---------------------------------------------------------------------------
  108.  
  109. void __fastcall TForm1::Button5Click(TObject *Sender) //5)
  110. {
  111.     int pole[256];
  112.     for (int i = 1; i < StringGrid1->RowCount; i++) {
  113.         pole[i-1] = StrToInt(StringGrid1->Cells[1][i]);
  114.     }
  115.     for ( int i = 1; i < StringGrid1->RowCount; i++ ) {
  116.         StringGrid->Cells[2][i] = pole[i-1];
  117.     }
  118.     i = 0;
  119.     while ( i != StringGrid1->RowCount ) {
  120.         if ( pole[i] < pole[i+1] ) {
  121.             int p = pole[i];
  122.             pole[i] = pole[i+1];
  123.             pole[i+1] = p;
  124.             i = 0;
  125.         } else {
  126.             i++;
  127.         }
  128.     }
  129.     for ( int i = 1; i < StringGrid1->RowCount; i++ ) {
  130.         StringGrid->Cells[3][i] = pole[i-1];
  131.     }
  132. }
  133. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment