:
¨ ;
¨
, . - & *.
& , . , v , &v (.. ).
( ) * , . p , *p , .
:
* _[=];
:
int a=10, b;
int *ptr = &a; // a
cout << =<<ptr<<\n =<<*ptr;
*ptr=8;
cout << a= << a << endl; // 8
( 4 ).
- --, ++, , . , . .
:
int *p;
p+=5;
int 4 , 5 20 .
, .
C++ . - ( ):
int a[10];
int *p=a; // , int *p = &a[0]; -
, - - []:
p[3]=11; // *(p+3)=11;,
cout << a[3]; //11
, -, .
new delete. new :
int *i, *j;
i = new int; // int
j = new int [50]; // 50 int
k = new int [20][30]; // 20*30 .
delete :
delete i;
|
|
delete [] j; delete [] k;
N . , . .
#include <iostream.h>
void main()
{ int n, *c;
cout<< ?; cin>>n; cin.get();
c=new int[n]; int*p=c;
cout<< <<endl;
for(;p<c+n;p++) cin>>*p;
if (*c>*(c+n-1))//c+n-1
{int temp=*c; *c=*(c+n-1); *(c+n-1)=temp;}
for(p=c;p<c+n;p++) cout<<*p<< ;
cout<<endl; cin.get();
}
, , ( ). , 4 5 , .
!
1
1. C N , . N , -100 100.
2. A N B M . L A B , A, B. N M , A B 0 100.
2
1. N , . , . "", "". N .
2. A B N . L 2*N :
L=(A(1)+B(1),A(1)-B(1),A(2)+B(2),A(2)-B(2),.....,A(N)+B(N),A(N)-B(N))
N A .
3
1. A N . B, co A, X. X, N A .
2. 20 . 1- 11-, 2- 12-, 3- 13- . . . A 0 100.
4
1. A 20 . A 0 100.
2. A N , - .. A 0 100. N .
5
1. 20 . 1- 11-, 2- 12-, 3- 13- . . . A 0 100.
|
|
2. 25 , 100 100. .
6
1. A N B M . L A B , A, B.
2. A N . B, A.
7
1. C N , . .
2. : , , ..
8
1. . .
2. , , .
9
1. C N , . .
2. A N B M . L A B , A, B. N M , A B 0 100.
10
1. C N , . .
2. : , , ..
11
1. C N , . -100 100.
2. A N B M . L A B , A, B. N M , A B 0 100.
12
1. A 20 . A .
2. A N , - .. A 0 100. N .
3
:
¨ ( )
¨ string.h
¨
C , c 0 (\0). char, char.
char color[16] .
char color [] = blue 5 .
char *colorPtr = blue , - colorPtr.
-
gets . . :
char s[256]; gets(s);
gets stdio.h. iostream cin.getline. :
|
|
char s[256]; cin.getline(s, 256);
, cin>>s .
puts(s) - s .
atof(s) s double
atoi(s) s int
atol(s) s long
strtod (s, *ends) s double. ends .
strcpy(s1, s2) - s2 s1. . :
strcpy(s,s+1); - s
strcpy(s+1,s); - .
strncpy(s1, s2, n) n s2 s1
strcat(s1, s2) s2 s1
strncat(s1, s2, n) s2 s1, n
strcmp(s1, s) s1 s.
0, <0, >0, s1 =s, <s, >s.
strncmp(s1, s2, n) n s1 s. 0, <0, >0, s1 =s, <s, >s.
strchr(s, c) c s .
strstr(s1, s2) s2 s1.
1. .
1 ,
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{ int r=0,i=0; char s[80],s1[80];
printf("?");gets(s);
for(i=0;s[i]!=0;i++)
if (s[i]!=' ') {s1[r]=s[i];r++;}
s1[r]=0;
strcpy(s,s1);
puts(s); getch();
}
2 , strcpy. , .
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{ char *n; char s[80],s1[80];
printf("?");gets(s);
while(n=strchr(s,' ')) strcpy(n,n+1);
puts(s);
getch();
}
2. , strtok, ( , strtok , , ,.!?)
#include <string.h>
#include <stdio.h>
#include <iostream.h>
int main(void)
{ char input[80]; char *p;
cin.getline(input,80);
p = strtok(input, " ");
while (p)
{ printf("%s\n", p);
p = strtok(NULL, " ");
}
cin.get(); return 0;
}
1
1. , . .
2. , ( ).
3. . .
. , . ( ).
. , ( ). ,
. (+ - * / %). , 0.
|
|
2
1. . ( ).
2. .
3. . .
. (, , )
. ( )
3
1. , . , .
2. , . , .
3. . .
.
. ( , )
. ( , ).
4
1. ( ).
2. .
3. . .
. , , ( )- .
. , , , .
. , , , . (, -)
5
1. , .
2. .
3. . .
. , , ( , )- .
. , , , . (, -)
. , , , .
6
1. , , .
2. , , .
3. . .
.
.
.
7
1. , .
2. . .
3. . .
. .
. .
. ( )
8
1. , , ( , , : , ).
2. . .
3. . .
. .
.
. .
9
1. . ( ).
2. .
3. . .
. , ( , ) - .
|
|
. ( ).
. .
10
1. . . , .
2. . .
3. . .
. , , .
. .
. .
11.
1. , , , , .
2. .
3. . .
. , , .
. .
. .
12
1. , , . ( ).
2. , , .
3. . .
. .
. .
. ( ) .
4
:
¨ ;
¨ .
stdio
: FILE , : FILE *f;
fopen(), FILE. .
FILE *fopen(const char *fname, const char *mode);
:
r
w
.
, NULL. :
FILE *f; //
f = fopen(c:\\autoexec.bat,r); //
if (f==NULL) // ,
{
printf(Error\n);
};
fclose. : fclose(f);
feof() , . :
while(!feof(f))
{
//
}
, fgetc:
int fgetc(FILE *stream);
:
char c = fgetc(f);
, fgets:
char *fgets(char *str, int num, FILE *stream);
str. , (num-1) . str .
:
char str[256];
fgets(str,256,f);
printf(str=%s\n,str);
fprintf . printf:
int fprintf(FILE *stream, const char *format, );
:
char s[]=;
int len = strlen(s); // s
fprintf(f, %s = %d,s,len);
1.
#include <stdio.h>
#include <conio.h>
main()
{FILE *fin,*fout;
if (fin=fopen("binfile.cpp","r"))
{ char s[100];fout=fopen("b1.cpp","w");
while (fgets(s,100,fin)) {puts(s);fputs(s,fout);}
} else puts("error");
fclose(fout);
getch();return 0;
}
2. , fprintf, fscanf
#include <stdio.h>
#include <conio.h>
main()
{FILE *fin,*fout;
if (fin=fopen("binfile.cpp","r"))
{ char s[100];fout=fopen("b1.cpp","w");
while (fscanf(fin,"%s",s)!=EOF) {printf("%s\n",s);fprintf(fout,"%s\n",s);}
} else puts("error");
fclose(fout);
getch();return 0;
}
3. 0 1 2 9
#include <stdio.h>
#include <conio.h>
main()
{FILE *f; f=fopen("a.dat","w+b"); int i;
for (i=0;i<10;i++) fwrite(&i,sizeof(int),1,f);
fseek(f,0,0); int x;
for (i=9;i>0;i--) {fseek(f,i*sizeof(int),0);fread(&x,sizeof(int),1,f); printf("%d ",x);}
getch();
}
1
1. X.TXT Y.TXT. , X.TXT, Y.TXT. , .
2. , . , , , 20. , .
3. , 20 [0,50]. , , . .
2
1. . , . "" " ". X.TXT.
2. , 6 . , : 12 , ; - 6 , .
3. , N [-100,100] (N - ). : , - . .
3
1. . B.TXT, C.TXT, . .
2. , 20 [0,100] ( RANDOM). . , , - .
3. , 20 [0,100] ( RANDOM). . , 50, . .
4
1. . , . , , A.TXT
2. , , . , , .
3. . , , .
5
1. . B.TXT, , L. L . B.TXT , , .
2. 2 , , . , , .
3. , 20 [0,100]. , .
6
1. ( ). , . . NUM.TXT
2. , . . . , ( ).
3. , , ( ).
7
1. ( ). , "a". NUM.TXT
2. , . . . , ( ).
3. , , ( ).
8
1. . , . A.TXT, .
2. , . , , .
3. . .
9
1. . , , . . , REZ.TXT, .
2. , . , ( , , ). .
3. . , ...
10
1. . , ( , , ). , REZ.TXT, - - -
2. 10 . 10 , ( ).
3. . , .
11
1. . A.TXT ( ), .
2. 20 . 10 ( ).
3. . , .
12
1. ( ). , "a". NUM.TXT
2. , . , , .
3. . .
, C++ .
.
1. , . / ., . , . ; . . .: , 2000. 384 .
2. , . /.. , .. .: , 2000. 191 .
3. , . . : / .. , .. , .. .: , 2001. 223 .
4. , . ++: / .. .: , 2002. 560.
5. , . ++ / . .: , 2000. 272 .
6. , . C++ 21 / . ; . . .: , 2000. 816 .
7. , . Visual ++ 6.0 / .. .: -, 1999. 416 .