Смекни!
smekni.com

Об’єктно-орієнтована програма обліку продуктового магазину (стр. 4 из 4)

AnsiStringstr; // название мебели

AnsiStringstr1; // производитель

AnsiString str2; // вид мебели

void save(FILE *f);

void zvit(FILE *f);

int load(FILE *f);// 0 - помилка завантаження; EOF - кінець файла; >0 - завантаження успішне

};

5. Date.cpp

#include <vcl.h>

#pragma hdrstop

#include "Date.h"

bool Date::load(int d,int m,int y)

{ if(y>=1900)

{ switch(m)

{

case 1:// січень 31 день

case 3:// березень 31 день

case 5:// травень 31 день

case 7:// липень 31 день

case 8:// серпень 31 день

case 10:// жовтень 31 день

case 12:// грудень 31 день

if((d<1)||(d>31)) return false;

break;

case 2:// лютий 28-29 день

if(y%4)

{

if((d<1)||(d>29)) return false;

}

else

{

if((d<1)||(d>28)) return false;

}

break;

case 4:// квітень 30 день

case 6:// червень 30 день

case 9:// вересень 30 день

case 11:// листопад 30 день

if((d<1)||(d>30)) return false;

break;

default: return false;

}

Day = d;

Month = m;

Year = y;

X = d + m*100 + year*10000;

Name = StrToInt(d)+"."+StrToInt(m)+"."+StrToInt(y);

}

else return false;

}

bool Date::load(const AnsiString &s)

{

int x = s.Pos(".");

if(x)

{

AnsiString str = s.SubString(1,x-1);

int d = StrToInt(str);

str = s.SubString(x+1,s.Lenth-x);

int y = str.Pos(".");

if(y)

{

str = s.SubString(x+1,y+x-1);

int m = StrToInt(str);

str = s.SubString(y+x+1,s.Lenth-x-y);

int y = StrToInt(str);

return load(d,m,y);

}

else return false;

}

else return false;

}


6. Date.h

#ifndef DateH

#define DateH

class Date

{ int Day;

int Month;

int Year;

long X;

AnsiString Name;

public: bool load(int d,int m,int y);

bool load(const AnsiString &s);

int day(){return Day;}

int month(){return Month;}

int year(){return Year;}

const char& str(){return Str.c_str();}

const Date& operator=(const Date &D){Day = D.Day; Month = D.Month; Year = D.Year;}

bool operator>(const Date &D){return X>D.X;}

bool operator>=(const Date &D){return X>=D.X;}

bool operator<(const Date &D){return X<D.X;}

bool operator<=(const Date &D){return X<=D.X;}

bool operator==(const Date &D){return X==D.X;}

bool operator!=(const Date &D){return X==D.X;}

};

7. Main.cpp

#include <vcl.h>

#pragma hdrstop

#include "Main.h"

#include "My_date.h"

#include "Date.h"

#pragma package(smart_init)

#pragma resource "*.dfm"

TMainForm *MainForm;

__fastcall TMainForm::TMainForm(TComponent* Owner)

: TForm(Owner)

{

}

//------------------------------------------------------------------------void __fastcall TMainForm::Add(TObject *Sender)

{

Ptr p = list.record();

p->d = StrToInt(NewInt->Text);

p->day = StrToInt(Day->Text);

p->year = StrToInt(Year->Text);

p->month = StrToInt(Month->Text);

//p->x = StrToFloat(NewDouble->Text);

p->str = String->Text;

p->str1 = FirmaString->Text;

p->str2 = WidString->Text;

redraw();

}

void TMainForm::redraw()

{ List->Clear();

StrList->Clear();

for(Ptr p = list.first();p.valid();++p)

{

List->Items->Add(IntToStr(p->d));

StrList->Items->Add(p->str);

}

}

//------------------------------------------------------------------------void __fastcall TMainForm::Del(TObject *Sender)

{ int n = List->ItemIndex;

int i;

Ptr p;

for(i=0,p=list.first();p.valid()&&(i<n);++i,++p);

if((i==n)&&p.valid())

{

p.remove();

redraw();

}

}

//------------------------------------------------------------------------void __fastcall TMainForm::Find(TObject *Sender)

{ int x = StrToInt(NewInt->Text);

Ptr p;

int i;

for(i=0,p=list.first();p.valid()&&(p->d!=x);++p,++i);

if(p.valid())

{ List->ItemIndex = i;

StrList->ItemIndex = i;

NewInt->Text = IntToStr(p->d);

Day->Text = IntToStr(p->day);

Year->Text = IntToStr(p->year);

Month->Text = IntToStr(p->month);

//NewDouble->Text = FloatToStr(p->x);

String->Text = p->str;

FirmaString->Text = p->str1;

WidString->Text = p->str2;

}

else List->ItemIndex = -1;

}

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

void __fastcall TMainForm::Sort(TObject *Sender)

{

Ptr t,p,q;

for(t=list.first().next();t.valid();t=q)

{

q = t.next();

for(p=t.prev();p.valid()&&(p->d>t->d);--p);

p>>t;

}

redraw();

}

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

void __fastcall TMainForm::Save(TObject *Sender)

{

FILE *f;

if(SaveDialog->Execute())

{

f = fopen(SaveDialog->FileName.c_str(),"w");

if(f!=NULL)

{

list.save(f);

fclose(f);

}

}

}

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

void __fastcall TMainForm::ZvitClick(TObject *Sender)

{

FILE *f;

if(SaveDialog->Execute())

{

f= fopen(SaveDialog->FileName.c_str(),"w");

if(f!=NULL)

{

list.zvit(f);

fclose(f);

}

}

}

void __fastcall TMainForm::Open(TObject *Sender)

{

FILE *f;

if(OpenDialog->Execute())

{

f = fopen(OpenDialog->FileName.c_str(),"r");

if(f!=NULL)

{

list.clear();

String->Text="";

NewInt->Text="";

list.load(f);

redraw();

fclose(f);

}

}

}

//------------------------------------------------------------------------void __fastcall TMainForm::Clear(TObject *Sender)

{

list.clear();

redraw();

}

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

void __fastcall TMainForm::Select(TObject *Sender, TMouseButton Button,

TShiftState Shift, int X, int Y)

{

TPoint point;

point.x = X;

point.y = Y;

int sel = List->ItemAtPos(point,true);

int i;

Ptr t;

if(sel>=0)

{

List->ItemIndex = sel;

StrList->ItemIndex = sel;

for(t=list.first(),i=0;t.valid()&&(i<sel);++i,++t);

if(t.valid())

{

NewInt->Text = IntToStr(t->d);

Day->Text = IntToStr(t->day);

Year->Text = IntToStr(t->year);

Month->Text = IntToStr(t->month);

//NewDouble->Text = FloatToStr(t->x);

String->Text = t->str;

FirmaString->Text = t->str1;

WidString->Text = t->str2;

}

}

}

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

void __fastcall TMainForm::Update(TObject *Sender)

{

Ptr t;

int i;

int sel;

sel = List->ItemIndex;

if(sel>=0)

{ for(t=list.first(),i=0;t.valid()&&(i<sel);++i,++t);

if(t.valid())

{

t->d = StrToInt(NewInt->Text);

t->day = StrToInt(Day->Text);

t->year = StrToInt(Year->Text);

t->month = StrToInt(Month->Text);

t->str = String->Text;

t->str1 = FirmaString->Text;

t->str2 = WidString->Text;

//t->x = StrToFloat(NewDouble->Text);

redraw();

}

}

}

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

void __fastcall TMainForm::SelectStr(TObject *Sender, TMouseButton Button,

TShiftState Shift, int X, int Y)

{

TPoint point;

point.x = X;

point.y = Y;

int sel = StrList->ItemAtPos(point,true);

int i;

Ptr t;

if(sel>=0)

{

List->ItemIndex = sel;

StrList->ItemIndex = sel;

or(t=list.first(),i=0;t.valid()&&(i<sel);++i,++t);

if(t.valid())

{

NewInt->Text = IntToStr(t->d);

Day->Text = IntToStr(t->day);

Year->Text = IntToStr(t->year);

Month->Text = IntToStr(t->month);

String->Text = t->str;

FirmaString->Text = t->str1;

WidString->Text = t->str2;

}

}

}

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

void __fastcall TMainForm::SortNum(TObject *Sender)

{

Ptr t,p,q;

for(t=list.first().next();t.valid();t=q)

{

q = t.next();

for(p=t.prev();p.valid()&&(p->d>t->d);--p);

p>>t;

}

redraw();

}

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

void __fastcall TMainForm::SortStr(TObject *Sender)

{

Ptr t,p,q;

for(t=list.first().next();t.valid();t=q)

{

q = t.next();

for(p=t.prev();p.valid()&&(p->str>t->str);--p);

p>>t;

}

redraw();

}

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

void __fastcall TMainForm::Button1Click(TObject *Sender)

{

Close();

}


8. Main.h

#ifndef MainH

#define MainH

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include "Number.h"

#include <Dialogs.hpp>

#include <ComCtrls.hpp>

#include <ExtCtrls.hpp>

#include <jpeg.hpp>

#include "Date.h"

#include "My_date.h"

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

typedef MyTable<Number> Data;

typedef MyTable<Number>::Iterator Ptr;

class TMainForm : public TForm

{

__published:// IDE-managed Components

TEdit *NewInt;

TLabel *NewIntLabel;

TButton *AddButton;

TListBox *List;

TButton *DelButton;

TButton *FindButton;

TButton *SortButton;

TButton *SaveButton;

TButton *OpenButton;

TButton *Button3;

TButton *Zvit;

TSaveDialog *SaveDialog;

TOpenDialog *OpenDialog;

TEdit *String;

TLabel *Label1;

TButton *ChangeButton;

TListBox *StrList;

TLabel *NumLabel;

TLabel *StrLabel;

TEdit *FirmaString;

TEdit *WidString;

TLabel *Label3;

TLabel *Label4;

TLabel *Label5;

TLabel *Label6;

TLabel *Label7;

TLabel *Label8;

TEdit *Day;

TEdit *Month;

TEdit *Year;

TButton *Button1;

TImage *Image1;

void __fastcall Add(TObject *Sender);

void __fastcall Del(TObject *Sender);

void __fastcall Find(TObject *Sender);

void __fastcall Sort(TObject *Sender);

void __fastcall Save(TObject *Sender);

oid __fastcall Open(TObject *Sender);

void __fastcall Clear(TObject *Sender);

void __fastcall Select(TObject *Sender, TMouseButton Button,

TShiftState Shift, int X, int Y);

void __fastcall Update(TObject *Sender);

void __fastcall SelectStr(TObject *Sender, TMouseButton Button,

TShiftState Shift, int X, int Y);

void __fastcall SortNum(TObject *Sender);

void __fastcall SortStr(TObject *Sender);

void __fastcall Button1Click(TObject *Sender);

private:// User declarations

Data list;

void redraw();

public:// User declarations

__fastcall TMainForm(TComponent* Owner);

};

9. Magazin.cpp

#include <vcl.h>

#include "Date.h"

#pragma hdrstop

USERES("Magazin.res");

USEFORM("Main.cpp", MainForm);

USEUNIT("Table.cpp");

USEUNIT("Number.cpp");

USEUNIT("Date.cpp");

USEFILE("Date.h");

USEUNIT("My_date.cpp");

USEUNIT("data.cpp");

//------------------------------------------------------------------------WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)

{

try

{

Application->Initialize();

Application->CreateForm(__classid(TMainForm), &MainForm);

&ProgramForm);

Application->Run();

}

catch (Exception &exception)

{

Application->ShowException(&exception);

}

return 0;

}