{if (x>y)return 1; else return 2;}
. , :
.
#include <stdio.h>
int sum(int x) {return 2*x;}
int sum(int x, int y=5) {return x+y;}
Void main()
{int S1=sum(10); // 20 15
}
1.11
( .. ) ++ .
template <class type1,class type2, class type3 >
type1, type2, type3. , , .
.
template <class Tip>
Tip max(Tip x, Tip y)
{if(x>y)return x;
return y;}
Int main()
{int a1=-7,a2=40,res1; float f1=-3.6,f2=-10.5,res2;
res1=max(a1,a2);
res2=max(f1,f2);
printf("res1=%d res2=%.1f\n",res1,res2);
//res1=40 res2=-3.6
return 0;}
, . res1=max(a1,a2); int max(int x,int y) . res2=max(f1,f2); float max(float x,float y) .
:
1. ;
2. ;
3. class;
4. , .. , , . ;
5. , , . .. , ,
template <class A,class B>
B func(A x){} // ,
//
.
template <class A>
Void func(A);
.
#include <stdio.h>
template <class A>
void swp(A *x,A *y) //
{A z=*x; //
*x=*y; *y=z;}
Int main()
{double d1=3.3, d2=2.2; swp(&d1,&d2);
printf("d1_n=%. 2lf d2_n=%. 2lf\n",d1,d2);
//d1_n=2.20 d1_n=3.30
char c1='M', c2='Z'; swp(&c1,&c2);
printf("c1_n=%c c2_n=%c\n",c1,c2);//c1_n=Z c2_n=M
char *st1="", *st2=""; swp(&st1,&st2);
printf("st1_n=%s st2_n=%s\n",st1,st2);
//st1_n= st1_n=
Swp(st1,st2);
|
|
printf("st1_nn=%s st2_nn=%s\n",st1,st2);
// st1_nn= st1_nn=
return 0;}
:
void swp(double *x,double*y), void swp(char*x,char*y),
void swp(char**x,char**y), void swp(char*x,char*y).
, , , .
.
template <class T>
int func_max(T x[], int n)
{T m=x[0]; int k=0;
for(int i=0; i<n; i++)
if(x[i]>max){max=x[i]; k=i;}
return k;}
Void main()
{int arr[5]={5,12,40,23,4}, in1, in2;
in1=func_max(arr,5); //in1=2
printf("max1=arr[%d]=%d\n",in1,arr[in1]);
float mas[4]={5.3,1.2,4.0,20.3};
in2=func_max(mas,4); //in2=3
printf("max2=mas[%d]=%.2f\n",in2,arr[in2]);}
:
max1=arr[2]=40 max2=mas[3]=20.30
1.12
, , , . , .
:
_ (*_)(_);
_ ;
_ ;
*_ .
:
int (*ptrfunc)(int); //
char (*pf1)(char*);
char* (*pf3)(char*);
, , .
int *ptrfunc(int); //
, .
.
float mul(float x,float y) {return x*y;}
float sum(float x,float y) {return x+y;}
Void main()
{float (*ptrmul)(float,float); //
float (*ptrsum}(float,float); //
ptrmul=mul; // mul
ptrsum=sum; // sum
float f1=3.0,f2=9.0,f3;
f3=(*ptrmul)(f1,f2)+(*ptrsum)(f1,f2);
printf("f3=%.1f", f3); // f3=39.0
float(*ptr)(float,float); //
ptr=mul; // mul
printf("mul=%.1f", (*ptr)(f1,f2)); // mul=27.0
ptr=sum; // sum
printf("sum=%.1f",(*ptr)(f1,f2)); }// sum=12.0
, . : _(*_[n])(_);
n ( ).
. , int int
#include <stdio.h>
int f1(int x){return x+x;};
int f2(int x){return x*x;};
int f3(int x){return ~x;}; //
Void main()
{int(*p[3])(int);
p[0]=f1; p[1]=f2; p[2]=f3;
|
|
int a=(*p[0])(5);
int b=(*p[1])(a);
int c=(*p[2])(a);
printf("a=%d b=%d c=%u\n",a,b,c);
}
:
a=10 b=100 c=65525
typedef. :
//
typedef _ (* _ _)(_ _ );
// ,
__ __-; __ __-[n];
:
typedef int (*ABC)(int); // ABC
1; // 1 ABC
2[4];// 4 2 ABC
.
#include <stdio.h>
void typ0(){printf(",");}
void typ1(){printf(" ");}
void typ2(){printf(" !");}
typedef void (*TYPE)();
TYPE mpf[]={typ0,typ1,typ2};
//void (*mpf[])()={typ0,typ1,typ2};
Void main()
{for(int i=0;i<3;i++) mpf[i]();}
. .
. , ‑ .
#include <stdio.h>
float sqr(float e){return e*e;} //
float cube(float y){return y*y*y;} //
typedef float(*PF)(float);
float summa(PF f,float a[],int N)//
{float sum=0.0;
for(int i=0;i<N;i++) sum+=(*f)(a[i]);
return sum;}
Void main()
{float res1, res2, A[3]={1.5,2.0,3.0};
res1=summa(sqr,A,3); //2.25+4+9=15.25
res2=summa(cube,A,3); //3.375+8+27=15.25
printf("res1=%.3f res2=%.3f\n",res1,res2);}
: res1=15.250 res2=38.375
2
: . , .
. , , (, , ).
- : 1) ; 2) ; 3) , , ; 4) - ( ) ..
2.1 -
: ( ), ( ), ( ). <stdio.h> stdin, stdout, stderr, ( ).
. , ( ) ( ). , FILE, . :
Struct FILE
{int level; //
unsigned flags; //
char fd; // ,
unsigned char hold; //
int bsize; //
unsigned char _FAR *buffer;//
unsigned char _FAR *curp; //
unsigned istemp; //
short token; //
};
flags.
, :
#define EOF (-1) //
: . . '\n' ( ) '\n' '\0', '\n' '\0'. Ctrl-Z ( ) '\0', .
|
|
.
<fcntl.h> <stdlib.h> _fmode, O_BINARY ( ) O_TEXT ( ). :
_fmode=O_BINARY;
2.2
.
:
FILE* fopen(const char* filename, const char* mode);
, . FILE (FILE*), . , NULL- . FILE*, .
char* filename , , .. . , .
char* mode ‑ , :
r , r+ ;
w , w+ . , , , ;
a , a+ . , ;
t , r, w, a;
b , r, w, a.
. , fp myfile.txt fin abc.txt D USER:
FILE *fp, *fin;
fp=fopen("D:\\User\\myfile.txt","w");
char *fname=("D:\\User\\abc.txt");
fin=fopen(fname,"r");
, NULL- . :
if(fp==NULL){puts( ); return -1;}
if(fin==NULL){puts(" ");return -1;}
.
:
int fclose(FILE *);
. FILE ( ), 0, EOF(1). FILE, , , , ( , ). : fclose(fp);
. . : int fcloseall(void);
, EOF, .
|
|
int n=fcloseall(void);
printf("n=%d",n); //n=2, )
// n=-1, -
:
FILE *freopen(const char *filename,const char*mode,FILE *fp);
fp , filename , mode. , fp, NULL. , -.
fp=freopen("D:\\User\\myfile.txt","a",fp);
FILE *fp1=freopen("D:\\User\\myfile1.txt","ab",fp);
freopen("D:\\User\\abc.txt","r",fin);
. , .
#include<stdio.h>
Int main()
{FILE *fp;
char buff[100], *fname ="myfile.txt";
fp=fopen(fname,"r"); //
if(fp!=NULL)fgets(buff,50,fp);//
else {puts("ERROR_1"); return -1;}
puts(buff); //
fp=freopen(fname,"a",fp); //
if(fp!=NULL) fputs("abcdef",fp);//
else {puts("ERROR_2"); return -1;}
fclose(fp); //
return 0;}
. c :
void perror(const char *str);
( ) str . <stdlib.h>.
FILE *ptr=fopen("abc.doc","r"); //
if(fp==NULL){perror(" : "); return -1;}
char c=fgetc(ptr); //
fclose(ptr); //
, :
: No such file or directory
-.
2.3 -
() :
int fputc(int ch, FILE *fp);
int ch char fp, . , EOF.
. fp
FILE *fp=fopen("abc.txt","w"); //
if(fp==NULL){puts(" "); return -1;}
char s1='Z', str1[]="",s2;
fputc(s1,fp); // fp
for(int i=0; i<9; i++)
fputc(str1[i],fp); // 9
fputc('\n',fp); // \n
printf(" Ctrl-Z");
while((s2=getchar())!=EOF)
fputc(s2,fp); //
fclose(fp); //
() :
int fgetc(FILE *fp);
fp, , EOF (1) .
. fp
FILE *fp=fopen("abc.txt","r"); //
if(fp==NULL){puts(" "); return -1;}
char z1, str2[9],z2;
z1=fgetc(fp); // fp
printf("%c\n",z1);//
for(int j=0;j<9;j++)
{str2[j]=fgetc(fp); // 9 -
printf("%c",str2[j]);} // c
while((z2=fgetc(fp))!=EOF) //
putchar(z2);//
fclose(fp); //
2.4 -
() :
int fputs(char*string, FILE *fp);
string fp. '\0', . , EOF. , '\0', .
|
|
. fp
FILE *fp=fopen("abc.txt","w"); //
if(fp==NULL){puts(" "); return -1;}
char *s1=,*ms1[3]={"","",""};
fputs(s1,fp); // fp
fputs("\n",fp); // \n fp
for (int i=0;i<3;i++)
{fputs(ms1[i],fp); // 3 - fp
fputs("\n",fp);}
fclose(fp);//
() :
char*fgets(char *string,int N,FILE *fp);
, string. N '\n', string. '\0'. string, NULL . .
. fp
FILE *fp=fopen("abc.txt","r"); //
if(fp==NULL){puts(" "); return -1;}
char s2[20], ms2[3][20]; //
fgets(s2,20,fp);// , < 20
puts(st2); //
for(int j=0;j<3;j++)
{fgets(ms2[j],20,fp); // 3 - , <20 -
puts(ms2[j]);} //
fclose(fp); //
fgets(), () ' \n ', ' \0 '. fputs(), ' \0 ' .
, ' \n ' fputs(), . fgets() puts() . '\n' , fputs(), ' \n '. fgets() ' \n ' ' \n ', ' \0 ', puts() ' \n ' , .. .
. ' \n ' ' \n '.
FILE* pf=fopen("b.txt","w"); //
if(pf==NULL){puts(" "); return -1;}
char st1[]="Anna",st2[]="ria",st3[20]; int n=0;
fputs(st1,pf); // 4
// fputs("\n",fp); // \n fp (2 )
fputs(st2,pf); // 5
//fputs("\n",fp); // \n fp (2 )
fclose(pf); // 9 ( 13 )
pf=fopen("b.txt","r");
if(pf==NULL){puts(" "); return -1;}
while(fgets(st3,20,pf)!=NULL)
{puts(st3); n++;}
printf(" =%d",n);
Fclose(pf);
: | |
Annaria =1 | Anna ria =2 |
2.5 -
- -. - , . , -.
() :
int fprintf(FILE *fp, char *format [,]);
fp, . (), EOF.
. fp
FILE *fp=fopen("abc.txt","w");//
if(fp==NULL){puts(" "); return -1;}
int a=5; float b=3.56; long c=50000;
fprintf(fp,"%d %f %ld\n",a,b,c);
int arr[5]={1,2,3,4,5};
float mas[5]={1.1,2.2,3.3,4.4,5.5};
// 5 - int, float
for(int i=0;i<5;i++)
fprintf(fp,"%d %f\n",arr[i],mas[i]);
() :
int fscanf(FILE *fp, char *format [,]);
, . () EOF (1) .
. fp
freopen("data.txt","r",fp); //
if(fp==NULL){perror(" "); return 1;}
Int n; float f; long l;
fscanf(fp,"%d %f %ld\n",&n,&f,&l);//
printf("%d %f %ld\n",n,f,l); //
int a[5]; float m[5];
// 5- int, float
for(int j=0;j<5;j++)
{fscanf(fp,"%d %f\n",&a[j],&m[j]);
printf("%d %f\n",a[j],m[j]); }
Fclose(fp);
2.6
.
() :
int sprintf(char *buf, char *format [,]);
buf. ' \0 '. (), EOF.
() :
int sscanf(char *buf, char *format [,]);
, . EOF(1) .
.
int a1=10; long b1=50000; char buf1[20];
sprintf(buf1,"a1=%d, b1=%ld",a,b);
puts(buf1);// a1=10, b1=50000
float e, pi; char buf2[25]="2.718281, 3.141592";
sscanf(buf2,"%f, %f",&e,&pi);
printf("e=%f pi=%f\n",e,pi););//e=2.718281 pi=3.141592
Int a2; long b2;
sscanf(buf1,"a1=%d, b1=%ld",&a2,&b2);
printf("a2=%d b2=%ld\n",a2,b2); //a2=10 b2=50000
2.7 -
- . , . ' \n ', ' \0 ', - . .
() :
int fwrite(void *ptr, int size, int N, FILE *fp);
fp N , size , , ptr. , EOF.
. fp
FILE *fp=fopen(abc.txt,"wb");
if(fp==NULL){puts(" "); return -1;}
struct REC{int a[2]; float b;}; // .
REC str1={1,2, 3.3}; //
fwrite(&str1,sizeof(REC),1,fp); //
REC str2[3]={{3,4, 5.5},{6,7, 8.8},{9,10, 1.1}};
fwrite(str2,sizeof(REC),3,fp);//
Fclose(fp);
() :
int fread(void *ptr,int size,int N,FILE *fp);
fp N , size , , ptr. , EOF.
. fp
FILE *fp=fopen(abc.txt,"rb");
if(fp==NULL){puts(" "); return -1;}
REC str3, str4[3]; //
fread(&str3,sizeof(REC),1,fp); //
printf("%d %d %f\n",str3.a[0],str3.a[1],str3.b);
fread(str4,sizeof(REC),3,fp);//
for(int i=0;i<3;i++) //
printf("%d %d %f\n",str4[i].a[0],str4[i].a[1],str4[i].b);
Fclose(fp);
2.8
( ) , , -. , . , , .. .
- void rewind(FILE *fp); : rewind(fp);
- :
int fseek(FILE *fp, long offset, int from_where);
- fp offset . offset>0, , offset<0, . from_where : SEEK_SET , SEEK_CUR , SEEK_END . 0, EOF.
- , - . :
// - 10 fseek(fp,10L,SEEK_SET);
- : long ftell(FILE *fp);
( long) - , fp. 1L. :
long cur=ftell(fp);
. () .
#include<stdio.h>
struct REC{unsigned id;char name[20];unsigned mark;};
Int main()
{FILE *out,*in; REC rec;
int flag, sz=sizeof(REC); long len,kol,n;
if((out=fopen("mark.txt","wb"))==NULL)
{perror(" "); return 1;}
do {puts(" , , ");
scanf("%d",&rec.id); gets(rec.name);
scanf("%d",&rec.mark);
fwrite(&rec,sz,1,out);//
puts(" ? -1, -0");
scanf("%d",&flag);
}while(flag);
fclose(out);//
if((in=fopen("mark.txt","rb"))==NULL)
{perror(" "); return 2;}
// -
Fseek(in,0,SEEK_END);
len=ftell(in); // - -
kol=len/sz; //
printf(" %d \n", kol);
puts(" :");
scanf("%ld",&n);
if(n>0&&n<=kol)
{fseek(in,(n-1)*sz,SEEK_SET); //
// -
fread(&rec,sz,1,in); //
printf("%d %s %d\n",rec.id_rec,rec.name,rec.mark);
}
else puts(" !");
fclose(in); //
return 0;}
3
. , , , - . .
, main(), .
.
: , #include.
.
// file1.cpp
extern int b; // b
int func2(int,int); // func2
int a=5; //
int func1(int c) // func1
{int f=func2(c,a); // f=30
return f*b; } // 30*10=300
// file2.cpp
extern int k; // k
int func2(int x, int y) // func2
{return x*y+k;} // 2*5+20=30
// file3.cpp
#include<stdio.h> //
#include "file1.cpp" // file1.cpp
#include "file2.cpp" // file2.cpp
int b=10; // b
int k=20; // k
Void main()
{int d=2,res;
res=func1(d);// func1 res=300
printf(k=%d res=%d,k,res);//
}
fil3.cpp fil1.cpp file2.cpp. a ( fil1), func1(), func2() fil3, . fil1 func2() ( fil2) . fil1, fil2 b, k ( fil3) extern.
.
1) , extern, . , .
2) (, , ..) , . , .. , extern, .
, .
, , . , .h, #include.
// common.h
Exrtern int a;
Exrtern int b;
Exrtern int k;
int func1(int);// func1
int func2(int, int);// func2
// fil1.cpp
#include "common.h"
int a=5; //
int func1(int c){int f=func2(c,a);return f*b;}
// file2.cpp
#include "common.h"
int func2(int x, int y) {return x*y+k;}
// file3.cpp
#include<stdio.h> //
#include "common.h" //
#include "file1.cpp" // file1.cpp
#include "file2.cpp" // file2.cpp
int b=10; // b
Void main()
{int d=2,res; res=func1(d+k);
printf(k=%d res=%d ,k,res);}
int k=20; // k
*.h , . .h .
.
( , ..) . .lib.
main(), . main() . .
// arith.h
Float sum(float, float);
Float sub(float, float);
Float mul(float, float);
// sum.cpp
float sum(float a, float b){return a+b;}
// sub.cpp
float sub(float a, float b){return a-b;}
// mul.cpp
float mul(float a, float b){return a*b;}
4
++ . .
(, , , , , , , ) .
, : 1) ; 2) , ; 3) ; 4) .
, :
( );
();
;
.
. . () : auto, register, static, extern.
auto , . , . , , . , .. .
register , . register auto, , . ( ) register auto.
static . , static. . . .
extern () . , . . . extern .
, . : (), .
() . , , .
. static extern. .
( ) . new malloc() , delete free().
() , . . , : , , , (), .
, .. , , . ( ). , . .
. , static int K, ( ), .
, . ( ). . , . :
Void main()
{char oper; ---ù
int x=2,y=4,z; |
... |
{int k=5; ù |
int x=3; | |
x+=3.. | |
} û |
} û
, . , , , .
, , , (). . . . , , .
5
.
:
& _=_;
_ , , .
:
int x=20; //
int &p=; // ,
//
float g=5.5;// g.
float &pg=g; // ,
// g
& , .. x p int. _ .
, . .
1) , . , :
double f=5.5;// f.
double &pf=f; // pf c f
double *fp1=&f; // fp1 c f.
f=2.5; // f 2.5
pf=1.5; // f 1.5
*fp1=10.5; // f 10.5
2) , , , 4 .
int n1=sizeof(pf); // n1=8 ( double)
int n2=sizeof(fp1);// n2=4( 2 -, -)
3) .
int x=100;
int &ref; // !!
int &ref=;// !!
4) , .. , .
int x1=50;
ref=1; // 1.
// ref - .
C , :
1) void.
2) new, .. .
3) .
4) .
const, .. . :
double d=2.728282; //
const double &ref_d=d; //
d=0.1;//
ref_f=0.2; // !!
, :
1) .
float & ref1; // !!
extern float &ref2; //!
2) .
3) .
, .. , . , -, . , , -.
.
#include<stdio.h>
int f1_ref(int &a, int &b) {return a+b;}
int f2_ptr(int *a, int *b) {return *a-*b;}
int f3_ref(int &a, int &b) {a=100;return a+b;}
int f4_ref(const int&a, int &b)
{//a=100;//
b=10;;//
return a+b;}
Void main()
{int x=20,y=5,res1,res2,res3,res4;
res1=f1_ref(x,y);//=20 y=5,
res2=f2_ptr(&x,&y); //=20 y=5, ,
res3=f3_ref(x,y); //=20 y=5, ,
// =100
res4=f4_ref(x,y); //=100 y=5, ,
//x , y=10, res4=110
printf(res1=%d res2=%d , res1,res2);
printf(res3=%d res4=%d\n, res3,res4);
}
:
res1=25 res2=15 res3=105 res4=110
1. .. ++: . . 5- . .: , 2000. 560 .
2. .. : . . .: , 2004. 576 .
3. .., .. : . . 2- ., .: , 2002. 600.
4. .. /++. . : , 2003. 461 .
5. .., .. : Turbo C Borland C++. .: . ., 1992. 240 .
6. .., .. ++. .: -, 2002. 288 .
7. .. - ++. : . . , 1999. 208 .
8. , . ++. / . . .: ̔, 1999. 816.: .
9. .., .., .. ++. : / .. , .. , .. . 2- ., . .: .-, 2000. 344.
10. . . .: , 2001. 256.
11. /++. . / .. , .. . .: , 2002. 240.
12. .. /++ .- .: : , 2001. 288.
13. .. , .. . ++. 5- . / . . .: "