Материал: Создание диалоговых программ с использованием структур и файлов в среде C++Builder

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам

{

//Çàïèñü òàáëèöû â ôàéë:i,n;str;

try { //Áëîê çàùèòû îïåðàòîðîâ.

//Ïåðåñ÷åò ñðåäíèõ áàëëîâ ïåðåä ñîõðàíåíèåì:

//Îòêðûòèå ôàéëà äëÿ çàïèñè:(SaveDialog1->Execute()){_Zap = SaveDialog1->FileName;=fopen(File_Zap.c_str(),"wb");=StringGrid1->RowCount-1;

//Çàïèñü ñòðîê òàáëèöû â ôàéë:(i=1;i<=n;i++) {.namber=StrToInt(StringGrid1->Cells[0][i]);=StringGrid1->Cells[1][i];(sprav.Naimenovanie,str.c_str());.nachas=StrToFloat(StringGrid1->Cells[2][i]);.rascenka=StrToFloat(StringGrid1->Cells[3][i]);.colvo=StrToInt(StringGrid1->Cells[4][i]);.obshee=StrToFloat(StringGrid1->Cells[5][i]);.sum=StrToFloat(StringGrid1->Cells[6][i]);(&sprav,size,1,f);

}

//Çàêðûòèå ôàéëà:(f);

ShowMessage("Äàííûå ñ òàáëèöû óñïåøíî çàïèñàíû â ôàéë!");

}

}( ... ){

//Îáðàáîòêà îøèáêè íåêîððåêòíîñòè äàííûõ:(f);

ShowMessage("Íåêîððåêòíûå äàííûå!");

}

}

//---------------------------------------------------------------------------__fastcall TForm1::Open1Click(TObject *Sender)

{(OpenDialog1->Execute()){_Zap = OpenDialog1->FileName;=fopen(File_Zap.c_str(),"rb");

//×òåíèå n (êîëè÷åñòâî çàïèñåé):D_f = fileno(f); // Íàõîäèì äåñêðèïòîð ôàéëàlen = filelength(D_f); // Íàõîäèì ðàçìåð ôàéëài,n;= len/size; //Êîëè÷åñòâî çàïèñåé â ôàéëå->RowCount=n+1;

//×òåíèå n çàïèñåé èç ôàéëà â òàáëèöó:(i=1;i<=n;i++) {(&sprav,size,1,f);->Cells[0][i]=IntToStr(sprav.namber);->Cells[1][i]=sprav.Naimenovanie;->Cells[2][i]=FloatToStr(sprav.nachas);->Cells[3][i]=FloatToStr(sprav.rascenka);->Cells[4][i]=IntToStr(sprav.colvo);->Cells[5][i]=FloatToStr(sprav.obshee);->Cells[6][i]=FloatToStr(sprav.sum);

}(f);

}

}

//---------------------------------------------------------------------------__fastcall TForm1::N2Click(TObject *Sender)

{j;->RowCount=2;(j=1;j<=StringGrid1->ColCount-1;j++)->Cells[j][1]="";

}

//---------------------------------------------------------------------------__fastcall TForm1::Button1Click(TObject *Sender)

{->RowCount++;->Cells[0][StringGrid1->RowCount-1]=IntToStr(StringGrid1->RowCount-1);

}

//---------------------------------------------------------------------------__fastcall TForm1::Button2Click(TObject *Sender)

{i,j;(StringGrid1->RowCount>2) {(i=StringGrid1->Row;i<StringGrid1->RowCount-1;i++) {->Cells[0][i]=IntToStr(i);(j=1;j<=StringGrid1->ColCount-1;j++) {->Cells[j][i]=StringGrid1->Cells[j][i+1];

}

}->RowCount--;

}

}

//-------------------------------------------------------------------------__fastcall TForm1::Edit1Change(TObject *Sender)

{->Clear();

}

//---------------------------------------------------------------------------__fastcall TForm1::N6Click(TObject *Sender)

{i;(i=1;i<StringGrid1->RowCount;i++) {.nachas=StrToFloat(StringGrid1->Cells[2][i]);.rascenka=StrToFloat(StringGrid1->Cells[3][i]);.colvo=StrToFloat(StringGrid1->Cells[4][i]);.obshee=sprav.nachas*sprav.colvo;.sum=sprav.rascenka*sprav.colvo;->Cells[5][i]=FloatToStrF(sprav.obshee,ffFixed,8,2);->Cells[6][i]=FloatToStrF(sprav.sum,ffFixed,8,2);

//---------------------------------------------------------------------------__fastcall TForm1::N4Click(TObject *Sender)

{sb,sb_next;i,j,n;p;st;=StringGrid1->RowCount-1;{=false;(i=1;i<n;i++) {=StrToFloat(StringGrid1->Cells[0][i]);_next=StrToFloat(StringGrid1->Cells[0][i+1]);(sb>sb_next) {(j=0;j<=StringGrid1->ColCount-1;j++) {=StringGrid1->Cells[j][i];->Cells[j][i]=StringGrid1->Cells[j][i+1];->Cells[j][i+1]=st;

}=true;

}

}

}(p);

}

//---------------------------------------------------------------------------__fastcall TForm1::N5Click(TObject *Sender)

{sb,sb_next;i,j,n;p;st;=StringGrid1->RowCount-1;{=false;(i=1;i<n;i++) {=StrToFloat(StringGrid1->Cells[5][i]);_next=StrToFloat(StringGrid1->Cells[5][i+1]);(sb>sb_next) {(j=0;j<=StringGrid1->ColCount-1;j++) {=StringGrid1->Cells[j][i];->Cells[j][i]=StringGrid1->Cells[j][i+1];->Cells[j][i+1]=st;

}=true;

}

}

}(p);

}

//---------------------------------------------------------------------------__fastcall TForm1::Exit1Click(TObject *Sender)

{->Close();

}

//---------------------------------------------------------------------------__fastcall TForm1::Button4Click(TObject *Sender)

{i,n, k;Summ;enteredsumm;=StringGrid1->RowCount;=0;=StrToFloat(Edit1->Text);(i=1;i<n;i++) {=StrToFloat(StringGrid1->Cells[6][i]);(Summ<enteredsumm){++;

//StringGrid2->Cells[0][k]=IntToStr(k);->Cells[1][k]=StringGrid1->Cells[1][i];->Cells[2][k]=StringGrid1->Cells[2][i];->Cells[3][k]=StringGrid1->Cells[3][i];->Cells[4][k]=StringGrid1->Cells[4][i];->Cells[6][k]=StringGrid1->Cells[6][i];->Cells[5][k]=StringGrid1->Cells[5][i];

}

}

}

//---------------------------------------------------------------------------.h

//---------------------------------------------------------------------------

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <Grids.hpp>

#include <Dialogs.hpp>

#include <ActnCtrls.hpp>

#include <ActnMan.hpp>

#include <ToolWin.hpp>

#include <Menus.hpp>

//---------------------------------------------------------------------------TForm1 : public TForm

{

__published: // IDE-managed Components*StringGrid1;*Button1;*Button2;*Button4;*OpenDialog1;*SaveDialog1;*MainMenu1;*N1;*Open1;*Save1;*Exit1;*N2;*N3;*N4;*N5;*Edit1;*N6;*StringGrid2;__fastcall FormCreate(TObject *Sender);__fastcall Save1Click(TObject *Sender);__fastcall Open1Click(TObject *Sender);__fastcall N2Click(TObject *Sender);__fastcall Button1Click(TObject *Sender);__fastcall Button2Click(TObject *Sender);__fastcall Edit1Change(TObject *Sender);__fastcall N6Click(TObject *Sender);__fastcall N4Click(TObject *Sender);__fastcall N5Click(TObject *Sender);__fastcall Exit1Click(TObject *Sender);__fastcall Button4Click(TObject *Sender);: // User declarations:  // User declarations

__fastcall TForm1(TComponent* Owner);

};

//---------------------------------------------------------------------------PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

-----------------------------------------