//--------------------------------------------------------------------------- /* Projekt prezentujący algorytm szybkiego sumowania liczb z wykorzystaniem objektu StringGrid. Autor: Cyfrowy Baron, 2002. */ //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Indeksowanie(int n, int c) { suma = 0; for(int i = 0; i < n; i++){ if(!StringGrid1->Cells[c][i].Trim().IsEmpty()){ String a = StringReplace(StringGrid1->Cells[c][i + 1].Trim(), " ", "", TReplaceFlags() << rfReplaceAll); try{id[i] = a.ToDouble();}catch(...){id[i] = 0;} } else{id[i] = 0;} suma = suma + id[i]; } Edit1->Text = Wpisz(suma); } //--------------------------------------------------------------------------- AnsiString TForm1::Wpisz(double s) { String a = FloatToStrF(s, ffNumber, 7 , 2); return a; } //--------------------------------------------------------------------------- void __fastcall TForm1::Dodawanie(int n, String t) { String a = StringReplace(t.Trim(), " ", "", TReplaceFlags() << rfReplaceAll); suma = suma - id[n]; try{id[n] = a.ToDouble();}catch(...){id[n] = 0;} suma = suma + id[n]; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormShow(TObject *Sender) { Indeksowanie(StringGrid1->RowCount - 1, 1); } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol, int ARow, bool &CanSelect) { Dodawanie(StringGrid1->Row, StringGrid1->Cells[1][StringGrid1->Row]); Edit1->Text = Wpisz(suma); } //---------------------------------------------------------------------------