Está en la página 1de 42

Nguyn Vn Su

2012

NI DUNG

CC KHI NIM V KIU D LIU C S

THUT TON V LU
CU TRC CA MT CHNG TRNH C
CC KIU D LIU C S
BIN, HNG, CU LNH, BIU THC, CHUI
CC LNH NHP XUT

CC CU TRC IU KHIN C BN
HM V QUY
STRUCT, FILE
MNG
CON TR
DANH SCH LIN KT
CY NH PHN
AVL

Cc k thut lp trnh

CC KHI NIM C BN: Thut ton

THUT TON
Tp hp cc quy tc c logic nhm gii mt lp bi ton
no c mt kt qu xc nh.

CC TNH CHT CA THUT TON


Tnh tng qut
Tnh gii hn
Tnh duy nht

PHN LOI THUT TON


Thut ton khng phn nhnh
Thut ton c phn nhnh
Thut ton theo chu trnh c bc lp xc nh v c
bc lp khng xc nh
Cc k thut lp trnh

CC KHI NIM C BN: Lu

BIU DIN THUT TON BNG LU


Lu l tp cc k hiu, lin kt nhau c trnh t, dng
m t qu trnh tnh ton mt cch c h thng
Tn khi
Khi m u
hoc kt thc
Khi vo ra
Khi tnh ton

K hiu

ngha
Dng m u hoc kt thc
chng trnh
a s liu vo hoc in kt qu

Khi iu kin

Biu din cc cng thc tnh ton


v thay i gi tr ca cc bin
Dng phn nhnh chng trnh

Chng trnh con

Dng gi chng trnh con

Mi tn

Ch hng truyn thng tin

Cc k thut lp trnh

V D - Thut ton khng phn nhnh

V d 1: Tnh A = x2 + y2

Begin
Nhap (x,y)
A = x2 + y2
Xuat (A)
End

Cc k thut lp trnh

V D - Thut ton khng phn nhnh

V d 2: Tnh S

Ax By C

x y
2

Begin
Nhap (A, B, C, x,y)
S = (Ax + By + C) / SQRT (x*x + y*y)
Xuat S
End
Cc k thut lp trnh

V D - Thut ton c phn nhnh

V d 3:
Tm gi tr max ca ba s thc a, b, c

Begin
Nhap (a, b, c)

a>b

Max = b

Max = a

Max < c

Max = c
Xuat (Max)
End
Cc k thut lp trnh

V D - Thut ton c phn nhnh

V d 4:
Gii phng trnh
bc nht Ax+B =0
vi cc nghim thc

Begin
Nhap (a, b)

a=0

Xuat (-b/a)

b=0

Xuat (PTVN)

Xuat (PTV)

End
Cc k thut lp trnh

V D - Thut ton c phn nhnh


Begin

V d 5:
Gii phng trnh
bc hai Ax2+Bx+C =0
vi cc nghim thc

Nhap (a, b, c)

a=0

PTB1 (b, c)

S
Delta = b*b - 4*a*c

Delta < 0

Xuat (PTVN)

S
Delta = 0

Xuat (-b / (2*a))

S
Xuat (X1= ,(-b + SQRT(Delta)) / (2*a))
Xuat (X2= ,(-b - SQRT(Delta)) / (2*a))

Cc k thut lp trnh

End

V D - Thut ton c chu trnh


Begin

V d 6: Tnh S=

x
i 1

Nhap (n)

vi cc xi c nhp t bn phm

i=1
S=0
Nhap (xi)
i = i+1
S = S+xi

i <= n
S
Xuat (S)
End

Cc k thut lp trnh

10

CU TRC CA MT CHNG TRNH C

[Khai bo tin x l]
[Khai bo cc hm]
void main()
{ [khai bo bin;]
[nhp d liu ;]
[x l ;]
[xut ;]
}
V d: Chng trnh hin ln mn hnh cu Chao cac ban

Cc k thut lp trnh

11

CU TRC CA MT CHNG TRNH C

void main()
{ printf(Chao cac ban\n);
}
Hm main() l hm chnh bt buc phi c. y chnh l
hm c thc hin u tin trong chng trnh
Cp du {} dng xc nh mt khi lnh
Hm printf( Chao cac ban\n) l hm chun ca C
dng xut chui Chao cac ban ra mn hnh
K t \n l k t c bit dng xung dng
Du ; chm dt mt lnh
Chng trnh C c phn bit ch thng vi ch hoa
Cc k thut lp trnh

12

CU TRC CA MT CHNG TRNH C

V d: In bng ly tha 2 ca cc s nguyn t 10 n 50


#include <stdio.h> /* Khai bo th vin hm*/
#define max 50 /* nh ngha max =50*/
void main()
{ int n;
/*Khai bo bin n kiu nguyn*/
for (n=10; n<=max; n++) /*Lp t 10 n 50*/
/*in n v n2 dng 5d l nm ch s*/
printf(%3d \t %5d\n, n, n*n);
} /*Ht main*/

Cc k thut lp trnh

13

CC KIU D LIU C S

L nhng kiu d liu c gi tr n, khng phn chia c


na nh s, k t
Tn kiu
char

ngha
K t

Kch
thc

Min gi tr

1 byte

-128 127

unsigned char K t khng du

1 byte

0255

int

S nguyn c du

2 bytes -3276832767

unsigned int

S nguyn khng du

2 bytes 0 65535

long

S nguyn di c du

4 bytes

-2147483648
2147483647

unsigned long S nguyn di khng du 4 bytes 04294967295


float

S thc chnh xc n 4 bytes 3.4 E-383.4 E+38

double

S thc chnh xc kp 8 bytes 1.7 E-308 1.7 E+308

Cc k thut lp trnh

14

BIN

C php:
<kiu> <tn bin>;
<kiu> <tn bin 1>, <tn bin 2>;

V d:
int i;
int j, k;
unsigned char dem;
float ketqua, delta;

Cc k
Ccthut
kiulp
d trnh
liu c s

15

HNG

HNG THNG:
C php:
<kiu> <tnhng> = <gi tr>;

V d:

int a
int b
int c
float

=
=
=
d

Cc k
Ccthut
kiulp
d trnh
liu c s

1506;
// 150610
01506;
// 15068
0x1506;
// 150616 (0x hay 0X)
= 15.06e-3;// 15.06*10-3 (e hay E)

16

HNG

HNG K HIU:
C php:
#define <tnhng> <gi tr> // Khng c ;
const <kiu> <tn hng> = <gi tr>;

V d:
#define MAX 100
// Khng c ;
#define PI 3.1416
// Khng c ;
const int MAX = 100;
const float PI = 3.14;

Cc k
Ccthut
kiulp
d trnh
liu c s

17

BIU THC

To thnh t cc ton t (Operator) v cc ton hng


(Operand)
Ton t tc ng ln cc gi tr ca ton hng v cho gi tr
c kiu nht nh
Ton t: +, , *, /, %.
Ton hng: hng, bin, li gi hm...

V d:

2 + 3,
a / 5,
(a + b) * 5,
pow(R,2) * 3.14159

Cc k
Ccthut
kiulp
d trnh
liu c s

18

TON T GN

Dng gn gi tr cho bin


C php

<bin> = <gi tr>;


<bin> = <bin>;
<bin> = <biu thc>;
<var 1>=<var 2>= ... =<var n>=<gi tr>;
(C th thc hin lin tip php gn)

Cc k
Ccthut
kiulp
d trnh
liu c s

19

TON T GN V d

void main()
{ int a, b, c, d, e, thuong;
a = 10;
b = a;
thuong = a / b;
a = b = c = d = e = 156;
e = 156;
d = e;
c = d;
b = c;
a = b;
}
Cc k
Ccthut
kiulp
d trnh
liu c s

20

TON T TON HC

TON T 1 NGI:
Ch c mt ton hng trong biu thc
++ (tng 1 n v), -- (gim 1 n v)
t trc ton hng:
V d ++x hay --x: thc hin tng/gim trc
t sau ton hng:
V d x++ hay x--: thc hin tng/gim sau
V d:
x = 10; y = x++; // y = 10 v x = 11
x = 10; y = ++x; // x = 11 v y = 11

Cc k
Ccthut
kiulp
d trnh
liu c s

21

TON T TON HC

TON T 2 NGI:
C hai ton hng trong biu thc
+, , *, /, % (chia ly phn d)
x = x + y x += y;

a
e
g
x

V d:
= 1 + 2; b = 1 2; c = 1 * 2; d = 1 / 2;
= 1*1.0 / 2; f = float(1) / 2;
= float(1 / 2); h = 1 % 2;
= x * (2 + 3*5); x *= 2 + 3*5;

Cc k
Ccthut
kiulp
d trnh
liu c s

22

TON T THAO TC BIT (BIT WISE)


Tc ng ln cc bit ca ton hng (nguyn)
& (and), | (or), ^ (xor), ~ (not hay ly s b 1)
>> (shift right), << (shift left)
Ton t gp: &=, |=, ^=, ~=, >>=, <<=
&

Cc k
Ccthut
kiulp
d trnh
liu c s

23

TON T THAO TC BIT V d

void main()
{

int a = 5;// 0000 0000 0000

int b = 6;// 0000 0000 0000

int z1, z2, z3, z4, z5, z6;

z1 = a & b;
// 0000 0000

z2 = a | b;
// 0000 0000

z3 = a ^ b;
// 0000 0000

z4 = ~a;
// 1111 1111

z5 = a >> 2;
// 0000 0000

z6 = a << 2;
// 0000 0000
}
Cc k
Ccthut
kiulp
d trnh
liu c s

0101
0110

0000
0000
0000
1111
0000
0001

0100
0111
0011
1010
0001
0100
24

TON T QUAN H

So snh 2 biu thc vi nhau


Cho ra kt qu 0 (hay false nu sai) hoc <>0 (hay true nu
ng)
==, >, <, >=, <=, !=
V d:
s1 = (1 == 2);
s3 = (1 > 2);
s5 = (1 < 2);

Cc k
Ccthut
kiulp
d trnh
liu c s

s2 = (1 != 2);
s4 = (1 >= 2);
s6 = (1 <= 2);

25

TON T LOGIC

T hp nhiu biu thc quan h vi nhau


&& (and), || (or), ! (not)
&&

||

V d:
s1 = (1 > 2) && (3 > 4);
s2 = (1 > 2) || (3 < 4);
s3 = !(1 > 2);
Cc k
Ccthut
kiulp
d trnh
liu c s

26

TON T IU KIN

y l ton t 3 ngi (gm c 3 ton hng)


<biu thc 1> ? <biu thc 2> : <biu thc 3>
<biu thc 1> ng th gi tr l <biu thc 2>
<biu thc 1> sai th gi tr l <biu thc 3>
V d:
s1 = (1 > 2) ? 2912 : 1706;
int s2 = 0;
1 < 2 ? s2 = 2912 : s2 = 1706;

Cc k
Ccthut
kiulp
d trnh
liu c s

27

TON T PHY

Cc biu thc t cch nhau bng du ,


Cc biu thc con ln lt c tnh t tri sang phi
Biu thc mi nhn c l gi tr ca biu thc bn phi
cng
V d:
x = (a++, b = b + 2);
( a++; b = b + 2; x = b;)

Cc k
Ccthut
kiulp
d trnh
liu c s

28

U TIN CA CC TON T
Ton t
() [] -> .
! ++ -- - + * (cast) & sizeof
* / %
+ << >>
< <= > >=
== !=
&
|
^
&&
||
?:
= += -= *= /= %= &=

Cc k
Ccthut
kiulp
d trnh
liu c s

u tin

29

U TIN CA CC TON T

Quy tc thc hin:


Thc hin biu thc trong ( ) su nht trc,
Thc hin theo th t u tin cc ton t
Ch ng thm ( )
V d
n = 2 + 3 * 5;
n = 2 + (3 * 5);
a > 1 && b < 2
(a > 1) && (b < 2)

Cc k
Ccthut
kiulp
d trnh
liu c s

30

CU LNH

L mt ch th hon chnh nhm ra lnh cho my tnh thc


hin mt s tc v nht nh no
Cu lnh kt thc bng mt du chm phy (;)
Trnh bin dch b qua cc khong trng (hay tab hoc
xung dng) chen gia lnh
Cu lnh n: Ch gm mt cu lnh.
Cu lnh phc (khi lnh): Gm nhiu cu lnh n c
bao bi { v }
V d:
a=
1235; a
= 1235;
{a=1; b= 2;}
Cc k
Ccthut
kiulp
d trnh
liu c s

31

CHUI

L mt mng m cc phn t ca n c kiu k t


Khai bo mt chui k t cha ti a 49 k t:
char
chui[50];
Tt c cc chui u c kt thc bng k t NULL (\0).
Do , nu chui di 50 th ta ch c th cha ti a 49 k t
Khi to gi tr cho chui:
char
char

Cc k
Ccthut
kiulp
d trnh
liu c s

chui[ ] = {A, N, H, \0};


chui[ ] = "ANH";

32

NHP XUT CHUI

Nhp chui:
gets (chui)
Xut chui:
puts (chui)
Ch : Khi nhp chui th khng c dng hm scanf v hm scanf
khng chp nhn khong trng.
scanf(%s, chui);
// ta nhp vo Nguyn Vn i th
// chui = Nguyn v hm scanf ct khong trng
Khi dng hm gets trong chng trnh th khng nn dng hm scanf
bt k u.
scanf(%d, &n);
// ta nhp s 5
gets (chui);
// lc ny chui = (chui rng)

Mt s hm lin quan n chui: (Xem ti liu trang 32)


Cc k
Ccthut
kiulp
d trnh
liu c s

33

CU LNH XUT (1)

Th vin:
#include <stdio.h> (standard input/output)

C php:
printf(<chui nh dng>[,<s1>,<s2>, ]);
<chui nh dng> l cch trnh by thng tin xut v
c t trong cp nhy kp
Vn bn thng (literal text)
K t iu khin (escape sequence)
c t (conversion specifier)

Cc k
Ccthut
kiulp
d trnh
liu c s

34

CU LNH XUT (2)

Chui nh dng l vn bn thng:


c xut y ht nh lc g trong chui nh dng.

V d:
Xut chui Hello World:
printf(Hello ); printf(World);
printf(Hello World);
Xut chui a + b:
printf(a + b);

Cc k
Ccthut
kiulp
d trnh
liu c s

35

CU LNH XUT (3)

Chui nh dng l k t iu khin:


Gm du \ v mt k t nh trong bng sau:
K t iu khin

\a
\b
\n
\t
\\
\?
\

ngha

Ting chung
Li li mt bc
Xung dng
Du tab
In du \
In du ?
In du

V d:
printf(\t); printf(\n);
printf(\t\n);
Cc k
Ccthut
kiulp
d trnh
liu c s

36

CU LNH XUT (4)

Chui nh dng l cc c t:
Gm du % v mt k t.
Xc nh kiu ca gi tr mun xut
Cc i s chnh l cc gi tr mun xut, c lit k
theo th t cch nhau du phy.
c t

ngha

%c
K t
%d, %ld S nguyn c du
%f, %lf S thc
%s
Chui k t
%u
S nguyn khng du

Cc k
Ccthut
kiulp
d trnh
liu c s

p dng cho

char
char, int, short, long
float, double
char[], char*
unsigned int/short/long
37

CU LNH XUT (5)

Chui nh dng l cc c t: (tt)


V d:
int a = 10, b = 20;
printf(%d, a);
printf(%d, b);
printf(%d %d, a, b);

Xut ra 10
Xut ra 20
Xut ra 10 20

float x = 15.06;
printf(%f, x);
Xut ra 15.060000
printf(%f, 1.0/3); Xut ra 0.333333

Cc k
Ccthut
kiulp
d trnh
liu c s

38

CU LNH XUT (6)

Chui nh dng l cc c t: (tt)


c t xut s nguyn: %nd
c t xut s thc: %n.kf
V d:
int a = 1706;
float x = 176.85;
printf(%10d, a);printf(\n);
printf(%10.2f, x);printf(\n);
printf(%.2f, x);printf(\n);

Cc k
Ccthut
kiulp
d trnh
liu c s

39

CU LNH XUT (5)

Chui nh dng l cc c t: (tt)


Phi hp cc thnh phn:
int a = 1, b = 2;
//Xut 1 cong 2 bang
printf(%d, a);
printf( cong );
printf(%d, b);
printf( bang );
printf(%d, a + b);
printf(\n);
printf(%d cong

Cc k
Ccthut
kiulp
d trnh
liu c s

3 v xung dng.
// Xut gi tr ca bin a
// Xut chui cong
// Xut gi tr ca bin b
// Xut chui bang
// Xut gi tr ca a + b
// Xut iu khin xung dng \n
%d bang %d\n, a, b, a+b);

40

CU LNH NHP (1)

Th vin:
#include <stdio.h> (standard input/output)

C php:
scanf(<chui nh dng>[, <s1>, <s1>, ]);
<chui nh dng> ging nh dng xut nhng ch c
cc c t.
Cc i s l tn cc bin s cha gi tr nhp v c t
trc bi du &

Cc k
Ccthut
kiulp
d trnh
liu c s

41

CU LNH NHP (2)

V d:
Cho 2 bin a v b kiu s nguyn
scanf(%d, &a); // Nhp gi tr cho bin a
scanf(%d, &b); // Nhp gi tr cho bin b
scanf(%d%d, &a, &b);
Cc cu lnh sau y l sai:
scanf(%d, a);
// Thiu du &
scanf(%d, &a, &b);// Thiu %d cho bin b
scanf(%f, &a); // a l bin kiu s nguyn
scanf(%9d, &a); // khng c c nh dng
scanf(a = %d, b = %d, &a, &b);
// khng c c vn bn thng
Cc k
Ccthut
kiulp
d trnh
liu c s

42