Text: |
ԲΠ= GVA
Çàïèòàííÿ:#include <iostream.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define DBFN "base.db" // imya bazi danih
#define MAXSTR 80 // MAXIMALNA DOVJUNA SRTOKI
struct TDataType{ // tup zapusyemuh danyh
char nazva[MAXSTR];
int obsag;
char data[MAXSTR];
double vartist;
};
long FileGetCount(){
// metod rahue kilkist elementiv v baza danyh
FILE *file = fopen(DBFN,"rb");
fseek (file , 0 , SEEK_END);
long Size = ftell (file) / sizeof(TDataType);
// rewind (file);
fclose(file);
return Size;
}
long EnterN(long max=0)
{
// programa prosut vvesty 4uslo NUMBER = [0;MAX]
if (max == -1) return -1;
long a;
cout<<endl;
do
{
cout<<" Enter # [ 0 - "<<max<<" ] : ";
cin>>a;
} while (a<0 && a>max);
return a;
}
TDataType FileShowByIndex(long index)
{
// povertae strukturu danuh pid miscem index v basi danuh (napramy)
long count = FileGetCount();
TDataType temp,temp0;
FILE *file = fopen(DBFN,"rb");
rewind(file);
long i = 0;
fseek(file, sizeof(TDataType)*index, 0);
// for (i = 0 ; i < count ; i++)
{
fread(&temp,sizeof(TDataType),1,file);
// if ( index == i )
temp0=temp;
}
fclose(file);
return temp0;
}
void FileEditByIndex(long index,TDataType a)
{
// zapusye struktury v BD v index pozuciy
TDataType temp;
FILE *file = fopen(DBFN,"r+b");
rewind(file);
fseek(file, sizeof(TDataType)*index, 0);
fwrite(&a,sizeof(TDataType),1,file);
fclose(file);
return ;
}
void ShowInfoDlg(TDataType &a)
{
// VUVODUT stdukturu v zrozymilomy formati dla korustuvacha
clrscr();
cout<<"\n";
cout<<" NAZVA : "<<a.nazva<<endl;
cout<<" OBSAG : "<<a.obsag<<endl;
cout<<" DATA : "<<a.data<<endl;
cout<<" VART : "<<a.vartist<<endl;
return ;
}
void FileAdd(TDataType temp)
{
// DODAE V KINEC FAILU strukruty danuh
FILE *file = fopen(DBFN,"a+b");
fwrite(&temp,sizeof(temp),1,file);
fclose(file);
return ;
}
void FileShowAll()
{
long count = FileGetCount();
TDataType temp;
FILE *file = fopen(DBFN,"rb");
rewind(file);
long i = 0;
if (count == 0) return ;
for (i = 0 ; i < count ; i++)
{
fread(&temp,sizeof(TDataType),1,file);
ShowInfoDlg(temp);
cout<<"\n\r\n\r REC# "<<i<<" OF "<<count-1<<endl<<endl;
system("PAUSE");
}
fclose(file);
return ;
}
int ConfirmYN(char *str)
{
// prosut korustyvacha natusnuty Y 4u N
int z;
cout<<str<<" [Y/N] ? ";
do
{
z = getch();
} while ( z != 'n' && z != 'N' && z != 'y' && z != 'Y');
cout<<endl;
if (z == 'y' || z == 'Y') return 1;
return 0;
}
int AddInfoDlg(TDataType &a)
{
// dialog dodavanna struktury
// !!!!!!!! ATENTION STR PISAT ODNIM SLOVOM!!!! CPU_CELERON ulu 12.12.12
clrscr();
cout<<"\n";
cout<<" Enter NAZVA [ STR ] : ";
cin>>a.nazva;
cout<<" Enter OBSAG [ INT ] : ";
cin>>a.obsag;
cout<<" Enter DATA [ STR ] : ";
cin>>a.data;
cout<<" Enter VART [ DBL ] : ";
cin>>a.vartist;
return ConfirmYN(" Applay changes");
}
int main()
{
int key;
do
{
clrscr();
cout<<" PROGRAM MAIN MENU:\n\n 1 ) Add new record\n 2 ) Edit record in database\n 3 ) Show record by indnex\n 4 ) Show all records \n 0 ) Destroy database\n\n ESC) exit \n\n";
cout<<" RECORD(S) : "<<FileGetCount()<<endl;
key=getch();
if (key == '1')
{
TDataType temp;
if (AddInfoDlg(temp)==1)
{
FileAdd(temp);
}
}
else
if (key == '2' && FileGetCount())
{
long n = EnterN(FileGetCount()-1);
ShowInfoDlg(FileShowByIndex(n));
TDataType temp;
if (ConfirmYN(" Change this record")==1)
{
if (AddInfoDlg(temp)==1)
{
FileEditByIndex(n,temp);
}
}
}
else
if (key == '3' && FileGetCount()!=0)
{
ShowInfoDlg(FileShowByIndex(EnterN(FileGetCount()-1)));
cout<<endl;
system("pause");
}
else
if (key == '4' && FileGetCount()!=0)
{
FileShowAll();
cout<<endl<<" DONE."<<endl;
system("pause");
}
else
if (key == '0' && FileGetCount())
{
if (ConfirmYN(" Delete DATABASE") == 1)remove(DBFN);
}
} while (key!=27);
return 0;
}
====================================
|