You are on page 1of 143

www.nhipsongcongnghe.

net

Gii thiu Tin hc l mt ngnh khoa hc mi nhn pht trin ht sc nhanh chng trong vi chc nm li y v ngy cng m rng lnh vc nghin cu, ng dng trong mi mt ca i sng x hi. Ngn ng lp trnh l mt loi cng c gip con ngi th hin cc vn ca thc t ln my tnh mt cch hu hiu. Vi s pht trin ca tin hc, cc ngn ng lp trnh cng dn tin ho p ng cc thch thc mi ca thc t. Khong cui nhng nm 1960 u 1970 xut hin nhu cu cn c cc ngn ng bc cao h tr cho nhng nh tin hc trong vic xy dng cc phn mm h thng, h iu hnh. Ngn ng C ra i t , n c pht trin ti phng th nghim Bell. n nm 1978, gio trnh " Ngn ng lp trnh C " do chnh cc tc gi ca ngn ng l Dennish Ritchie v B.W. Kernighan vit, c xut bn v ph bin rng ri. C l ngn ng lp trnh vn nng. Ngoi vic C c dng vit h iu hnh UNIX, ngi ta nhanh chng nhn ra sc mnh ca C trong vic x l cho cc vn hin i ca tin hc. C khng gn vi bt k mt h iu hnh hay my no, v mc du n c gi l " ngn ng lp trnh h thng" v n c dng cho vic vit h iu hnh, n cng tin li cho c vic vit cc chng trnh x l s, x l vn bn v c s d liu. V by gi chng ta i tm hiu th gii ca ngn ng C t nhng khi nim ban u c bn nht. H ni thng 11 nm 1997 Nguy n H u Tu n

Chng 1 cc khi nim c bn 1.1. Tp k t dng trong ngn ng C : Mi ngn ng lp trnh u c xy dng t mt b k t no . Cc k t c nhm li theo nhiu cch khc nhau to nn cc t. Cc t li c lin kt vi nhau theo mt qui tc no to nn cc cu lnh. Mt chng trnh bao gm nhiu cu lnh v th hin mt thut ton gii mt bi ton no . Ngn ng C c xy dng trn b k t sau : 26 ch ci hoa : A B C .. Z 26 ch ci thng : a b c .. z 10 ch s : 0 1 2 .. 9 Cc k hiu ton hc : + - * / = ( ) K t gch ni : _ Cc k t khc : . , : ; [ ] {} ! \ & % # $ ... Du cch (space) dng tch cc t. V d ch VIET NAM c 8 k t, cn VIETNAM ch c 7 k t. Ch : Khi vit chng trnh, ta khng c s dng bt k k t no khc ngoi cc k t trn. V d nh khi lp chng trnh gii phng trnh bc hai ax2 +bx+c=0 , ta cn tnh bit thc Delta = b2 - 4ac, trong ngn ng C khng cho php dng k t , v vy ta phi dng k hiu khc thay th. 1.2. T kho : T kho l nhng t c s dng khai bo cc kiu d liu, vit cc ton t v cc cu lnh. Bng di y lit k cc t kho ca TURBO C : asm char do extern goto interrupt register break const double far huge long return case continue else float if near short cdecl default enum for int pascal signed

sizeof tipedef volatile

static union while

struct unsigned

switch void

ngha v cch s dng ca mi t kho s c cp sau ny, y ta cn ch : - Khng c dng cc t kho t tn cho cc hng, bin, mng, hm ... - T kho phi c vit bng ch thng, v d : vit t kho khai bo kiu nguyn l int ch khng phi l INT. 1.3. Tn : Tn l mt khi nim rt quan trng, n dng xc nh cc i lng khc nhau trong mt chng trnh. Chng ta c tn hng, tn bin, tn mng, tn hm, tn con tr, tn tp, tn cu trc, tn nhn,... Tn c t theo qui tc sau : Tn l mt dy cc k t bao gm ch ci, s v gch ni. K t u tin ca tn phi l ch hoc gch ni. Tn khng c trng vi kho. di cc i ca tn theo mc nh l 32 v c th c t li l mt trong cc gi tr t 1 ti 32 nh chc nng : Option-Compiler-SourceIdentifier length khi dng TURBO C. V d : Cc tn ng : a_1 Cc tn sai : 3MN m#2 f(x) do te ta Y-3 Ch : Trong TURBO C, tn bng ch thng v ch hoa l khc nhau v d tn AB khc vi ab. trong C, ta thng dng ch hoa t tn cho cc hng v dng ch thng t tn cho hu ht cho cc i lng khc nh bin, bin mng, hm, cu trc. Tuy nhin y khng phi l iu bt buc. K t u tin l s S dng k t # S dng cc du ( ) Trng vi t kho S dng du trng S dng du delta x1 _step GAMA

1.4. Kiu d liu : Trong C s dng cc cc kiu d liu sau : 1.4.1. Kiu k t (char) : Mt gi tr kiu char chim 1 byte ( 8 bit ) v biu din c mt k t thng qua bng m ASCII. V d : K t 0 1 2 A B a b M ASCII 048 049 050 065 066 097 098

C hai kiu d liu char : kiu signed char v unsigned char. Kiu Char ( Signed char ) Unsigned char Phm vi biu din -128 n 127 0 n 255 S k t 256 256 Kch thc 1 byte 1 byte

V d sau minh ho s khc nhau gia hai kiu d liu trn : Xt on chng trnh sau : char ch1; unsigned char ch2; ...... ch1=200; ch2=200; Khi thc cht : ch1=-56; ch2=200; Nhng c ch1 v ch2 u biu din cng mt k t c m 200. Phn loi k t : C th chia 256 k t lm ba nhm :

Nhm 1: Nhm cc k t iu khin c m t 0 n 31. Chng hn k t m 13 dng chuyn con tr v u dng, k t 10 chuyn con tr xung dng di ( trn cng mt ct ). Cc k t nhm ny ni chung khng hin th ra mn hnh. Nhm 2 : Nhm cc k t vn bn c m t 32 n 126. Cc k t ny c th c a ra mn hnh hoc my in. Nhm 3 : Nhm cc k t ho c m s t 127 n 255. Cc k t ny c th a ra mn hnh nhng khng in ra c ( bng cc lnh DOS ). 1.4.2. Kiu nguyn : Trong C cho php s dng s nguyn kiu int, s nguyn di kiu long v s nguyn khng du kiu unsigned. Kch c v phm vi biu din ca chng c ch ra trong bng di y : Kiu int unsigned int long unsigned long Ch : Kiu k t cng c th xem l mt dng ca kiu nguyn. 1.4.3. Kiu du phy ng : Trong C cho php s dng ba loi d liu du phy ng, l float, double v long double. Kch c v phm vi biu din ca chng c ch ra trong bng di y : Kiu Float Double long double Gii thch : My tnh c th lu tr c cc s kiu float c gi tr tuyt i t 3.4E-38 n 3.4E+38. Cc s c gi tr tuyt i nh hn3.4E-38 c xem bng 0. Phm vi biu din ca s double c hiu theo ngha tng t. Phm vi biu din 3.4E-38 n 3.4E+38 1.7E-308 n 1.7E+308 3.4E-4932 n 1.1E4932 S ch s c ngha 7 n 8 15 n 16 17 n 18 4 byte 8 byte 10 byte Kch thc Phm vi biu din -32768 n 32767 0 n 65535 -2147483648 n 2147483647 0 n 4294967295 Kch thc 2 byte 2 byte 4 byte 4 byte

1.5. nh ngha kiu bng TYPEDEF : 1.5.1. Cng dng : T kho typedef dng t tn cho mt kiu d liu. Tn kiu s c dng khai bo d liu sau ny. Nn chn tn kiu ngn v gn d nh. Ch cn thm t kho typedef vo trc mt khai bo ta s nhn c mt tn kiu d liu v c th dng tn ny khai bo cc bin, mng, cu trc, vv... 1.5.2. Cch vit : Vit t kho typedef, sau kiu d liu ( mt trong cc kiu trn ), ri n tn ca kiu. V d cu lnh : typedef int nguyen; s t tn mt kiu int l nguyen. Sau ny ta c th dng kiu nguyen khai bo cc bin, cc mng int nh v d sau ; nguyen x,y,a[10],b[20][30]; Tng t cho cc cu lnh : typedef float mt50[50]; t tn mt kiu mng thc mt chiu c 50 phn t tn l mt50. typedef int m_20_30[20][30]; t tn mt kiu mng thc hai chiu c 20x30 phn t tn l m_20_30. Sau ny ta s dng cc kiu trn khai bo : mt50 a,b; m_20_30 x,y; 1.6. Hng : Hng l cc i lng m gi tr ca n khng thay i trong qu trnh tnh ton. 1.6.1. Tn hng : Nguyn tc t tn hng ta xem xt trong mc 1.3. t tn mt hng, ta dng dng lnh sau : #define tn hng gi tr V d : #define MAX 1000

Lc ny, tt c cc tn MAX trong chng trnh xut hin sau ny u c thay bng 1000. V vy, ta thng gi MAX l tn hng, n biu din s 1000. Mt v d khc : #define pi 3.141593 t tn cho mt hng float l pi c gi tr l 3.141593. 1.6.2. Cc loi hng : 1.6.2.1. Hng int : Hng int l s nguyn c gi tr trong khong t -32768 n 32767. V d : #define number1 -50 #define sodem 2732 Ch : Cn phn bit hai hng 5056 v 5056.0 : y 5056 l s nguyn cn 5056.0 l hng thc. 1.6.2.2. Hng long : Hng long l s nguyn c gi tr trong khong t -2147483648 n 2147483647. Hng long c vit theo cch : 1234L hoc 1234l ( thm L hoc l vo ui ) Mt s nguyn vt ra ngoi min xc nh ca int cng c xem l long. V d : #define sl 8865056L #define sl 8865056 1.6.2.3. Hng int h 8 : Hng int h 8 c vit theo cch 0c1c2c3.... y ci l mt s nguyn dng trong khong t 1 n 7. Hng int h 8 lun lun nhn gi tr dng. nh nghi hng long sl c gi tr l 8865056 nh nghi hng long sl c gi tr l 8865056 nh nghi hng int number1 c gi tr l -50 nh nghi hng int sodem c gi tr l 2732

V d : #define h8 0345 nh nghi hng int h 8 c gi tr l 3*8*8+4*8+5=229 1.6.2.4. Hng int h 16 : Trong h ny ta s dng 16 k t : 0,1..,9,A,B,C,D,E,F. Cch vit a hoc A b hoc B c hoc C d hoc D e hoc E f hoc F Gi tr 10 11 12 13 14 15

Hng s h 16 c dng 0xc1c2c3... hc 0Xc1c2c3... y ci l mt s trong h 16. V d : #define h16 0xa5 #define h16 0xA5 #define h16 0Xa5 #define h16 0XA5 Cho ta cc hng s h16 trong h 16 c gi tr nh nhau. Gi tr ca chng trong h 10 l : 10*16+5=165. 1.6.2.5. Hng k t : Hng k t l mt k t ring bit c vit trong hai du nhy n, v d 'a'. Gi tr ca 'a' chnh l m ASCII ca ch a. Nh vy gi tr ca 'a' l 97. Hng k t c th tham gia vo cc php ton nh mi s nguyn khc. V d : '9'-'0'=57-48=9 V d : #define kt 'a' nh nghi hng k t kt c gi tr l 97

Hng k t cn c th c vit theo cch sau : ' \c1c2c3' trong c1c2c3 l mt s h 8 m gi tr ca n bng m ASCII ca k t cn biu din. V d : ch a c m h 10 l 97, i ra h 8 l 0141. Vy hng k t 'a' c th vit di dng '\141'. i vi mt vi hng k t c bit ta cn s dng cch vit sau ( thm du \ ) : Cch vit '\'' '\"' '\\' '\n' '\0' '\t' '\b' '\r' '\f' Ch : Cn phn bit hng k t '0' v '\0'. Hng '0' ng vi ch s 0 c m ASCII l 48, cn hng '\0' ng vi kt \0 ( thng gi l k t null ) c m ASCII l 0. Hng k t thc s l mt s nguyn, v vy c th dng cc s nguyn h 10 biu din cc k t, v d lnh printf("%c%c",65,66) s in ra AB. 1.6.2.5. Hng xu k t : Hng xu k t l mt dy k t bt k t trong hai du nhy kp. V d : #define xau1 "Ha noi" #define xau2 "My name is Giang" Xu k t c lu tr trong my di dng mt bng c cc phn t l cc k t ring bit. Trnh bin dch t ng thm k t null \0 vo cui mi xu ( k t \0 c xem l du hiu kt thc ca mt xu k t ). Ch : K t ' " \ \n (chuyn dng ) \0 ( null ) Tab Backspace CR ( v u dng ) LF ( sang trang )

Cn phn bit hai hng 'a' v "a". 'a' l hng k t c lu tr trong 1 byte, cn "a" l hng xu k t c lu tr trong 1 mng hai phn t : phn t th nht cha ch a cn phn t th hai cha \0. 1.7. Bin : Mi bin cn phi c khai bo trc khi a vo s dng. Vic khai bo bin c thc hin theo mu sau : Kiu d liu ca bin V d : int a,b,c; long dai,mn; char kt1,kt2; float x,y double canh1, canh2; Khai bo ba bin int l a,b,c Khai bo hai bin long l dai v mn Khai bo hai bin k t l kt1 v kt2 Khai bo hai bin float l x v y Khai bo hai bin double l canh1 v canh2 tn bin ;

Bin kiu int ch nhn c cc gi tr kiu int. Cc bin khc cng c ngha tng t. Cc bin kiu char ch cha c mt k t. lu tr c mt xu k t cn s dng mt mng kiu char. V tr ca khai bo bin : Cc khai bo cn phi c t ngay sau du { u tin ca thn hm v cn ng trc mi cu lnh khc. Sau y l mt v d v khai bo bin sai : ( Khi nim v hm v cu trc chng trnh s nghin cu sau ny) main() { int a,b,c; a=2; int d; /* V tr ca khai bo sai */ ..... } Khi u cho bin :

10

Nu trong khai bo ngay sau tn bin ta t du = v mt gi tr no th y chnh l cch va khai bo va khi u cho bin. V d : int a,b=20,c,d=40; float e=-55.2,x=27.23,y,z,t=18.98; Vic khi u v vic khai bo bin ri gn gi tr cho n sau ny l hon ton tng ng. Ly a ch ca bin : Mi bin c cp pht mt vng nh gm mt s byte lin tip. S hiu ca byte u chnh l a ch ca bin. a ch ca bin s c s dng trong mt s hm ta s nghin cu sau ny ( v d nh hm scanf ). ly a ch ca mt bin ta s dng php ton : & tn bin 1.8 Mng : Mi bin ch c th biu din mt gi tr. biu din mt dy s hay mt bng s ta c th dng nhiu bin nhng cch ny khng thun li. Trong trng hp ny ta c khi nim v mng. Khi nim v mng trong ngn ng C cng ging nh khi nim v ma trn trong i s tuyn tnh. Mng c th c hiu l mt tp hp nhiu phn t c cng mt kiu gi tr v chung mt tn. Mi phn t mng biu din c mt gi tr. C bao nhiu kiu bin th c by nhiu kiu mng. Mng cn c khai bo nh r : Loi mng : int, float, double... Tn mng. S chiu v kch thc mi chiu. Khi nim v kiu mng v tn mng cng ging nh khi nim v kiu bin v tn bin. Ta s gii thch khi nim v s chiu v kch thc mi chiu thng qua cc v d c th di y. Cc khai bo : int a[10],b[4][2]; float x[5],y[3][3]; s xc nh 4 mng v ngha ca chng nh sau :

11

Th t 1 2

Tn mng A B

Kiu mng Int Int

S chiu 1 2

Kch thc 10 4x2

Cc phn t a[0],a[1],a[2]...a[9] b[0][0], b[0][1] b[1][0], b[1][1] b[2][0], b[2][1] b[3][0], b[3][1]

3 4

X Y

Float Float

1 2

5 3x3

x[0],x[1],x[2]...x[4] y[0][0], y[0][1], y[0][2] y[1][0], y[1][1], y[1][2] y[2][0], y[2][1], y[1][2]

Ch : Cc phn t ca mng c cp pht cc khong nh lin tip nhau trong b nh. Ni cch khc, cc phn t ca mng c a ch lin tip nhau. Trong b nh, cc phn t ca mng hai chiu c sp xp theo hng. Ch s mng : Mt phn t c th ca mng c xc nh nh cc ch s ca n. Ch s ca mng phi c gi tr int khng vt qu kch thc tng ng. S ch s phi bng s chiu ca mng. Gi s z,b,x,y c khai bo nh trn, v gi s i,j l cc bin nguyn trong i=2, j=1. Khi : a[j+i-1] b[j+i][2-i] y[i][j] Ch : Mng c bao nhiu chiu th ta phi vit n c by nhiu ch s. V th nu ta vit nh sau s l sai : y[i] ( V y l mng 2 chiu ) vv.. Biu thc dng lm ch s c th thc. Khi phn nguyn ca biu thc thc s l ch s mng. V d : a[2.5] l a[2] l l l a[2] b[3][0] y[2][1]

12

b[1.9] l a[1] * Khi ch s vt ra ngoi kch thc mng, my s vn khng bo li, nhng n s truy cp n mt vng nh bn ngoi mng v c th lm ri lon chng trnh. Ly a ch mt phn t ca mng : C mt vi hn ch trn cc mng hai chiu. Chng hn c th ly a ch ca cc phn t ca mng mt chiu, nhng ni chung khng cho php ly a ch ca phn t ca mng hai chiu. Nh vy my s chp nhn php tnh : &a[i] nhng khng chp nhn php tnh &y[i][j]. a ch u ca mt mng : Tn mng biu th a ch u ca mng. Nh vy ta c th dng a thay cho &a[0]. Khi u cho bin mng : Cc bin mng khai bo bn trong thn ca mt hm ( k c hm main() ) gi l bin mng cc b. Mun khi u cho mt mng cc b ta s dng ton t gn trong thn hm. Cc bin mng khai bo bn ngoi thn ca mt hm gi l bin mng ngoi. khi u cho bin mng ngoi ta p dng cc qui tc sau : Cc bin mng ngoi c th khi u ( mt ln ) vo lc dch chng trnh bng cch s dng cc biu thc hng. Nu khng c khi u my s gn cho chng gi tr 0. V d : .... float y[6]={3.2,0,5.1,23,0,42}; int z[3][2]={ {25,31}, {12,13}, {45,15} { .... main() { ....

13

} Khi khi u mng ngoi c th khng cn ch ra kch thc ( s phn t ) ca n. Khi , my s dnh cho mng mt khong nh thu nhn danh sch gi tr khi u. V d : .... float a[]={0,5.1,23,0,42}; int m[][3]={ {25,31,4}, {12,13,89}, {45,15,22} }; Khi ch ra kch thc ca mng, th kch thc ny cn khng nh hn kch thc ca b khi u. V d : .... float m[6]={0,5.1,23,0}; int z[6][3]={ {25,31,3}, {12,13,22}, {45,15,11} }; .... i vi mng hai chiu, c th khi u vi s gi tr khi u ca mi hng c th khc nhau : V d : .... float z[][3]={ {31.5}, {12,13}, {-45.76} };

14

int z[13][2]={ {31.11}, {12}, {45.14,15.09} }; Khi u ca mt mng char c th l Mt danh sch cc hng k t. Mt hng xu k t. V d : char ten[]={'h','a','g'} char ho[]='tran' char dem[10] ="van"

15

Chng 2 Cc lnh vo ra Chng ny gii thiu th vin vo/ra chun l mt tp cc hm c thit k cung cp h thng vo/ra chun cho cc chng trnh C. Chng ta s khng m t ton b th vin vo ra y m ch quan tm nhiu hn n vic nu ra nhng iu c bn nht vit chng trnh C tng tc vi mi trng v h iu hnh.

2.1. Thm nhp vo th vin chun : Mi tp gc c tham tr ti hm th vin chun u phi cha dng : #include <conio.h> cho cc hm getch(), putch(), clrscr(), gotoxy() ... #include <stdio.h> cho cc hm khc nh gets(), fflus(), fwrite(), scanf()... gn ch bt u chng trnh. Tp stdio.h nh ngha cc macro v bin cng cc hm dng trong th vin vo/ra. Dng du ngoc < v > thay cho cc du nhy thng thng ch th cho trnh bin dch tm kim tp trong danh mc cha thng tin tiu chun. 2.2. Cc hm vo ra chun - getchar() v putchar() - getch() v putch() : 2.2.1. Hm getchar () : C ch vo n gin nht l c tng k t t thit b vo chun, ni chung l bn phm v mn hnh ca ngi s dng, bng hm getchar(). Cch dng : Dng cu lnh sau : bin = getchar(); Cng dng : Nhn mt k t vo t bn phm v khng a ra mn hnh. Hm s tr v k t nhn c v lu vo bin. V d : int c; c = getchar()

16

2.2.2. Hm putchar () : a mt k t ra thit b ra chun, ni chung l mn hnh, ta s dng hm putchar() Cch dng : Dng cu lnh sau : putchar(ch); Cng dng : a k t ch ln mn hnh ti v tr hin ti ca con tr. K t s c hin th vi mu trng. V d : int c; c = getchar(); putchar(c); 2.2.3. Hm getch() : Hm nhn mt k t t b m bn phm, khng cho hin ln mn hnh. Cch dng : Dng cu lnh sau : getch(); Cng dng : Nu c sn k t trong b m bn phm th hm s nhn mt k t trong . Nu b m rng, my s tm dng. Khi g mt k t th hm nhn ngay k t ( khng cn bm thm phm Enter nh trong cc hm nhp khc ). K t va g khng hin ln mn hnh. Nu dng : bin=getch(); Th bin s cha k t c vo.

17

V d : c = getch(); 2..2.4. Hm putch() : Cch dng : Dng cu lnh sau : putch(ch); Cng dng : a k t ch ln mn hnh ti v tr hin ti ca con tr. K t s c hin th theo mu xc nh trong hm textcolor. Hm cng tr v k t c hin th. 2.3. a kt qu ln mn hnh - hm printf : Cch dng : prinf(iu khin, i s 1, i s 2, ...); Hm printf chuyn, to khun dng v in cc i ca n ra thit b ra chun di s iu khin ca xu iu khin. Xu iu khin cha hai kiu i tng : cc k t thng thng, chng s c a ra trc tip thit b ra, v cc c t chuyn dng, mi c t s to ra vic i dng v in i tip sau ca printf. Chui iu khin c th c cc k t iu khin : \n \f \b \t sang dng mi sang trang mi li li mt bc du tab

Dng tng qut ca c t : %[-][fw][.pp]k t chuyn dng Mi c t chuyn dng u c a vo bng k t % v kt thc bi mt k t chuyn dng. Gia % v k t chuyn dng c th c :

18

Du tr : Khi khng c du tr th kt qu ra c dn v bn phi nu di thc t ca kt qu ra nh hn rng ti thiu fw dnh cho n. Cc v tr d tha s bn tri s c lp y bng cc s 0. Khi c du tr th kt qu c dn v bn tri v cc v tr d tha v bn phi ( nu c ) lun c lp y bng cc khong trng. fw : Khi fw ln hn di thc t ca kt qu ra th cc v tr d tha s c lp y bi cc khong trng hoc s 0 v ni dung ca kt qu ra s c y v bn tri. Khi khng c fw hoc fw nh hn hay bng di thc t ca kt qu ra th rng trn thit b ra dnh cho kt qu s bng chnh di ca n. Ti v tr ca fw ta c th t du *, khi fw c xc nh bi gi tr ca i tng ng. V d : Kt qu ra -2503 -2503 -2503 -2503 "abcdef" "abcdef" "abcdef" pp : Tham s pp ch c s dng khi i tng ng l mt xu k t hoc mt gi tr kiu float hay double. Trong trng hp i tng ng c gi tr kiu float hay double th pp l chnh xc ca trng ra. Ni mt cch c th hn gi tr in ra s c pp ch s phn. Khi vng mt pp th chnh xc s c xem l 6. Khi i l xu k t : sau s thp fw 8 08 8 08 8 08 08 Du c c khng khng khng c khng -2503 -2503 -2503 000-2503 abcdef abcdef abcdef Kt qu a ra nguyn bn phi hoc c lp y bng cc khong trng. Ring i vi cc trng s, nu dy s fw bt u bng s 0 th cc v tr d tha

19

Nu pp nh hn di ca xu th ch pp k t u tin ca xu c in ra. Nu khng c pp hoc nu pp ln hn hay bng di ca xu th c xu k t V d : Kt qu ra -435.645 -435.645 -435.645 "alphabeta" "alphabeta" "alpha" fw 10 10 8 8 vng 8 pp 2 0 vng 3 vng 6 Du c c c vng vng c Kt qu a ra -435.65 -436 -435.645000 alp alphabeta alpha di trng ra 7 4 11 3 9 5 s c in ra.

Cc k t chuyn dng v ngha ca n : K t chuyn dng l mt hoc mt dy k hiu xc nh quy tc chuyn dng v dng in ra ca i tng ng. Nh vy s c tnh trng cng mt s s c in ra theo cc dng khc nhau. Cn phi s dng cc k t chuyn dng theo ng qui tc nh sn. Bng sau cho cc thng tin v cc k t chuyn dng. K t chuyn dng d o x u c s ngha i c chuyn sang s nguyn h thp phn i c chuyn sang h tm khng du ( khng c s 0 ng trc ) i c chuyn sang h mi su khng du ( khng c 0x ng trc ) i c chuyn sang h thp phn khng du i c coi l mt k t ring bit i l xu k t, cc k t trong xu c in cho ti khi gp k t khng hoc cho ti khi s lng k t c xc nh bi cc c t v chnh xc pp. e f i c xem l float hoc double v c chuyn sang dng thp phn c dng [-]m.n..nE[+ hoc -] vi di ca xu cha n l pp. i c xem l float hoc double v c chuyn sang dng thp phn c dng [-]m..m.n..n vi di ca xu cha n l pp. chnh xc mc nh l 6. Lu rng chnh xc khng xc nh ra s cc

20

ch s c ngha phi in theo khun dng f. g Dng %e hoc %f, tu theo loi no ngn hn, khng in cc s 0 v ngha. Ch : Mi dy k t khng bt u bng % hoc khng kt thc bng k t chuyn dng u c xem l k t hin th. hin th cc k t c bit : Cch vit \' \" \\ Cc v d : 1 2 printf("\" Nang suat tang : %d % \" \n\\d"",30,-50); n=8 float x=25.5, y=-47.335 printf("\n%f\n%*.2f",x,n,y); Lnh ny tng ng vi printf("\n%f\n%8.2f",x,n,y); V n=8 tng ng vi v tr * 2.4. Vo s liu t bn phm - hm scanf : Hm scanf l hm c thng tin t thit b vo chun ( bn phm ), chuyn dch chng ( thnh s nguyn, s thc, k t vv.. ) ri lu tr n vo b nh theo cc a ch xc nh. Cch dng : scanf(iu khin,i 1, i 2, ...); Xu iu khin cha cc c t chuyn dng, mi c t s to ra vic i dng bin tip sau ca scanf. c t c th vit mt cch tng qut nh sau : %[*][d...d]k t chuyn dng "Nang suat tang ; 30 %" \d=-50 25.500000 -47.34 Hin th ' " \

21

Vic c mt ca du * ni ln rng trng vo vn c d c bnh thng, nhng gi tr ca n b b qua ( khng c lu vo b nh ). Nh vy c t cha du * s khng c i tng ng. d...d l mt dy s xc nh chiu di cc i ca trng vo, ngha ca n c gii thch nh sau : Nu tham s d...d vng mt hoc nu gi tr ca n ln hn hay bng di ca trng vo tng ng th ton b trng vo s c c, ni dung ca n c dch v c gn cho a ch tng ng ( nu khng c du * ). Nu gi tr ca d...d nh hn di ca trng vo th ch phn u ca trng c kch c bng d...d c c v gn cho a ch ca bin tng ng. Phn cn li ca trng s c xem xt bi cc c t v i tng ng tip theo. V d : int a; float x,y; char ch[6],ct[6] scanf("%f%5f%3d%3s%s",&x&y&a&ch&ct0; Vi dng vo : 54.32e-1 25 12452348a Kt qu l lnh scanf s gn 5.432 cho x 25.0 cho y 124 cho a xu "523" v du kt thc \0 cho ch xu "48a" v du kt thc \0 cho ct K t chuyn dng : K t chuyn dng xc nh cch thc d c cc k t trn dng vo cng nh cch chuyn dch thng tin c c trc khi gn n cho cc a ch tng ng. Cch d c th nht l c theo trng vo, khi cc khong trng b b qua. Cch ny p dng cho hu ht cc trng hp. Cch d c th hai l c theo k t, khi cc khong trng cng c xem xt bnh ng nh cc k t khc. Phng php ny ch xy ra khi ta s dng mt trong ba k t chuyn dng sau : C, [ dy k t ], [^ dy k t ]

22

Cc k t chuyn dng v ngha ca n : c d ld o lo x lx f hay e lf hay le s Vo mt k t, i tng ng l con tr k t. C xt k t khong trng Vo mt gi tr kiu int, i tng ng l con tr kiu int. Trng phi vo l s nguyn Vo mt gi tr kiu long, i tng ng l con tr kiu long. Trng phi vo l s nguyn Vo mt gi tr kiu int h 8, i tng ng l con tr kiu int. Trng phi vo l s nguyn h 8 Vo mt gi tr kiu long h 8, i tng ng l con tr kiu long. Trng phi vo l s nguyn h 8 Vo mt gi tr kiu int h 16, i tng ng l con tr kiu int. Trng phi vo l s nguyn h 16 Vo mt gi tr kiu long h 16, i tng ng l con tr kiu long. Trng phi vo l s nguyn h 16 Vo mt gi tr kiu float, i tng ng l con tr float, trng vo phi l s du phy ng Vo mt gi tr kiu double, i tng ng l con tr double, trng vo phi l s du phy ng Vo mt gi tr kiu double, i tng ng l con tr kiu char, trng vo phi l dy k t bt k khng cha cc du cch v cc du xung dng [ Dy k t ], [ ^Dy k t ] Cc k t trn dng vo s ln lt c c cho n khi no gp mt k t khng thuc tp cc k t t trong[]. i tng ng l con tr kiu char. Trng vo l dy k t bt k ( khong trng c xem nh mt k t ). V d : int a,b; char ch[10], ck[10]; scanf("%d%[0123456789]%[^0123456789]%3d",&a,ch,ck,&b); Vi dng vo : 35 13145 xyz 584235 S gn : 35 cho a xu "13145" cho ch

23

xu "xyz' cho ck 584 cho b Ch : Xt on chng trnh dng nhp ( t bn phm ) ba gi tr nguyn ri gn cho ba bin a,b,c nh sau : int a,b,c; scanf("%d%d%d,&a,&b,&c); vo s liu ta c th thao tc theo nhiu cch khc nhau: Cch 1 : a ba s vo cng mt dng, cc s phn cch nhau bng du cch hoc du tab. Cch 2 : a ba s vo ba dng khc nhau. Cch 3 : Hai s u cng mt dng ( cch nahu bi du cch ho tab ), s th ba trn dng tip theo. Cch 4 : S th nht trn mt dng, hai s sau cng mt dng tip theo ( cch nahu bi du cch ho tab ), s th ba trn dng tip theo. Khi vo sai s bo li v nhy v chng trnh cha li gi n. 2.5. a kt qu ra my in : a kt qu ra my in ta dng hm chun fprintf c dng sau : fprintf(stdprn, iu khin, bin 1, bin 2,...); Tham s stdprn xc nh thit b a ra l my in. iu khin c dng c t nh lnh printf. Dng ging nh lnh printf, ch khc l in ra my in. V d : on chng trnh in ma trn A, c 8x6. Mi hng ca ma trn c in trn mt dng : float a[8][6]; int i,j; fprintf(stdprn,"\n%20c MA TRAN A\n\n\n",' '); for (i=0;i<8;++i) { for (j=0;j<6;++j)

24

fprintf(stdprn,"%10.2f",a[i][j]); fprintf(stdprn,"\n"); }

25

Chng 3 Biu thc Ton hng c th xem l mt i lng c mt gi tr no . Ton hng bao gm hng, bin, phn t mng v hm. Biu thc lp nn t cc ton hng v cc php tnh to nn nhng gi tr mi. Biu thc dng din t mt cng thc, mt qui trnh tnh ton, v vy n l mt thnh phn khng th thiu trong chng trnh. 3.1. Biu thc : Biu thc l mt s kt hp gia cc php ton v cc ton hng din t mt cng thc ton hc no . Mi biu thc c s c mt gi tr. Nh vy hng, bin, phn t mng v hm cng c xem l biu thc. Trong C, ta c hai khi nim v biu thc : Biu thc gn. Biu thc iu kin . Biu thc c phn loi theo kiu gi tr : nguyn v thc. Trong cc mnh logic, biu thc c phn thnh ng ( gi tr khc 0 ) v sai ( gi tr bng 0 ). Biu thc thng c dng trong : V phi ca cu lnh gn. Lm tham s thc s ca hm. Lm ch s. Trong cc ton t ca cc cu trc iu khin. Ti y, ta c hai khi nim chnh to nn biu thc l ton hng v php ton. Ton hng gm : hng, bin, phn t mng v hm trc y ta xt. Di y ta s ni n cc php ton. Hm s c cp trong chng 6. 3.2. Lnh gn v biu thc: Biu thc gn l biu thc c dng : v=e Trong v l mt bin ( hay phn t mng ), e l mt biu thc. Gi tr ca biu thc gn l gi tr ca e, kiu ca n l kiu ca v. Nu t du ; vo sau biu thc gn ta s thu c php ton gn c dng : v=e;

26

Biu thc gn c th s dng trong cc php ton v cc cu lnh nh cc biu thc khc. V d nh khi ta vit a=b=5; th iu c ngha l gn gi tr ca biu thc b=5 cho bin a. Kt qa l b=5 v a=5. Hon ton tng t nh : a=b=c=d=6; gn 6 cho c a, b, c v d V d : z=(y=2)*(x=6); { y * l php ton nhn } gn 2 cho y, 6 cho x v nhn hai biu thc li cho ta z=12. 3.3. Cc php ton s hc : Cc php ton hai ngi s hc l Php ton + * / % nghi Php cng Php tr Php nhn Php chia Php ly phn d V d a+b a-b a*b a/b ( Chia s nguyn s cht phn thp phn ) a%b ( Cho phn d ca php chia a cho b ) C php ton mt ngi - v du -(a+b) s o gi tr ca php cng (a+b). V d : 11/3=3 11%3=2 -(2+6)=-8 Cc php ton + v - c cng th t u tin, c th t u tin nh hn cc php * , / , % v c ba php ny li c th t u tin nh hn php tr mt ngi. Cc php ton s hc c thc hin t tri sang phi. S u tin v kh nng kt hp ca php ton c ch ra trong mt mc sau ny 3.4. Cc php ton quan h v logic :

27

Php ton quan h v logic cho ta gi tr ng ( 1 ) hoc gi tr sai ( 0 ). Ni cch khc, khi cc iu kin nu ra l ng th ta nhn c gi tr 1, tri li ta nhn gi tr 0. Cc php ton quan h l : Php ton > >= < <= == != nghi So snh ln hn So snh ln hn hoc bng So snh nh hn So snh nh hn hoc bng So snh bng nhau So snh khc nhau V d a>b 4>5 c gi tr 0 a>=b 6>=2 c gi tr 1 a<b 6<=7 c gi tr 1 a<=b 8<=5 c gi tr 0 a==b 6==6 c gi tr 1 a!=b 9!=9 c gi tr 0 Bn php ton u c cng s u tin, hai php sau c cng s th t u tin nhng thp hn s th t ca bn php u. Cc php ton quan h c s th t u tin thp hn so vi cc php ton s hc, cho nn biu thc : i<n-1 c hiu l i<(n-1). Cc php ton logic : Trong C s dng ba php ton logic : Php ph nh mt ngi ! a khc 0 bng 0 Php v (AND) && Php hoc ( OR ) || a b a&&b a||b !a 0 1

28

khc 0 khc 0 bng 0 bng 0 biu thc nh : (a<b)&&(c>d) c th vit li thnh : a<b&&c>d Ch :

khc 0 bng 0 khc 0 bng 0

1 0 0 0

1 1 1 0

Cc php quan h c s u tin nh hn so vi ! nhng ln hn so vi && v ||, v vy

C a v b c th l nguyn hoc thc. 3.5. Php ton tng gim : C a ra hai php ton mt ngi tng v gim cc bin ( nguyn v thc ). Ton t tng l ++ s cng 1 vo ton hng ca n, ton t gim -- th s tr ton hng i 1. V d : n=5 ++n --n Cho ta n=6 Cho ta n=4

Ta c th vit php ton ++ v -- trc hoc sau ton hng nh sau : ++n, n++, --n, n--. S khc nhau ca ++n v n++ ch : trong php n++ th tng sau khi gi tr ca n c s dng, cn trong php ++n th n c tng trc khi s dng. S khc nhau gia n-- v --n cng nh vy. V d : n=5 x=++n x=n++ Cho ta x=6 v n=6 Cho ta x=5 v n=6

3.6. Th t u tin cc php ton : Cc php ton c u tin khc nhau, iu ny c ngha trong cng mt biu thc s c mt s php ton ny c thc hin trc mt s php ton khc.

29

Th t u tin ca cc php ton c trnh by trong bng sau : TT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Ch thch : Cc php ton tn mt dng c cng th t u tin, cc php ton hng trn c s u tin cao hn cc s hng di. i vi cc php ton cng mc u tin th trnh t tnh ton c th t tri qua phi hay ngc li c ch ra trong ct trnh t kt hp. V d : *--px=*(--px) ( Phi qua tri ) 8/4*6=(8/4)*6 ( Tri qua phi ) Nn dng cc du ngoc trn vit biu thc mt cch chnh xc. Cc php ton l : Dng 1 [ ] Dng biu din phn t mng, v d : a[i][j] . Dng biu din thnh phn cu trc, v d : ht.ten -> Dng biu din thnh phn cu trc thng qua con tr Php ton () [] -> ! ~ & * - ++ -- (type ) sizeof * ( php nhn ) / % +<< >> < <= > >= == != & ^ | && || ?: = += -= *= /= %= <<= >>= &= ^= |= , Trnh t kt hp Tri qua phi Phi qua tri Tri qua phi Tri qua phi Tri qua phi Tri qua phi Tri qua phi Tri qua phi Tri qua phi Tri qua phi Tri qua phi Tri qua phi Phi qua tri Phi qua tri Tri qua phi

30

Dng 2 * Dng khai bo con tr, v d : int *a & Php ton ly a ch, v d : &x ( type) l php chuyn i kiu, v d : (float)(x+y) Dng 15 Ton t , thng dng vit mt dy biu thc trong ton t for. 3.7. Chuyn i kiu gi tr : Vic chuyn i kiu gi tr thng din ra mt cch t ng trong hai trng hp sau : Khi gn biu thc gm cc ton hng khc kiu. Khi gn mt gi tr kiu ny cho mt bin ( hoc phn t mng ) kiu khc. iu ny xy ra trong ton t gn, trong vic truyn gi tr cc tham s thc s cho cc i. Ngoi ra, ta c th chuyn t mt kiu gi tr sang mt kiu bt k m ta mun bng php chuyn sau : ( type ) biu thc V d : (float) (a+b) Chuyn i kiu trong biu thc : Khi hai ton hng trong mt php ton c kiu khc nhau th kiu thp hn s c nng thnh kiu cao hn trc khi thc hin php ton. Kt qu thu c l mt gi tr kiu cao hn. Chng hn : Gia int v long th int chuyn thnh long. Gia int v float th int chuyn thnh float. Gia float v double th float chuyn thnh double. V d : 1.5*(11/3)=4.5 1.5*11/3=5.5 (11/3)*1.5=4.5 Chuyn i kiu thng qua php gn :

31

Gi tr ca v phi c chuyn sang kiu v tri l kiu ca kt qu. Kiu int c th c c chuyn thnh float. Kiu float c th chuyn thnh int do cht i phn thp phn. Kiu double chuyn thnh float bng cch lm trn. Kiu long c chuyn thnh int bng cch ct b mt vi ch s. V d : int n; n=15.6 gi tr ca n l 15

i kiu dng (type)biu thc : Theo cch ny, kiu ca biu thc c i thnh kiu type theo nguyn tc trn. V d : Php ton : (int)a cho mt gi tr kiu int. Nu a l float th y c s chuyn i t float sang int. Ch rng bn thn kiu ca a vn khng b thay i. Ni cch khc, a vn c kiu float nhng (int)a c kiu int. i vi hm ton hc ca th vin chun, th gi tr ca i v gi tr ca hm u c kiu double, v vy tnh cn bc hai ca mt bin nguyn n ta phi dng php p kiu chuyn kiu int sang double nh sau : sqrt((double)n) Php p kiu c cng s u tin nh cc ton t mt ngi. Ch : Mun c gi tr chnh xc trong php chia hai s nguyn cn dng php p kiu : ((float)a)/b i gi tr thc r sang nguyn, ta dng : (int)(r+0.5) Ch th t u tin : (int)1.4*10=1*10=10 (int)(1.4*10)=(int)14.0=14

32

Chng 4 Cu trc c bn ca chng trnh 4.1. Li ch thch : Cc li bnh lun, cc li gii thch c th a vo bt k ch no ca chng trnh cho chng trnh d hiu, d c hn m khng lm nh hng n cc phn khc. Li gii thch c t gia hai du /* v */. Trong mt chng trnh cn ( v lun lun cn ) vit thm nhng li gii thch chng trnh thm r rng, thm d hiu. V d : #include "stdio.h" #include "string.h" #include "alloc.h" #include "process.h" int main() { char *str; /* Cp pht b nh cho xu k t */ if ((str = malloc(10)) == NULL) { printf("Not enough memory to allocate buffer\n"); exit(1); /* Kt thc chng trnh nu thiu b nh */ } /* copy "Hello" vo xu */ strcpy(str, "Hello"); /* Hin th xu */ printf("String is %s\n", str); /* Gii phng b nh */ free(str); return 0; }

33

4.2. Lnh v khi lnh : 4.2.1. Lnh : Mt biu thc kiu nh x=0 hoc ++i hoc scanf(...) tr thnh cu lnh khi c i km theo du ; V d : x=0; ++i; scanf(...); Trong chng trnh C, du ; l du hiu kt thc cu lnh. 4.2.2. Khi lnh : Mt dy cc cu lnh c bao bi cc du { } gi l mt khi lnh. V d : { a=2; b=3; printf("\n%6d%6d",a,b); } TURBO C xem khi lnh cng nh mt cu lnh ring l. Ni cch khc, ch no vit c mt cu lnh th cng c quyn t mt khi lnh. Khai bo u khi lnh : Cc khai bo bin v mng chng nhng c th t u ca mt hm m cn c th vit u khi lnh : { int a,b,c[50]; float x,y,z,t[20][30]; a==b==3; x=5.5; y=a*x; z=b*x; printf("\n y= %8.2f\n z=%8.2f",y,z); } S lng nhau ca cc khi lnh v phm vi hot ng ca cc bin v mng :

34

Bn trong mt khi lnh li c th vit lng khi lnh khc. S lng nhau theo cch nh vy l khng hn ch. Khi my bt u lm vic vi mt khi lnh th cc bin v mng khai bo bn trong n mi c hnh thnh v c hnh thnh v c cp pht b nh. Cc bin ny ch tn ti trong thi gian my lm vic bn trong khi lnh v chng lp tc bin mt ngay sau khi my ra khi khi lnh. Vy : Gi tr ca mt bin hay mt mng khai bo bn trong mt khi lnh khng th a ra s dng bt k ch no bn ngoi khi lnh . bt k ch no bn ngoi mt khi lnh ta khng th can thip n cc bin v cc mng c khai bo bn trong khi lnh Nu bn trong mt khi ta dng mt bin hay mt mng c tn l a th iu ny khng lm thay i gi tr ca mt bin khc cng c tn l a ( nu c ) c dng u bn ngoi khi lnh ny. Nu c mt bin c khai bo ngoi mt khi lnh v khng trng tn vi cc bin khai bo bn trong khi lnh ny th bin cng c th s dng c bn trong cng nh bn ngoi khi lnh. V d : Xt on chng trnh sau : { int a=5,b=2; { int a=4; b=a+b; printf("\n a trong =%3d b=%3d",a,b); } printf("\n a ngoai =%3d b=%3d",a,b); } Khi on chng trnh s in kt qu nh sau : a trong =4 b=6 a ngoi =5 b=6 Do tnh cht bin a trong v ngoi khi lnh. 4.3. Cu trc c bn ca chng trnh :

35

Cu trc chng trnh v hm l mt trong cc vn quan trng ca C. V hm ta s c mt chng ni t m v n. y ta ch a ra mt s qui tc chung : Hm l mt n v c lp ca chng trnh. Tnh c lp ca hm th hin hai im : Khng cho php xy dng mt hm bn trong cc hm khc. Mi hm c cc bin, mng .. ring ca n v chng ch c s dng ni b bn trong hm. Ni cch khc hm l n v c tnh cht khp kn. Mt chng trnh bao gm mt hoc nhiu hm. Hm main() l thnh phn bt buc ca chng trnh. Chng trnh bt u thc hin cc cu lnh u tin ca hm main() v kt thc khi gp du } cui cng ca hm ny. Khi chng trnh lm vic, my c th chy t hm ny sang hm khc. Cc chng trnh C c t chc theo mu : ..... hm 1 ..... hm 2 ..... ..... hm n Bn ngoi cc hm cc v tr (..... ) l ch t : cc ton t #include ... ( dng khai bo s dng cc hm chun ), ton t #define ... ( dng nh ngha cc hng ), nh ngha kiu d liu bng typedef, khai bo cc bin ngoi, mng ngoi.... Vic truyn d liu v kt qu t hm ny sang hm khc c thc hin theo mt trong hai cch : S dng i ca hm. S dng bin ngoi, mng ngoi ... Vy ni tm li cu truc c bn ca chng trnh nh sau : Cc #include Cc #define Khai bo cc i tng d liu ngoi ( bin, mng, cu trc vv..). Khai bo nguyn mu cc hm. Hm main(). nh ngha cc hm ( hm main c th t sau hoc xen vo gia cc hm khc ).

36

V d : Chng trnh tnh x ly tha y ri in ra my in kt qu : #include "stdio.h" #include "math.h" main() { double x,y,z; printf("\n Nhap x va y"); scanf("%lf%lf",&x,&y); z=pow(x,y); /* hm ly lu tha y lu tha x */ fprintf(stdprn,"\n x= %8.2lf \n y=%8.2lf \n z=%8.2lf",x,y,z); } 4.4. Mt s qui tc cn nh khi vit chng trnh : Qui tc u tin cn nh l : Mi cu lnh c th vit trn mt hay nhiu dng nhng phi kt thc bng du ; Qui tc th hai l : Cc li gii thch cn c t gia cc du /* v */ v c th c vit Trn mt dng Trn nhiu dng Trn phn cn li ca dng Qui tc th ba l : Trong chng trnh, khi ta s dng cc hm chun, v d nh printf(), getch() ,... m cc hm ny li cha trong file stdio.h trong th mc ca C, v vy u chng trnh ta phi khai bo s dng ; #include "stdio.h " Qui tc th t l :

37

Mt chng trnh c th ch c mt hm chnh ( hm main() ) hoc c th c thm vi hm khc.

38

Chng 5 Cu trc iu khin Mt chng trnh bao gm nhiu cu lnh. Thng thng cc cu lnh c thc hin mt cch ln lt theo th t m chng c vit ra. Cc cu trc iu khin cho php thay i trt t ni trn, do my c th nhy thc hin mt cu lnh khc mt v tr trc hoc sau cu lnh hin thi. Xt v mt cng dng, c th chia cc cu trc iu khin thnh cc nhm chnh : Nhy khng c iu kin. R nhnh. T chc chu trnh. Ngoi ra cn mt s ton t khc c chc nng b tr nh break, continue. 5.1. Cu trc c iu kin : 5.1.1. Lnh if-else : Ton t if cho php la chn chy theo mt trong hai nhnh tu thuc vo s bng khng v khc khng ca biu thc. N c hai cch vit sau : if ( biu thc ) khi lnh 1; /* Dng mt */ if ( biu thc ) khi lnh 1; else khi lnh 2 ; /* Dng hai */

Hot ng ca biu thc dng 1 : My tnh gi tr ca biu thc. Nu biu thc ng ( biu thc c gi tr khc 0 ) my s thc hin khi lnh 1 v sau s thc hin cc lnh tip sau lnh if trong chng trnh. Nu biu thc sai ( biu thc c gi tr bng 0 ) th my b qua khi lnh 1 m thc hin ngay cc lnh tip sau lnh if trong chng trnh. Hot ng ca biu thc dng 2 : My tnh gi tr ca biu thc. Nu biu thc ng ( biu thc c gi tr khc 0 ) my s thc hin khi lnh 1 v sau s thc hin cc lnh tip sau khi lnh 2 trong chng trnh. Nu

39

biu thc sai ( biu thc c gi tr bng 0 ) th my b qua khi lnh 1 m thc hin khi lnh 2 sau thc hin tip cc lnh tip sau khi lnh 2 trong chng trnh. V d : Chng trnh nhp vo hai s a v b, tm max ca hai s ri in kt qu ln mn hnh. Chng trnh c th vit bng c hai cch trn nh sau : #include "stdio.h" main() { float a,b,max; printf("\n Cho a="); scanf("%f",&a); printf("\n Cho b="); scanf("%f",&b); max=a; if (b>max) max=b; printf(" \n Max cua hai so a=%8.2f va b=%8.2f la Max=%8.2f",a,b,max); } #include "stdio.h" main() { float a,b,max; printf("\n Cho a="); scanf("%f",&a); printf("\n Cho b="); scanf("%f",&b); if (a>b) max=a; else max=b; printf(" \n Max cua hai so a=%8.2f va b=%8.2f la Max=%8.2f",a,b,max); }

S lng nhau ca cc ton t if :

40

C cho php s dng cc ton t if lng nhau c ngha l trong cc khi lnh ( 1 v 2 ) trn c th cha cc ton t if - else khc. Trong trng hp ny, nu khng s dng cc du ng m ngoc cho cc khi th s c th nhm ln gia cc if-else. Ch l my s gn ton t else vi ton t if khng c else gn nht. Chng hn nh on chng trnh v d sau : if ( n>0 ) /* if th nht*/ /* if th hai*/ z=a; else z=b; th else y s i vi if th hai. on chng trnh trn tng ng vi : if ( n>0 ) { if ( a>b ) z=a; else z=b; } Trng hp ta mun else i vi if th nht ta vit nh sau : if ( n>0 ) { if ( a>b ) z=a; } else z=b; /* if th hai*/ /* if th nht*/ /* if th hai*/ /* if th nht*/ if ( a>b )

5.1.2. Lnh else-if : Khi mun thc hin mt trong n quyt nh ta c th s dng cu trc sau : if ( biu thc 1 ) khi lnh 1;

41

else if ( biu thc 2 ) khi lnh 2; ...... else if ( biu thc n-1 ) khi lnh n-1; else khi lnh n; Trong cu trc ny, my s i kim tra t biu thc 1 tr i n khi gp biu thc no c gi tr khc 0. Nu biu thc th i (1,2, ...n-1) c gi tr khc 0, my s thc hin khi lnh i, ri sau i thc hin lnh nm tip theo khi lnh n trong chng trnh. Nu trong c n-1 biu thc khng c biu thc no khc 0, th my s thc hin khi lnh n ri sau i thc hin lnh nm tip theo khi lnh n trong chng trnh. V d : Chng trnh gii phng trnh bc hai. #include "stdio.h" main() { float a,b,c,d,x1,x2; printf("\n Nhap a, b, c:"); scanf("%f%f%f,&a&b&c); d=b*b-4*a*c; if (d<0.0) printf("\n Phuong trinh vo nghiem "); else if (d==0.0) printf("\n Phuong trinh co nghiem kep x1,2=%8.2f",-b/(2*a)); else { printf("\n Phuong trinh co hai nghiem "); printf("\n x1=%8.2f",(-b+sqrt(d))/(2*a)); printf("\n x2=%8.2f",(-b-sqrt(d))/(2*a)); }

42

5.2. Lnh nhy khng iu kin - ton t goto : Nhn c cng dng nh tn bin v c du : ng pha sau. Nhn c th c gn cho bt k cu lnh no trong chng trnh. V d : ts : s=s++; th y ts l nhn ca cu lnh gn s=s++. Ton t goto c dng : goto nhn; Khi gp ton t ny my s nhy ti cu lnh c nhn vit sau t kho goto. Khi dng ton t goto cn ch : Cu lnh goto v nhn cn nm trong mt hm, c ngha l ton t goto ch cho php nhy t v tr ny n v tr khc trong thn mt hm v khng th dng nhy t mt hm ny sang mt hm khc. Khng cho php dng ton t goto nhy t ngoi vo trong mt khi lnh. Tuy nhin vic nhy t trong mt khi lnh ra ngoi l hon ton hp l. V d nh on chng trnh sau l sai. goto n1; ....... { ..... n1: printf("\n Gia tri cua N la: "); ..... } V d : Tnh tng s=1+2+3+....+10 #include "stdio.h" main() { int s,i; i=s=0; tong:

43

++i; s=s+i; if (i<10) goto tong; printf("\n tong s=%d",s); } 5.3. Cu trc r nhnh - ton t switch: L cu trc to nhiu nhnh c bit. N cn c vo gi tr mt biu thc nguyn chn mt trong nhiu cch nhy. Cu trc tng qut ca n l : switch ( biu thc nguyn ) { case n1 khi lnh 1 case n2 khi lnh 2 ....... case nk khi lnh k [ default khi lnh k+1 ] } Vi ni l cc s nguyn, hng k t hoc biu thc hng. Cc ni cn c gi tr khc nhau. on chng trnh nm gia cc du { } gi l thn ca ton t switch. default l mt thnh phn khng bt buc phi c trong thn ca switch. S hot ng ca ton t switch ph thuc vo gi tr ca biu thc vit trong du ngoc ( ) nh sau : Khi gi tr ca biu thc ny bng ni, my s nhy ti cc cu lnh c nhn l case ni. Khi gi tr biu thc khc tt c cc ni th cch lm vic ca my li ph thuc vo s c mt hay khng ca lnh default nh sau : Khi c default my s nhy ti cu lnh sau nhn default. Khi khng c default my s nhy ra khi cu trc switch. Ch :

44

My s nhy ra khi ton t switch khi n gp cu lnh break hoc du ngoc nhn ng cui cng ca thn switch. Ta cng c th dng cu lnh goto trong thn ca ton t switch nhy ti mt cu lnh bt k bn ngoi switch. Khi ton t switch nm trong thn mt hm no th ta c th s dng cu lnh return trong thn ca switch ra khi hm ny ( lnh return s cp sau ). Khi my nhy ti mt cu lnh no th s hot ng tip theo ca n s ph thuc vo cc cu lnh ng sau cu lnh ny. Nh vy nu my nhy ti cu lnh c nhn case ni th n c th thc hin tt c cc cu lnh sau cho ti khi no gp cu lnh break, goto hoc return. Ni cch khc, my c th i t nhm lnh thuc case ni sang nhm lnh thuc case th ni+1. Nu mi nhm lnh c kt thc bng break th ton t switch s thc hin ch mt trong cc nhm lnh ny. V d : Lp chng trnh phn loi hc sinh theo im s dng cu trc switch : #include "stdio.h" main() { int diem; tt: printf("\nVao du lieu :"); printf("\n Diem ="); scanf("%d",&diem); switch (diem) { case 0: case 1: case 2: case 3:printf("Kem\n");break; case 4:printf("Yeu\n");break; case 5: case 6:printf("Trung binh\n");break; case 7: case 8:printf("Kha\n");break; case 9: case 10:printf("Gioi\n");break; default:printf(Vao sai\n);

45

} printf("Tiep tuc 1, dung 0 :") scanf("%d",&diem); if (diem==1) goto tt; getch(); return; } 5.4. Cu trc lp : 5.4.1. Cu trc lp vi ton t while v for : 5.4.1.1. Cu trc lp vi ton t while : Ton t while dng xy dng chu trnh lp dng : while ( biu thc ) Lnh hoc khi lnh; Nh vy ton t while gm mt biu thc v thn chu trnh. Thn chu trnh c th l mt lnh hoc mt khi lnh. Hot ng ca chu trnh nh sau : My xc nh gi tr ca biu thc, tu thuc gi tr ca n my s chn cch thc hin nh sau : Nu biu thc c gi tr 0 ( biu thc sai ), my s ra khi chu trnh v chuyn ti thc hin cu lnh tip sau chu trnh trong chng trnh. Nu biu thc c gi tr khc khng ( biu thc ng ), my s thc hin lnh hoc khi lnh trong thn ca while. Khi my thc hin xong khi lnh ny n li thc hin xc nh li gi tr biu thc ri lm tip cc bc nh trn. Ch : Trong cc du ngoc ( ) sau while chng nhng c th t mt biu thc m cn c th t mt dy biu thc phn cch nhau bi du phy. Tnh ng sai ca dy biu thc c hiu l tnh ng sai ca biu thc cui cng trong dy. Bn trong thn ca mt ton t while li c th s dng cc ton t while khc. bng cch ta i xy dng c cc chu trnh lng nhau. Khi gp cu lnh break trong thn while, my s ra khi ton t while su nht cha cu lnh ny.

46

Trong thn while c th s dng ton t goto nhy ra khi chu trnh n mt v tr mong mun bt k. Ta cng c th s dng ton t return trong thn while ra khi mt hm no . V d : Chng trnh tnh tch v hng ca hai vc t x v y : Cch 1 : #include "stdio.h" float x[]={2,3.4,4.6,21}, y[]={24,12.3,56.8,32.9}; main() { float s=0; int i=-1; while (++i<4) s+=x[i]*y[i]; printf("\n Tich vo huong hai vec to x va y la :%8.2f",s); } Cch 2 : #include "stdio.h" float x[]={2,3.4,4.6,21}, y[]={24,12.3,56.8,32.9}; main() { float s=0; int i=0; while (1) { s+=x[i]*y[i]; if (++i>=4) goto kt; } kt:printf("\n Tich vo huong hai vec to x va y la :%8.2f",s); }

47

Cch 3 : #include "stdio.h" float x[]={2,3.4,4.6,21}, y[]={24,12.3,56.8,32.9}; main() { float s=0; int i=0; while ( s+=x[i]*y[i], ++i<=3 ); printf("\n Tich vo huong hai vec to x va y la :%8.2f",s); } 5.4.1.2. Cu trc lp vi ton t for : Ton t for dng xy dng cu trc lp c dng sau : for ( biu thc 1; biu thc 2; biu thc 3) Lnh hoc khi lnh ; Ton t for gm ba biu thc v thn for. Thn for l mt cu lnh hoc mt khi lnh vit sau t kho for. Bt k biu thc no trong ba biu thc trn c th vng mt nhng phi gi du ; . Thng thng biu thc 1 l ton t gn to gi tr ban u cho bin iu khin, biu thc 2 l mt quan h logic biu th iu kin tip tc chu trnh, biu thc ba l mt ton t gn dng thay i gi tr bin iu khin. Hot ng ca ton t for : Ton t for hot ng theo cc bc sau : Xc nh biu thc 1 Xc nh biu thc 2 Tu thuc vo tnh ng sai ca biu thc 2 my la chn mt trong hai nhnh : Nu biu thc hai c gi tr 0 ( sai ), my s ra khi for v chuyn ti cu lnh sau thn for. Nu biu thc hai c gi tr khc 0 ( ng ), my s thc hin cc cu lnh trong thn for. Tnh biu thc 3, sau quay li bc 2 bt u mt vng mi ca chu trnh.

48

Ch : Nu biu thc 2 vng mt th n lun c xem l ng. Trong trng hp ny vic ra khi chu trnh for cn phi c thc hin nh cc lnh break, goto hoc return vit trong thn chu trnh. Trong du ngoc trn sau t kho for gm ba biu thc phn cch nhau bi du ;. Trong mi biu thc khng nhng c th vit mt biu thc m c quyn vit mt dy biu thc phn cch nhau bi du phy. Khi cc biu thc trong mi phn c xc nh t tri sang phi. Tnh ng sai ca dy biu thc c tnh l tnh ng sai ca biu thc cui cng trong dy ny. Trong thn ca for ta c th dng thm cc ton t for khc, v th ta c th xy dng cc ton t for lng nhau. Khi gp cu lnh break trong thn for, my ra s ra khi ton t for su nht cha cu lnh ny. Trong thn for cng c th s dng ton t goto nhy n mt v tr mong mun bt k. V d 1: Nhp mt dy s ri o ngc th t ca n. Cch 1: #include stdio.h float x[]={1.3,2.5,7.98,56.9,7.23}; int n=sizeof(x)/sizeof(float); main() { int i,j; float c; for (i=0,j=n-1;i<j;++i,--j) { c=x[i];x[i]=x[j];x[j]=c; } fprintf(stdprn,\n Day so dao la \n\n); for (i=0;i<n;++i) fprintf(stdprn,%8.2f,x[i]); }

49

Cch 2 : #include stdio.h float x[]={1.3,2.5,7.98,56.9,7.23}; int n=sizeof(x)/sizeof(float); main() { int i,j; float c; for (i=0,j=n-1;i<j;c=x[i],x[i]=x[j],x[j]=c,++i,--j) fprintf(stdprn,\n Day so dao la \n\n); for (i=0;++i<n;) fprintf(stdprn,%8.2f,x[i]); } Cch 3 : #include stdio.h float x[]={1.3,2.5,7.98,56.9,7.23}; int n=sizeof(x)/sizeof(float); main() { int i=0,j=n-1; float c; for ( ; ; ) { c=x[i];x[i]=x[j];x[j]=c; if (++i>--j) break; } fprintf(stdprn,\n Day so dao la \n\n); for (i=-1;i++<n-1; fprintf(stdprn,%8.2f,x[i])); } V d 2: Tnh tch hai ma trn mxn v nxp. #include "stdio.h"

50

float x[3][2],y[2][4],z[3][4],c; main() { int i,j; printf("\n nhap gia tri cho ma tran X "); for (i=0;i<=2;++i) for (j=0;j<=1;++j) { printf("\n x[%d][%d]=",i,j); scanf("%f",&c); x[i][j]=c; } printf("\n nhap gia tri cho ma tran Y "); for (i=0;i<=1;++i) for (j=0;j<=3;++j) { printf("\n y[%d][%d]=",i,j); scanf("%f",&c); y[i][j]=c; } for (i=0;i<=3;++i) for (j=0;j<=4;++j) z[i][j] } 5.4.2. Chu trnh do-while Khc vi cc ton t while v for, vic kim tra iu kin kt thc t u chu trnh, trong chu trnh do while vic kim tra iu kin kt thc t cui chu trnh. Nh vy thn ca chu trnh bao gi cng c thc hin t nht mt ln. Chu trnh do while c dng sau : do Lnh hoc khi lnh; while ( biu thc ); Lnh hoc khi lnh l thn ca chu trnh c th l mt lnh ring l hoc l mt khi lnh.

51

Hot ng ca chu trnh nh sau : My thc hin cc lnh trong thn chu trnh. Khi thc hin xong tt c cc lnh trong thn ca chu trnh, my s xc nh gi tr ca biu thc sau t kho while ri quyt nh thc hin nh sau : Nu biu thc ng ( khc 0 ) my s thc hin lp li khi lnh ca chu trnh ln th hai ri thc hin kim tra li biu thc nh trn. Nu biu thc sai ( bng 0 ) my s kt thc chu trnh v chuyn ti thc hin lnh ng sau ton t while. Ch : Nhng iu lu vi ton t while trn hon ton ng vi do while. V d : on chng trnh xc nh phn t m u tin trong cc phn t ca mng x. #include "stdio.h" float x[5],c; main() { int i=0; printf("\n nhap gia tri cho ma tran x "); for (i=0;i<=4;++i) { printf("\n x[%d]=",i); scanf("%f",&c); y[i]=c; } do ++i; while (x[i]>=0 && i<=4); if (i<=4) printf("\n Phan tu am dau tien = x[%d]=%8.2f",i,x[i]); else printf("\n Mang khong c phan tu am ");

52

} 5.5. Cu lnh break : Cu lnh break cho php ra khi cc chu trnh vi cc ton t for, while v switch. Khi c nhiu chu trnh lng nhau, cu lnh break s a my ra khi chu trnh bn trong nht cha n khng cn iu kin g. Mi cu lnh break c th thay bng cu lnh goto vi nhn thch hp. V d : Bit s nguyn dng n s l s nguyn t nu n khng chia ht cho cc s nguyn trong khong t 2 n cn bc hai ca n. Vit on chng trnh c vo s nguyn dng n, xem n c l s nguyn t. # include "stdio.h" # include "math.h" unsigned int n; main() { int i,nt=1; printf("\n cho n="); scanf("%d",&n); for (i=2;i<=sqrt(n);++i) if ((n % i)==0) { nt=0; break; } if (nt) printf("\n %d la so nguyen to",n); else printf("\n %d khong la so nguyen to",n); } 5.6. Cu lnh continue : Tri vi cu lnh break, lnh continue dng bt u mt vng mi ca chu trnh cha n. Trong while v do while, lnh continue chuyn iu khin v thc hin ngay phn kim tra,

53

cn trong for iu khin c chuyn v bc khi u li ( tc l bc : tnh biu thc 3, sau quay li bc 2 bt u mt vng mi ca chu trnh). Ch : Lnh continue ch p dng cho chu trnh ch khng p dng cho switch. V d : Vit chng trnh t mt nhp mt ma trn a sau : Tnh tng cc phn t dng ca a. Xc nh s phn t dng ca a. Tm cc i trong cc phn t dng ca a. #include "stdio.h" float a[3[4]; main() { int i,j,soptd=0; float tongduong=0,cucdai=0,phu; for (i=0;i<3;++i) for (j=0;i<4;++j) { printf("\n a[%d][%d]=",i,j ); scanf("%f",&phu); a[i][j]=phu; if (a[i][j]<=0) continue; tongduong+=a[i][j]; if (cucdai<a[i][j]) cucdai=a[i][j]; ++soptd; } printf("\n So phan tu duong la : %d",soptd); printf("\n Tong cac phan tu duong la : %8.2f",tongduong); printf("\n Cuc dai phan tu duong la : %8.2f",cucdai); }

54

55

Chng 6 Hm Mt chng trnh vit trong ngn ng C l mt dy cc hm, trong c mt hm chnh ( hm main() ). Hm chia cc bi ton ln thnh cc cng vic nh hn, gip thc hin nhng cng vic lp li no mt cch nhanh chng m khng phi vit li on chng trnh. Th t cc hm trong chng trnh l bt k, song chng trnh bao gi cng i thc hin t hm main(). 6.1. C s : Hm c th xem l mt n v c lp ca chng trnh. Cc hm c vai tr ngang nhau, v vy khng c php xy dng mt hm bn trong cc hm khc. Xy dng mt hm bao gm: khai bo kiu hm, t tn hm, khai bo cc i v a ra cu lnh cn thit thc hin yu cu ra cho hm. Mt hm c vit theo mu sau : type tn hm ( khai bo cc i ) { Khai bo cc bin cc b Cc cu lnh [return[biu thc];] } Dng tiu : Trong dng u tin ca hm cha cc thng tin v : kiu hm, tn hm, kiu v tn mi i. V d : float max3s(float a, float b, float c) khai bo cc i c dng : Kiu i 1 tn i 1, kiu i 2 tn i 2,..., kiu i n tn i n Thn hm : Sau dng tiu l thn hm. Thn hm l ni dung chnh ca hm bt u v kt thc bng cc du { }. Trong thn hm cha cc cu lnh cn thit thc hin mt yu cu no ra cho hm.

56

Thn hm c th s dng mt cu lnh return, c th dng nhiu cu lnh return cc ch khc nhau, v cng c th khng s dng cu lnh ny. Dng tng qut ca n l : return [biu thc]; Gi tr ca biu thc trong cu lnh return s c gn cho hm. V d : Xt bi ton : Tm gi tr ln nht ca ba s m gi tr m gi tr ca chng c a vo bn phm. Xy dng chng trnh v t chc thnh hai hm : Hm main() v hm max3s. Nhim v ca hm max3s l tnh gi tr ln nht ca ba s c vo, gi s l a,b,c. Nhim v ca hm main() l c ba gi tr vo t bn phm, ri dng hm max3s tnh nh trn, ri a kt qu ra mn hnh. Chng trnh c vit nh sau : #include "stdio.h" float max3s(float a,float b,float c ); /* Nguyn mu hm*/ main() { float x,y,z; printf("\n Vao ba so x,y,z:"); scanf("%f%f%f",&x&y&z); printf("\n Max cua ba so x=%8.2f y=%8.2f z=%8.2f la : %8.2f", x,y,z,max3s(x,y,z)); } /* Kt thc hm main*/

float max3s(float a,float b,float c) { float max; max=a; if (max<b) max=b; if (max<c) max=c; return(max); } /* Kt thc hm max3s*/

57

Quy tc hot ng ca hm : Mt cch tng qut li gi hm c dng sau : tn hm ([Danh sch cc tham s thc]) S cc tham s thc t thay vo trong danh sch cc i phi bng s tham s hnh thc v ln lt chng c kiu tng ng vi nhau. Khi gp mt li gi hm th n s bt u c thc hin. Ni cch khc, khi my gp li gi hm mt v tr no trong chng trnh, my s tm di ch v chuyn n hm tng ng. Qu trnh din ra theo trnh t sau : Cp pht b nh cho cc bin cc b. Gn gi tr ca cc tham s thc cho cc i tng ng. Thc hin cc cu lnh trong thn hm. Khi gp cu lnh return hoc du } cui cng ca thn hm th my s xo cc i, bin cc b v ra khi hm. Nu tr v t mt cu lnh return c cha biu thc th gi tr ca biu thc c gn cho hm. Gi tr ca hm s c s dng trong cc biu thc cha n. Cc tham s thc, cc i v bin cc b : Do i v bin cc b u c phm vi hot ng trong cng mt hm nn i v bin cc b cn c tn khc nhau. i v bin cc b u l cc bin t ng. Chng c cp pht b nh khi hm c xt n v b xo khi ra khi hm nn ta khng th mang gi tr ca i ra khi hm. i v bin cc b c th trng tn vi cc i lng ngoi hm m khng gy ra nhm ln no. Khi mt hm c gi ti, vic u tin l gi tr ca cc tham s thc c gn cho cc i ( trong v d trn hm max3s, cc tham s thc l x,y,z, cc i tng ng l a,b,c ). Nh vy cc i chnh l cc bn sao ca cc tham s thc. Hm ch lm vic trn cc i. Cc i c th b bin i trong thn hm, cn cc tham s thc th khng b thay i. Ch : Khi hm khai bo khng c kiu trc n th n c mc nh l kiu int. Khng nht thit phi khai bo nguyn mu hm. Nhng ni chung nn c v n cho php chng trnh bin dch pht hin li khi gi hm hay t ng vic chuyn dng.

58

Nguyn mu ca hm thc cht l dng u tin ca hm thm vo du ;. Tuy nhin trong nguyn mu c th b tn cc i. Hm thng c mt vi i. V d nh hm max3s c ba i l a,b,c. c ba i ny u c gi tr float. Tuy nhin, cng c hm khng i nh hm main. Hm thng cho ta mt gi tr no . L d nhin gi tr ca hm ph thuc vo gi tr cc i. 6.2. Hm khng cho cc gi tr : Cc hm khng cho gi tr ging nh th tc ( procedure ) trong ngn ng lp trnh PASCAL. Trong trng hp ny, kiu ca n l void. V d hm tm gi tr max trong ba s l max3s trn c th c vit thnh th tc hin th s cc i trong ba s nh sau : void htmax3s(float a, float b, float c) { float max; max=a; if (max<b) max=b; if (max<c) max=c; } Lc ny, trong hm main ta gi hm htmax3s bng cu lnh : htmax3s(x,y,z); 6.3. Hm qui : 6.3.3. M u : C khng nhng cho php t hm ny gi ti hm khc, m n cn cho php t mt im trong thn ca mt hm gi ti chnh hm . Hm nh vy gi l hm qui. Khi hm gi qui n chnh n, th mi ln gi my s to ra mt tp cc bin cc b mi hon ton c lp vi tp cc bin cc b c to ra trong cc ln gi trc. minh ho chi tit nhng iu trn, ta xt mt v d v tnh giai tha ca s nguyn dng n. Khi khng dng phng php qui hm c th c vit nh sau : long int gt(int n) /* Tnh n! vi n>=0*/ { long int gtphu=1; int i;

59

for (i=1;i<=n;++i) gtphu*=i; return s; } Ta nhn thy rng n! c th tnh theo cng thc truy hi sau : n!=1 n!=n*(n-1)! long int gtdq(int n) { if (n==0 || n==1) return 1; else return(n*gtdq(n-1)); } Ta i gii thch hot ng ca hm qui khi s dng trong hm main di y : #include "stdio.h" main() { printf("\n 3!=%d",gtdq(3)); } Ln gi u tin ti hm gtdq c thc hin t hm main(). My s to ra mt tp cc bin t ng ca hm gtdq. Tp ny ch gm cc i n. Ta gi i n c to ra ln th nht l n th nht. Gi tr ca tham s thc ( s 3 ) c gn cho n th nht. Lc ny bin n trong thn hm c xem l n th nht. Do n th nht c gi tr bng 3 nn iu kin trong ton t if l sai v do my s la chn cu lnh else. Theo cu lnh ny, my s tnh gi tr biu thc : n*gtdq(n-1) (*) tnh biu thc trn, my cn gi chnh hm gtdq v th ln gi th hai s thc hin. My s to ra i n mi, ta gi l n th hai. Gi tr ca n-1 y li l i ca hm , c truyn cho hm v hiu l n th hai, do vy n th hai c gi tr l 2. By gi, do n th hai vn cha tho mn iu kin if nn my li tip tc tnh biu thc : n*gtdq(n-1) (**) nu n=0 nu n>0

Hm tnh n! theo phng php qui c th c vit nh sau :

60

Biu thc trn li gi hm gtdq ln th ba. My li to ra i n ln th ba v y n th ba c gi tr bng 1. i n=1 th ba li c truyn cho hm, lc ny iu kin trong lnh if c tho mn, my i thc hin cu lnh : return 1=gtdq(1) (***) Bt u t y, my s thc hin ba ln ra khi hm gtdq. Ln ra khi hm th nht ng vi ln vo th ba. Kt qu l i n th ba c gii phng, hm gtdq(1) cho gi tr l 1 v my tr v xt gi tr biu thc n*gtdq(1) y l kt qu ca (**) y, n l n th hai v c gi tr bng 2. Theo cu lnh return, my s thc hin ln ra khi hm ln th hai, i n th hai s c gii phng, kt qu l biu thc trong (**) c gi tr l 2.1. Sau my tr v biu thc (*) lc ny l : n*gtdq(2)=n*2*1 n li hiu l th nht, n c gi tr bng 3, do vy gi tr ca biu thc trong (*) l 3.2.1=6. Chnh gi tr ny c s dng trong cu lnh printf ca hm main() nn kt qu in ra trn mn hnh l : 3!=6 Ch : Hm qui so vi hm c th dng vng lp th n gin hn, tuy nhin vi my tnh khi dng hm qui s dng nhiu b nh trn ngn xp v c th dn n trn ngn xp. V vy khi gp mt bi ton m c th c cch gii lp ( khng dng qui ) th ta nn dng cch lp ny. Song vn tn ti nhng bi ton ch c th gii bng qui. 6.3.2. Cc bi ton c th dng qui : Phng php qui thng p dng cho cc bi ton ph thuc tham s c hai c im sau : Bi ton d dng gii quyt trong mt s trng hp ring ng vi cc gi tr c bit ca tham s. Ngi ta thng gi l trng hp suy bin. Trong trng hp tng qut, bi ton c th qui v mt bi ton cng dng nhng gi tr tham s th b thay i. Sau mt s hu hn bc bin i d qui n s dn ti trng hp suy bin. Bi ton tnh n giai tha nu trn th hin r nt c iu ny. 6.3.3. Cch xy dng hm qui : Hm qui thng c xy dng theo thut ton sau :

61

if ( trng hp suy bin) { Trnh by cch gii bi ton khi suy bin } else /* Trng hp tng qut */ { Gi qui ti hm ( ang vit ) vi cc gi tr khc ca tham s } 6.3.4. Cc v d v dng hm qui : V d 1 : Bi ton dng qui tm USCLN ca hai s nguyn dng a v b. Trong trng hp suy bin, khi a=b th USCLN ca a v b chnh l gi tr ca chng. Trong trng hp chung : uscln(a,b)=uscln(a-b,b) nu a>b uscln(a,b)=uscln(a,b-a) nu a<b Ta c th vit chng trnh nh sau : #include "stdio.h" int uscln(int a,int b ); /* Nguyn mu hm*/ main() { int m,n; printf("\n Nhap cac gia tri cua a va b :"); scanf("%d%d",&m,&n); printf("\n USCLN cua a=%d va b=%d la :%d",m,m,uscln(m,n)) } int uscln(int a,int b) { if (a==b) return a; else if (a>b) return uscln(a-b,b);

62

else return uscln(a,b-a); } V d 2 : Chng trnh c vo mt s ri in n ra di dng cc k t lin tip. # include "stdio.h" # include "conio.h" void prind(int n); main() { int a; clrscr(); printf("n="); scanf("%d",&a); prind(a); getch(); } void prind(int n) { int i; if (n<0) { putchar('-'); n=-n; } if ((i=n/10)!=0) prind(i); putchar(n%10+'0'); } 6.4. B tin s l C :

63

C a ra mt s cch m rng ngn ng bng cc b tin s l macro n gin. C hai cch m rng chnh l #define m ta hc v kh nng bao hm ni dung ca cc file khc vo file ang c dch. Bao hm file : d dng x l mt tp cc #define v khai bo ( trong cc i tng khc ), C a ra cch bao hm cc file khc vo file ang dch c dng : #include "tn file" Dng khai bo trn s c thay th bi ni dung ca file c tn l tn file. Thng thng c vi dng nh vy xut hin ti u mi file gc gi vo cc cu lnh #define chung v cc khai bo cho cc bin ngoi. Cc #include c php lng nhau. Thng th cc #include c dng nhiu trong cc chng trnh ln, n m bo rng mi file gc u c cung cp cng cc nh ngha v khai bo bin, do vy trnh c cc li kh chu do vic thiu cc khai bo nh ngha. Tt nhin khi thay i file c bao hm vo th mi file ph thuc vo n u phi dch li. Php th MACRO : nh ngha c dng : #define biu thc 1 [ biu thc 2 ] s gi ti mt macro thay th biu thc 2 (nu c) cho biu thc 1. V d : #define YES 1 Macro thay bin YES bi gi tr 1 c ngha l h c ch no trong chng trnh c xut hin bin YES th n s c thay bi gi tr 1. Phm vi cho tn c nh ngha bi #define l t im nh ngha n cui file gc. C th nh ngha li tn v mt nh ngha c th s dng cc nh ngha khc trc . Php th khng thc hin cho cc xu du nhy, v d nh YES l tn c nh ngha th khng c vic thay th no c thc hin trong on lnh c "YES". V vic thit lp #define l mt bc chun b ch khng phi l mt phn ca chng trnh bin dch nn c rt t hn ch v vn phm v vic phi nh ngha ci g. Chng hn nh nhng ngi lp trnh a thch PASCAL c th nh ngha : #define then #define begin { #define end; }

64

sau vit on chng trnh : if (i>0) then begin a=i; ...... end; Ta cng c th nh ngha cc macro c i, do vy vn bn thay th s ph thuc vo cch gi ti macro. V d : nh ngha macro gi max nh sau : #define max(a,b) ((a)>(b) ?(a):(b)) Vic s dng : x=max(p+q,r+s); tng ng vi : x=((p+q)>(r+s) ? (p+q):(r+s)); Nh vy ta c th c hm tnh cc i vit trn mt dng. Chng no cc i cn gi c tnh nht qun th macro ny vn c gi tr vi mi kiu d liu, khng cn phi c cc loi hm max khc cho cc kiu d liu khc nhng vn phi c i cho cc hm. Tt nhin nu ta kim tra li vic m rng ca hm max trn, ta s thy rng n c th gy ra s by. Biu thc c tnh li hai ln v iu ny l khng tt nu n gy ra hiu qu ph kiu nh cc li gi hm v ton t tng. Cn phi thn trng dng thm du ngoc m bo trt t tnh ton. Tuy vy, macro vn rt c gi tr. Ch : Khng c vit du cch gia tn macro vi du m ngoc bao quanh danh sch i. V d : Xt chng trnh sau : main() { int x,y,z; x=5;

65

y=10*5; z=x+y; z=x+y+6; z=5*x+y; z=5*(x+y); z=5*((x)+(y)); printf("Z=%d",z); getch(); return; } Chng trnh s dng MACRO s nh sau : #define BEGIN { #define END } #define INTEGER int #define NB 10 #define LIMIT NB*5 #define SUMXY x+y #define SUM1 (x+y) #define SUM2 ((x)+(y)) main() BEGIN INTEGER x,y,z; x=5; y=LIMIT; z=SUMXY; z=5*SUMXY; z=5*SUM1; z=5*SUM2; printf("\n Z=%d",z); getch(); return; END

66

Chng 7 Con tr Con tr l bin cha a ch ca mt bin khc. Con tr c s dng rt nhiu trong C, mt phn l do chng i khi l cch duy nht biu din tnh ton, v phn na do chng thng lm cho chng trnh ngn gn v c hiu qu hn cc cch khc . Con tr tng b coi nh c hi chng km g lnh goto do cch s dng chng to ra cc chng trnh kh hiu. iu ny chc chn l ng khi ngi ta s dng chng mt cch ln xn v do to ra cc con tr tr n u khng bit trc c. 7.1. Con tr v a ch : V con tr cha a ch ca i tng nn n c th xm nhp vo i tng gin tip qua con tr. Gi s x l mt bin kiu int, v gi s px l con tr c to ra theo mt cch no . Php ton mt ngi & s cho a ch ca i tng, nn cu lnh : px=&x; s gn a ch ca bin x cho tr px, v px by gi c gi l " tr ti bin x ". Php ton & ch p dng c cho cc bin v phn t bng, kt cu kiu &(x+1) v &3 l khng hp l. Ly i ch ca bin register cng l sai. Php ton mt ngi * coi l ton hng ca n l i ch cn xt v thm nhp ti a ch ly ra ni dung. Nu bin y c kiu int th th lnh : y=*px; s gn gi tr ca bin m tr px tr ti. Vy dy lnh : px=&x; y=*px; s gn gi tr ca x cho y nh trong lnh : y=x; Cc khai bo cho cc bin con tr c dng : tn kiu *tn con tr V d : Nh trong v d trn, ta khai bo con tr px kiu int : int *px;

67

Trong khai bo trn ta ng ni rng l mt cch tng trng, rng t hp *px c kiu int, tc l nu px xut hin trong ng cnh *px th n cng tng ng vi bin c kiu int. Con tr c th xut hin trong cc biu thc. Chng hn, nu px tr ti s nguyn x th *px c th xut hin trong bt k ng cnh no m x c th xut hin. V d : Lnh Lnh y=*px+1; printf("%d",*px); s t y ln hn x mt n v. s in ra gi tr hin ti ca x Lnh : d=sqrt((double) *px); s gn cho bin d cn bc hai ca x, gi tr ny b buc phi chuyn sang double trc khi c chuyn cho sqrt ( cch dng hm sqrt ). Trong cc biu thc kiu nh : y=*px+1; php ton mt ngi * v & c mc u tin cao hn cc php ton s hc, cho nn biu thc ny ly bt k gi tr no m px tr ti, cng vi 1 ri gn cho y. Con tr cng c th xut hin bn v tri ca php gn. Nu px tr ti x th sau lnh : *px=0; x s c gi tr bng 0. Cng tng t cc lnh: *px+=1; (*px)++; s tng gi tr ca x ln 1 dn v. Cc du ngoc n cu lnh cui l cn thit , nu khng th biu thc s tng px thay cho tng bin m n tr ti v php ton mt ngi nh * v ++ c tnh t phi sang tri. Cui cng, v con tr l bin nn ta c thao tc chng nh i vi cc bin khc. Nu py cng l con tr int th lnh : py=px; s sao ni dung ca px vo py, ngha l lm cho py tr ti ni m px tr. 7.2. Con tr v mng mt chiu :

68

Trong C c mi quan h cht ch gia con tr v mng : cc phn t ca mng c th c xc nh nh ch s hoc thng qua con tr. 7.2.1.Php ton ly a ch : Php ton ny ch p dng cho cc phn t ca mng mt chiu. Gi s ta c khai bo : double b[20]; Khi php ton : &b[9] s cho a ch ca phn t b[9]. 7.2.2. Tn mng l mt hng a ch : Khi khai bo : float a[10]; my s b tr b tr cho mng a mi khong nh lin tip, mi khong nh l 4 byte. Nh vy, nu bit a ch ca mt phn t no ca mng a, th ta c th d dng suy ra a ch ca cc phn t khc ca mng. Vi C ta c : a tng ng vi &a[0] a+i tng ng vi &a[i] *(a+i) tng ng vi a[i] 7.2.3. Con tr tr ti cc phn t ca mng mt chiu : Khi con tr pa tr ti phn t a[k] th : pa+i tr ti phn t th i sau a[k], c ngha l n tr ti a[k+i]. pa-i tr ti phn t th i trc a[k], c ngha l n tr ti a[k-i]. *(pa+i) tng ng vi pa[i]. Nh vy, sau hai cu lnh : float a[20],*p; p=a; th bn cch vit sau c tc dng nh nhau : a[i] V d : Vo s liu ca cc phn t ca mt mng v tnh tng ca chng : *(a+i) p[i] *(p+i)

69

Cch 1: #include "stdio.h" main() { float a[4],tong; int i; for (i=0;i<4;++i) { printf("\n a[%d]=",i); scanf("%f",a+i); } tong=0; for (i=0;i<4;++i) tong+=a[i]; printf("\n Tong cac phan tu mang la :%8.2f ",tong); } Cch 2 : #include "stdio.h" main() { float a[4],tong, *troa; int i; troa=a; for (i=0;i<4;++i) { printf("\n a[%d]=",i); scanf("%f",&troa[i]); } tong=0; for (i=0;i<4;++i) tong+=troa[i]; printf("\n Tong cac phan tu mang la :%8.2f ",tong);

70

} Cch 3 : #include "stdio.h" main() { float a[4],tong,*troa; int i; troa=a; for (i=0;i<4;++i) { printf("\n a[%d]=",i); scanf("%f",troa+i); } tong=0; for (i=0;i<4;++i) tong+=*(troa+i); printf("\n Tong cac phan tu mang la :%8.2f ",tong); } Ch : Mng mt chiu v con tr tng ng phi cng kiu. 7.2.4. Mng, con tr v xu k t : Nh ta bit trc y, xu k t l mt dy k t t trong hai du nhy kp, v d nh : "Viet nam" Khi gp mt xu k t, my s cp pht mt khong nh cho mt mng kiu char ln cha cc k t ca xu v cha thm k t '\0' l k t dng lm k t kt thc ca mt xu k t. Mi k t ca xu c cha trong mt phn t ca mng. Cng ging nh tn mng, xu k t l mt hng a ch biu th a ch u ca mng cha n. V vy nu ta khai bo bin xau nh mt con tr kiu char : char *xau; th php gn :

71

xau="Ha noi" l hon ton c ngha. Sau khi thc hin cu lnh ny trong con tr xau s c a ch u ca mng (kiu char) ang cha xu k t bn phi. Khi cc cu lnh : puts("Ha noi"); puts(xau); s c cng mt tc dng l cho hin ln mn hnh dng ch Ha noi. Mng kiu char thng dng cha mt dy k t c vo b nh. V d, np t bn phm tn ca mt ngi ta dng mt mng kiu char vi di 25, ta s dng cc cu lnh sau : char ten[25]; printf("\n Ho ten :"); gets(ten); By gi ta xem gia mng kiu char v con tr kiu char c nhng g ging v khc nhau. thy c s khc nhau ca chng, ta a ra s so snh sau : char *xau, ten[15]; ten="Ha noi" gets(xau); Cc cu lnh trn l khng hp l. Cu lnh th hai sai ch : ten l mt hng a ch v ta khng th gn mt hng a ch ny cho mt hng a ch khc. Cu lnh th ba khng thc hin c, mc ch ca cu lnh l c t bn phm mt dy k t v lu vo mt vng nh m con tr xau tr ti. Song ni dung ca con tr xau cn cha xc nh. Nu tr xau tr ti mt vng nh no th cu lnh ny hon ton c ngha. Chng hn nh sau khi thc hin cu lnh : xau=ten; th cch vit : gets(ten) ; v gets(xau); u c tc dng nh nhau. 7.3. Con tr v mng nhiu chiu : Vic s l mng nhiu chiu phc tp hn so vi mng mt chiu. Khng phi mi qui tc ng vi mng mt chiu u c th p dng cho mng nhiu chiu. 7.3.1.Php ly a ch : Php ly a ch i vi cc phn t mng hai chiu ch c th p dng khi cc phn t mng hai chiu c kiu nguyn, cn li th php ly a ch cho cc phn t mng nhiu chiu l khng thc hin c .V d nh ta c th ly a ch &a[1][2] khi a l mng nguyn.

72

Th thut c t bn phm phn t mng hai chiu dng lnh scanf : Chng trnh c vo s liu cho mt ma trn hai chiu s c thc hin thng qua vic c vo mt bin trung gian, c mt gi tr v cha tm vo mt bin trung gian sau ta gn bin cho phn t mng: #include "stdio.h" main() { float a[2][3], tg; int i,j; for (i=0;i<2;++i) for (j=0;j<2;++j) { printf("\n a[%d][%d]=",i,j); scanf("%8.2f",&tg); a[i][j]=tg; } } 7.3.2. Php cng a ch trong mng hai chiu: Gi s ta c mng hai chiu a[2][3] c 6 phn t ng vi su a ch lin tip trong b nh c xp theo th t sau : Phn t a ch a[0][0] 1 a[0][1] 2 a[0][2] 3 a[1][0] 4 a[1][1] 5 a[1][2] 6

Tn mng a biu th a ch u tin ca mng. Php cng a ch y c thc hin nh sau : C coi mng hai chiu l mng ( mt chiu ) ca mng, nh vy khai bo float a[2][3]; th a l mng m mi phn t ca n l mt dy 3 s thc ( mt hng ca mng ). V vy : a tr phn t th nht ca mng : phn t a[0][0] a+1 tr phn t u hng th hai ca mng : phn t a[1][0] ........

73

7.3.3. Con tr v mng hai chiu : ln lt duyt trn cc phn t ca mng hai chiu ta c th dng con tr nh minh ho v d sau : float *pa,a[2][3]; pa=(float*)a; lc : pa tr ti a[0][0] pa+1 tr ti a[0][1] pa+2 tr ti a[0][2] pa+3 tr ti a[1][0] pa+4 tr ti a[1][1] pa+5 tr ti a[1][2] V d : Dng con tr vo s liu cho mng hai chiu. Cch 1 : #include "stdio.h" main() { float a[2][3],*pa; int i; pa=(float*)a; for (i=0;i<6;++i) scanf("%f",pa+i); } Cch 2 : #include "stdio.h" main() { float a[2][3],*pa; int i; for (i=0;i<6;++i)

74

scanf("%f",(float*)a+i); }

7.4. Kiu con tr, kiu a ch, cc php ton trn con tr : 7.4.1. Kiu con tr v kiu a ch : Con tr dng lu a ch. Mi kiu a ch cn c kiu con tr tng ng. Php gn a ch cho con tr ch c th thc hin c khi kiu a ch ph hp vi kiu con tr. V d theo khai bo : float a[20][30],*pa,(*pm)[30]; th : pa l con tr float pm l con tr kiu float [30] a l a ch kiu float [30] V th php gn : pa=a; l khng hp l. Nhng php gn : pm=a; 7.4.2. Cc php ton trn con tr: C 4 php ton lin quan n con tr v i ch l : Php gn. Php tng gim a ch. Php truy cp b nh. Php so snh. Php gn : Php gn ch thc hin vi cc con tr cng kiu. Mun gn cc con tr khc kiu phi dng php p kiu nh v d sau : int x; char *pc; pc=(char*)(&x); Php tng gim a ch :

75

minh ho chi tit cho php ton ny, ta xt v d sau : Cc cu lnh : float x[30],*px; px=&x[10]; cho con tr px l con tr float tr ti phn t x[10]. Kiu a ch float l kiu a ch 4 byte, nn cc php tng gim a ch c thc hin trn 4 byte. V th : px+i tr ti phn t x[10+i] px-i tr ti phn t x[10-i] Xt v d khc : Gi s ta khai bo : float b[40][50]; Khai bo trn cho ta mt mng b gm cc dng 50 phn t thc. Kiu a ch ca b l 50*4=200 byte. Do vy : b tr ti u dng th nht ( phn t b[0][0]). b+1 tr ti u dng th hai ( phn t b[1][0]). .......... b+i tr ti u dng th i ( phn t b[i][0]). Php truy cp b nh : Con tr float truy nhp ti 4 byte, con tr int truy nhp 2 byte, con tr char truy nhp 1 byte. Gi s ta c c khai bo : float *pf; int *pi; char *pc; Khi : Nu tr pi tr n byte th 100 th *pf biu th vng nh 4 byte lin tip t byte 100 n 103. Nu tr pi tr n byte th 100 th *pi biu th vng nh 2 byte lin tip t byte 100 n 101. Nu tr pc tr n byte th 100 th *pc biu th vng nh 1 byte chnh l byte 100.

76

Php so snh : Cho php so snh cc con tr cng kiu, v d nu p1 v p2 l cc con tr cng kiu th nu : p1<p2 nu a ch p1 tr ti thp hn a ch p2 tr ti. p1=p2 nu a ch p1 tr ti cng l a ch p2 tr ti. p1>p2 nu a ch p1 tr ti cao hn a ch p2 tr ti. V d : V d 1 : on chng trnh tnh tng cc s thc dng php so snh con tr : float a[100],*p,*pcuoi,tong=0.0; int n; pcuoi=a+n-1; s+=*p; V d 2 : Dng con tr char tch cc byte ca mt bin nguyn, ta lm nh sau : Gi s ta c lnh : unsigned int n=0xABCD; /* S nguyn h 16*/ char *pc; pc=(char*)(&n); Khi : *pc=0xAB (byte th nht ca n) *pc+1=0xCD (byte th hai ca n) 7.4.3. Con tr kiu void : Con tr kiu void c khai bo nh sau : void *tn_con_tr; y l con tr c bit, con tr khng kiu, n c th nhn bt k kiu no. Chng hn cu lnh sau l hp l : void *pa; /* a ch cui dy*/ for (p=a;p<=pcuoi;++p)

77

float a[20][30]; pa=a; Con tr void thng dng lm i nhn bt k a ch kiu no t tham s thc. Trong thn hm phi dng php chuyn i kiu chuyn sang dng a ch cn s l. Ch : Cc php ton tng gim a ch, so snh v truy cp b nh khng dng c trn con tr void. V d : Vit hm thc hin cng ma trn : void congmt(void *a,void *b,void *c,int N,int N, int m); { float *pa,*pb,*pc; int i,j; pa=(float*)a; pb=(float*)b; pc=(float*)c; for (i=1;i<m;++i) for (j=1;j<m;++j) *(pc+i*N+j)=*(pa+i*N+j)+*(pb+i*N+j); } V i l con tr void nn n c th nhn c a ch ca cc ma trn trong li gi hm. Tuy nhin ta khng th s dng trc tip cc i con tr void trong thn hm m phi chuyn kiu ca chng sang thnh float. 7.5. Mng con tr : Mng con tr l s m rng khi nim con tr. Mng con tr l mt mng m mi phn t ca n cha c mt a ch no . Cng ging nh con tr, mng con tr c nhiu kiu : Mi phn t ca mng con tr kiu int s cha c cc a ch kiu int. Tng t cho cc mng con tr ca cc kiu khc. Mng con tr c khai bo theo mu :

78

Kiu *Tn_mng_con_tr[N]; Trong Kiu c th l int, float, double, char ... cn Tn_mng_con_tr l tn ca mng, N l mt hng s nguyn xc nh ln ca mng. Khi gp khai bo trn, my s cp pht N khong nh lin tip cho N phn t ca mng Tn_mng_con_tr. V d : Lnh : double *pa[100]; Khai bo mt mng con tr kiu double gm 100 phn t. Mi phn t pa[i] c th dng lu tr mt a ch kiu double. Ch : Bn thn cc mng con tr khng dng lu tr s liu. Tuy nhin mng con tr cho php s dng cc mng khc lu tr s liu mt cch c hiu qu hn theo cch : chia mng thnh cc phn v ghi nh a ch u ca mi phn vo mt phn t ca mng con tr. Trc khi s dng mt mng con tr ta cn gn cho mi phn t ca n mt gi tr. Gi tr ny phi l gi tr ca mt bin hoc mt phn t mng. Cc phn t ca mng con tr kiu char c th c khi u bng cc xu k t. V d : Xt mt t lao ng c 10 ngi, m ca mi ngi chnh l s th t. Ta lp mt hm khi bit m s ca nhn vin th xc nh c h tn ca nhn vin . #include "stdio.h" #include "ctype.h" void tim(int code); main() { int i; tt:printf("\n Tim nguoi co so TT la :"); scanf("%d",&i); tim(i); printf("Co tiep tuc nua khong C/K : '); if (tupper(getch())='C')

79

goto tt; } void tim(int code); { static char *list[]= { "Khong co so thu tu nay " " Nguyen Van Toan" "Huynh Tuan Nghia" "Le Hong Son" "Tran Quang Tung" "Chu Thanh Tu" "Mac Thi Nga" "Hoang Hung" "Pham Trong Ha" "Vu Trung Duc" "Mai Trong Quat" }; printf("\n\n Ma so : %d",code); printf(": %s",()); } 7.6. Con tr ti hm : 7.6.1. Cch khai bo con tr hm v mng con tr hm : Ta s trnh by quy tc khai bo thng qua cc v d : V d 1: Cu lnh : float (*f)(float),(*mf[50])(int); khai bo : f l con tr hm kiu float c i l float mf l mng con tr hm kiu float c i kiu int ( c 50 phn t )

V d 2:

80

Cu lnh : double (*g)(int, double),(*mg[30])(double, float); khai bo : g l con tr hm kiu double c cc i kiu int v double mg l mng con tr hm kiu double c cc i kiu double v float ( c 30 phn t )

7.6.2. Tc dng ca con tr hm : Con tr hm dng cha a ch ca hm. Mun vy ta thc hin php gn tn hm cho con tr hm. php gn c ngha th kiu hm v kiu con tr phi tng thch. Sau php gn, ta c th dng tn con tr hm thay cho tn hm. V d 1: #include "stdio.h" double fmax(double x, double y ) /* Tnh max x,y */ { return(x>y ? x:y); } double (*pf)(double,double)=fmax; /*Khai bo v gn tn hm cho con tr hm */ main() /* S dng con tr hm*/ { printf("\n max=%f",pf(5.0,9.6)); } V d 2: #include "stdio.h" double fmax(double x, double y ) /* Tnh max x,y */ { return(x>y ? x:y); } double (*pf)(double,double); /* Khai bo con tr hm*/ main() /* S dng con tr hm*/ {

81

pf=fmax; printf("\n max=%f",pf(5.0,9.6)); } 7.6.3. i ca con tr hm : C cho php thit k cc hm m tham s thc trong li gi ti n li l tn ca mt hm khc. Khi tham s hnh thc tng ng phi l mt con tr hm. Cch dng con tr hm trong thn hm : Nu i c khai bo : double (*f)(double, int); th trong thn hm ta c th dng cc cch vit sau xc nh gi tr ca hm ( do con tr f tr ti ) : f(x,m) hoc (f)(x,m) hoc (*f)(x,m) y x l bin kiu double cn m l bin kiu int. V d : Dng mng con tr lp bng gi tr cho cc hm : x*x, sin(x), cos(x), exp(x) v sqrt(x). Bin x chay t 1.0 n 10.0 theo bc 0.5 #include "stdio.h" #include "math.h" double bp(double x) /* Hm tnh x*x */ { return x*x; } main() { int i,j; double x=1.0; typedef double (*ham)(double);

82

ham f[6]; /* Khai bao mng con tr hm*/ /* C th khai bo nh sau double (*f[6](double)*/ f[1]=bp; f[2]=sin; f[3]=cos; f[4]=exp; f[5]=sqrt; /* Gn tn hm cho cc phn t mng con tr hm */ while (x<=10.0) /* Lp bng gi tr */ { printf("\n"); for (j=1;j<=5;++j) printf("%10.2f ",f[j](x)); x+=0.5; } }

83

Chng 8 Cu trc Cu trc l tp hp ca mt hoc nhiu bin, chng c th khc kiu nhau, c nhm li di mt ci tn duy nht tin s l. Cu trc cn gi l bn ghi trong mt s ngn ng khc, chng hn nh PASCAL. Cu trc gip cho vic t chc cc d liu phc tp, c bit trong nhng chng trnh ln v trong nhiu tnh hung chng cho php nhm cc bin c lin quan li x l nh mt n v thay v cc thc th tch bit. Mt v d c cp nhiu n l cu trc phiu ghi lng, trong mi nhn vin c m t bi mt tp cc thuc tnh chng hn nh : tn, a ch, lng, ph cp vv.. mt s trong cc thuc tnh ny li c th l cu trc bi trong n c th cha nhiu thnh phn : Tn ( H, m, tn ), a ch ( Ph, s nh ) vv. Trong chng ny chng ta s minh ho cch s dng ca cc cu trc trong chng trnh. 8.1. Kiu cu trc : Khi xy dng cu trc, ta cn m t kiu ca n. iu ny cng tng t nh vic phi thit k ra mt kiu nh trc khi ta i xy dng nhng cn nh thc s cc a im khc nhau. Cng vic nh ngha mt kiu cu trc bao gm vic nu ra tn ca kiu cu trc v cc thnh phn ca n theo mu sau : struct tn_kiu _cu_trc { Khai bo cc thnh phn ca cu trc }; Trong : struct l t kho tn_kiu _cu_trc l mt tn bt k do ngi lp trnh t t theo qui tc t tn nu ra trong chng 1. Thnh phn ca cu trc c th l : bin, mng, cu trc khc c nh ngha trc vv.. V d : V d 1: (1)

84

on chng trnh : struct ngay { int ngaythu; char thang[12]; int nam; }; m t mt kiu cu trc c tn l ngay gm c ba thnh phn : Bin nguyn ngaythu, mng thang, v bin nguyn nam. V d 2: on chng trnh : struct nhancong { char ten[15]; char diachi[20] double bacluong; struc ngay ngaysinh; struc ngay ngaybatdaucongtac; }; to ra kiu cu trc c tn l nhancong gm c nm thnh phn. Ba thnh phn u khng c g cn ni thm. Ch c hai thnh phn cn li l cc cu trc ngaysinh v ngaybatdaucongtac c xy dng theo cu trc ngay c nh ngha trong v d 1. nh ngha cu trc bng typedef : C th dng ton t typedef nh ngha cc kiu cu trc ngay v nhancong trn nh sau : typedef struct { int ngaythu; char thang[12]; int nam;

85

} ngay; typedef struct { char ten[15]; char diachi[20] double bacluong; struc ngay ngaysinh; struc ngay ngaybatdaucongtac; } nhancong; 8.2. Khai bo theomt kiu cu trc nh ngha : Xy dng nhng cu trc thc s theo cc kiu khai bo trc . Vn ny hon ton ging nh vic khai bo cc bin v cc mng. Gi s ta c cc kiu cu trc ngay v nhancong nh trong mc trn. Khi ta khai bo : V d 1 : struct ngay ngaydi, ngayden; s cho ta hai cu trc vi tn l ngaydi v ngayden. C hai cu trc u c xy dng theo cu trc kiu ngay. V d 2 : struct nhancong nhom1,nhom2; s cho ta hai cu trc vi tn l nhom1 v nhom2. C hai cu trc u c xy dng theo cu trc kiu nhancong. Nh vy, mt cch tng qut, vic khai bo cu trc c thc hin theo mu sau : Cch 1 : struct tn_kiu_cu_trc__khai_bo danh_sch_tn_cc_cu_trc; (2) Ch :

86

Cc bin cu trc c khai bo theo mu trn s c cp pht b nh mt cch y cho tt c cc thnh phn ca n. Vic khai bo c th thc hin ng thi vi vic nh ngha kiu cu trc. Mun vy, ch cn t danh sch tn bin cu trc cn khai bo sau du } ca (* ) nh trn . Ni cch khc, va khai bo kiu va khai bo bin ta dng cch sau : Cch 2 : struct tn_kiu_cu_trc { Cc thnh phn ca cu trc } danh_sch_tn_cc_cu_trc; V d : V d 1 : struct ngay { int ngaythu; char thang[12]; int nam; } ngaydi,ngayden; V d 2 : struct nhancong { char ten[15]; char diachi[20]; double bacluong; struc ngay ngaysinh; struc ngay ngaybatdaucongtac; } nhom1,nhom2; (3)

87

Khi va nh ngha kiu cu trc va khai bo cu trc nh trong v d trn, ta khng th khng cn n tn kiu cu trc. Ni cch khc cu trc c th c khai bo theo cch sau : struct { Cc thnh phn ca cu trc } danh_sch_tn_cc_cu_trc; V d : struct { int ngaythu; char thang[12]; int nam; } ngaydi,ngayden; S khc nhau ca cc cch khai bo cu trc trong (3) v (4) l ch : Vi (3) ta va khai bo c mt kiu cu trc va khai bo c cc cu trc, v c th dng kiu cu trc ny khai bo cho cc cu trc khc nh trong (2), cn (4) ch khai bo c cc cu trc. Ch : Nu dng t kho typedef nh ngha kiu cu trc nh trong mc 8.1 th khi khai bo cc cu trc mi ta khng cn dng t kho struct, ch cn dng tn kiu. V d nh kiu cu trc ngay c khai bo bng typedef trong 8.1 th khi khai bo cc cu trc mi l ngaydi v ngayden c cng kiu ngay ta dng dng lnh sau : ngay ngaydi,ngayden; 8.3. Truy nhp n cc thnh phn cu trc : Ta kh quen vi vic s dng cc bin, cc phn t ca mng v tn mng trong cc cu lnh. Trn y ta cng cp n cc thnh phn ca cu trc l bin v mng. Vic x l mt cu trc bao gi cng phi c thc hin thng qua cc thnh phn ca n. truy cp n mt thnh phn c bn ( l bin hoc mng ) ca mt cu trc ta s dng mt trong cc cch vit sau : (4)

88

tn_cu_trc.tn_thnh_phn tn_cu_trc.tn_cu_trc.tn_thnh_phn tn_cu_trc. tn_cu_trc.tn_cu_trc.tn_thnh_phn ..... Cch vit th nht nh trn c s dng khi bin hoc mng l thnh phn trc tip ca mt cu trc. V d nh bin ngaythu, bin nam v mng thang l cc thnh phn trc tip ca cc cu trc ngaydi, ngayden. Cc bin bacluong, cc mng ten, diachi l cc thnh phn trc tip ca cc cu trc nhancong. Cc cch vit cn li nh trn c s dng khi bin hoc mng l thnh phn trc tip ca mt cu trc m bn thn cu trc ny li l thnh phn ca cc cu trc ln hn. V d : Ta xt php ton trn cc thnh phn ca cu trc nhom1, nhom2 : Cu lnh : printf("%s",nhom1.ten); s a ln mn hnh tn ca nhom1. Cu lnh : tongluong=nhom1.bacluong+nhom2.bacluong; s gn tng lng ca nhom1 v nhom2 ri gn cho bin tongluong. Cu lnh : printf("%d",nhom1.ngaysinh.ten); s a ln mn hnh ngy sinh ca nhom1. Cu lnh : printf("%d",nhom1. ngaybatdaucongtac.nam); s a ln mn hnh ngy bt u cng tc ca nhom1. Ch : C th s dng php ton ly a ch i vi cc thnh phn cu trc nhp s liu trc tip vo cc thnh phn cu trc. V d nh ta vit : scanf("%d",&nhom1. ngaybatdaucongtac.nam); Nhng i vi cc thnh phn khng nguyn, vic lm trn c th dn n treo my. V th nn nhp s liu vo mt bin trung gian sau mi gn cho thnh phn ca cu trc.

89

Cch lm nh sau : int year; scanf("%d",&year); nhom1. ngaybatdaucongtac.nam=year; trnh di dng khi lm vic vi cc thnh phn cu trc ta c th dng lnh #define. V d trong cu lnh scanf v d trn, ta c th vit nh sau : #define p nhom1. ngaybatdaucongtac ..... scanf("%d",&p.nam); V d : Gi s ta lp trnh qun l thng tin cn b. Gi s mi d liu ca mt cn b gm : Ngy thng nm sinh. Ngy thng nm vo c quan. Bc lng. Xy dng cu trc c s d liu cho cn b. Vo s lu ca mt cn b. a s liu ra my in.

Yu cu vit mt chng trnh :

Chng trnh c vit nh sau : #include "stdio.h" typedef struct { int ngay; char thang[10]; int nam; } date; typedef struct { date ngaysinh; date ngayvaocq; float luong; } canbo;

90

main() { canbo p; printf("\n Sinh ngay : "); scanf("%d",&p.ngaysinh.ngay); printf("\n Thang : "); scanf("%d",&p.ngaysinh.thang); printf("\n Nam : "); scanf("%d",&p.ngaysinh.nam); printf("\n Vao co quan ngay : "); scanf("%d",&p.ngayvaocq.ngay); printf("\n Thang : "); scanf("%d",&p.ngayvaocq.thang); printf("\n Nam : "); scanf("%d",&p.ngayvaocq.nam); printf("\n Luong : "); scanf("%d",&p.luong); fprintf(stdprn,"\n Ngay sinh:%d%s%d",p.ngaysinh.ngay,p.ngaysinh.thang, p.ngaysinh.nam); fprintf(stdprn,"\n Ngay vao co quan:%d%s%d",p.ngayvaocq.ngay, p.ngayvaocq.thang,p.ngayvaocq.nam); fprintf(stdprn,"\n Luong : %8.2f",p.luong); } 8.4. Mng cu trc : Nh cp cc chng trc, khi s dng mt kiu gi tr ( v d nh kiu int ) ta c th khai bo cc bin v cc mng kiu . V d nh khai bo : int a,b,c[10]; cho ta hai bin nguyn l a,b v mt mng nguyn c c 10 phn t. Hon ton tng t nh vy : ta c th s dng mt kiu cu trc m t khai bo cc cu trc v mng cu trc. Cch khai bo mng cu trc : struct tn_kiu_cu_trc__nh_ngha tn_mng_cu_trc[s phn t ca mng];

91

V d : V d 1 : Gi s kiu cu trc canbo c nh ngha nh mc trn. Khi dng khai bo : struct canbo cb1,cb2,nhom1[10],nhom2[7]; s cho : Hai bin cu trc cb1 v cb2. Hai mng cu trc nhom1 co 10 phn t v nhom2 c 7 phn t v mi phn t nhm ny c kiu canbo. V d 2 : on chng trnh sau s tnh tng lng cho cc phn t nhm 1: double tongluong=0; for (i=0;i<10;++i) tongluong+=nhom1[i].luong; Ch : Khng cho php s dng php ton ly a ch i vi cc thnh phn ca mng cu trc khc kiu nguyn. Chng hn khng cho php s dng cu lnh sau : scanf("%f",&nhom1[5].luong); Trong trng hp ny ta dng bin trung gian. 8.5. Khi u mt cu trc : C th khi u cho mt cu trc ngoi, cu trc tnh, mng cu trc ngoi v mng cu trc tnh 8.6. Php gn cu trc : C th thc hin php gn trn cc bin v phn t mng cu trc cng kiu nh sau : Gn hai bin cu trc cho nhau Gn bin cu trc cho phn t mng cu trc Gn phn t mng cu trc cho bin cu trc Gn hai phn t mng cu trc cho nhau ca hai

Mi mt php gn trn tng ng vi mt dy php gn cc thnh phn tng ng.

92

V d : on chng trnh sau minh ho cch dng php gn cu trc sp xp n th sinh theo th t gim ca tng im : struct thisinh { char ht[25]; float td; } tg,ts[100]; for (i=1;i<=n-1;++i) for (j=1;j<=n;++j) if (ts[i].td<ts[j].td) { tg=ts[i]; ts[i]=ts[j]; ts[j]=tg; } 8.7. Con tr cu trc v a ch cu trc : 8.7.1. Con tr v a ch : Ta xt v d sau : struct ngay { int ngaythu; char thang[10]; int nam; }; struct nhancong { char ten[20]; char diachi[25]; double bacluong; struct ngay ngaysinh; };

93

Nu khai bo : struct nhancong *p,*p1,*p2,nc1,nc2,ds[100]; ta c : p, p1, p2 l con tr cu trc nc1, nc2 l cc bin cu trc ds l mng cu trc

Con tr cu trc dng lu tr a ch ca bin cu trc v mng cu trc. V d : p1=&nc1; p2=&ds[4]; p=ds; 8.7.2. Truy nhp qua con tr: C th truy nhp n cc thnh phn thng qua con tr theo mt trong hai cch sau : Cch mt : Tn_con_tr->Tn_thnh_phn Cch hai : (*Tn_con_tr).Tn_thnh_phn V d : nc1.ngaysinh.nam p1-> ngaysinh.nam ds[4].ngaysinh.thang (*p2). ngaysinh.thang 8.7.3. Php gn qua con tr: Gi s ta gn : p1=&nc1; p2=&ds[4]; Khi c th dng : *p1 thay cho nc1 /* Gi a ch nc1 vo p1 */ /* Gi a ch ds[4] vo p2 */ /* Gi a ch ds[0] vo p */

94

*p2 thay cho ds[4] Tc l vit: ds[5]=nc1; ds[4]=nc2; Tng ng vi : ds[5]=*p1; *p2=nc2; 8.7.4. Php cng a ch : Sau cc php gn : p=ds; p2=&ds[4]; th p tr thi ds[[0]] v p2 tr ti ds[4]. Ta c th dng cc php cng, tr a ch lm cho p v p2 tr ti cc thnh phn bt k no khc. V d : Sau cc lnh : p=p+10; p2=p2-4; th p tr ti ds[10] cn p2 tr ti ds[0] 8.7.5. Con tr v mng : Gi s con tr p tr ti u mng ds, khi : Ta c th truy nhp ti cc thnh phn cu trc bng cc cch sau : + ds[i].thnh_phn + p[i].thnh_phn ds[i].ngaysinh.nam p[i].ngaysinh.nam

+ (p+i)->thnh_phn (p+i)->ngaysinh.nam Khi ta s dng c cu trc th cc cch vit sau l tng ng : ds[i] p[i] *(p+i)

8.8. Cu trc t tr v danh sch lin kt :

95

Khi ta lp mt chng trnh qun l m bn thn s bin (cu trc) cha c bit trc, nu ta s dng mng ( cp pht b nh tnh ) th ta phi s dng s cc phn t l ti a. Nh vy s c rt nhiu vng nh c cp pht m khng bao gi dng n. Lc ta c cch cp pht b nh ng. S vng nh cp ra s bin cn dng. Cu trc c t nht mt thnh phn l con tr kiu cu trc ang nh ngha gi l cu trc t tr. V d : Cc cch nh ngha cu trc t tr person: Cch 1 : typedef struct pp { char ht[20]; char qq[25]; int tuoi; struct pp *tiep; } person; Cch 2 : typedef struct pp person struct pp { char ht[20]; char qq[25]; int tuoi; person *tiep; }; Cch 3 : struct pp { char ht[20]; char qq[25];

96

int tuoi; struct pp *tiep; }; typedef pp person; Cu trc t tr c dng xy dng danh sch lin kt ( mc ni ), l mt nhm cc cu trc c tnh cht sau : ( Mc ni theo chiu thun ). Bit a ch cu trc u ang c lu tr trong mt con tr no . Trong mi cu trc ( tr cu trc cui ) cha a ch ca cu trc tip sau ca danh sch. V d : Cu trc cui cha hng NULL.

......... Pdau NULL

Vi danh sch ny, ta c th ln lt t cu trc u n cu trc cui theo chiu t trn xung di. Nhm cu trc mc ni theo chiu ngc c tnh cht sau : Bit a ch cu trc cui. Trong mi cu trc ( tr cu trc u ) u ch a ch ca cu trc trc. Cu trc u cha hng NULL.

Vi danh sch ny, ta c th ln lt t cu trc cui ln cu trc u theo chiu t di ln trn. Ngoi ra, ta c th xy dng cc danh sch m mi phn t cha hai a ch ca cu trc trc v cu trc sau. Vi loi danh sch ny, ta c th truy nhp theo c hai chiu trn. Khi lm vic vi danh sch mc ni, ta thng phi tin hnh cc cng vic sau sau : ( Gi s ta c con tr p, tr pdau ch cu trc u ca danh sch, con tr tiep l thnh phn con tr ca cu trc ) To danh sch mi : Cp pht b nh cho mt cu trc Nhp mt bin cu trc vo vng nh va cp Gn a ch ca cu trc sau cho thnh phn con tr ca cu trc trc

97

Duyt qua tt c cc phn t ca danh sch : a tr p v tr cng cu trc vi pdau bng lnh : p=pdau chuyn tip n ngi tip theo ta dng lnh : p=p->tiep Du hiu bit ang xt cu trc cui cng ca danh sch l : p->tiep==NULL Loi mt cu trc ra khi danh sch : Lu tr a ch ca cu trc cn loi vo mt con tr ( gii phng b nh ca cu trc ny) Sa cu trc trc c a ch ca cu trc cn loi Gii phng b nh cu trc cn loi

B xung hoc chn mt cu trc vo danh sch: Cp pht b nh v nhp b xung Sa thnh phn con tr trong cc cu trc c lin quan m bo mi cu trc cha a ch ca cu trc tip theo Hm cp pht b nh : void *malloc(kichthuoc_t kichthuoc); Hm ly trong th vin alloc.h hoc stdlib.h. kichthuoc tnh bng s by te. Hm s a con tr v v tr nh va c cp hoc v NULL nu khng b nh cn thit. Nu kichthuoc == 0 th n tr v NULL. V d : #include "stdio.h" #include "string.h" #include "alloc.h" #include "process.h" int main() { char *str; /* Cp pht b nh cho xu k t */

98

if ((str = malloc(10)) == NULL) { printf("Not enough memory to allocate buffer\n"); exit(1); /* Kt thc chng trnh nu thiu b nh */ } /* copy "Hello" vo xu */ strcpy(str, "Hello"); /* Hin th xu */ printf("String is %s\n", str); /* Gii phng b nh */ free(str); return 0; } V d : To mt danh sch lin kt. Cc bin cu trc gm cc trng : H tn, Qu qun, tui, v mt trng con tr l Tip. Mc ni theo chiu thun (Vo trc ra trc FIFO first in first out ): #include "stdio.h" #include "alloc.h" #include "conio.h" #include "string.h" typedef struct pp { char ht[25]; char qq[20]; int tuoi; struct pp *tiep; } nhansu; main() { char tt; nhansu *pdau,*pcuoi,*p; char tam[10];

99

clrscr(); pdau=NULL; do { p=(nhansu*)malloc(sizeof(nhansu)); printf("\n Ho ten : "); gets(p->ht); printf(" Que quan : "); gets(p->qq); printf(" Tuoi: "); gets(tam); p->tuoi=atoi(tam); if (pdau==NULL) { pdau=p; pcuoi=p; p->tiep=NULL; } else { pcuoi->tiep=p; pcuoi=p; p->tiep=NULL; } printf("\nBam phim bat ky de tiep tuc, ESC de dung"); tt=getch(); } while(tt!=27) ; /* a danh sch lin kt ra mn hnh, tr pdau tro */ printf("\n Danh sach nhu sau :\n"); p=pdau; while (p!=NULL) { printf("\n Ho ten: %25s Que : %20s Tuoi : %d",(*p).ht,(*p).qq,(*p).tuoi);

100

p=p->tiep; } getch(); } Mc ni theo chiu ngc (Vo sau ra trc LIFO last in first out ): #include "stdio.h" #include "alloc.h" #include "conio.h" #include "string.h" typedef struct pp { char ht[25]; char qq[20]; int tuoi; struct pp *tiep; } nhansu; main() { char tt; nhansu *pdau,*pcuoi,*p; char tam[10]; clrscr(); pdau=NULL; do { p=(nhansu*)malloc(sizeof(nhansu)); printf("\n Ho ten : "); gets(p->ht); printf(" Que quan : "); gets(p->qq); printf(" Tuoi: "); gets(tam); p->tuoi=atoi(tam); if (pdau==NULL)

101

{ pdau=p; pcuoi=p; p->tiep=NULL; } else { p->tiep=pcuoi; pcuoi=p; } printf("\nBam phim bat ky de tiep tuc, ESC de dung"); tt=getch(); } while(tt!=27) ; /* a danh sch lin kt ra mn hnh, tr pdau tro */ printf("\n Danh sach nhu sau :\n"); p=pcuoi; while (p!=NULL) { printf("\n Ho ten: %25s Que : %20s Tuoi : %d",(*p).ht,(*p).qq,(*p).tuoi); p=p->tiep; } getch(); }

102

Chng 9 tp tin - file 9.1. Khi nim v tp tin : Tp tin hay tp d liu l mt tp hp cc d liu c lin quan vi nhau v c cng mt kiu c nhm li vi nhau thnh mt dy. Chng thng c cha trong mt thit b nh ngoi ca my tnh (a mm, a cng...) di mt ci tn no . Tn ting Anh ca tp l file, n c dng ch ra mt hp ng cc phiu hay th ghi ca th vin. Mt hnh nh r nt gip ta hnh dung ra tp l t phiu ca th vin. Mt hp c nhiu phiu ging nhau v hnh thc v t chc, song li khc nhau v ni dung. y, t phiu l tp, cc l phiu l cc thnh phn ca tp. Trong my tnh, mt a cng hoc mt a mm ng vai tr chic t ( cha nhiu tp). Tp c cha trong b nh ngoi, iu c ngha l tp c lu tr dng nhiu ln v tn ti ngay c khi chng trnh kt thc hoc mt in. Chnh v l do trn, ch nhng d liu no cn lu tr ( nh h s chng hn) th ta nn dng n tp. Tp l mt kiu d liu c cu trc. nh ngha tp c phn no ging mng ch chng u l tp hp ca cc phn t d liu cng kiu, song mng thng c s phn t c nh, s phn t ca tp khng c xc nh trong nh ngha. Trong C, cc thao tc tp c thc hin nh cc hm th vin. Cc hm ny c chia lm hai nhm : nhm 1 v nhm 2. Cc hm cp 1 l cc hm nhp / xut h thng, chng thc hin vic c ghi nh DOS. Cc hm cp 2 lm vic vi tp thng qua mt bin con tr tp. Do cc hm cp 2 c nhiu kiu truy xut v d dng hn so vi cc hm cp 1 nn trong cc chng trnh vit trong C, cc hm cp 2 hay c s dng hn. Mt tp tin d c xy dng bng cch no i na cng ch n gin l mt dy cc byte ghi trn a (c gi tr t 0 n 255). S byte ca dy chnh l di ca tp. C hai kiu nhp xut d liu ln tp : Nhp xut nh phn v nhp xut vn bn. Nhp xut nh phn : D liu ghi ln tp theo cc byte nh phn nh b nh, trong qu trnh nhp xut, d liu khng b bin i. Khi c tp, nu gp cui tp th ta nhn c m kt thc tp EOF ( c nh ngha trong stdio.h bng -1) v hm feof cho gi tr khc 0.

103

Nhp xut vn bn: Kiu nhp xut vn bn ch khc kiu nh phn khi x l k t chuyn dng ( m 10) v k t m 26. i vi cc k t khc, hai kiu u c ghi nh nhau. LF Khi c, 2 k t lin tip CR v LF trn tp ch cho ta mt k t LF M kt thc tp : Trong khi c, nu gp k t c m 26 hoc cui tp th ta nhn c m kt thc tp EOF ( bng -1) v hm feof(fp) cho gi tr khc 0 ( bng 1). 9.2. Khai bo s dng tp - mt s hm thng dng khi thao tc trn tp : 9.2.1. Khai bo s dng tp : khai bo s dng tp, ta dng lnh sau : FILE bin_con_tr_tp; Trong bin_con_tr_tp c th l bin n hay mt danh sch cc bin phn cch nhau bi du phy ( du , ). V d : FILE *vb, *np; /* Khai bo hai bin con tr tp */ 9.2.2. M tp - hm fopen : Cu trc ng php ca hm : FILE *fopen(const char *tn_tp, const char *kiu); Nguyn hm trong : stdio.h . Trong : i th nht l tn tp, i th hai l kiu truy nhp. Cng dng : M chuyn dng : Khi ghi, mt k t LF (m 10) c chuyn thnh 2 k t CR (m 13) v

104

Hm dng m tp. Nu thnh cng hm cho con tr kiu FILE ng vi tp va m. Cc hm cp hai s lm vic vi tp thng qua con tr ny. Nu c li hm s tr v gi tr NULL. Bng sau ch ra cc gi tr ca kiu : Tn kiu "r" "rt" "w" "wt" "a" "at" "rb" "wb" "ab" "r+" "r+t" "w+" "w+t" "a+" "a+t" "r+b" "w+b" "a+b" ngha M mt tp c theo kiu vn bn. Tp cn c phi tn ti, nu khng s c li M mt tp ghi theo kiu vn bn. Nu tp tn ti th n s b xo. M mt tp ghi b xung theo kiu vn bn. Nu tp cha tn ti th to tp mi. M mt tp c theo kiu nh phn. Tp cn c phi tn ti, nu khng s c li. M mt tp mi ghi theo kiu nh phn. Nu tp tn ti th n s b xo. M mt tp ghi b xung theo kiu nh phn. Nu tp cha tn ti th to tp mi. M mt tp c/ghi theo kiu vn bn. Tp cn c phi tn ti, nu khng s c li M mt tp c/ghi theo kiu vn bn. Nu tp tn ti th n s b xo. M mt tp c/ghi b xung theo kiu vn bn. Nu tp cha tn ti th to tp mi. M mt tp c/ghi theo kiu nh phn. Tp cn c phi tn ti, nu khng s c li. M mt tp mi c/ghi theo kiu nh phn. Nu tp tn ti th n s b xo. M mt tp c/ghi b xung theo kiu nh phn. Nu tp cha tn ti th to tp mi. Ch : Trong cc kiu c ghi, ta nn lm sch vng m trc khi chuyn t c sang ghi hoc ngc li. Ta s cp n cc hm vi tnh nng xo sau ny. V d :

105

f=fopen("TEPNP","wb"); 9.2.3. ng tp - hm fclose : Cu trc ng php ca hm : int fclose(FILE *fp); Nguyn hm trong : stdio.h . Trong : fp l con tr ng vi tp cn ng. Cng dng : Hm dng ng tp khi kt thc cc thao tc trn n. Khi ng tp, my thc hin cc cng vic sau : Khi ang ghi d liu th my s y d liu cn trong vng m ln a Khi ang c d liu th my s xo vng m Gii phng bin tr tp. Nu lnh thnh cng, hm s cho gi tr 0, tri li n cho hm EOF.

V d : fclose(f); 9.2.4. ng tt c cc tp ang m- hm fcloseall : Cu trc ng php ca hm : int fcloseall(void); Nguyn hm trong : stdio.h . Cng dng : Hm dng ng tt c cc tp ang m . Nu lnh thnh cng, hm s cho gi tr bng s l s tp c ng, tri li n cho hm EOF. V d : fcloseall(); 9.2.5. Lm sch vng m - hm fflush :

106

Cu trc ng php ca hm : int fflush(FILE *fp); Nguyn hm trong : stdio.h . Cng dng : Dng lm sch vng m ca tp fp. Nu lnh thnh cng, hm s cho gi tr 0, tri li n cho hm EOF. V d : fflush(f); 9.2.6. Lm sch vng m ca cc tp ang m - hm fflushall : Cu trc ng php ca hm : int fflushall(void); Nguyn hm trong : stdio.h . Cng dng : Dng lm sch vng m ca tt c cc tp ang m. Nu lnh thnh cng, hm s cho gi tr bng s cc tp ang m, tri li n cho hm EOF. V d : fflushall(); 9.2.7. Kim tra li file - hm ferror : Cu trc ng php ca hm : int ferror(FILE *fp); Nguyn hm trong : stdio.h . Trong fp l con tr tp. Cng dng : Hm dng kim tra li khi thao tc trn tp fp. Hm cho gi tr 0 nu khng c li, tri li hm cho gi tr khc 0.

107

9.2.8. Kimtra cui tp - hm feof : Cu trc ng php ca hm : int feof(FILE *fp); Nguyn hm trong : stdio.h . Trong fp l con tr tp. Cng dng : Hm dng kim tra cui tp. Hm cho gi tr khc 0 nu gp cui tp khi c, tri li hm cho gi tr 0. 9.2.9. Truy nhp ngu nhin - cc hm di chuyn con tr ch v : 9.2.7.1. Chuyn con tr ch v v u tp - Hm rewind : Cu trc ng php : void rewind(FILE *fp); Nguyn hm trong : stdio.h . Trong fp l con tr tp. Cng dng : Chuyn con tr ch v ca tp fp v u tp. Khi vic nhp xut trn tp fp c thc hin t u. V d : rewind(f); 9.2.9.2. Chuyn con tr ch v tr cn thit - Hm fseek : Cu trc ng php : int fseek(FILE *fp, long sb, int xp); Nguyn hm trong : stdio.h . Trong fp l con tr tp. sb l s byte cn di chuyn.

108

xp cho bit v tr xut pht m vic dch chuyn c bt u t . xp c th nhn cc gi tr sau : xp=SEEK_SET hay 0 : Xut pht t u tp. xp=SEEK_CUR hay 1: Xut pht t v tr hin ti ca con tr ch v. xp=SEEK_END hay 2 : Xut pht t cui tp. Cng dng : Chuyn con tr ch v ca tp fp v v tr xc nh bi xp qua mt s byte xc nh bng gi tr tuyt i ca sb. Chiu di chuyn l v cui tp nu sb dng, tri li n s di chuyn v u tp. Khi thnh cng, hm tr v gi tr 0. Khi c li hm tr v gi tr khc khng. Ch : Khng nn dng fseek trn tp tin vn bn, do s chuyn i k t s lm cho vic nh v thiu chnh xc. V d : fseek(stream, SEEK_SET, 0); 9.2.9.3. V tr hin ti ca con tr ch v - Hm ftell : Cu trc ng php : int ftell(FILE *fp); Nguyn hm trong : stdio.h . Trong fp l con tr tp. Cng dng : Hm cho bit v tr hin ti ca con tr ch v (byte th my trn tp fp) khi thnh cng. S th t tnh t 0. Tri li hm cho gi tr -1L. V d : Sau lnh Sau lnh fseek(fp,0,SEEK_END); fseek(fp,-1,SEEK_END); ftell(fp) cho gi tr 3. ftell(fp) cho gi tr 2.

109

9.2.10. Ghi cc mu tin ln tp - hm fwrite : Cu trc ng php ca hm : int fwrite(void *ptr, int size, int n, FILE *fp); Nguyn hm trong : stdio.h . Trong : ptr l con tr tr ti vng nh cha d liu cn ghi. size l kch thc ca mu tin theo byte n fp l s mu tin cn ghi l con tr tp

Cng dng : Hm ghi n mu tin kch thc size byte t vng nh ptr ln tp fp. Hm s tr v mt gi tr bng s mu tin thc s ghi c. V d : #include "stdio.h" struct mystruct { int i; char ch; }; main() { FILE *stream; struct mystruct s; stream = fopen("TEST.TXT", "wb") /* M tp TEST.TXT */ s.i = 0; s.ch = 'A'; fwrite(&s, sizeof(s), 1, stream); /* Vit cu trc vo tp */ fclose(stream); /* ng tp */ return 0; }

110

9.2.11. c cc mu tin t tp - hm fread : Cu trc ng php ca hm : int fread(void *ptr, int size, int n, FILE *fp); Nguyn hm trong : stdio.h . Trong : ptr l con tr tr ti vng nh cha d liu cn ghi. size l kch thc ca mu tin theo byte n fp l s mu tin cn ghi l con tr tp

Cng dng : Hm c n mu tin kch thc size byte t tp fp ln ln vng nh ptr. Hm s tr v mt gi tr bng s mu tin thc s c c. V d : #include "string.h" #include "stdio.h" main() { FILE *stream; char msg[] = "Kim tra"; char buf[20]; stream = fopen("DUMMY.FIL", "w+"); /* Vit vi d liu ln tp */ fwrite(msg, strlen(msg)+1, 1, stream); /* Tm im u ca file */ fseek(stream, SEEK_SET, 0); /* c s liu v hin th */ fread(buf, strlen(msg)+1, 1, stream); printf("%s\n", buf); fclose(stream); return 0;

111

} 9.2.10. Nhp xut k t : 9.2.10.1. Cc hm putc v fputc : Cu trc ng php : int putc(int ch, FILE *fp); int fputc(int ch, FILE *fp); Nguyn hm trong : stdio.h . Trong : ch l mt gi tr nguyn fp l mt con tr tp. Cng dng : Hm ghi ln tp fp mt k t c m bng m=ch % 256. ch c xem l mt gi tr nguyn khng du. Nu thnh cng hm cho m k t c ghi, tri li cho EOF V d : #include "stdio.h" main() { char msg[] = "Hello world\n"; int i = 0; while (msg[i]) putc(msg[i++], stdout); /* stdout thit b ra chun - Mn hnh*/ return 0; } 9.2.12.2. Cc hm getc v fgettc : Cu trc ng php : int gretc(FILE *fp); int fputc(FILE *fp);

112

Nguyn hm trong : stdio.h . Trong : fp l mt con tr tp. Cng dng : Hm c mt k t t tp fp. Nu thnh cng hm s cho m c c ( c gi tr t 0 n 255). Nu gp cui tp hay c li hm s tr v EOF. Trong kiu vn bn, hm c mt lt c hai m 13, 10 v tr v gi tr 10. Khi gp m 26 hm s tr v EOF. V d : #include "string.h" #include "stdio.h" #include "conio.h" main() { FILE *stream; char string[] = "Kiem tra"; char ch; /* M tp cp nht*/ stream = fopen("DUMMY.FIL", "w+"); /*Vit mt xu k t vo tp */ fwrite(string, strlen(string), 1, stream); /* Tm v tr u ca tp */ fseek(stream, 0, SEEK_SET); do { /* c mt k t t tp */ ch = fgetc(stream); /* Hin th k t */ putch(ch); } while (ch != EOF); fclose(stream);

113

return 0; } 9.2.13. Xo tp - hm unlink: Cu trc ng php : int unlink(const char *tn_tp) Nguyn hm trong : dos.h, io.h, stdio.h . Trong tn_tp l tn ca tp cn xo. Cng dng : Dng xo mt tp trn a. Nu thnh cng, hm cho gi tr 0, tri li hm cho gi tr EOF. V d : #include <stdio.h> #include <io.h> int main(void) { FILE *fp = fopen("junk.jnk","w"); int status; fprintf(fp,"junk"); status = access("junk.jnk",0); if (status == 0) printf("Tp tn ti\n"); else printf("Tp khng tn ti\n"); fclose(fp); unlink("junk.jnk"); status = access("junk.jnk",0); if (status == 0) printf("Tp tn ti\n"); else printf("Tp khng tn ti\n"); return 0;

114

115

Chng 10 ho Chng ny s gii thiu cc hm v th tc khi ng h ho, v cc ng v hnh c bn nh hnh trn, cung elip, hnh qut, ng gy khc, a gic, ng thng, hnh ch nht, hnh hp ch nht.... Cc hm v th tc ho c khai bo trong file graphics.h. 10.1. Khi ng ho : Mc ch ca vic khi ng h thng ho l xc nh thit b ho (mn hnh) v mode ho s s dng trong chng trnh. lm cng vic ny, ta c hm sau : void initgraph(int *graphdriver,int graphmode,char *driverpath); Trong : driverpath l xu k t ch ng dn n th mc cha cc tp tin iu khin ho. graphdriver cho bit mn hnh ho s dng trong chng trnh. graphmode cho bit mode ho s dng trong chng trnh. graphmode CGAC0 (0) CGAC1 (1) CGAC2 (2) CGAC3 (3) CGAHi (4) MCGA (2) MCGA0 (0) MCGA1 (1) MCGA2 (2) MCGA3 (3) MCGAMed (4) MCGAHi (5) EGA (3) EGA64 (4) EGAL0 (0) EGAHi (1) EGA64LO (0) phn gii 320x200 320x200 320x200 320x200 640x200 320x200 320x200 320x200 320x200 640x200 640x480 640x200 640x350 640x200

Bng di y cho cc gi tr kh d ca graphdriver v graphmode : graphdriver DETECT (0) CGA (1)

116

EGA64Hi (1) EGAMONO (5) VGA (9) EGAMONOHi (0) VGALO (0) VGAMED (1) VGAHI (2) HERCMONO (7) ATT400 (8) HERCMONOHI ATT400C0 (0) ATT400C1 (1) ATT400C2 (2) ATT400C3 (3) ATT400MED (4) ATT400HI (5) PC3270 (10) IBM8514 (6) PC3270HI (0) PC3270LO (0) PC3270HI (1) Ch :

640x350 640x350 640x200 640x350 640x480 720x348 320x200 320x200 320x200 320x200 640x400 640x400 720x350 640x480 256 mu 1024x768 256 mu

Bng trn cho ta cc hng v gi tr ca chng m cc bin graphdtriver v graphmode c th nhn. Chng hn hng DETECT c gi tr 0, hng VGA c gi tr 9, hng VGALO c gi tr 0 vv... Khi lp trnh ta c th thay th vo v tr tng ng ca chng trong hm tn

hng hoc gi tr ca hng . V d : Gi s my tnh c mn hnh VGA, cc tp tin ho cha trong th mc C:\TC \BGI, khi ta khi ng h thng ho nh sau : #include "graphics.h" main() { int mh=VGA,mode=VGAHI; /*Hoc mh=9,mode=2*/ initgraph(&mh,&mode,"C:\\TC\\BGI"); /* V k t \ trong C l k t c bit nn ta phi gp i n */ }

117

Bng trn cn cho thy phn gii cn ph thuc c vo mn hnh v mode. V d nh trong mn hnh EGA nu dng EGALo th phn gii l 640x200 ( Hm getmaxx() cho gi tr cc i ca s im theo chiu ngang ca mn hnh. Vi mn hnh EGA trn : 639, Hm getmaxy() cho gi tr cc i ca s im theo chiu dc ca mn hnh. Vi mn hnh EGA trn : 199 ). Nu khng bit chnh xc kiu mn hnh ang s dng th ta gn cho bin graphdriver bng DETECT hay gi tr 0. Khi , kt qu ca initgraph s l : Kiu mn hnh ang s dng c pht hin, gi tr ca n c gn cho bin graphdriver. Mode ho phn gii cao nht ng vi mn hnh ang s dng cng c pht hin v tr s ca n c gn cho bin graphmode. Nh vy dng hng s DETECT chng nhng c th khi ng c h thng ho vi mn hnh hin c theo mode c phn gii cao nht m cn gip ta xc nh kiu mn hnh ang s dng.

V d : Chng trnh di y xc nh kiu mn hnh ang s dng : #include "graphics.h" #include "stdio.h" main() { int mh=0, mode; initgraph(&mh,&mode,"C:\\TC\\BGI"); printf("\n Gia tri so cua man hinh la : %d",mh); printf("\n Gia tri so mode do hoa la : %d",mode); closegraph(); } Nu chui dng xc nh driverpath l chui rng th chng trnh dch s tm kim cc file iu khin ho trn th mc ch ( Th mc hin thi ). 10.2. Cc hm ho : 10.2.1. Mu v mu : t mu nn : t mu cho nn ta dng th tc sau :

118

void setbkcolor(int mu); t mu ng v : t mu v ng ta dng th tc sau : void setcolor(int mu); t mu (kiu) t v mu t : t mu (kiu) t v mu t ta dng th tc sau : void setfillstyle(int mu, int mu); Trong c ba trng hp mu xc nh m ca mu. Cc gi tr kh d ca mu cho bi bng di y : Bng cc gi tr kh d ca mu Tn hng BLACK BLUE GREEN CYAN RED MAGENTA BROWN LIGHTGRAY DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN LIGHTRED LIGHTMAGENTA YELLOW WHITE Gi tr s 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 Mu hin th en Xanh da tri Xanh l cy Xanh l Tm Nu Xm nht Xm m Xanh xa tri nht Xanh l cy nht Xanh l nht nht Tm nht Vng Trng

Cc gi tr kh d ca mu cho bi bng di y : Bng cc gi tr kh d ca mu Tn hng EMPTY_FILL SOLID_FILL Gi tr s 0 1 Kiu mu t T bng mu nn T bng ng lin nt

119

LINE_FILL LTSLASH_FILL SLASH_FILL BKSLASH_FILL LTBKSLASH_FILL HATCH_FILL XHATCH_FILL INTERLEAVE_FILL WIDE_DOT_FILL CLOSE_DOT_FILL

2 3 4 5 6 7 8 9 10 11

T bng ng -------T bng /// T bng /// in m T bng \\\ in m T bng \\\ T bng ng gch bng nht T bng ng gch bng ch thp T bng ng t qung T bng du chm tha T bng du chm mau

Chn gii mu : thay i gii mu c nh ngha trong bng trn, ta s dng hm : void setpalete(int s_th_t_mu, int mu ); V d : Cu lnh : setpalete(0,lightcyan); bin mu u tin trong bng mu thnh mu xanh l nht. Cc mu khc khng b nh hng. Ly gii mu hin thi : + Hm getcolor tr v mu xc nh bng th tc setcolor ngay trc n. + Hm getbkcolor tr v mu xc nh bng hm setbkcolor ngay trc n. 10.2.2. V v t mu : C th chia cc ng v hnh thnh bn nhm chnh : Cung trn v hnh trn. ng gp khc v a gic. ng thng. Hnh ch nht.

10.2.2.1. Cung trn v ng trn :

120

Nhm ny bao gm : Cung trn, ng trn, cung elip v hnh qut. Cung trn : v mt cung trn ta dng hm : void arc(int x, int y, int gd, int gc, int r); Trong : (x,y) l to tm cung trn. gd l gc u cung trn(0 n 360 ). gc l gc cui cung trn (gd n 360 ). r l bn knh cung trn . V d : V mt cung trn c tm ti (100,50), gc u l 0, gc cui l 180, bn knh 30. arc(100,50,0,180,30); ng trn : v ng trn ta dng hm : void circle(int x, int y, int r); Trong : (x,y) l to tm cung trn. r l bn knh ng trn. V d : V mt ng trn c tm ti (100,50) v bn knh 30. circle(100,50,30); Cung elip v mt cung elip ta dng hm : void ellipse(int x, int y, int gd, int gc, int xr, int yr); Trong : (x,y) l to tm cung elip. gd l gc u cung trn(0 n 360 ). gc l gc cui cung trn (gd n 360 ). xr l bn trc nm ngang. yr l bn trc thng ng.

121

V d : V mt cung elip c tm ti (100,50), gc u l 0, gc cui l 180, bn trc ngang 30, bn trc ng l 20. ellipse(100,50,0,180,30,20); Hnh qut : v v t mu mt hnh qut ta dng hm : void pieslice(int x, int y, int gd, int gc, int r); Trong : (x,y) l to tm hnh qut. gd l gc u hnh qut (0 n 360 ). gc l gc cui hnh qut (gd n 360 ). r l bn knh hnh qut . V d : Chng trnh di y s v mt cung trn gc phn t th nht, mt cung elip gc phn t th ba, mt ng trn v mt hnh qut qut t 90 n 360 . # include "graphics.h" #include "stdio.h" #include "conio.h" main() { int md=0,mode; initgraph(&md,&mode,"C:\\TC\\BGI"); setbkcolor(BLUE); setcolor(YELLOW); setfillstyle(SOLID_FILL,RED);; arc(160,50,0,90,45); circle(160,150,45); pieslice(480,150,90,360,45); getch(); closegraph(); }

122

10.2.3. V ng gp khc v a gic : V ng gp khc : Mun v ng gp khc i qua n im : (x1,y1), (x2,y2), ...., (xn,yn) th trc ht ta phi gn cc to (xi,yi) cho mt mng a kiu int no theo nguyn tc sau : To x1 gn cho a[0] To y1 gn cho a[1] To x2 gn cho a[2] To y2 gn cho a[3] .... To xn gn cho a[2n-2] To yn gn cho a[2n-1] Sau gi hm : drawpoly(n,a); Nu im cui cng (xn,yn) trng vi im u (x1,y1) th ta nhn c mt ng gp khc khp kn. T mu a gic : Gi s ta c a l mng cp n trong mc trn, khi ta gi hm : fillpoly(n,a); s v v t mu mt a gic c nh l cc im (x1,y1), (x2,y2), ...., (xn,yn) V d : V mt ng gp khc v hai ng tam gic. #include "graphics.h" #include "stdio.h" #include "conio.h" int poly1[]={5,200,190,5,100,300}; int poly2[]={205,200,390,5,300,300}; int poly3[]={405,200,590,5,500,300,405,200}; main() { int md=0,mode; initgraph(&md,&mode,"C:\\TC\\BGI");

123

setbkcolor(CYAN); setcolor(YELLOW); setfillstyle(SOLID_FILL,MAGENTA); drawpoly(3,poly1); fillpoly(3,poly2); fillpoly(4,poly3); getch(); closegraph(); } V ng thng : v ng thng ni hai im bt k c to (x1,y1) v (x2,y2) ta s dng hm sau : void line(int x1, int y1, int x2, int y2); Con chy ho gi nguyn v tr. v ng thng ni t im con chy ho n mt im bt c to (x,y) ta s dng hm sau : void lineto(int x, int y); Con chy s chuyn n v tr (x,y). v mt ng thng t v tr con chy hin ti ( gi s l im x,y ) n im c to (x+dx,y+dy) ta s dng hm sau : void linerel(int dx, int dy); Con chy s chuyn n v tr (x+dx,y+dy). Di chuyn con chy ho : di chuyn con chy n v tr (x,y), ta s dng hm sau : void moveto(int x, int y); Chn kiu ng : Hm void setlinestyle(int kiu_ng, int mu, int _dy);

tc ng n nt v ca cc th tc v ng line, lineto,linerel , circle, rectangle (hm v hnh ch nht, ta s hc trong phn v min di). Hm ny s cho php ta xc nh ba yu t khi v ng thng, l : Kiu ng, b dy v mu t to.

124

Dng ng do tham s kiu_ng ca kiu_ng : Tn hng SOLID_LINE DOTTED_LINE CENTER_LINE DASHED_LINE USERBIT_LINE

xc nh. Bng di y cho cc gi tr kh d

Gi tr s 0 1 2 3 4

Kiu ng Nt lin Nt chm Nt chm gch Nt gch Mu t to

B dy ca ng v do tham s _dy xc nh,. bng di y cho cc gi tr kh d ca _dy : Tn hng NORM_WIDTH THICK_WIDTH Gi tr s 1 3 B dy B dy bnh thng B dy gp ba

Mu t to : Nu tham s th nht l USERBIT_LINE th ta c th to ra mu ng thng bng tham s mu. V d ta xt on chng trnh : int pattern = 0x1010; setlinestile(USERBIT_LINE,pattern,NORM_WIDTH); line(0,0,100,200); Gi tr ca pattern trong h 16 l 1010, trong h 2 l : 0001 0000 0001 0000 Bit 1 s cho im sng, bit 0 s lm tt im nh. V d : Chng trnh v mt ng gp khc bng cc on thng. ng gp khc i qua cc nh sau : (20,20),(620,20),(620,180),(20,180) v (320,100) #include "graphics.h" #include "stdio.h" #include "conio.h" main() { int mh=0, mode;

125

initgraph(&mh,&mode,"C:\\TC\\BGI"); setbkcolor(BLUE); setcolor(YELLOW); setlinestyle(SOLID-LINE,0,THICK_WIDTH); moveto(320,100); /* con chy v tr ( 320,100 ) */ line(20,20,620,20); /* con chy vn v tr ( 320,100 ) */ linerel(-300,80); lineto(620,180); lineto(620,20); getch(); closegraph(); } 10.2.4. V im, min : V im : Hm : void putpixel(int x, int y, int color); s t im (x,y) theo mu xc nh bi color. Hm : unsigned getpixel(int x, int y); s tr v s hiu mu ca im nh v tr (x,y). Ch : Nu im ny cha c t mu bi cc hm v hoc hm putpixel (m ch mi c to mu nn bi setbkcolor) th hm cho gi tr 0. T min : t mu cho mt min no trn mn hnh, ta dng hm sau : void floodfill(int x, int y, int border); y : (x,y) l to ca mt im no gi l im gieo. Tham s border cha m ca mu. S hot ng ca hm floodfill ph thuc vo gi tr ca x,y,border v trng thi mn hnh.

126

+ Khi trn mn hnh c mt ng cong khp kn hoc ng gp khc khp kn m m mu ca n bng gi tr ca border th : - Nu im gieo (x,y) nm trong min ny th min gii hn pha trong ng s c t mu. - Nu im gieo (x,y) nm ngoi min ny th min pha ngoi ng s c t mu. + Trong trng hp khi trn mn hnh khng c ng cong no nh trn th c mn hnh s c t mu. V d : V mt ng trn mu trn mn hnh mu xanh. To (x,y) ca im gieo c np t bn phm. Tu thuc gi tr c th ca x,y chng trnh s t mu vng cho hnh trn hoc phn mn hnh bn ngoi hnh trn. #include "graphics.h" #include "stdio.h" main() { int mh=mode=0, x, y; printf("\nVao toa do x,y:"); scanf("%d%d",&x,&y); initgraph(&mh,&mode,""); if (graphresult != grOk) exit(1); setbkcolor(BLUE); setcolor(RED); setfillstyle(11,YELLOW); circle(320,100,50); moveto(1,150); floodfill(x,y,RED); closegraph(); } 10.2.5. Hnh ch nht : Hm : void rectangle(int x1, int y1, int x2, int y2);

127

s v mt hnh ch nht c cc cnh song song vi cc cnh ca mn hnh. To nh tri trn ca hnh ch nht l (x1,y1) v to nh phi di ca hnh ch nht l (x2,y2). Hm : void bar(int x1, int y1, int x2, int y2); s v v t mu mt hnh ch nht. To nh tri trn ca hnh ch nht l (x1,y1) v to nh phi di ca hnh ch nht l (x2,y2). Hm : void bar3d(int x1, int y1, int x2, int y2, int depth, int top); s v mt khi hp ch nht, mt ngoi ca n l hnh ch nht xc nh bi cc to (x1,y1), (x2,y2). Hnh ch nht ny c t mu thng qua hm setfillstyle . Tham s depth xc nh s im nh trn b su ca khi 3 chiu. Tham s top c th nhn cc gi tr 1 hay 0 v khi 3 chiu tng ng s c np hoc khng.

top=1
V d :

top=0

Chng trnh di y to nn mt hnh ch nht, mt khi hnh ch nht v mt hnh hp c np : #include "graphics.h" main() { int mh=mode=0; initgraph(&mh,&mode,""); if (graphresult != grOk) exit(1); setbkcolor(GREEN); setcolor(RED); setfillstyle(CLOSE_DOT_FILL,YELLOW);

128

rectangle(5,5,300,160); bar(3,175,300,340); bar3d(320,100,500,340,100,1); closegraph(); } 10.2.6. Ca s (Viewport) : Thit lp viewport : void setviewport(int x1, int y1, int x2, int y2, int clip); trong (x1,y1) l to gc trn bn tri, (x2,y2) l to gc di bn phi. Bn gi tr ny v th phi tho mn : 0 x1 x2 0 y1 y2 Tham s clip c th nhn mt trong hai gi tr : clip=1 khng cho php v ra ngoi viewport. clip=0 cho php v ra ngoi viewport. V d : setviewport(100,50,200,150,1); Lp nn mt vng viewport hnh ch nht c to gc tri cao l (100,50) v to gc phi thp l (200,150) (l to trc khi t viewport). Ch : Sau khi lp viewport, ta c h to mi m gc trn bn tri s c to (0,0). Nhn din viewport hin hnh : void getviewsetting(struct viewporttype *vp); y kiu viewporttype c nh ngha nh sau : struct viewporttype { int left,top,right,bottom;

Viewport l mt vng ch nht trn mn hnh ho. thit lp viewport ta dng hm :

nhn viewport hin thi ta dng hm :

129

int clip; }; Xa viewport : S dng hm : void clearviewport(void); Xo mn hnh, a con chy v to (0,0) ca mn hnh : S dng hm : void cleardevice(void); To m dng : Nh s dng viewport c th vit cc chng trnh ho theo to m dng. Mun vy ta thit lp viewport v cho clip bng 0 c th v ra ngoi gii hn ca viewport. Sau y l on chng trnh thc hin cng vic trn : int xc,yc; xc=getmaxx()/2; yc=getmaxy()/2; setviewport(xc,yc,getmaxx(),getmaxy(),0); Nh th, mn hnh s c chia lm bn phn vi to m dng nh sau : Phn t tri trn : x m, y m. x : t -getmaxx()/2 n 0. y : t -getmaxy()/2 n 0. Phn t tri di : x m, y dng. x : t -getmaxx()/2 n 0. y : t 0 n getmaxy()/2. Phn t phi trn : x dng, y m. x : t 0 n getmaxx()/2. y : t -getmaxy()/2 n 0. Phn t phi di : x dng, y dng. x : t 0 n getmaxx()/2. y : t 0 n getmaxy()/2. V d :

130

Chng trnh v th hm sin x trong h trc to m dng. Honh x ly cc gi tr t -4 n 4. Trong chng trnh c s dng hai hm mi l settextjustify v outtextxy ta s cp ngay trong phn sau. #include "graphics.h" #include "conio.h" #include "math.h" #define TYLEX 20 #define TYLEY 60 main() { int mh=mode=DETECT; int x,y,i; initgraph(mh,mode,""); if (graphresult!=grOK ) exit(1); setviewport(getmaxx()/2,getmaxy()/2,getmaxx(),getmaxy(),0); setbkcolor(BLUE); setcolor(YELLOW); line(-getmaxx()/2,0,getmaxx()/2,0); line(0,-getmaxy()/2,0,getmaxy()/2,0); settextjustify(1,1); setcolor(WHITE); outtextxy(0,0,"(0,0)"); for (i=-400;i<=400;++i) { x=floor(2*M_PI*i*TYLEX/200); y=floor(sin(2*M_PI*i/200)*TYLEY); putpixel(x,y,WHITE); } getch(); closegraph(); } 10.3. X l vn bn trn mn hnh ho : Hin th vn bn trn mn hnh ho :

131

Hm : void outtext(char *s); cho hin chui k t ( do con tr s tr ti ) ti v tr con tr ho hin thi. Hm : void outtextxy(int x, int y,char *s); cho hin chui k t ( do con tr s tr ti ) ti v tr (x,y). V d : Hai cch vit di y : outtextxy(50,50," Say HELLO"); v moveto(50,50); outtext(" Say HELLO"); cho cng kt qu. S dng cc Fonts ch : Cc Fonts ch nm trong cc tp tin *.CHR trn a. Cc Fonts ny cho cc kch thc v kiu ch khc nhau, chng s c hin th ln mn hnh bng cc hm outtext v outtextxy. chn v np Fonts ta dng hm : void settextstyle(int font, int direction, int charsize); Tham s font chn kiu ch v nhn mt trong cc hng sau : DEFAULT_FONT=0 TRIPLEX_FONT=1 SMALL_FONT=2 SANS_SERIF_FONT=3 GOTHIC_FONT=4 Tham s derection chn hng ch v nhn mt trong cc hng sau : HORIZ_DIR=0 vn bn hin th theo hng nm ngang t tri qua phi. VERT_DIR=1 vn bn hin th theo hng thng ng t di ln trn.

Tham s charsize l h s phng to ca k t v c gi tr trong khong t 1 n 10.

132

Khi charsize=1, font hin th trong hnh ch nht 8*8 pixel. Khi charsize=2 font hin th trong hnh ch nht 16*16 pixel. ............ Khi charsize=10, font hin th trong hnh ch nht 80*80 pixel. Cc gi tr do settextstyle lp ra s gi nguyn ti khi gi mt settextstyle mi. V d : Cc dng lnh : settextstyle(3,VERT_DIR,2); outtextxy(30,30,"GODS TRUST YOU"); s hin th ti v tr (30,30) dng ch GODS TRUST YOU theo chiu t di ln trn, font ch chn l SANS_SERIF_FONT v c ch l 2. t v tr hin th ca cc xu k t cho bi outtext v outtextxy : Hm settextjustify cho php ch nh ra ni hin th vn bn ca outtext theo quan h vi v tr hin ti ca con chy v ca outtextxy theo quan h vi to (x,y); Hm ny c dng sau : void settextjustify(int horiz, int vert); Tham s horiz c th l mt trong cc hng s sau : LEFT_TEXT=0 CENTER_TEXT RIGHT_TEXT ( Vn bn xut hin bn phi con chy). ( Chnh tm vn bn theo v tr con chy). (Vn bn xut hin bn tri con chy).

Tham s vert c th l mt trong cc hng s sau : BOTTOM_TEXT=0 ( Vn bn xut hin pha trn con chy). CENTER_TEXT=1 ( Chnh tm vn bn theo v tr con chy). TOP_TEXT=2 V d : settextjustify(1,1); outtextxy(100,100,"ABC"); s cho dng ch ABC trong im (100,100) s nm di ch B. B rng v chiu cao vn bn : ( Vn bn xut hin pha di con chy).

Chiu cao :

133

Hm : textheight(char *s); cho chiu cao ( tnh bng pixel ) ca chui do con tr s tr ti. V d 1 : Vi font bit map v h s phng i l 1 th textheight("A") ch gi tr l 8. V d 2 : #include "stdio.h" #include "graphics.h" main() { int mh=mode=DETECT, y,size; initgraph(mh,mode,"C:\\TC\\BGI"); y=10; settextjustify(0,0); for (size=1;size<5;++size) { settextstyle(0,0,size); outtextxy(0,y,"SACRIFICE"); y+=textheight("SACRIFICE")+10; } getch(); closegraph(); } B rng : Hm : textwidth(char *s); cho b rng chui ( tnh theo pixel ) m con tr s tr ti da trn chiu di chui, kch thc font ch, h s phng i.

134

Mc lc Gii thiu Chng 1 Cc khi nim c bn 1.1. Tp k t dng trong ngn ng C 1.2. T kho 1.3. Tn 1.4. Kiu d liu 1.4.1. Kiu k t (char) 1.4.2. Kiu nguyn 1.4.3. Kiu du phy ng 1.5. nh ngha kiu bng TYPEDEF 1.5.1. Cng dng 1.5.2. Cch vit 1.6. Hng 1.6.1. Tn hng 1.6.2. Cc loi hng 1.6.2.1. Hng int 1.6.2.2. Hng long 1.6.2.3. Hng int h 8 1.6.2.4. Hng int h 16 1.6.2.5. Hng k t 1.6.2.5. Hng xu k t 1.7. Bin 1.8. Mng Chng 2 Cc lnh vo ra 2.1. Thm nhp vo th vin chun 2.2. Cc hm vo ra chun - getchar() v putchar() 2.2.1. Hm getchar() 2.2.2. Hm putchar() 2.2.3. Hm getch()

135

2.2.4. Hm putch() 2.3. a kt qu ln mn hnh - hm printf 2.4. Vo s liu t bn phm - hm scanf 2.5. a kt qu ra my in Chng 3 Biu thc 3.1. Biu thc 3.2. Lnh gn v biu thc 3.3. Cc php ton s hc 3.4. Cc php ton quan h v logic 3.5. Php ton tng gim 3.6. Th t u tin cc php ton 3.7. Chuyn i kiu gi tr Chng 4 Cu trc c bn ca chng trnh 4.1. Li ch thch 4.2. Lnh v khi lnh 4.2.1. Lnh 4.2.2. Khi lnh 4.3. Cu trc c bn ca chng trnh 4.4. Mt s qui tc cn nh khi vit chng trnh Chng 5 Cu trc iu khin 5.1. Cu trc c iu kin 5.1.1. Lnh if-else 5.1.2. Lnh else-if 5.2. Lnh nhy khng iu kin - ton t goto 5.3. Cu trc r nhnh - ton t switch 5.4. Cu trc lp 5.4.1. Cu trc lp vi ton t while v for 5.4.1.1. Cu trc lp vi ton t while

136

5.4.1.2. Cu trc lp vi ton t for : 5.4.2. Chu trnh do-while 5.5. Cu lnh break 5.6. Cu lnh continue Chng 6 Hm 6.1. C s 6.2. Hm khng cho cc gi tr 6.3. Hm qui 6.3.3. M u 6.3.2. Cc bi ton c th dng qui 6.3.3. Cch xy dng hm qui 6.3.4. Cc v d v dng hm qui 6.4. B tin s l C Chng 7 Con tr 7.1. Con tr v a ch 7.2. Con tr v mng mt chiu 7.2.1.Php ton ly a ch 7.2.2. Tn mng l mt hng a ch 7.2.3. Con tr tr ti cc phn t ca mng mt chiu 7.2.4. Mng, con tr v xu k t 7.3. Con tr v mng nhiu chiu 7.3.1.Php ly a ch 7.3.2. Php cng a ch trong mng hai chiu 7.3.3. Con tr v mng hai chiu 7.4. Kiu con tr kiu a ch, cc php ton trn con tr 7.4.1. Kiu con tr v kiu a ch 7.4.2. Cc php ton trn con tr 7.4.3. Con tr kiu void 7.5. Mng con tr 7.6. Con tr ti hm

137

7.6.1. Cch khai bo con tr hm v mng con tr hm 7.6.2. Tc dng ca con tr hm 7.6.3. i ca con tr hm Chng 8 Cu trc 8.1. Kiu cu trc 8.2. Khai bo theo mt kiu cu trc nh ngha 8.3. Truy nhp n cc thnh phn cu trc 8.4. Mng cu trc 8.5. Khi u mt cu trc 8.6. Php gn cu trc 8.7. Con tr cu trc v a ch cu trc 8.7.1. Con tr v a ch 8.7.2. Truy nhp qua con tr 8.7.3. Php gn qua con tr 8.7.4. Php cng a ch 8.7.5. Con tr v mng 8.8. Cu trc t tr v danh sch lin kt Chng 9 Tp tin - file 9.1. Khi nim v tp tin 9.2. Khai bo s dng tp - mt s hm thng dng khi thao tc trn tp 9.2.1. Khai bo s dng tp 9.2.2. M tp - hm fopen 9.2.3. ng tp - hm fclose 9.2.4. ng tt c cc tp ang m- hm fcloseall 9.2.5. Lm sch vng m - hm fflush 9.2.6. Lm sch vng m ca cc tp ang m - hm fflushall 9.2.7. Kim tra li file - hm ferror 9.2.8. Kimtra cui tp - hm feof 9.2.9. Truy nhp ngu nhin - cc hm di chuyn con tr ch v 9.2.9.1. Chuyn con tr ch v v u tp - Hm rewind

138

9.2.9.2. Chuyn con tr ch v tr cn thit - Hm fseek 9.2.9.3. V tr hin ti cu con tr ch v - Hm ftell 9.2.10. Ghi cc mu tin ln tp - hm fwrite 9.2.11. c cc mu tin t tp - hm fread 9.2.12. Nhp xut k t 9.2.12.1. Cc hm putc v fputc 9.2.12.2. Cc hm getc v fgettc 9.2.13. Xo tp - hm unlink Chng 10 ho 10.1. Khi ng ho 10.2. Cc hm ho 10.2.1. Mu v mu 10.2.2. V v t mu 10.2.3. V ng gp khc v a gic 10.2.4. V im, min 10.2.5. Hnh ch nht 10.2.6. Ca s (Viewport) 10.3. S l vn bn trn mn hnh ho

Bi tp.
Phn th nht : Nhm cc bi tp v tnh ton,hm v chu trnh .
Bi tp 1 : Vit chng trnh hin th thp Pascal :

139

ti liu tham kho 1. Cc ti liu ting Vit : 1.1. Ng Trung Vit - Ngn ng lp trnh C v C++ - Bi ging- Bi tp - Li gii mu NXB giao thng vn ti 1995 1.2. Vin tin hc - Ngn ng lp trnh C H ni 1990 1.3. L Vn Doanh - 101 thut ton v chng trnh bng ngn ng C 2. Cc ti liu ting Anh : 2.1. B. Kernighan and D. Ritchie - The C programming language Prentice Hall 1989 2.2. Programmer's guide Borland C++ Version 4.0 Borland International, Inc 1993 2.3. Bile - Nabaiyoti - TURBO C++ The Waite Group's UNIX 1991

BI tp Ngn ng lp trnh C
Phn 1 : Nhm cc bI tp v tnh ton, hm v chu trnh . BI tp 1 : Vit chng trnh hin th thp PASCAL : 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 Vit chng trnh hin th thp o ngc. BI tp 2 :

140

Vit chng trnh nhp ba s thc. Kim tra xem ba s c th l chiu di ca ba cnh ca mt tam gic c khng? Nu c th tnh chu vi v din tch tam gic . BI tp 3 : Vit chng trnh tnh hm s : f(x) = K0 x K1+ ----------------------------------------x K2 + -----------------------x K3 + ---------------------x K4 + -------------------- x Kn-1 + ------------Kn Bi tp 4 : Vit chng trnh tnh tch hai ma trn C mxn = A mxn * B nxk . Bi tp 5 : Vit chng trnh nhp vo mt dy s sau tch dy ny thnh hai dy ch cha cc s dng v ch cha cc s m. Tnh tng s phn t ca mi dy sau sp xp hai dy c gi tr gim dn. Bi tp 6 : Vit chng trnh nhp vo mt ma trn A nxm. Tm gi tr cc i v cc tiu ca cc phn t ca mng . Bi tp 7 : Trm tru,trm c Tru ng n nm Tru nm n ba L kh tru gi Ba con mt b. Tnh s tru mi loi . Bi tp 8 : Va g va ch B li cho trn ng ba su con Mt trm chn chn . 141

Tnh s g, s ch . Bi tp 9 :

142