C

222
Εισαγωγή στις Γλώσσες Προγραμματισμού Βασικά στοιχεία της Γλώσσας C Εντολές Ελέγχου Ροής Προγράμματος Συναρτήσεις, Αρθρωτός Προγραμματισμός Πίνακες και Δείκτες Δομές Αρχεία Δεδομένων Βιβλιογραφία Τμ. Μαθηματικών Πρώτη Σελίδα JJ II J I Σελίδα 1 από 223 Πίσω Όλη η οθόνη Κλείσε Έξοδος Πανεπιστήμιο Αιγαίου url: http://www.aegean.gr Εισαγωγή στις γλώσσες προγραμματισμού με τη γλώσσα C Παναγιώτης Νάστου Πανεπιστήμιο Αιγαίου Τμήμα Μαθηματικών 832 00 Καρλόβασι Σάμος © Copyright Πανεπιστήμιο Αιγαίου, Τμήμα Μαθηματικών All rights reserved

Transcript of C

C , . 1 223 url:http://www.aegean.gr C 832 00 Copyright , All rights reserved - - C . . .. 2 223 1 , . . . . . . - - C . . .. 3 223 -. . ; - . , ( , , ). -. . , , . Pascal C -. - . - - .1.1. . , . - - C . . .. 4 223 . - . . : (opera-tion code) (operands) 0 1. - . . . - - C . . .. 5 223 CONTROL UNITINTERNAL BUSMARMBRADDRESSDATAMEMORYBUSCONTROL SIGNALSR0R1R15IRPCALUYZADDSUBANDXORCARRY_INALUCONTROLLINES 1.1: . - - C . . .. 6 223 1.1 . :1. ALU . - : () ( -). - 2 bit 22= 4 1.1,2. IR ,3. PC ,4. R0 . . . R15 ,5. Memory Address Register (MAR) - / ,6. Memory Buffer Register (MBR) MAR, MAR,7. CONTROL UNIT - IR CONTROL SIGNALS - . - - C . . .. 7 223 0 00 ADD1 01 SUB2 10 - AND3 11 - XOR 1.1: . - ( ) . . - R2 R3 R1. - , . :00 0001 0010 00112bit00 1.1, 4 bit 0001 = (1)10 R1. bit 0010 0011 R2 R3. , R1= R2 + R3. - . , - - - C . . .. 8 223 , , ., - . - (AssemblyLanguages). , - (mnemonics) . ADD R1, R2, R3 . - . , - (Assembler) . .1.2. 1.1, - . , - - C . . .. 9 223 , . -, . - . . d= a +bc ( b, c - d). - . :1. , ,2. , ( ) ,3. , , , - - C . . .. 10 223 4. , , . , - , . , . , - (reusability). - . FORTRAN (FORmula TRANslation) - IBM , LISP(LIStProcessing) - COBOL(COmmonBusiness-OrientedLanguage) . C, UNIX . - C. systemprogrammingLanguage. Prolog . (Object Oriented Programming OOP) 60 Small- - - C . . .. 11 223 talk. , . OOP , - . , - . . . - OOP C++ Java. - C.1.3. A= a, b, c, . . . , x, y, z . - n A - n-. chapter 7-((((((c,h),a),p),t),e),r). n An, A. A _ , :A= a, b, c, . . . , x, y, z, A, B, C, . . . , X, Y, Z, ., , , :, ; , !, _ p - , p A. - - C . . .. 12 223 : 1.3.1 A , A A. A . B CB = a, b, c, . . . , x, y, z, A, B, C, . . . , X, Y, Z, 0, 1, . . . , 8, 9,+, , /, %, , , &, ,, , !, , (, ), #, ?, :, ; , ., , , , , _, , \ C - B. - . - , . :1. .2. . , - , . - . 1.3.2 : - - C . . .. 13 223 1. T: ,2. N: , ,3. P. , {,, , , , , . . .}. x y xy . x y. :1. , - .2. ,3. - 1 2 . . - - C . . .. 14 223 , +, , = . T = A, B, C, +, , (, ), = N = asgn_stat, exp, term, factor, id :asgn_stat id= expexp exp + termtermterm term factorfactorfactor (exp)idid ABC id -. . :asgn_stat id= exp (1.1) id= exp + term (1.2) id= exp + term factor (1.3) id= exp + term id (1.4) id= exp + term C (1.5) id= exp + factor C (1.6) id= exp + id C (1.7) id= exp + B C (1.8)(1.9) - - C . . .. 15 223 id= term+ B C (1.10) id= factor+ B C (1.11) id= id + B C (1.12) id= A + B C (1.13) A= A + B C (1.14) 1.1 asgn_stat id=exp. - exp exp+term 1.2. - . -1.14 A B C. . - 2. 2. 2 (context-free grammar) . - . , - - C . . .. 16 223 - . . - . . - (typesystem) . - type theory. . , - (runtime exceptions).1.4. , . , , C, - (compilation). - - C . . .. 17 223 . C, - . (compiler) - .PreprocessorLexicalAnalysisSyntacticAnalysisObject CodeGenerationSource CodeLinkingExecutebleLibrariesMachine Dependent 1.2: . 1.2 - C. (preprocessor) , . (Lexical Analysis) (token) . (Syntax Analysis) - . (object code generation) - - C . . .. 18 223 - . , (Linking) ( ), . , - . . . . C - C - . . .. 19 223 2 C2.1. C C . main C main . : (standard lib) ANSI C (user dened). - C, ( .h) . - #include (preprocessor) C. . . . C - C - . . .. 20 223 C C. 2.1 ANSI C . stdarg.h io.h / limits.h & oat.h .math.h mem.h stdio.h stdlib.h string.h & ctype.h time.h 2.1: 2.1 C This is my rst code!!!. main - . main printf This is my rst code!!! ( 3 ). printf stdio.h ( 0 ) . . . C - C - . . .. 21 223 .0: #include 1:2: int main(){3: printf("This is my first code!!!\n");4: return 0;5: } 2.1: . 2.1 .c .. my-FirstCode.c. UNIX, cc - 2.1 #ccmyfirstCode.c myrstCode .2.2. C : char: byte 256 . - ASCII 128. int: byte . . . . C - C - . . .. 22 223 oat: double: , , - C byte . byte - sizeof sizeof(). oat sizeof(oat). < limits.h > < float.h >. , - :#include < limits.h >#include < float.h > C -. int short long. short int short long int long . - sizeof (short) sizeof (int) sizeof (long). int long 4 byte (32-bit) short 2 byte(16-bit). - (oat) 4 byte (32-bit) (double) 8 byte (64-bit). 754 IEEE (Institute of Electrical andElectronics Engineers). long double, long double, . . . C - C - . . .. 23 223 . - long double double. signed () unsigned ( -) char - 754 IEEE . - . char 8-bit bit , 1 0 +. 8-bit 10000001 1 ( ) unsigned 128 , 127+120= 128. - unsigned char 0. . . 255 128. . . 127. int. C typedef :typedef < newTypeName > < oldTypename >; ; C . int integer :typedef integerint; . . . C - C - . . .. 24 223 byte0. . . 255 :typedef byte unsignedchar;2.3. C case-sensitive . : _ . C . 2.2 (reservedwords). . . . C - C - . . .. 25 223 - int, char, double, oat long, short, signed, unsigned, register, static, extern, auto, voidconst, volatile break, case, continue, switch,default, do, while, for, if, else, return, gototypedef, enum struct, union 2.2: - . :< typename > < variable_name >=< value >; (2.1) . 2.1 = . . . C - C - . . .. 26 223 . ; C, . :intcount= 5; byte ( int) count 00000000 00000000 00000000 00000101 5. :double percent= 28.5, rate= 5.6; percent rate 28.5 5.6 . - . . count =5; 5 count.2.4. C, , -. . . . C - C - . . .. 27 223 . C: literal: =3.14159 literal 3.14159. , literal 3.14159 - PI . 2 C :1. const: constfloatPI = 3.14159;2. #dene:#define PI 3.14159 PI. #undef. PI :#undef PI int (signed) long. long L U, .. 2048L 2048U 2048UL. 0 0x 0X , .. 077= (63)10 0x2f = (63)10. . . . C - C - . . .. 28 223 (.. 23.45) (2e-3) ( 2103) double :F f oatL l double. .. x. , 8 bit ASCII x 120 x ASCII . . 2.3 ASCII - . C, \ . . - n n \n ASCII ASCII 10. \ . \t \ - ASCII . , \0 -. , , . (string constant) - , .. Hello World string. \0 . . . C - C - . . .. 29 223 , \0 Hello World Hello World\0. . . . C - C - . . .. 30 223 97(a) 65(A) 48(0)98(b) 66(B) 49(1)99(c) 67(C) 50(2)100(d) 68(D) 51(3)101(e) 69(E) 52(4)102(f) 70(F) 53(5)103(g) 71(G) 54(6)104(h) 72(H) 55(7)105(i) 73(I) 56(8)106(j) 74(J) 57(9)107(k) 75(K)108(l) 76(L)109(m) 77(M)110(n) 78(N)111(o) 79(O)112(p) 80(P)113(q) 81(Q)114(r) 82(R)115(s) 83(S)116(t) 84(T)117(u) 85(U)118(v) 86(V)119(w) 87(W)120(x) 88(X)121(y) 89(Y)122(z) 90(Z) 2.3: ASCII . . . . C - C - . . .. 31 223 2.5. C .2.5.1. C - . : ++, . - x++ x 1 x+ + + + x x= x+ 1. x x x= x 1. x+ + + + x. x+ + x . + + x 1 . :intz, y, x= 5;y= + + x;z= x+ +; y 6, z 6 x 7. . . . C - C - . . .. 32 223 : +, -, /, *, % . / % modulo x%y x y. -. *,/,% + -. . . :8%36. % * , , 12.3+4*6. 27.(3+4)*6. , 42. w x/y z. * / , , ((w x)/y) y. wx/y+z/y. . + . . , ((w x)/y) + (z/y). . . . C - C - . . .. 33 223 2.5.2. C , bit . x > n (x < n) - bit x, n () 0 n () x. . x =(12)10=(00001100)2 n=2 x >n( -) x 0 (00000011)2= (3)10. n= 3 x < n x 0 (01100000)2= (96)10 x > n (00000001)2= (1)10. x , (12)10= (00001100)2. , 2. n , bit 2n. n , . 7 3 2. , ; : ., bit - 0 ( ) 1 ( ) bit . . . . C - C - . . .. 34 223 2.5.3. , C 2.4. - : 1 0 . == ? x==y > ? x>y < ? x= x>=y? z). , . , x +6 > y . . . C - C - . . .. 35 223 (x+ 6) > y. 1 () [ ] .2 !+ + ( ) (cast)&( ) sizeof +() -()3 () / %4 + 5 < >6 > < >= < expr1 > - < expr2 >. z a b, z= (a>b) ? a : b, . . . C - C - . . .. 38 223 (a>b) a z b. 2.5 .2.5.7. C, -. bit op. :< variable >=< variable > op < expr >; < variable > op=< expr >; op=, op , - bit, . :x= x+ (8 b + a); - x (8 b + a). += - :x+ = (8 b + a); . . . C - C - . . .. 39 223 , . , . , - . x= (a+ + , b + +); a=0 b=4, x 4 x= (a+ + , + + b); x 5 (?).2.6. , . : , - . (cast) . . . . C - C - . . .. 40 223 intw= 4, x= 3;floaty;y= (float) w/x; (2.2) 2.2, (float) w . , x , . y 1.333. . .. 2.7 C. . . . C - C - . . .. 41 223 1 long double long double.2 double double.3 oat oat.4 char short int int unsigned int.5 unsigned long unsigned long.6 unsigned int long, long unsigned int long unsigned long.7 long, long.8 unsigned int, unsigned int.9 int. 2.7: C, , -. char int; C . - . . . C - C - . . .. 42 223 char (signed unsigned).2.7. C -:printf()scanf()2.7.1. printf() printf() . stdio.h :intprintf (char format, arg1, arg2, . . .); , - . 7 - C . - ( ). : . . . C - C - . . .. 43 223 . % - 2.8. printf:printf(The result is:%d, v); The result is:%d The result is: v . %d. 3 The result is:3. , v. printf . , . printf :printf(%d %f, v, p); printf v p 2 . 2.8 printf. . . . C - C - . . .. 44 223 d,i u c x a, b, c, d, e, f X a, b, c, d, e, f o f m.dddddd d 6 e, E m.dddddde + / xx m.ddddddE + / xx d 6 g, G 4 %e/%E %fs p 2.8: printf . . . C - C - . . .. 45 223 % , . :1. -, .2. +, .3. , .4. 0, 0. .5. #2.8 : o 0 . x X 0x 0X.e, E f .g G, - .6. -. 0 .7. , . . . . C - C - . . .. 46 223 8. : , , , - 0,, - (e, E f) (g G).9. h short int, l long int L long double.0: #include 1: #include 2:3: #define LOWLET "abcdefghijklmnopqrstuvwxyz"4:5: int main()6: {7: printf("%20.1s\n", LOWLET);8: printf("%#X\n", 30);9: printf("%6.2f\n", 80.756);10:11: return 0;12: } 2.2: printf. . . . C - C - . . .. 47 223 2.2 C printf. 3 - LOWLET . 7 a 20 . , 8 0X1E 30 9 80.75 lite-ral 80.756. \n (newline ) .2.7.2. scanf() scanf() . stdio.h :intscanf (char format, var1, var2, . . .); , - , . 7 C . ( ). scanf:scanf(%d, &v); %d . . . C - C - . . .. 48 223 v. & . & , 7, scanf . -:1. 2. - 3. , % - , , h, l, L 2.9. . . . C - C - . . .. 49 223 d i , 0 0x 0X u c x 0x 0X o 0 f, e, g , , E s 2.9: scanf ( , , ). -, - . 2.3 scanf. , scanf 7 . Arithmos - . . . C - C - . . .. 50 223 5 . Arithmos543.645 scanf y 543.6 - 5 scanf 7. , 45, scanf 9 % f y.0: #include 1: #include 2:3: int main()4: {5: float y;6:7: scanf("Arithmos%5f", &y);8: printf("%f\n",y);9: scanf("%*f",&y);10: printf("%f\n",y);11:12: return 0;13: } 2.3: scanf.2.8. 2.8.1 ; ; . . . C - C - . . .. 51 223 intx, y= 6, z= 8;x= y + +++ + z;printf (%d %d %d\n , x, y, z);x= + + y +z + +;printf (%d %d %d\n , x, y, z);x= + + y ++ + z;printf (%d %d %d\n , x, y, z);x= y +z ;printf (%d %d %d\n , x, y, z); 2.8.2 ;d= (a= 4) (c= 8); ; 2.8.3 . , - .intx= 5, y= 6, z= 8, w= 7, u= 11; x/y/z9+z / w u4 x % y +z 223 / u+12 % zx+ =y =z =4 8 . . . C - C - . . .. 52 223 2.8.4. ;intu= 3;unsigned intv= 3;if (u > 1 == v > 1) printf (Eiserxontaimhdenika kata thn metatopish\n );else printf (To bitproshmou eiserxetaikata thn metatopish\n ); 2.8.5 .intx= 3, w= 6, u= 4, v= 2;floaty= 2.0; x > w && u > vx 1: #include 2:3: int main()4: {5:6: int a, b;7:8: printf("\nGive an Integer Value for a: ");9: scanf("%d", &a);10:11: printf("\n Give an Integer Value for b: ");12: scanf("%d", &b);13:14: if (a == b)15: printf("\n a is equal to b\n");16: if (a > b)17: printf("\n a is greater than b\n");18: if (a < b)19: printf("\n a is smaller than b\n");20:21: return 0;22: } 3.1: if 3.1 if. 8 9 a. , 11 12 , b. 14. a b . . . If swi-tch , . . .. 57 223 . , 16 a b. 18, a b. . - . :if (-1) --1else if (-2) --2else --3-; , a b 3.2. . . . If swi-tch , . . .. 58 223 0: #include 1: #include 2:3: int main()4: {5:6: int a, b;7:8: printf("\nGive an Integer Value for a: ");9: scanf("%d", &a);10:11: printf("\n Give an Integer Value for b: ");12: scanf("%d", &b);13:14: if (a == b)15: printf("\n a is equal to b\n");16: else if (a > b)17: printf("\n a is greater than b\n");18: else printf("\n a is smaller than b\n");19:20: return 0;21: } 3.2: if/else 3.3. switch , , . , 1 . . . If swi-tch , . . .. 59 223 2 if-else. if-else , C switch .0: #include 1: #include 2:3: main()4: {5: int key;6:7: printf("Enter a number between 1 and 5:");8: scanf("%d", &key);9: switch (key)10: {11: case 1: printf("You entered 1.\n");12: case 2: printf("You entered 2.\n");13: case 3: printf("You entered 3.\n");14: case 4: printf("You entered 4.\n");15: case 5: printf("You entered 5.\n");16: default: printf("Out of range, try again.\n");17: }18: return 0;19: } 3.3: switch switch . switch . . . If swi-tch , . . .. 60 223 . switch . switch default . :switch() {case --1: ;case --2: ;. . .default: ;} . - , case . case switch. default , case . , . . . . If swi-tch , . . .. 61 223 0: #include 1: #include 2:3: main()4: {5: int key;6:7: printf("Enter a number between 1 and 5:");8: scanf("%d", &key);9: switch (key)10: {11: case 1: printf("You entered 1.\n");12: break;13: case 2: printf("You entered 2.\n");14: break;15: case 3: printf("You entered 3.\n");16: break;17: case 4: printf("You entered 4.\n");18: break;19: case 5: printf("You entered 5.\n");20: break;21: default:printf("Out of range, try again.\n");22: break;23: }24: return 0;25: } 3.4: switch 3.3 switch . 2? :You entered 2. . . . If swi-tch , . . .. 62 223 You entered 3.You entered 4.You entered 5.Out of range, try again. . break C . 3.4 break C case. 2 printf break switch, switch. , :You entered 2.3.4. , , . . C for, while do-while . . . . If swi-tch , . . .. 63 223 3.4.1. while while . :while () ;while () - . . - , while. . . while while, while. c= getchar();while(c== j c== \nj c== \t) c= getchar(); c= getchar() getchar() C ( stdio.h) . , c getchar().while (c ==jc == \njc == \t)whilec = . . . If swi-tch , . . .. 64 223 getchar(). c , \n \t . , , c while. while . while . whilewhile((c= getchar()) == j c== \nj c== \t); while . . while while.3.4.2. for , for C. :for (_; ; -) ;for (_; ; -) - for . . . . If swi-tch , . . .. 65 223 . - - for. - . for(i =0; i sqrt() pow(). sqrt() x y xy. 13. . . . If swi-tch , . . .. 74 223 : =arctan(yx ) if x > 0 andy 0arctan(yx ) + 2 if x > 0 andy < 0arctan(yx ) + if x < 0 atan2() y, x yx. 14 . 15 - . . . . If swi-tch , . . .. 75 223 3.5.2. (plaintextstring) AttackatDawn f (x) =65 +(5x +4) mod 26 x ASCII (ciphered string). .0: #include 1:2: main()3: {4: char p[]="Attack At Dawn";5: int i;6:7: for(i=0; p[i]!=\0; i++){8: char x;9:10: switch(p[i]){11: case \n:12: case \t:13: case : break;14: default:15: x=65 + (5*p[i]+4) % 26;16: printf("%c", x);17: break;18: }19: }20: printf("\n");21: } 3.10: 3.10 - f (x) x. . . . If swi-tch , . . .. 76 223 2 ASCII . D x 68. , x (5x+4) mod 26 (568+4) mod 26 = 6 G ( 0). ASCII 65 A. . . switch 10 18 ASCII ., for 7 19 switch 10 18. for 8 .3.5.3. bit x n, bits x n x bits . bits n , . . . If swi-tch , . . .. 77 223 n nbits n bits .0: #include 1:2: void main()3: {4: unsigned int x, n, y;5:6: printf("Dvste ton akeraio kai to plhthos tvn thesevn\n");7: scanf("%u %u", &x, &n);8:9: y = (x (sizeof(int)*8 - n));10: printf("%0x %0x\n", x, y);11: }12: 3.11: bit n x x (8 sizeof (int) n) x < n x > (8sizeof (int) n), x < nx > (8sizeof (int) n) . . . . If swi-tch , . . .. 78 223 3.5.4. 2 n-bit - 2, [0. . . 2n1] 2 .0: #include 1:2: #define MASK80xff /*255*/3: #define LEASTMASK 0x014:5:6: void main()7: {8: unsigned char x;9:10: printf("Positive Numbers\t Negative Numbers\n");11: printf("\t (%d) %0x \t\t\t \n", 0, 0);12: for(x=1; x> (i-1)) & LEASTMASK)) i++;17: y = x ^ (MASK8 (5.5, 6.0) :p1.x= 5.5;p1.y= 6.0; p1 p2 :p1 = p2; p2 p1. . , ., - , - . , : . . . , - - . 175 223 struct date {int day, month, year;char d_name[10], m_name[10];}; , :struct date yesterday, today, tomorrow; :struct{int day, month, year;char d_name[10], m_name[10];}yesterday, today, tomorrow; tag . tag . . .struct complex {oat re, im;}; struct tag complex : . . . , - - . 176 223 struct complex x, y; tag typedef . :typedef struct complex {oat re, im;} complex; complex. - :complex x, y; tag :typedef struct{oat re, im;} complex;complex x, y; ? . , (5.0, 6.0) :struct point a = {5.0, 6.0}; :struct date yesterday = {6, 5, 2008, Monday, May}; . . . , - - . 177 223 5.2 int, float, double char. . N N structpoint, :struct point curve[N]; N N . . 2 2 :complex x[2][2]={{{3.0, 4.0}, {-1.0, -4.0}},{{2.0, 5.0}, {-5.0, -4.0}}}; - .6.1. . , . return . . . . , - - . 178 223 point e;e = updatePoint(e);point updatePoint(point d) { return d;} updatePoint e - d. return . - - e updatePoint.0: #include 1:2: typedef struct {3: float x;4: float y;5: } point;6:7: point AddPoint(point a, point b)8: {9: a.x = a.x + b.x;10: a.y = a.y + b.y;11:12: return a;13: }14:15: main(void)16: {17: point p1, p2, target;18:19: p1.x = 3.0;20: p2.x = 4.0;21: p1.y = p2.y = 5.2;22: target = AddPoint(p1, p2);23: printf("The new coordinates are:(%f, %f)\n", target.x, target.y);24: } 6.1: . . . . , - - . 179 223 6.1 . 2 5 point. p1 p2 (3.0, 5.2) (4.0, 5.2) . AddPoint point a . target point ( ) printf 25.6.2. , - , . -:typedef struct{char last_name[20];char rst_name[20], grade;int student_id;} student; student - 20 , . 41 . . . , - - . 180 223 student. - . C, . :student *st; st student. :typedef struct{char last_name[20];char rst_name[20], grade;int student_id;} *student;student st; student . student, . (4 bytes) . st malloc ( ):st = (student) malloc(sizeof(*st));if(st == NULL) {printf(there is no available memory for pointer st);exit(1);} . . . , - - . 181 223 sizeof (st) byte st - *. malloc student. - NULL . ? *st.grade; :*(st.grade); . grade :(*st).grade; C ( ) [ ] , :st grade (st).grade6.3. , - . . . , - - . 182 223 . 64bit . - . C . : .6.3.1. - . - - - . (LIFO(LastInFirstOut)). - pop push. . . . , - - . 183 223 0: #include 1: #include 2:3: #define MAXVAL 1004: #define EMPTY 05: #define FULL MAXVAL6:7: typedef struct{8: int sp;9: double val[MAXVAL];10: } stack;11:12: typedef enum {FALSE, TRUE} bool;13:14: void push(double f, stack *st)15: {16: if (st -> sp < MAXVAL)st->val[st->sp++] = f;17: else printf("error: stack full, cant push %g\n", f);18: }19:20: double pop(stack *st)21: {22: if (st->sp > 0) return st->val[--st->sp];23: else {24: printf("error: stack empty\n");25: return 0.0;26: }27: }28:29: void reset(stack *st)30: {31: st->sp = EMPTY;32: }33:34: bool isEmpty(stack *st)35: {36: if (st->sp == EMPTY) return TRUE;37: return FALSE;38: }39:40: bool isFULL(stack *st)41: {42: if (st->sp == FULL) return TRUE;43: return FALSE;44: } 6.2: Module stack:typedef struct{double val[MAXVAL];int sp;} stack; . . . , - - . 184 223 val , sp . stack push pop . reset isEmpty TRUE isFull TRUE . 6.2 module . module st stack. st ., 12 bool .6.3.2. (stack),(FIFO, FirstInFirstOut), . - . . . . , - - . 185 223 datanextstruct linkedList {int data;struct linkedList *next;} ;struct linkedList x, y;x.data = 5;y.data = 6;x.next = y.next = NULL;5NULL6NULLx yx.next = &y;5 6NULLx y 6.3: . . . , - - . 186 223 :1. (head) ,2. ,3. (tail) . 6.3 taglinkedList : linkedList. -xy linkedList data 5 6 next NULL . x y :x.next= &y; next x y. :1. .2. .3. .4. .5. .6. .7. . . . . , - - . 187 223 - 4.0: #include 1: #include 2:3: typedef struct linkedList {4: int data;5: struct linkedList *next;6: } element;7:8: typedef element *link;9:10: int countList(link head)11: {12: int count=0;13:14: for(; head!=NULL; head=head->next) ++count;15:16: return count;17: }18:19: void printList(link head)20: {21: if (head == NULL) printf("NULL\n");22: else{23: printf("%d ->", head-> data);24: printList(head->next);25: }26: }27:28: void insert(link a1, link b)29: {30: b ->next = a1->next;31: a1->next = b;32: }33:34: void delete(link a1)35: {36: link p;37:38: p = a1->next;39: a1->next = p->next;40:41: free(p);42: } 6.4: Module . . . , - - . 188 223 6.4 module . : element link element. . countList element . head , , ( )? printList -. . . . . , - - . 189 223 6 87 NULL6 87a1ba1b6 7 8a16 7 8a1p = a1->next;a1->next = p->next;Delete OperationInsert Operationb->next = a1->next;a1->next = b;p 6.5: . . . , - - . 190 223 insert a1 b . 6.5 ., delete - a1 . 6.5 . - free.6.4. 6.4.1 . .structstudentintid;charfirstName[20];charfamilyName[20];chargrade;} tmp, w= &tmp;tmp.grade= C;tmp.id= 1040tmp.familyName= Petrou; . . . , - - . 191 223 tmp.gradetmp.familyName(w).idw familyName + 4(w familyName + 3) 6.4.2 - P Q (x1, y1) (x2, y2) . 6.4.3 - :1. .2. : , . : N , N N -, - , . . . , - - . 192 223 . 6.4.4 :typedef struct doubleLList{int data;struct doubleLList *prev;struct doubleLList *next;} DLLElement;NULL NULLhead 6.6: 6.6 - . insert delete . 6.4.5( queue) FIFO(First InFirst Out) 6.7size . . . , - - . 193 223 , front tail .NULLHeadtailsize 6.7: :1. ,2. ,3. 4. TRUE FALSE.5. TRUE FALSE. . . . / / . 194 223 7 . (devices). , . :: .: . : . C (streams). ( byte ). . . . / / . 195 223 (inputstream). (outputstream). . , C byte . . C :1. (text), . 255 .2. (binary), (- ). . , byte ASCII - . , 65 A 65. . . ANSI C stan-dard input / output les: stdout: (default) , - , . . . / / . 196 223 stdin: (default) , - , stderr: (default) , . , - < stdlib.h > : scanf () - stdin. printf () stdout. - .7.1. / / , stdin , stdout. ? . (text stream) (binary stream): 0 255 . . . / / . 197 223 .r . NULL.w . . , .a . . , .r+ . . , .w+ . . , .a+ . . , . 7.1: . C (string). - command line Windows XP . . . / / . 198 223 C:\ tmp\ myle.txt. C - char *lename=C:\\ tmp\\ myle.txt. \ 2 \ - \\. Unix directories /. . fopen() - < stdio.h > . :FILE *fopen(const char *lename, const char *mode) 7.1 . - . b . w+b 7.1. fopen FILE < stdio.h > :typedef struct _iobuf {char *_ptr;int _cnt;char *_base;int _ag;int _le;int _charbuf; . . . / / . 199 223 int _bufsiz;char *_tmpfname;}FILE; . / . - / . fopen() FILE. - fclose :int fclose(FILE *fp);fp . 0 1 . ( main() exit()) . - . , byte , . . (.. , ) , . . . . / / . 200 223 fflush :int ush(FILE *fp) - . EOF stdio.h 1. EOF . , 1 feof () :int feof(FILE *fp);feof 0-.7.2. / . : (getc(), ) (scanf (), ) - . : . . . / / . 201 223 (buered) - , stdin. , . (unbuered) stdin . , . scanf ., : (putc, .) (printf , .) : - . - . stderr . .7.2.1. / . . . . . / / . 202 223 C. / :int fgetc(FILE *fp): fp fp EOF. getchar() fgetc(stdin).int fputc(int ch, FILE *fp): ch fp, EOF . putchar(ch) fputc(ch, stdout). / - :char *fgets(char *buf, int N, FILE *fp): fp buf \n N 1 . N . buf NULL EOF.int fputs(char *buf, FILE *fp): - buf fp. \0 . - \n \0. EOF . . . . / / . 203 223 0: #include 1:2: main(int argc, char *argv[])3: {4: FILE *fp=NULL;5: char c;6:7: fp = fopen(argv[1], "r");8:9: while((c=fgetc(fp)) != EOF) printf("%c", c);10: printf("\\text");11: } 7.1: / / . 7.1 -. readFile Command ( Windows , cmd ) :readFile < filename > . , argcmain 2argv[0] =readFile argv[1] =. 7 argv[1] fp FILE. 9 fp EOF. . . . . / / . 204 223 7.2.2. / . . spreadsheets C. fprintf () printf () :void fprintf(FILE *fp, const char *fmt, . . .); fscanf () scanf () :void fscanf(FILE *fp, const char *fmt, . . .); . . . / / . 205 223 0: #include 1: #include 2:3: main(int argc, char *argv[])4: {5: FILE *fp1, *fp2;6: char a[20], b[20];7:8: if ((fp1 = fopen(argv[1], "r")) == NULL) {9: printf("Error in opening Input File\n");10: exit(1);11: }12:13: fgets(a, 20, fp1);14: fgets(b, 20, fp1);15:16: if ((fp2 = fopen(argv[2], "w")) == NULL) {17: printf("Error in opening Output File\n");18: exit(1);19: }20: fprintf(fp2, "%f = %f + %f\n", atof(a)+atof(b), atof(a), atof(b));21:22: fclose(fp1);23: fclose(fp2);24: }25: 7.2: . 7.2 . addNumbers Command ( Windows , cmd . . . / / . 206 223 ) :addNumbers < InputFile > < OutputFile >. - argcmain 3 argv[0] =addNumbersargv[1] =argv[2] =. .8 11 argv[1]. fp1 NULL. . fgets 13 14 20 a b. 16 19 argv[2] . fp2 NULL. - . , fprintf () 20 fp2, atof () a b . :cat 7.2.3. / . . . . / / . 207 223 C. , - . . - C. fwrite() :int fwrite(void *buf, int size, int count, FILE *fp) fp , buf ,size byte count . . (buf ) 20 :fwrite(buf, sizeof(oat), 20, fp); - fread() :int fread(void *buf, int size, int count, FILE *fp)fp, buf ,size byte count . . . . . / / . 208 223 0: #include 1: #include 2:3: int main()4: {5: int i, x, outBuffer[30], inBuffer[30];6: FILE *fp;7:8: for (i = 0; i < 30; i++)9: outBuffer[i] = 2 * i;10:11: if ((fp = fopen("direct", "w+b")) == NULL)12: {13: fprintf(stderr, "Error opening file.");14: exit(1);15: }16:17: if (fwrite(outBuffer, sizeof(int), 30, fp) != 30)18: {19: fprintf(stderr, "Error writing to file.");20: exit(1);21: }22:23: rewind(fp);24:25: if (fread(inBuffer, sizeof(int), 30, fp) != 30)26: {27: fprintf(stderr, "Error reading file.");28: exit(1);29: }30:31: for (i = 0; i < 30; i++)32: printf("%d\t%d\n", inBuffer[i], outBuffer[i]);33:34: fclose(fp);35: return(0);36: } 7.3: / / . . . . / / . 209 223 30 ai=2 i, iN. 30 fwrite. 7.3 - . 8 9 outBuffer. 11 15 direct (w+b) fopen fp. fp. NULL . fwrite() 17outBuffer fp. 30 , - . FILE . 0. fwrite() 30 sizeof (int) = 30 4= 120byte( int 4byte) . 120 fwrite(). 17 0. rewind() 23 . . . / / . 210 223 rewind() . fread() 25 30 - inBuffer. 30( ) - . , 120 ., 31 32 outBuffer inBuffer .7.3. - 7.3 FILE . , - byte . 0 . / / , byte . - . C . . . / / . 211 223 - . - . . - 150 ( ). , , 150 . . stdio.h :1. int fseek(FILE *fp, long oset, int position): fp. position SEEK_SET,SEEK_CUR SEEK_END, , . offset position. reset 0.2. void rewind(FILE *fp): fp , reset . fseek(fp, 0L, SEEK_SET) 0 byte SEEK_SET.3. long ftell(FILE *fp): -. , fseek . . . / / . 212 223 . MS DOS UNIX mode .0: #include 1: #include 2:3: main(int argc, char *argv[])4: {5: FILE *fp=NULL;6: char c;7:8: fp = fopen(argv[1], "rb");9: fseek(fp, 0, SEEK_END);10: fseek(fp, -1, SEEK_CUR);11:12: while(ftell(fp) > 0){13: c=getc(fp);14: putchar(c);15: fseek(fp, -2, SEEK_CUR);16: }17: return 0;18: } 7.4: . . . 7.4 reverseRead.c. . . . / / . 213 223 , Command (Windows , cmd - ) :reverseRead , main :argv[0]=reverseRead argv[1]=. ( 8) . - fseek 9, . fseek 10 byte . while, ftell . FALSE while . while byte . fseek 15 - 2 byte byte . while ftell() 0 .7.4. createRand : . . . / / . 214 223 createRand o filename s seed nN N 0. . . RAND_MAX filename. seed . - 7 argv main. , :argv[0]=createRandargv[1]=-oargv[2]=lenameargv[3]=-sargv[4]=seedargv[5]=-nargv[6]=N 66. 6 argv[2] . 7.5 . . . . / / . 215 223 0: #include 1: #include 2:3: void createRandNum(int, int *, int);4:5: int main(int argc, char *argv[])6: {7: FILE *fp=NULL;8: int *randNum, size, seed, i;9:10: if (--argc == 6) {11:12: //Opens the file as binary in read/write mode13: fp = fopen(argv[2], "w+b");14:15: //Converts strings argv[6] and argv[4] into size seed respectively.16: size = atoi(argv[6]);17: seed = atoi(argv[4]);18:19: //Dynamic Array of size Elements20: randNum = (int *) malloc(size * sizeof(int));21:22: //Random Number Generation23: createRandNum(seed, randNum, size);24:25: //Writes Dynamic Array into file fp26: fwrite(randNum, sizeof(int), size, fp);27: }28: else {29: printf("The syntax of the command:");30: printf("createRand -o filename -s -n number_of_Rands\n");31: return -1;32: }33: fclose(fp);34:35: return 0;36: }37:38: void createRandNum(int seed, int *randArray, int N)39: {40: int i;41:42: srand(seed);43: for(i=0; i 10 double : N int , N double. 10 f (x) = 3x2+ 6. . C , . 218 223 8[KR] C Brian W. Kernighan, Dennis M. Ritchie, , .[ER] C, Eric S. Roberts, .[KP] A Book On C, Programming in C Fourth Edition, Al Kelley and Ira Pohl, AddisonWesley.[DPV] Algorithms, Sanjoy Dasgupta, Christos Papadimitriou and Umesh Vazirani, McGraw Hill. C , . 219 223 . . . , - & C . . .. 220 223 : 1 < k < n n%k== 0 n .2 4.8.2 . . . / / . 221 223 : fread fwrite. 2 7.5.4 . . . / / . 222 223 : 10 -(rand()%N)+ 1, srand C. 1. . . N fseek . 2 7.5.5