Смекни!
smekni.com

База данных продуктового магазина (стр. 3 из 3)

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

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

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

}

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

int row = 1;

int rowtab = -1;

class Ta

{

public:

AnsiString Naimenovanie, Date1, Kod, Price, Quantity;

FILE *f;

void input(AnsiString, AnsiString, AnsiString, AnsiString, AnsiString);

void sozdanie_form();

void add();

void clear();

void delet();

void close();

void form_save();

void all();

void search();

void plus();

};

void Ta::input(AnsiString Naimenovanie, AnsiString Date, AnsiString Kod, AnsiString Price, AnsiString Quantity)

{

this->Naimenovanie = Naimenovanie ;

this->Date1 = Date1 ;

this->Kod = Kod;

this->Price = Price;

this->Quantity = Quantity;

}

void Ta::sozdanie_form()

{

Form1->DataGrid1->RowCount = 1;

Form1->DataGrid1->Cells[0][0] = "Íàèìåíîâàíèå";

Form1->DataGrid1->Cells[1][0] = "Äàòà ïîñòóïëåíèÿ";

Form1->DataGrid1->Cells[2][0] = "Êîä òîâàðà";

Form1->DataGrid1->Cells[3][0] = "Öåíà";

Form1->DataGrid1->Cells[4][0] = "Êîëè÷åñòâî";

char a1[50];

char b1[50];

char c1[50];

char d1[50];

char e1[50];

if ((this->f = fopen("salon.txt","rb")) != NULL)

{

f = fopen("salon.txt","rb");

while(!(feof(this->f)))

{

fread(a1,sizeof(char),50,this->f);

fread(b1,sizeof(char),50,this->f);

fread(c1,sizeof(char),50,this->f);

fread(d1,sizeof(char),50,this->f);

fread(e1,sizeof(char),50,this->f);

this->Naimenovanie = a1;

this->Date1 = b1;

this->Kod = c1;

this->Price = d1;

this->Quantity = e1;

if (!(feof(this->f)))

{

row++;

Form1->DataGrid1->RowCount = row;

Form1->DataGrid1->Cells[0][row - 1] = this->Naimenovanie;

Form1->DataGrid1->Cells[1][row - 1] = this->Date1;

Form1->DataGrid1->Cells[2][row - 1] = this->Kod;

Form1->DataGrid1->Cells[3][row - 1] = this->Price;

Form1->DataGrid1->Cells[4][row - 1] = this->Quantity;

}

}

}

else {this->f = fopen("salon.txt","wb");};

fclose(this->f);

}

void Ta::add()

{

Form1->DataGrid1->Cells[0][Form1->DataGrid1->RowCount] = Form1->Edit1->Text;

Form1->DataGrid1->Cells[1][Form1->DataGrid1->RowCount] = Form1->Edit2->Text;

Form1->DataGrid1->Cells[2][Form1->DataGrid1->RowCount] = Form1->Edit3->Text;

Form1->DataGrid1->Cells[3][Form1->DataGrid1->RowCount] = Form1->Edit4->Text;

Form1->DataGrid1->Cells[4][Form1->DataGrid1->RowCount] = Form1->Edit5->Text;

row++;

Form1->DataGrid1->RowCount = row;

Form1->Edit1->Clear();

Form1->Edit2->Clear();

Form1->Edit3->Clear();

Form1->Edit4->Clear();

Form1->Edit5->Clear();

}

void Ta::form_save()

{

this->f = fopen("salon.txt", "w+b");

char one[50];

char two[50];

char three[50];

char four[50];

char five[50];

for(int i = 1; i < row; i++)

{

AnsiString a1 = Form1->DataGrid1->Cells[0][i];

AnsiString a2 = Form1->DataGrid1->Cells[1][i];

AnsiString a3 = Form1->DataGrid1->Cells[2][i];

AnsiString a4 = Form1->DataGrid1->Cells[3][i];

AnsiString a5 = Form1->DataGrid1->Cells[4][i];

strncpy(one,a1.c_str(),50);

strncpy(two,a2.c_str(),50);

strncpy(three,a3.c_str(),50);

strncpy(four,a4.c_str(),50);

strncpy(five,a5.c_str(),50);

fwrite(one,sizeof(char),50,this->f);

fwrite(two,sizeof(char),50,this->f);

fwrite(three,sizeof(char),50,this->f);

fwrite(four,sizeof(char),50,this->f);

fwrite(five,sizeof(char),50,this->f);

}

fclose(this->f);

}

void Ta::delet()

{

int k = 0;

for(int i = 1; i < row; i++)

k++;

if (Form1->DataGrid1->RowCount == 1)ShowMessage( "Òàáëèöà ïóñòà" );

else

{

Form1->DataGrid1->Cells[0][rowtab] = Form1->DataGrid1->Cells[0][k];

Form1->DataGrid1->Cells[1][rowtab] = Form1->DataGrid1->Cells[1][k];

Form1->DataGrid1->Cells[2][rowtab] = Form1->DataGrid1->Cells[2][k];

Form1->DataGrid1->Cells[3][rowtab] = Form1->DataGrid1->Cells[3][k];

Form1->DataGrid1->Cells[4][rowtab] = Form1->DataGrid1->Cells[4][k];

Form1->DataGrid1->Cells[5][rowtab] = Form1->DataGrid1->Cells[5][k];

Form1->DataGrid1->RowCount--;

}

}

void Ta::all()

{

int k = 0;

for(int i = 1; i < row; i++)

k++;

Form1->Label6->Caption = "Êîëè÷åñòâî çàïèñåé â áàçå " + IntToStr(k);

}

void Ta::search()

{

int k = 0;

for (int i = 1; i < row; i++)

{

if ((Form1->DataGrid1->Cells[0][i] == Form1->Edit1->Text)||(Form1->DataGrid1->Cells[1][i] == Form1->Edit2->Text)||(Form1->DataGrid1->Cells[2][i] == Form1->Edit3->Text)||(Form1->DataGrid1->Cells[3][i] == Form1->Edit4->Text)||(Form1->DataGrid1->Cells[4][i] == Form1->Edit5->Text))

{

Form1->SearchGrid1->Cells[0][k] = Form1->DataGrid1->Cells[0][i];

Form1->SearchGrid1->Cells[1][k] = Form1->DataGrid1->Cells[1][i];

Form1->SearchGrid1->Cells[2][k] = Form1->DataGrid1->Cells[2][i];

Form1->SearchGrid1->Cells[3][k] = Form1->DataGrid1->Cells[3][i];

Form1->SearchGrid1->Cells[4][k] = Form1->DataGrid1->Cells[4][i];

Form1->SearchGrid1->Cells[5][k] = Form1->DataGrid1->Cells[5][i];

k++;

Form1->SearchGrid1->RowCount = k;

}

Form1->SearchGrid1->Visible = True;

}

Form1->Edit1->Clear();

Form1->Edit2->Clear();

Form1->Edit3->Clear();

Form1->Edit4->Clear();

Form1->Edit5->Clear();

}

void Ta::plus()

{

float k = StrToFloat(Form1->Edit6->Text);

k = k/100;

for (int i = 1; i < row; i++)

Form1->DataGrid1->Cells[3][i] = ((float)((int)((Form1->DataGrid1->Cells[3][i]*(1 + k))*100))/100);

}

void __fastcall TForm1::AddClick(TObject *Sender)

{

Ta a;

a.add();

}

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

void __fastcall TForm1::SaveClick(TObject *Sender)

{

Ta a;

a.form_save();

}

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

void __fastcall TForm1::FormCreate(TObject *Sender)

{

Ta a;

a.sozdanie_form();

}

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

void __fastcall TForm1::DeleteClick(TObject *Sender)

{

Ta a;

a.delet();

}

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

void __fastcall TForm1::SearchClick(TObject *Sender)

{

Ta a;

a.search();

}

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

void __fastcall TForm1::NumberClick(TObject *Sender)

{

Ta a;

a.all();

}

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

void __fastcall TForm1::plusClick(TObject *Sender)

{

Ta a;

a.plus();

}

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

void __fastcall TForm1::DataGrid1SelectCell(TObject *Sender, int ACol,

int ARow, bool &CanSelect)

{

rowtab = ARow;

}

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