Вы находитесь на странице: 1из 20

6. .

.
- ()
, -: fread() fwrite().
: ,
ij.
int i,j;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *f=fopen("my.dat", "wb");
fwrite(&i, sizeof(int), 1, f);
fwrite(&j, sizeof(int), 1, f);
fclose(f);
}
//-------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender)
{
FILE *g=fopen("my.dat", "rb");
fread(&i, sizeof(int), 1, g);
fread(&j, sizeof(int), 1, g);
fclose(g);
}

r rb


( ).
w wb
.
(
a ab
)
r+ rb+ r+b
w+ wb+ w+b .
.
a+ ab+ a+b (
)

..

7. . .
.
, .
:
* ID_;
: int *a; double *f; char *w;
,
,
(, , ,
).
ID ,
, ,
.
:
int *a, *b;
.
,
.
- & *.
& . *
, ( ).
,
C :
();
( ):
ID_ = &ID_; - ;
*ID_
- .

:
->

();

, ->,
, ->.

-- ,
(**). *
.
. :
int y=5;
int *p1=&y;
int **pp1=&p1;
int ***ppp1=&pp1;

8. . .
.
C++ Builder:
,
, , , .
, .
, ,
. :
int A[5]; // 5 int
int A[5][5]; // 55
int
int A[]={1,2,3}; //

int A[5]={1,2}; //

static int A[5]; // .
0

int **mas;
m- n-
mas=new int *[m];
m-.
.
for(int a =0;a < m;a++)
mas[ a ]=new int[n];
, n -.

for(int i =0; i < m; i ++)
{ for(int j=0;j < n;j++)
mas[ i ][ j ]=5; }
.
,
for(int a =0;a < m;a++)
delete [] mas[a]; //
delete [] mas; //
.
.
: ( )
, .

9. () . .
: , . ,
: , , .
, ,
, .
,
. ,
.
main,
.
, , - ,
.
.
, .. ,
.
,
, ,
.
.
.
.
,
double :
double *p;
p = new double;
,
, 1.5:
double *p = new double(1.5);
:
double *p = new double;
*p = 1.5;
new
. ,
. ,

m*n:
double *a;
int m = 100, n = 101;
a = new double[m * n];
----------------------------------------------- delete , new,
,
double *p = new double(1.5); //
...
delete p; //

new,
delete,
delete :
double *a = new double[100]; //
...
delete[] a; //
------------------------------------------------
,
, ,
.
,
,
( , , ),
.
, , .
:
1) ;
2) .
10. - . , ,
.
,
,
, .
, ,
, .. "
".
,
.
:
1) ;
2) ;
3) , ;
4) ;
5) .
,
.
. 100 -.
const int MAXSIZE = 100;
struct Stack {
char data[MAXSIZE];
int size; };
,
(Push), (Pop)
(Peek).

16-20. ,
, , .. , FIFO
(First In, First Out) , .
(
begin) ( end).
, ()
.

,
( ), :
struct Spis1 {
int info;
Spis1 *next;
} *begin, *end;
//
:
;
(, , );
.
:
.
Spis1
(b
, e ):
void Create(Spis1 **b, Spis1 **e, int in) { // in
Spis1 *t = new Spis1;
t -> info = in;//
t -> next = NULL;
//
if(*b == NULL)
//
*b = *e = t;
else {
//
(*e) -> next = t;
*e = t;
}
}
Create(&begin, &end, in);
, :
...
struct Stack {
//
int info;
Stack * next;
} *begin, *t;
//------------ ---------Stack* InStack(Stack*, int);
void View(Stack*);
void Del_All(Stack **);
//------------- - -----------int i, in, n = StrToInt(Edit1->Text);
if(begin != NULL){
ShowMessage(" !");

return;
}
for(i = 1; i <= n; i++){
in = random(20);
begin = InStack(begin, in);
}
Memo1->Lines->Add(" " + IntToStr(n) + " -.");
//-------------- - -----------int i, in, n = StrToInt(Edit1->Text);
for(i = 1; i <= n; i++){
in = random(20);
begin = InStack(begin, in);
}
Memo1->Lines->Add(" " + IntToStr(n) + " -.");
//--------------- - ----if(!begin){
ShowMessage(" !");
return;
}
Memo1->Lines->Add("--- ---");
View(begin);
//--------------- - --------if (begin != NULL) Del_All(&begin);
ShowMessage(" !");
//--------------- - EXIT ----------------if(begin != NULL) Del_All(&begin);
Close();
//--------------- -----------------------Stack* InStack(Stack *p, int in) {
Stack *t = new Stack;
t -> info = in;
t -> next = p;
return t;
}
//--------------- ---------------------------------void View(Stack *p) {
Stack *t = p;
while( t != NULL) {
Form1->Memo1->Lines->Add(" " + IntToStr( t->info));
// , , cout << " " << t->info << endl;
t = t -> next;
}
}
//---------------- ----------------------void Del_All(Stack **p) {
while(*p != NULL) {
t = *p;
*p = (*p) -> next;
delete t;
} }

1.

, . ,
, .

,
:
struct ID { };
:
struct Stud_type {
char Number[10];
char Fio[40];
double S_b;
};

.

1. , :
struct Stud_type student;
- ;
Stud_type Stud[100];
- ;
int Fun(Stud_type W);
- ,
2:
struct Stud_type {
char Number[10], Fio[40];
double S_b;
} student, Stud[100];

, :
struct Stud_type
{
char Number[10], Fio[40];
double S_b;
} student = {123456, .., 6.53 };
:
Stud_Type stud1 = {123456, .. };

- ,
,
.
:
union ID_ { };
:
union word {
int nom;
char str[20];
};
:
union word p_w, mas_w[100];
,

.

2.
,
(.) :
ID_ . ID_
:
Stud_Type s1, s2;
:
s1.Number,
s1. Fio,
s1. S_b;
s2. Number,
s2. Fio,
s2. S_b;
:
s1.S_b=7;
s2.S_b=6.5;
int k=strcmp(s1. Number, s2. Number);

:
1)
: s1=s2;
2)
: s1==s2 s1!=s2
3)
:
int Fun(Stud_type W);
...
int d=Fun(S1);

:
struct Point{
int x,y;
} r, *p, *q;
p=&r;
, ,
:
p=new Point;
q=new Point[10];
:
p->x=3;
p->y=4;
Edit1->Text Edit1 ( TEdit * )
( TEdit) Text .
Edit1->Text="A";
Edit2->Text="B";
Edit1=Edit2;
// Edit1 2- Edit !
Edit1->Text="C";

3.
,
. ,
.
,
stdio.h io.h.
,
. ( )
( ) -
, /.
/
, / /
.
.
.
.
( ,
- . ) ,
.
.
. Windows c ""
, ,
.
( * ) :
(*.txt)
( ) (*.cpp)
:
Word (*.doc) - , .., ,
(, )
( )
(*.exe)
(*.bmp .)
, " ", ,
" " - ,
.
; , ,
. ,
, ,
, (,
3-4 ).

4.
, ,
, .
( ) - , .. ,
.
:
FILE * ;
FILE - , stdio.h
( ):
type struct {
short level;
- ;
- 512 ; level=0,
;
unsigned flags;
- - , , ;
char fd;
- , .. ,
;
unsigned char hold;
- , .. ungetc-;
short bsize;
- ;
unsigned char buffer;
- , ..
,

;
unsigned char *curp;
-
, ..
;
unsigned istemp;
- ;
short token;
- ;
} FILE;

FILE* fopen(char * _, char *);
- (,
) .
, - , ,
d:\\work\\Sved.txt .
- , :
w - ; ,
; ,
;
r - ; , ;
a - ;
, ;
r+, w+, a+ - r, w, a ,
( , ).
, r+ , -
, . , r,
.
, , t
b:
t - ;
b - .

.
fopen() (
- ). NULL,
. ,
- ,
, (,
d:\work\). ,
, - ,
r+ , .
:
FILE *f;
- f;
f = fopen ("d:\\work\\Dat_sp.cpp", "w");
-
f, Dat_sp.cpp, d,
work.
:
FILE *f = fopen ("d:\\work\\Dat_sp.cpp", "w");
, fopen() ,
. "" ;
, ,
, .
fopen() NULL.

fclose( ). ,
: fclose (f);
:
void fcloseall(void);
,
, , .
:
FILE* freopen (char *_, char *, FILE *_);
, _ (
fopen), _ ,
.
,

. fopen :
FILE* tmpfile (void);
w+b,

.

5.
fprintf(),
fscanf(), fgets() fputs().

.
:
#include<stdio.h>
void main(void) {
FILE *f1;
int a=2, b=3;
If(!(f1=fopen(d:\\work\\f_rez.txt,w+t)))
{
// (!f1) (f1==NULL)
puts( !);
return;
}
fprintf(f1, \n);
fprintf(f1, %d %d = %d\n,a,b,a+b);
fclose(f1);
}
, ,
, printf().
-
: stdin
- ( - ), stdout - ( - ), stderr
- ( - ).
FILE * , ,
freopen() , :
freopen("results.txt", "w", stdout);
results.txt ,
.
. stdin
fflush(), .. , , -
(. -).
. , scanf() gets()
, gets().

12.
(push),
,
return:
Stack* InStack(Stack *p, int in) {
Stack *t = new Stack;
//
t -> info = in;
//
t -> next = p;
//
return t; }
,
begin: begin = InStack(begin, a);
13. , :
void Del_All(Stack **p) {
Stack *t;
while( *p != NULL) {
t = *p;
*p = (*p) -> Next;
delete t;
} }
: Del_All(&begin);
begin NULL.
14.
void del(Stack **p){
if (*p != NULL){
t = *p;
*p = (*p) -> next;
delete t; } }
15.
void View(Stack *p) {
Stack *t = p;
while( t != NULL) {
// , , cout << t -> info << endl;
t = t -> Next; } }
: View(begin);
- View . 3.1.

. 3.1

21. .

begin .
1. : t = begin;
2. (t ->info),
, .
3. ,
:
t = t->Next;

, , .. begin NULL
( NULL

).
1. : t = begin;
2.
, , .
3. (2-)

begin = begin->Next;
4. , 1- : free(t);
5. , , !.
22.. . .

( ) ,
.

, .

. ,
,
.
() .
, , .. .
,
.
, .
,
.
.
1. .
2. - ,
, ,
,
.
3.

.

23. . .

,
,
. (
, ). ,
(, , .).
, .

. 50- . XX
. ,
,
( ) .
.


.
.

.
, ,
.
.
,
.
, .
, ,
.
.
:


, .


.

- .
,
. .

P{Q}R
P , Q,
R , .
,
:

,
, ,
.

24. . . .


.
, .
,
. ,
.

qsort(), .
. ,
, ,
.

.

(. stability)


.
,
.

. ,
, .
(
), . (
) .
.
:

,
.
.


.
.


, , ( ),
,
.

, .
, ,
.

:
, .

.
:

,
,

25. QuickSort.

(. quicksort),

qsort (

) ,

1960 .
:

; -
.
QuickSort
(
), , , .
,

. :

.
:

, .
, ;
(.).

,
, :
,


, .
, : ,
; ,
(. ).
;
,
, .
,
-
.

33. (<<, >>)




. :

, ;

,
( ) ;
.
, C++,
, .
.
, (unsigned),
, ( ),
.
int a;
unsigned int b;
a = a << 2;
a >>= 2;
b >>= 3;

// , 4
// , 4
// , 8

32 . (~, &, ^, | )
(),
.
, char int. float, double, long
double, void .
. 2.6 ,
() .
2.6.

&

^

~
(, 1)
>>

<<

++ :
1. , : &
2. , : ^
3. , : |

.
: X, Y ; F

1
X

.
3
X

2
X