Документ взят из кэша поисковой машины. Адрес оригинального документа : http://tex.bog.msu.ru/numtask/chlp12.pdf
Дата изменения: Sat Aug 4 16:20:00 2012
Дата индексирования: Thu Feb 27 20:06:37 2014
Кодировка:


.. ..

"C"

2012



1 2 3 4 5 "" "" "C" 5.1 5.2 . . . . 5.3 . . . . 5.4 . . . . . 5.5 for . . . . . 5.6 . . . . 5.7 "" ......... ......... ......... ......... ......... ........ 2 2 3 4 5 5 6 6 7 7 9 10

...... ...... ...... ...... ...... ...... .... .... .... .. .. ..

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

6

6.1 . . . . . 6.2 6.3 - . . . . . .

....... ....... .......

"" 10 . . . . . . . . . . . . . . . 10 . . . . . . . . . . . . . . . 11 . . . . . . . . . . . . . . . 12 .... .... .... "" 15 . . . . . . . . . . . 15 . . . . . . . . . . . 16 . . . . . . . . . . . 18

7

7.1 "main" . . . . . . . . 7.2 . . . . . . . . . 7.3 .

1


1



"", , . "" . . , , . . . . -, . , . , - (front end - ), , , . , . : " , , " , , ( ). . -, . , , . : " , ". : " ". . , . , , . ( , 100% , , . ). , . .

2

""
1. . ( ), . 2. , , . 3. , . 4. . -. 5. , , , "".

2


6. 7, 666 .. #define . , , . 7. , , . 8. main(). . , , , , , .. 9. , , . , , return .

3

""
1. -, char, .. . , 1 (.. 8 ) -128 . . . 127. , unsigned: unsigned char 0 . . . 255. char (. ). char , - , buk='D'; ASCII- " buk=68;". "" () : '\n' ; '\t' ; '\r' ; '\a' "", .. ; '\" ' ; '\' ' ; '\\' " \" ( ). 2. -, int, .. . unsigned long: " long int", " unsigned int" " unsigned long int". . int 2 (.. -32768 . . . 32767 0 . . . 65536 unsigned int), long int 4 (.. -2147483648 . . . 2147483647 0 . . . 4294967296 unsigned long int). int long int 4 . 3. -, double. : 1.2345, -0.7777777777, -33.4444444e-86, -2.1111e33 ( float, . , , , . , float .) 3


. : char buk; int i,j,k; double energy; . . , " static" ( . ).

4

"C"

. : int numer[9],indeks[20][30]; double coord[7]; . indeks[5][27]=555; coord[3]=coord[0]; . () numer numer[8]. numer[9] . "C" ( , ). , , . , . ( ) . , , . double *vect; , (, 95), : vect=(double *)malloc(95*sizeof(double)); if(vect==NULL) {printf("No mem for vect!\n"); exit(1); } malloc , " void *". sizeof, double , " (double *)" " void *" " double *". , . , double: " n=1.5;", " n=(int)1.5;". , " 3/2" , " 3/2.0" " 3/(double)2" 1.5. , malloc NULL. exit. malloc exit, #include : int **dat;

4


(, 20 в 30), : dat=(int **)malloc(20*sizeof(int *)); if(dat==NULL) {printf("No mem for dat!\n"); exit(1); } 20 : 30 : for(i=0;i<20;i++) {dat[i]=(int *)malloc(30*sizeof(int)); if(dat[i]==NULL) {printf("No mem for dat!\n"); exit(1); }} (0 . . . 19) , , .. 20 : int *dat[20]; , , . , , . " int *" " double *" . ( double 1 8 , long int 4 ). : int *uk,ind[21],num; uk=&(ind[10]); & ( ) . uk ind -10 . . . 10, uk[-10] ind[0], uk[0] ind[10], uk[10] ind[20]. , uk=# "0", uk[0] , num. "C" . , , , .

5

""

, "" , . , switch goto .

5.1



4 : x=3+2; = x=5 x=3+2.0; = x=5.0 x=3-2; = x=1 x=3.0-2; = x=1.0 x=3*2; = x=6 x=3.0*2; = x=6.0 x=3/2; = x=1 x=3/2.0; = x=1.5 , " 3/2" , . .

5


x=85%20; = x=5 x=80%20; = x=0 ( ) x=99%20; = x=19 ( ) x=7; x++; = x=8 x=7; x--; = x=6 x=7; x+=3; = x=10 x=7; x-=3; = x=4 x=7; x*=3; = x=21 x=33; x/=3; = x=11 , .

5.2



"" (true) "" (false). : (0) false, true. if(i%2) printf("Nechetnoe"); else printf("Chetnoe"); if(i%2!=0) printf("Nechetnoe"); else printf("Chetnoe"); > >= < <= != == . if(x=3) . : , "x" "3", "3", .. "3", , .. . , if(x=0) . && "" () || "" () ! "" ()

5.3



, FFT ( ). , . unsigned char 1 . unsigned char a,b,c; a=12; = 0000 1100 b=10; = 0000 1010 c=a&b; = 0000 1000 c=8, "" c=a|b; = 0000 1110 c=14, "" c=!b; = 1111 0101 c=245, "" c=a<<1; = 0001 1000 c=24=2*a, 1 c=a>>1; = 0000 0110 c=6=a/2, 1 c=b<<3; = 0101 0000 c=80=8*b, 3 6


c=128, 6 . char buk; long int num; buk=127; buk++; num=2147483647 ; num++; buk= -128 num= -2147483648, . c=b<<6; 1000 0000

=

5.4



if(x>3) {a=7; b=8; c=9; } else {a=9; b=8; c=7; } if(x>3) {a=7; b=8; c=9; } if(x>3) a=7; else a=9; if(x>3) ; else a=7; if(x>3) a=7; if , () , , else. ( else) . . : x=5; if(x=3) a=7; else a=9; , a 7. x=1; a=9; if(x>3); a=7; , a 7. , : if(x>3) {a=7; b=8; c=9; }; else {a=9; b=8; c=7; }; , . , ( ).

5.5

for

: double x[100]; for(i=0;i<100;i++) { x[i]=20.0-0.666*i; if(i%10==0) x[i]+=7.0; } . i i = 0, . . . 99, i , , , i<100, .. i=99. , , , , : for(i=0 ; i<0 ; i++) { } 7


; for(i=0 ; i<1 ; i++) { } 1 i=0; for(i=0 ; i<3 ; i++) { } 3 i=0,1,2. a=0; for(i=3 ; i<100 && 3*i<290 ; i=i+17+i%3) a+=i; , , . i=5; a=0; for(;;) {if(i>=1000) break; i=i*(i-2); if(i%7<3 && i<250) continue; a+=i; } , , . , , .. i=0; for(;i<5;i++) . . . for(i=0;;) { if(i>=5) break; i++; } .. for-, . continue break. continue, , . , , , . , , continue . break continue, , (.. ). , . , , , , . i j for(i=0;i<100;i++) { for(j=99;j>=0;j­) {if(i%10==0) continue; if(j<10) break; a+=j; } a=a+7; } continue break j ( i 10 j 99 0, a , i j 99 9, a 99 10. i break continue . , break; break; ( break j, i) .

8


. for(i=0;i<100;i++); { a+=9; b*=a; c=i; a=b+c; } , " " (, ) 1 , i=100. for(i=0;i<100;i++) { for(j=0;j<50;i++) { a+=i+j; }} , j , () , "". , , for-. while-: while(fscanf(fp,"%s",str)!=EOF) { str } for(;;) { if(fscanf(fp,"%s",str)==EOF) break; str }

5.6



. . : double devjat(double x, int n) { int i; double tmp; tmp=1.0; for(i=0;i1.0e10) return 1.0e10; if(tmp<-1.0e10) return -1.0e10; return tmp; } x n 1010 . (.. ) . () , . void krik(int flag) { if(flag>0) printf("Polozitelen\n"); if(flag<0) printf("Otritsatelen\n"); } flag ( ). void vopl(void) { printf("Eto function vopl\n"); } 9


.

5.7



( , ). " main" (. ). int main(void) void main(void) , , "main" " int". , " int", " return" . , . "0" () , . : " exit(7);" , " return 13;" " main".1 . , " main" : int main(int npar, char **par) . , : ./myprog -w 1.2345 myout77 "npar" "4" , "char", .. , : · · · · par[0] par[1] par[2] par[3] " " " " ./myprog" -w" 1.2345" myout77"

par[3] , double- "1.2345" par[2] " sscanf" (. ).

6
6.1

""
.

, #include cos(x) sin(x) tan(x) cosh(x) sinh(x) tanh(x)
- (environment variable), , PIPESTATUS.
1

10


acos(x) asin(x) atan(x) acosh(x) asinh(x) atanh(x) , , exp(x) log(x) sqrt(x) floor(x) = [x] ceil(x) = [x] + 1 fabs(x) = |x| floor(5.9)= 5.0 ceil(5.9)= 6.0 floor(-5.9)= -6.0 ceil(-5.9)= -5.0 ( , double int , floor ceil, , (int)(5.9)= 5, (int)(-5.9)= -5). double double. pow(x,y) = xy double, double. atan2(y,x) = arctg(y /x) x y : atan2( 1.0, 1.0) = /4 atan2( 1.0,-1.0) = 3 /4 atan2(-1.0,-1.0) = -3 /4 atan2(-1.0, 1.0) = - /4 double, double. . IBM- sqrt(4); int i; i=25; sqrt(i); , , sqrt(4)=1.596e-138, pow(2,3)=7.371e+069. pow. , pow(4.0,3/2) 4.0, 8.0, pow(x,2.5) , x*x*sqrt(x), y=pow(x,9.0) tmp=x*x; tmp=tmp*tmp; y=tmp*tmp*x; x. ( pow(x,y) exp(y*ln(x)) ). abs(n) = |n| int int.

6.2



"C" , '\0', ( ) . , char. ( '\0' ) '\0' . , ( char) char str[50]; 49. , "abc" "012ASD345" "line N 1 \nline N 2 \a (bell)\n" -, , .

11


, #include strlen(str); str, .. , '\0'. . strcpy(str1,str2); str2 str1. , strcpy(str,"Dfg"); str[0] 'D', str[1] 'f', str[2] 'g', str[3] '\0', str[i] . strcat(str1,str2); str2 str1. strchr(str,buk); () buk str . , NULL. , char str[50],*ukaz; strcpy(str,"abcd1234"); ukaz=strchr(str,'d'); ukaz[0] 'd', ukaz[1] '1', ukaz[2] '2', .. strstr(str1,str2); () str1, str2 . str2 , NULL.

6.3

-

-, #include , - : · . · -. · . . FILE *fp1,*fp2; fp1=fopen("myfile.txt","r"); fopen . fp1=fopen("/home/username/mydir/myfile.txt","r"); , . fopen . "r" . "w" , , "" , ( 1 ). "a" , "" ("append"), .. , , . "b", .. "rb" ; "wb" ; "ab" . : fp2=fopen("myfile.txt","w"); 12


; fp2=fopen("myfile.txt","ab"); . : EOF ( , ASCII 26). ( "26") , EOF .2 fopen NULL, fp=fopen("myfil.dat","r"); if(fp==NULL) {printf("Input file not found!\n"); exit(1); } , : fclose(fp1); : int n; fscanf(fp1,"%d",&n); n. , . . "%d" , int. , "%ld" long int, "%u" unsigned, "%lu" unsigned long, "%le" double, "%s" ( ). , . , . , , . "%d" double . fscanf , . , , , .. &(dat[7][33]). , . , &(str[0]) str. char str[200]; fscanf(fp1,"%s",str); , " fscanf(fp1,"%s",&str); ", , . , , . , : num1 -555 Xvalue[13]: 1.2345e-66 : char str1[200],str2[100];
DOS/Windows: DOS/Windows : CR ( , ASCII 13) LF ( , ASCII 10). '\n' : CR LF, , CR LF '\n'. , , . , UNIX LF, DOS' UNIX' .
2

13


int num; double x[40]; fscanf(fp1,"%s%d%s%le",str1,&num,str2,&(x[7])); fscanf ( ). , EOF. 4 -555 num, 1.2345·10-66 x. str1 str2 "num1" "Xvalue[13]:". for(;;) { if(fscanf(fp,"%d",&nnn)==EOF) break; . . . } . : int n; fprintf(fp1,"Numer=%d\n",n); , , , n. "%d" "%3d", 3 , ( ): Numer= 1 n=1 Numer=111 n=111 Numer=11111 n=11111 , "%s" , "%14s" 14 , "%ld" long int. double , : "%le" 6 , "%23le" 6 23 , "%.14le" 14 , "%23.14le" 14 23 . fscanf, : fprintf(fp1,"1: N1=%d N2=%d\n2: X0=%.12le X5=%.12le \n\n", n1,n2,x[0],x[5]); - - . , , stdin (), stdout () stderr (). fscanf(stdin,"%d",&n) scanf("%d",&n), fprintf(stdout,"%d",n) printf("%d",n). stdout stderr . stderr . stdout -. printf , . (, 128 ), '\n'. if(i%100==0) {printf("[%d]",i/100); fflush(stdout); } fflush . , - char str1[80],str2[160]; str1[0]=' '; str1[1]='-'; str1[2]='2'; str1[3]='5'; str1[4]=0; sscanf(str1,"%d",&n); sprintf(str2,"Num=%6d",n); -25 n, str2

14


Num=

-25

7
7.1

""
"main"

main. , . , , , . , double myfun(double x, int n) { return x+n; } main() { printf("%le",myfun(5.0,3)); } , , . , , . double myfun(double, int); main() { printf("%le",myfun(5.0,3)); } double myfun(double x, int n) { return x+n; } , , . #include , ( ). , .. , #include .... : #include #include #include : #include . : #define NNN 200 NNN 200. #define , : #define kub(x) x*x*x

15


, . , kub(3) kub(a) , kub(d+f), , . . #include #define. . , (.. # ).

7.2



. , . . , . , . , ("" ) . . , , 80 Kb. , . , , , , . . double-, double- , , 50 , " static" : static double massiv[50]; , . ( , , ). ("") , (, ""). , . , ( ). , , . , . . . , " " , . , ( ) , . , . , , , . ( int double), .

16


, : i, () . : int i; double sloz(double x) { double z; z=0; for(i=0;i<7;i++) z+=x; return z; } double umnoz(double x) { double z; z=1; for(i=0;i<5;i++) z*=sloz(x); return z; } , : umnoz . , , . int double, , , double vosem(double x) { x=x*x; x=x*x; x=x*x; return x; } x vosem(x); , , 2 256 y=vosem(2);. . . ( fscanf , ). void zamena(char *str) { int i; for(i=0;i

#define, , , , , main. main (, ).

7.3



. n, 10 в n . : n3 x1 1.0 x2 2.0 x3 3.0 .

#include #include #include #define INFILE "myprog.in" #define OUTFILE "myprog.out" #define NUM #define FC 10 0.123

int nnn; double *invect,*matr[NUM]; double outvect[NUM]; void matr_x_vect(void) { int i,j; for(i=0;i
}

18


if(fscanf(fp,"%s%d",tmpstr,&nnn)!=2) { printf("Error reading n!"); exit(1); } invect=(double *)malloc(nnn*sizeof(double)); if(invect==NULL) { printf("Not enough memory for invect!"); exit(1); } for(i=0;i
19