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

1

.
main(),
.
.
.
-
, "{" "}"
.
.
#
main()
{ main}
function1()
{ function1}
function2()
{ function2}
,
#.
,
.
.
#define #include.
#define
, .
: #define NAME .
NAME .
: #define COUNT 100 COUNT
100.
.
#include
.
,
.

INCLUDE, :
#include <stdio.h> .
: .h .hpp,
. ..

, , ..
.


32 .
.
.
.
: Doxod_2002, x1, Summ.
:

, :
< >< ()>;

.
:
float x; double y, z ; int i, j, k;


unsigned char 8 0 255
char
8 -128 127
short int
16 -32,768 32,767
unsigned int 32 0 4,294,967,295
int
32 -2,147,483,648 2,147,483,647
unsigned
long
32 0 4,294,967,295
long
32 -2,147,483,648 2,147,483,647
float
32 3.4 x 10-38 3.4 x 10+38
double
64 1.7 x 10-308 1.7 x 10+308
long double 80 3.4 x 10-4932 1.1 x 10+4932

.
, .
, unsigned char,
- unsigned long.
. float 7-8
, double 16-17.

,
,
:
V1 = V2=...Vn = E,

3
V1,V2,... - ; .
,
.
,
sum = + b; sum - b.
,
, . ,
s = s + i s += i , = /b
/=b.

,
,
,
.
,
56 64 32767. 0
, , 0
7. 011 077.
0x 0X
0 F. - 0x898 0xFFFE.
:
3.1416 - 0.3146E+1.

char ,
, .

( ).
char c; c=a; c=b;
, ,
:
\n - (
Enter); \t- ; \a- ; \b-
; \v- , \r- .

Escape-,

\, \
\\.


.


printf
scanf,
printf .
printf(< >[, < >]);

, ,
.
,
,
. %
.
-, ., l,
.
:
d- ;
u- ;
o- ;
x- ;
c- ;
s- ;
g- : f, e;
e- float double
;
f- float double
;
p- ;
,
.
:
\
\n
\t
\b
\r.
scanf , printf:
scanf (< >[, < >]);
scanf
( &).

.

#include <stdio.h> // -
void main() // main
{ float m,f,i,sum,sb; // main

5
printf(" . \n");
printf(" \n );
scanf(%f %f %f,&m,&f,&i);
printf( %f %f %f\n,m,f,i);
sum=m+f+i;
sb=sum/3;
printf( %f\n,sb);
}


<math.h> , .
sin(x), cos(x) , sqrt(x), tan(x),
asin(x) , acos(x), atan(x),
fabs(x) , exp(x), pow(x,y),
log(x) log10(x) double
double.
++

1
()
[]
-> ::
.
2
!
~
+ ++ -- & * sizeof new delete
3
*
/
%
4
+
5
<< >>
6
<
<= > >=
7
== !=
8
&
9
^
10
|
11
&&
12
||
13
?:
14
=
*= /= %= += -= &= ^= |= <<= >>=
15
,
1. [ ] - ; Student[i], X[3][2]
.
2. ( ) ; : sin().
3. . ; : Product.Cost.
4. -> - ;

6
5. :: - (

),

;
6. ++ -- - inc dec. ++C ++
;
: i++ i = i + 1.
7. () (int)x (
);
8. ~ - ;
9. ! ;
10. - - ( );
11. & - ( );
12. *p - ;
: int i=3, *Ptr;
Ptr = &i;
// Ptr i.
*Ptr , ,
i, Ptr.
13. * (), / (), % ( );
14. + (), - ();
15. >>, << - ;
: int i=3;
i=i<<2; //i=0112<<2=011002=1210
16. >=, <=, <> - ;
17. ==, != - ( );
18. & - ( );
19. ^ - XOR;
20. | - ;
: = 1011, b = 0110, a&b 0010,
a^b 1101, a|b 1111, a<<3 1000, ~a 0100, b>>1 0011.
21. && - ;
22. || - ;
23. ?: - ( - );
: min = a>b? b: a ( (a>b) ,
b, - a).
24. = - .
,

: +=, -=,
*=, /=, %=, <<=, >>=, &=, ^=, |=.
: x = y = z = 5; S += a;( S = S + a;).
25. , - .
.

7
: 1) for (x=p1, y=p2, i=2; i<n; z=x+y, x=y, y=z, i++)

if :
if (< >)
( 1);
[ else < 2>:]
1, 2.
, , y = |x|, :
if (x >= 0) y = x; else y = -x;
switch:
switch ( )
{case 1: 1; break;
case 2: 2; break;
[ default:<>;] }
switch case,
.
break ,
switch.

,
default ().
default .
switch ( Znak ){
case +: y = a + b; break;
case -: y = a - b; break;
case *: y = a * b; break;
case / : y = a / b; break;
default : printf( \ n); }

:
while ()
;
while .
while ; . ,
:
#inciude<conio.h>
#include<stdio.h>
main ()
{char ch; int len = 0;
puts(\n , <Enter>:\n);
while ((ch = getch())!=\r) { putch(ch); len++;}
printf(\n %d . \n,len); }

8

do
<> ;
while(<>);
while .
a b:
#include <stdio.h>
void main()
{ float a,b,FDiv;
char Symbol;
Symbol='y';
do
{printf(" : ");
scanf(%f %f,&a,&b);
if (b<=0.1E-6) printf("! !\n);
else {FDiv=a/b;
printf( : %f",FDiv);}
printf(" a,b?\n");
printf("('y' - ; - )\n");
scanf(%c,Symbol);}
while (Symbol=='y');}

for ( 1; 2; 3)
;
1 (
)
2 (
)
3 (
,
)
n :
#include <stdio.h>
void main()
{ int i, Summa ,n;
printf(" ?\n");
scanf(%d,&n);
Summa=0;
for (i=1;i<=n;i++)
Summa+=i;
printf(": ",Summa);}

- .
<> [ ]; - .
<>[ ][ ]; .
.
,
.
.
0.
.

.
:
int A[3] ; // 3
3*sizeof(int)
A[0]=11; // 11
printf(%d \n,A[0]); // 11
++A[0];// 1
printf(%d \n,A[0]); // 12
.
int A[3] = {6,7,8};
4* , 4*3=12
.
int A1[2] [2] = {{3,4},{5,6}};
char: char A[10];
char
char s1[6] = {, ,,,A, \0};
har \ 0
, :
char s1[ ] = {,,,,,\0}.
:
char s2[ ] = ,
.
:
int i; float s=0;
for(i=0;i<kol_day; i++ )
s+= mastemp[i];
s=s/ kol_day;

10

, .

* _;
4 .
: char * s3; //s3 Char
char * s3 = ; // s3 ,
7
s3.

() .
, int* px=NULL; // NULL
int x,*px1;
px1=&x; // px
x.
< > - ,
++ --.
.

. .
, int A[3]; ==&A[0].
:
* == [0].
i - *(+I) == A[I].
.
int B[10][10];
B [0] .
( B[0] + j)- j .
*(B[0] + j) j .
*(*(B+i)+j ) j i .
:
.
#include <iostream.h>
#define NSt 3
#define NBal 5
void main()
{ int i,j,SumBal;
int Exams[NSt][NBal];

11
float SrBal;
//
for (i=0;i<NSt;i++)
{ printf( N%d",i);
for (j=0;j<NBal;j++)
scanf(%d,(*(Exams+i)+j));}
//
for (i=0;i<NSt;i++)
{ SumBal=0;
for (j=0;j<NBal;j++)
SumBal+=*(*(Exams+i)+j);
SrBal=SumBal/NBal;
printf( N%d: %f,i,SrBal);
}}
.
,
,
#include <alloc.h>.

malloc(< >),
. ,
NULL.
void, ,
(*).
, 20
:
int *p;
// int.
p = (int*) malloc(40);
//
// ,
//40 .
40 ,
,
int, .
:
malloc(20*sizeof(int));
, malloc 0.
free(p) , .. .

new. a = new int[size];
size .
.
. new
, .

12

delete[] a.

[i]
*(a+i) .

, ..
.
.
<string.h>.

char *strcat(char *s1,
const char *s2)
char *strcpy(char *s1,
const char *s2)
char *strstr(const char
*s1, const char *s2)
int *strcmp(const char
*s1, const char *s2)
unsigned strlen(char *s)


,
s1
s2 s1
s1
s2. s1,
s2
. 0,
; >0, s1>s2; <0, s1<s2
-

:
.
#include<iostream.h>
#include<string.h>
#define NStr 100
#define NWord 10
main(){
char String[NStr];
char Word[NWord];
char WordCh[NWord];
printf(" :"); gets(String);
printf(" :"); gets(Word);
printf(" :"); gets(WordCh);
char *PosStr=NULL;//
char ResStr [120];//
int i=0;
while(strstr(String,Word)!=0)//
{PosStr=strstr(String,Word);//
strcpy(ResStr,String);//

13
ResStr[strlen(ResStr)-

strlen(PosStr)]='\0';//

*PosStr=S[strlen(ResStr)+strlen(Word)];//

strcat(ResStr,WordCh);//
strcat(ResStr,PosStr);//
strcpy(String,ResStr);
i=i+1;}
if (i==0) printf(" !");
else{
printf(" :");
puts(String);}}

.

. 4.
auto, register, extern, static.
: static int x;

. , , auto extern.

(extern).
(auto).
.
auto .
:
, , ..
. .
, .
.
.
: register int z;
.
, .
. .
short int
( ).
.
,
. .
, ,
. .
.

14

.
.
: extern int z = 5;
.

.
static.
, .
,
, ,

.

, .
,
extern,
staric.
.
,
,
.

.
:
- (). ,
.
. :
1. ;
2. ();
3. ;
4. ;.
- . (;
).
(.. ).
.
,
, (
).

15
++ ,
, .
main (..
).

return ,
.
.
1) :
, , , ..
.
: .
int Sum(int x, int y)
{int S;
S= x + y;
return(S); }
: Sum(x1, y1)
2) :
, 2 .
, . :
.
void Obmen (int*x, int *y)
{int temp;
temp = *x;
*x = *y;
*y = temp;}
: Obmen (&x1,&y1)
3) :
void Swap(int &p1, int &p2)
{int temp;
temp = p1;
p1=p2;
p2 = temp;}
: Swap(x1,y1)

float f(float a; float b=5; float c=0){}
:
f(4,6,8);
f(4,6); f(4);
f(4,,3),
, ,
.

16
.
inline
.
inline float Sum(float a1,float a2)
{return a1+a2}
,
.
:
- 5 ;
- ; goto.

++ ,
.
, .
:
,
.
float S(float r) //
{puts(s );
return 3.14*r*r}
float S(float x, float H)
{puts (s );
return 1\2 * x*H}
main.
int main (int argc, char * argv[ ] ) argv[ ] -
DOS
argv[0] ,
. argv[1]
, argv[2] - ..
argc ,
1

17
.
.
.
,
:
typedef float ft (float) ; ft *ftp;
:
typedef float(*ftp)(float)
float 1 float.
: , cos(x)
sin(x)
#include "math.h"
#include "stdio.h"
typedef float (*func) (float);
float Func1(float x)
{
return sin(x);
}
float Func2(float x)
{
return cos(x);
}
float Calc(func f, float x)
{
return f(x);
}
void main (void)
{
int i;
float x;
func funcMas[2];
funcMas[0] = Func1;
funcMas[1] = Func2;
printf("Input x\n");
scanf("%f",&x);
printf("1 - Calculate sin(x)\n");
printf("2 - Calculate cos(x)\n");
scanf("%d",&i);
if ((i==1)||(i==2))
printf("F=%f\n",Calc(funcMas[i-1],x));
}

,

.
.
.
template < >

18

)
{ }

.
template <class T >
void Exchange (T&x , T &y)
{T temp;
temp=x; x =y; y = temp; }
, .
:
float x=3.6, y=7.5;
char c=3, b=5;
Exchange(x,y);
Exchange(c,b);
, . . ,

.
:
:
1) ,
.
2) ,
.
3) .

.

. - .
struct < ()>
{ 1;
n;} < ->;
//
, .
:
struct Man
{ char name[20];
char age;
int hight; };
Man D = { ..,18,170};
Man *PD;
PD = &D;

19
: D.name.
, , ,
& .
(*PD).name PD->name.
:
Man masMan[20];
(masMan + i)->name ( i- ).
Man *p = new Man; (
Man).
Man *p = new Man( ..,50,190);
Delete p;

, , . .

( ).
,
.
:
#include<stdio.h>
union Ud
{ char a[10]; // , ,
float b; //
double z; };
REGS ,
.
REGS a;
a.x ;
a.h ;
a.l .
-
.

, .
.
:
struct B
{ int a:3;
// 3
int b:3;
//
int d:2; };

20
:
#include <stdio.h>
#include <string.h>
//
struct man { char fam[15];//
char name[10];//
int vozr;//
int wes;//
int rost;//
};
//
// x- , n- -
void vvod(man* x, int n)
{int i;
for(i=0;i<=n-1;i++)
{printf(" %d",i+1);
printf(" "); gets((x+i)->fam);
printf(" ");
gets((x+i)->name);
printf(" "); scanf(%d,&(x+i)->vozr);
printf(" ");
scanf(%d, &(x+i)->rost);
printf(" ");
scanf(%d, &(x+i)->wes);
}
return ;}
//
// x- , n- -
void vivod(man* x, int n)
{int i;
for(i=0;i<=n-1;i++)
{ printf(" %d"<<i+1;
printf(" "); puts((x+i)->fam);
printf(" "); puts((x+i)->name);
printf(" %d ",(x+i)->vozr);
printf(" %d ",(x+i)->rost);
printf(" %d \n ",(x+i)->wes);
}
return ; }
//
// x- , i-
void vivod1(man* x, int i)
{ printf(" %d"<<i+1;
printf(" "); puts((x+i)->fam);
printf(" "); puts((x+i)->name);

21
printf(" %d ",(x+i)->vozr);
printf(" %d ",(x+i)->rost);
printf(" %d \n ",(x+i)->wes);
return ;}
//
// x- , n- -
// st-
int Findfam(man* x, int n, char* st)
{int i, flag=0;
for(i=0;i<=n-1;i++)
if(strcmp((x+i)->fam,st)==0)//
{ flag=1; return i;}
//
if (flag==0)
{printf(" !\n); return -1;}
}
//
// x- , n- -
// trost-
void Findlong(man* x, int n, int trost)
{int i, flag=0;
for(i=0;i<=n-1;i++)
if((x+i)->rost>trost)
//
{flag=1; vivod1(x,i); }//
if (flag==0)
printf(" , %d \n",trost);
return ;}
void main()
{int i, k, tr;
char tempfam[20];//
man* a;//
printf("- : \n);
scanf(%d,&i);
a=new man[i]; // i
if (a!=0)//
{vvod(a,i); vivod(a,i);//
printf(" , : \n);
gets(tempfam);
k=Findfam(a,i,tempfam);//
if(k!=-1) vivod1(a,k);
printf(" , c :\n);
scanf(%d,&tr);
Findlong(a,i,tr);//

22
delete a;//
} else printf( !);}

.
.
,
, .. ,

.
, .. ,
.

<stdio.h>
FILE* name;

name =fopen( _, );
0 ,
.
, ,
(, ). : "r"
, "w" ,
, "a"
, ,
, "r+" ,
,"w+" ,
, "a+"
.

fprintf(name,< >[, < >])
fscanf (name,< >[, < >])


fseek(name, long offset, int point),
offset
, point ( 0-

23
, 1- , 2- ).
0, , 1, .
fclose(name).
:
( man vvod,vivod
)
int writefile(man* x,int n)
{ FILE* fp; int i; char namefile[11];
printf(" \n);
gets(namefile);
fp=fopen(namefile,"w");
if (fp==0) {printf(" !\n); getch(); return -1;}
else
for(i=0;i<=n-1;i++)
{fprintf(fp,"%s\n", (x+i)->fam);
fprintf(fp,"%s\n",(x+i)->name);
fprintf(fp,"%d\n",(x+i)->vozr);
fprintf(fp,"%d\n",(x+i)->rost);
fprintf(fp,"%d\n",(x+i)->wes);}
fclose(fp);return 1;}
:
writefile(a,i), a=new man[i].
:
( man vvod,vivod
)
int readfile(man* x,int n)
{ FILE* fp; int i; char namefile[11];
printf("- : %d \n",n);
printf(" \n);
gets(namefile);
fp=fopen(namefile,"r");
if (fp==0) {printf(" !\n); getch(); return -1; }
else
for(i=0;i<n;i++)
{fscanf(fp,"%s",(x+i)->fam);
fscanf(fp,"%s",(x+i)->name);
fscanf(fp,"%d",&(x+i)->vozr);
fscanf(fp,"%d",&(x+i)->rost);
fscanf(fp,"%d",&(x+i)->wes);}

24
fclose(fp); return 1;}
:
readfile(a,i), a=new man[i].

:
int writef(man* x,int n)
{FILE* fp;int i;
char namefile[11];
printf(" \n);
gets(namefile);
fp=fopen(namefile,"wb");
if (fp==0) {printf(" !\n); getch(); return -1;}
else
for(i=0;i<=n-1;i++)
fwrite((x+i),sizeof(man),1,fp);
fclose(fp) ;return 1;}
:
writef(a,i), a=new man[i].
:
int readfilebin(man* x,int n,char* namefile)
{FILE* fp;int i;
fp=fopen(namefile,"rb");
if (fp==0){printf(" !\n); getch(); return -1; }
else
for(i=0;i<n;i++)
{fread((x+i),sizeof(man),1,fp);}
fclose(fp); return 1; }
:
readfilebin(a,i,namefile), a=new man[i].
:
unsigned long filelen(char* namefile)
{FILE* fp;unsigned long DL;
fp=fopen(namefile,"rb");
if (fp==0) { printf(" !\n); getch(); return -1;}
else {fseek(fp,0,SEEK_END); DL=ftell(fp); }
fclose(fp) ; return DL; }
:
dlfile= filelen(namefile);

Оценить