( )
050703
050704
(: , )
, , ,
(, )
: , .
++, . , . .
1
. , . , , , . .
2
, . :
1 2 3 N
5<=N<=12.
1. ( - ).
2. , .
3. () .
:
- ;
- ;
- ;
- ;
- ;
- .
4. , ( ) .
5. ++, 15-20 .
6. -.
3
3.1
, , (. ), , ( , [3]) .
|
|
:
- ;
- ;
- ( ), ;
- ;
- () , ;
- , ;
- .
3.2
3.2.1.
() . :
- ( ), , (), (), ;
- . , ( , ).
3.2.2
-
(),
. ++
. ,
.
.
3.2.3.
. ++
,
.
3.2.4.
,
,
.
,
( ).
, ,
,
.
3.2.5.
.
,
.
|
|
.
: ,
( ),
; ; .
,
.
[8].
:
"" ( ) (,
).
.
,
, .
. ""
, ,
.
.
, .
()
.
3.2.6.
.
.
,
, ,
.
:
- ;
- ,
.
3.2.7 ,
:
- ,
,
;
-
;
-
;
-
, , ,
.
3.2.8
.
, ,
, , .
, :
- - .
, ..
".cpp",".exe".
.
3.2.9
.
.
4.
4.1 ,
:
- / ;
- ;
- / , .
|
|
, .
, ,
, , ,
- .
4.2.
4.2.1. /
/ ,
/. /,
<fstream.h>,
, ifstream, ofstream
fstream.
++ .
: , /.
, , -, .
ifstream.
ofstream. ,
fstream.
:
ifstream fin; //
ofstream fout; //
fstream fio; // /
,
open().
.
:
void ifstream::open(char *_,
openmode = ios::in);
void ofstream::open(char *_,
openmode = ios::out | ios::trunc);
void fstream::open(char *_,
openmode = ios::in | ios::out);
_ ,
. .
openmode, ,
ios.
:
ios::app
ios::ate
ios::binary
ios::in
ios::nocreate
ios::noreplace
ios::trunc
ios::app .
,
( );
ios::ate .
, ,
/ ;
ios::in ( );
ios::out ( );
ios::binary .
.
, ,
/
.
, . ,
,
,
.
;
ios::nocreate .
, ;
ios::noreplace .
, ;
ios::trunc-
|
|
.
ofstream
.
prim.txt :
ofstream mystream; //
mystream.open(prim.txt); //
// prim.txt
open()
, ,
.
, ,
open().
(!).
:
if (!mystream)
{
cerr << \n;
//
}
open()
ifstream, ofstream fstream,
. ,
, open().
prim1.txt ( )
:
ifstream fi(prim1.txt);
close().
, , fi
:
fi.close();
,
. << >> ,
/,
cin cout , . ++
/.
4.2.2.
istream& get(char &); -
.
.
istream &get(char *, int _); -
, , ,
, _,
. ,
, get() .
, ,
.
istream &get(char *, int _,
char =\n); -
, , ,
, _,
, =\n,
. ,
, get() .
, ,
.
int get(); - .
.
istream &getline(char *, int _,
char = '\n'); -
, ,
, ,
_, =\n,
. ,
, getline() .
, , .
int peek(); - ,
, ,
.
istream &putback(char ); -
.
istream& read(char *, int _); -
,
_ , .
, _
, ,
, .
4.2.3.
ostream &put (char ); -
.
|
|
ostream & write(char *, int _); -
, ,
_ .
ostream &flush(); -
.
4.2.4.
/ ++ ,
. (get pointer),
,
. (put pointer),
, .
. ,
.
4.2.4.1.
istream &seekg(long ); -
,
.
istream &seekg(long , seek_dir ); -
,
.
seek_dir (enum), ios
:
enum seek_dir { beg=0, cur=1, end=2 };
:
ios::beg ;
ios::cur ;
ios::end - .
long tellg(); -
.
4.2.4.2.
ostream &seekp(long ); -
,
.
ostream &seekp(long , seek_dir ); -
,
. (
seek_dir seekg.)
long tellp(); -
.
4.2.5. /
/ ++
/.
/, io_state,
, ios
:
goodbit
eofbit
failbit
badbit
/
:
int rdstate(); - ;
int eof();
( eofbit);
int fail(); - ,
( failbit);
int bad(); - ,
( badbit);
int good(); - ,
( );
,
. :
void clear (int = 0); - .
4.3. / ,
>> <<
, ,
,
. << >> ,
, ,
.
:
ostream &operator << (ostream &stream, _ )
{
//
return stream;
}
ostream.
, stream . ( ostream
ios.)
. (, ,
-.)
stream, ostream.
, <<
/:
cout << ob1 << ob2 << ob3;
.
:
istream &operator >> (istream &stream, _ &)
{
//
return stream;
}
istream, .
. ,
.
- .
(, -
,
- -
).
- -
. -
, istream ifstream,
ostream ofstream.
1. . . C/C++./ . . .:
, 1996.
2. . . ++ 3- ./ . . .: BHV--
, 1998.
3. .., .. . -
.: , 1999.
4. .. ++. - .: , 1999.
5. .., .. Turbo C Borland C++/ .
... : , 1992.
6. .., .. ++. .: -
, 1996.
1
-
________________________________
____________ ______________
_____________________________
________________
-
2
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include <iomanip.h>
#include <stdlib.h>
#include <io.h>
#define FIO 20
#define NAME 26
#define GOD 6
#define IZD 12
#define KOL 6
ofstream fprot; //
struct BOOK
{
char fio[FIO]; // ...
char name[NAME]; //
char god[GOD]; //
char izd[IZD]; //
char tirag[KOL]; //
};
//
void begtabl();
void endtabl();
void sozdanie(char);
void prosmotr();
void vibor();
void del();
// p- pp pistream &operator >> (istream &in, BOOK &x){ int v; cout << "\n :"; in.seekg(0,ios::end); in.get(x.fio,FIO-1,'\n'); cout << "\n :"; in.seekg(0,ios::end); in.get(x.name,NAME-1,'\n'); cout<<"\n :"; in.seekg(0,ios::end); in >> v; itoa(v,x.god,10); // cout<<"\n:"; in.seekg(0,ios::end); in.get(x.izd,IZD-1,'\n'); cout<<"\n:"; in.seekg(0,ios::end); in >> v; itoa(v,x.tirag,10); return in;} // p- pp ostream &operator << (ostream &out, BOOK x){ out.setf(ios::left); out << "" << setw(FIO) << x.fio << "" << setw(NAME) << x.name << "" << setw(4) << x.god << "" << setw(IZD) << x.izd << "" << setw(5) << x.tirag << "\n"; fprot.setf(ios::left); fprot << "" << setw(FIO) << x.fio << "" << setw(NAME) << x.name << "" << setw(4) << x.god << "" << setw(IZD) << x.izd << "" << setw(5) << x.tirag << "\n"; return out;} 22 // p- pp c ifstream &operator >> (ifstream &in, BOOK &x){ in.getline(x.fio,FIO,'\n'); in.getline(x.name,NAME,'\n'); in.getline(x.god,GOD,'\n'); in.getline(x.izd,IZD,'\n'); in.getline(x.tirag,KOL,'\n'); return in;} // p- pp ofstream &operator << (ofstream &out, BOOK &x){ out << x.fio << '\n'; out << x.name << '\n'; out << x.god << '\n'; out << x.izd << '\n'; out << x.tirag << '\n'; return out;} void main(){ char c;// ofstream fprot; textbackground(BLUE); textcolor(15); fprot.open("protokol.txt"); // if (!fprot) { cerr << " \n"; return; } 23 while (1){ clrscr(); gotoxy(25,7); cout << " ╔═════════════════════════╗\n"; gotoxy(25,8); cout << " ║ ║\n"; gotoxy(25,9); cout << " ║ ║\n"; gotoxy(25,10); cout << " ║ 1. ║\n"; gotoxy(25,11); cout << " ║ 2. ║\n"; gotoxy(25,12); cout << " ║ 3. ║\n"; gotoxy(25,13); cout << " ║ 4. ║\n"; gotoxy(25,14); cout << " ║5. ║\n"; gotoxy(25,15); cout << " ║6. ║ \n"; gotoxy(25,16); cout << " ╚═════════════════════════╝\n"; gotoxy(25,18); cout << " -> "; cin.seekg(0,ios::end); c = cin.get(); switch(c){ case '1': case '2': sozdanie(c); break; case '3': prosmotr(); break; 24 case '4': vibor(); break; case '5': del(); break; case '6': fprot.close(); // return; default: cout << " 1 4" << endl; cout << " - Enter\n"; cin.seekg(0,ios::end); cin.get(); } }} // void sozdanie(char reg){ char c; // ff book.txt ofstream ff; BOOK s; if (reg == '1') { // ff.open("book.txt", ios::binary); fprot << " \n\n"; } else { // ff.open("book.txt", ios::app | ios::binary); fprot << " \n\n"; } 25 if (!ff) { cerr << " book.txt \n"; return; } // do { clrscr(); if (reg == '1') cout << " \n\n"; else cout << " \n\n"; cin >> s; // p ff << s; // cout<<"\np ?(Y/N /H)"; cin.seekg(0,ios::end); cin.get(c); } while (c =='y' || c=='Y' || c=='' || c==''); ff.close(); // p } // void prosmotr(){ ifstream finp; BOOK s; // fi kniga.txt finp.open("book.txt", ios::binary); if (!finp) { cerr << " book.txt \n"; return; } 26 clrscr(); gotoxy(30,1); cout<<" \n"; fprot<< "\n\n\n \n"; begtabl(); while (!finp.eof()) // { finp >> s; // if (strlen(s.fio) == 0) break; cout << s; // } finp.close(); // p endtabl(); cout << " - "; cin.seekg(0,ios::end); cin.get();} // void vibor(){ char c,fio[FIO], nazv[NAME], izd[IZD]; int god; ifstream fi; BOOK z; long l; fi.open("book.txt", ios::binary); if (!fi) { cerr << " book.txt \n"; return; } 27 while (1){ clrscr(); gotoxy(25,7); cout << "╔═════════════════════╗\n"; gotoxy(25,8); cout << "║ ║\n"; gotoxy(25,9); cout << "║ ║\n"; gotoxy(25,10); cout << "║ 1. ... ║\n"; gotoxy(25,11); cout << "║ 2. ║\n"; gotoxy(25,12); cout << "║3. ║\n"; gotoxy(25,13); cout << "║4. ║\n"; gotoxy(25,14); cout << "║5. ║\n"; gotoxy(25,15); cout << "╚═════════════════════╝\n"; gotoxy(25,18); fi.seekg(0L); //,ios::beg); fi.clear(!ios::eofbit); // cout << " -> "; cin.seekg(0,ios::end); cin.get(c); clrscr(); cout << " \n "; fprot << "\n\n \n "; switch(c) { case '5': return; 28 case '1': cout << " .. :"; cin.seekg(0,ios::end); cin.get(fio,FIO-1,'\n'); fprot << " :" << fio << endl; break; case '2': cout << " :"; cin.seekg(0,ios::end); cin.get(nazv,NAME-1,'\n'); fprot << " :" << nazv << endl; break; case '3': cout << " :"; cin.seekg(0,ios::end); cin.get(izd,IZD-1,'\n'); fprot << " :" << izd << endl; break; case '4': cout << " :"; cin.seekg(0,ios::end); cin >> god; fprot << " :" << god << endl; break;}begtabl();while (fi){ fi >> z; if ((c=='1' && strcmp(fio,z.fio) == 0) || (c=='2' && strcmp(nazv,z.name) == 0) || (c=='3' && strcmp(izd,z.izd) == 0) || (c=='4' && atoi(z.god)==god)) cout << z; // } 29 endtabl(); cout << " - \n"; cin.seekg(0,ios::end); cin.get(); }} // void del(){ ofstream fo; // ifstream fi; char fam[FIO]; int flag = 0; BOOK z; long dl; clrscr(); cout << " \n"; fprot << "\n\n \n"; cout << "\n "; cin.seekg(0,ios::end); cin.get(fam,FIO,'\n'); fprot << "\n " << fam << endl; fi.open("book.txt", ios::binary); if (!fi) { cerr << " "; exit(1); } fi.seekg(0,ios::beg); fo.open("tmp.txt", ios::binary); if (!fo) { cerr << " "; exit(1); } 30 fo.seekp(0,ios::beg); while (!fi.eof()) { fi >> z; if (strlen(z.fio) == 0) break; if (strcmp(z.fio, fam) == 0) { flag = 1; cout << "\n \n"; fprot << " \n"; cout << " - Enter\n"; cin.seekg(0,ios::end); cin.get(); } else fo << z; // // } fi.close(); fo.close(); if (flag == 0) { cout << " " << endl; fprot << " " << endl; } else { remove("book.txt"); rename("tmp.txt","book.txt"); } return;} // void begtabl(){ // cout << "╔═════════╤═════════════════╤═══╤" << "═════════╤═════╗\n"; cout << "║ ... │ │ │" << " │ ║\n"; cout << "╟──────────┼────────────────┼───┼ " << "──────────┼─────╢\n"; // fprot << "╔═════════╤═════════════════╤═══╤" << "═════════╤═════╗\n"; fprot << "║ ... │ │ │" << " │ ║\n"; fprot << "╟──────────┼────────────────┼───┼ " << "──────────┼─────╢\n"; return;} // void endtabl(){cout << "╚═════════╧═════════════════╧═══╧" << "═════════╧═════╣\n";fprot << "╚═════════╧═════════════════╧═══╧" << "═════════╧═════╣\n"; return;} 32 3 ... . ++ 1998 BHV 5000 .. 1990 1000 .. ++ 1999 3000 .. 1988 1000 . 2000 2000 . 1998 3000 1998 BHV 4000 ... . ++ 1998 BHV 5000 .. 1990 1000 .. ++ 1999 3000 .. 1988 1000 . 2000 2000 . 1998 3000 1998 BHV 4000 . 1996 BHV 5000 :... .. 1990 1000 1998 BHV 4000 . 1996 BHV 5000 33 .. : : .... . ++ 1998 BHV 5000 . 1996 BHV 5000 .. : :... ... . ++ 1998 BHV 5000 .. 1990 1000 .. ++ 1999 3000 .. 1988 1000 . 2000 2000 . 1998 3000 1998 BHV 4000 . 1996 BHV 5000 . 34 ... . ++ 1998 BHV 5000 .. 1990 1000 .. ++ 1999 3000 .. 1988 1000 . 1998 3000 1998 BHV 4000 . 1996 BHV 5000 35 充.. 31. . 32. .. 33. 腅. 4 3.1. ⅅ.. 4 3.2. . 5 3.2.1. 充 5 3.2.2. .. 5 3.2.3. .. 6 3.2.4. 酅. 6 3.2.5. 酅. 6 3.2.6. 酅... 7 3.2.7. , 7 3.2.8. .. 8 3.2.9. 84. 녅.. 8 4.1. , .. 8 4.2. ⅅ.. 9 4.2.1. / 9 4.2.2. .. 12 4.2.3. 13 4.2.4. . 14 4.2.4.1. 14 4.2.4.2. 腅 15 4.2.5. / 15 4.3. / , 셅 16. 18 1. .. 19 2. 酅 20 3. . 32 36 2002 . N 020308 14.02.97 ____________________________________________________________ 6084 1/16 ..-. .. .. ____________________________________________________________ - , - - 191186, -, . , 5