K THUT LP TRNH
This presentation is the property of its rightful owner.
Sponsored Links
1 / 85

KỸ THUẬT LẬP TRÌNH PowerPoint PPT Presentation


  • 124 Views
  • Uploaded on
  • Presentation posted in: General

KỸ THUẬT LẬP TRÌNH. GV: Phạm Thị Xuân Diệu Email: [email protected] Blog: xuandieu.wordpress.com. Nội dung chi tiết học phần. [ 1] Giáo trình phương pháp lập trình - Khoa KH&KT Máy tính - ĐHCN Tp.HCM

Download Presentation

KỸ THUẬT LẬP TRÌNH

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


K thu t l p tr nh

K THUT LP TRNH

GV: Phm Th Xun Diu

Email: [email protected]

Blog: xuandieu.wordpress.com


K thu t l p tr nh

Ni dung chi tit hc phn


K thu t l p tr nh

[1]Gio trnh phng php lp trnh - Khoa KH&KT My tnh - HCN Tp.HCM

[2] C++ How to Program, H. M Deitel and P. J. Deitel, 3rd Edition, Prentice-Hall, 2001

[3] Lp trnh C++ GS Phm Vn t NXB KH-KT

Ti liu hc tp


K thu t l p tr nh

1. Bin, hng, kiu d liu v khai bo

2. Cc hm nhp, xut

3. Cc ton t (ton t quan h, logic.).

4. Cu lnh if, switch, break,

continue, go to... v nguyn tc hoat ng

5.Cu trc for( ) while( ) , do..while( )


K thu t l p tr nh

1. Bin, hng, kiu d liu v khai bo


K thu t l p tr nh

  • C/C++ c nm kiu d liu c s:

    • K t (char)

    • S nguyn (int)

    • S thc (float, double,)

    • Kiu v nh (void).

  • Kch thc v phm vi ca nhng kiu d liu ny c th thay i ty theo loi CPU v trnh bin dch.

1. Kiu d liu (Data type)


K thu t l p tr nh

1.1. Kiu s nguyn


K thu t l p tr nh

V d

1.1. Kiu s nguyn


K thu t l p tr nh

1. 2.Kiu s thc


K thu t l p tr nh

1.3. Kiu lun l


K thu t l p tr nh

1.3. Kiu lun l


K thu t l p tr nh

Kiu char c dng gi cc gi tr ca b m ASCII (Amercican Standard Code for Information Interchange). Kiu char chim 1 byte b nh.

1.4. Kiu k t


K thu t l p tr nh

  • Bin l nh danh ca mt vng trong b nh dng gi mt gi tr m c th b thay i bi chng trnh.

  • Tt c bin phi c khai bo trc khi s dng.

  • Cch khai bo:

    type variableNames;

    • type: l mt trong cc kiu d liu hp l.

    • variableNames: tn ca mt hay nhiu bin phn cch nhau bi du phy.

2. Bin (variables)


K thu t l p tr nh

  • Ngoi ra, ta c th va khai bo va khi to gi tr ban u cho bin:

    typevarName1=value, ... ,varName_n=value;

  • V d:

    float mark1, mark2, mark3, average = 0;

2. Bin (variables)


K thu t l p tr nh

  • Bin cc b (local variables)

  • Nhng bin c khai bo bn trong mt hm gi l bin cc b.

  • Cc bin cc b ch c tham chiu n bi nhng lnh trong khi (block) c khai bo bin.

  • Mt khi c t trong cp du { }.

  • Bin cc b ch tn ti trong khi khi cha n ang thc thi v b hy khi khi cha n thc thi xong.

2. 1.Phm vi ca bin


K thu t l p tr nh

V d:

void func1(void)

{

int x;

x = 10;

}

void func2(void)

{

int x;

x = -199;

}

2.1. Phm vi ca bin


K thu t l p tr nh

  • Tham s hnh thc(formal parameters)

    • Nu mt hm c nhn cc i s truyn vo hm th n phi khai bo cc bin nhn gi tr ca cc i s khi hm c gi.

    • Nhng bin ny gi l cc tham s hnh thc. Nhng bin ny c s dng ging nh cc bin cc b.

2. 1.Phm vi ca bin


K thu t l p tr nh

V d:

int sum(int from, int to)

{

int total=0;

for(int i=from ; i<=to ; i++)

total +=i;

return total;

}

2.1. Phm vi ca bin


K thu t l p tr nh

  • Bin ton cc (global variables)

  • Bin ton cc c phm vi l ton b chng trnh.

  • Tt c cc lnh c trong chng trnh u c th tham chiu n bin ton cc.

  • Bin ton cc c khai bo bn ngoi tt c hm.

2. 1.Phm vi ca bin


K thu t l p tr nh

#include <iostream.h>

int gVar = 100;

void increase()

{ gVar = gVar + 1;}

void decrease()

{ gVar = gVar -1;}

void main()

{

cout << Value of gVar= << gVar; increase();

cout << After increased, gVar= << gVar; decrease();

cout << After decreased, gVar= << gVar;

}

2. 1.Phm vi ca bin


K thu t l p tr nh

  • Gi tr ca bin thay i trong sut qu trnh thc thi chng trnh.

  • gi tr ca bin khng b thay i, ta t trc khai bo bin t kha const.

  • Thng thng ta dng ch HOA t tn cho nhng bin ny.

    V d:

    const int MAX = 200;

3. T kha const


K thu t l p tr nh

  • Hng l nhng gi tr c nh (fixed values) m chng trnh khng th thay i. Mi kiu d liu u c hng tng ng. Hng cn c gi l literals.

  • Hng k t c t trong cp nhy n.

    V d: 'a

  • Hng nguyn l nhng s m khng c phn thp phn.

    V d 100 , -100

4. Hng (constants)


K thu t l p tr nh

  • Hng s thc yu cu mt du chm phn cch phn nguyn v phn thp phn.

    V d: 123.45

  • Cch vit mt s loi hng s

4. Hng (constants)


K thu t l p tr nh

  • Hng chui k t l mt tp cc k t t trong cp nhy kp .

    V d:

    • "This is a string" //l mt chui.

    • a //l mt hng k t.

    • a //l mt hng chui.

4. Hng chui k t (string constants)


K thu t l p tr nh

4. Hng k t c bit(escape sequences)


K thu t l p tr nh

#include <iostream.h>

void main(void)

{

cout <<"Items:\n";

cout <<\tItem1\n;

cout <<\tItem2\n;

cout <<\tItem3\n;

}

4. Hng k t c bit(escape sequences)


K thu t l p tr nh

  • Trong C/C++, tn bin, hng, hm, c gi l nh danh

  • Nhng nh danh ny c th l 1 hoc nhiu k t. K t u tin phi l mt ch ci hoc du _ (underscore), nhng k t theo sau phi l ch ci, ch s, hoc du _

  • C/C++ phn bit k t HOA v thng.

  • nh danh khng c trng vi t kha (keywords).

6.nh danh (Identifier Name)


K thu t l p tr nh

  • L nhng t c dnh ring bi ngn ng lp trnh cho nhng mc ch ring ca n

  • Tt c cc t kha trong C/C++ u l ch thng (lowercase).

  • Danh sch cc t kha trong C/C++

7.T kha (keywords)


K thu t l p tr nh

  • C php:

  • <Kiu d liu> tn bin;

  • V d:

  • int a;//Khai bo bin lu s nguyn tn a

    • float c;// Khai bo bin lu s thc tn c

  • Khai bo nhiu bin cng kiu

  • <Kiu d liu>tn bin 1, tn bin 2, tn bin 3;

  • V d:

  • int a,x,y;

8. Khai bo bin


K thu t l p tr nh

C php:

<Kiu d liu> tn bin = gi tr;

V d:

int a = 5;

float b = 5.4, c=9.2;

Char ch = n;

int a,x,y;

8. Khai bo v khi gn gi tr ban u cho bin


K thu t l p tr nh

2. CC HM NHP XUT


K thu t l p tr nh

Th vin hm: iostream.h

Hm nhp gi tr t bn phm: cin>>tn bin;

V d: int a;

cin>>a;//Ngi dng nhp gi tr t phm s lu vo bin a

Hm xut gi tr ra mn hnh: cout<<tn bin hoc chui k t;

V d: int a = 5;

Cout<<Gi tr ca a = <<a;

Cc bin v chui cch nhau bi du<<

( chui nm trong cp du nhy kp )

1.Hm nhp xut


K thu t l p tr nh

  • Th vin hm: Cout<<setw(n)<<tn bin;

  • Cha mt khong n k t xut gi tr

  • V d:

  • int a=7, b=9;

  • Cout<<a<<setw(5)<<b;// Cha 5 v tr xut gi tr b.

  • Kt qu: 7 9

2. Xut c nh dng


K thu t l p tr nh

  • cout<<setprecision(n);

  • Xut s gm n-1 ch s thp phn lm trn.

  • V d:

  • float a=7.56745, b=5.339;

    • cout<<a<<endl;

    • cout<<setprecision(3)<<a<<endl;

    • cout<<setprecision(2)<<b<<endl;

    • cout<<setprecision(5)<<b;

2. Xut c nh dng

  • Kt qu:

    • 7.56745

    • 7.57

    • 5.3

    • 5.339


K thu t l p tr nh

3. Xut k t c bit


K thu t l p tr nh

3. Xut k t c bit


K thu t l p tr nh

2. CC TON T ( TON T QUAN H, LOGIC,)


K thu t l p tr nh

C php tng qut

variableName = expression;

  • variableName: Tn bin

  • expression: Biu thc

    Lu : pha bn tri du = phi l mt bin hay con tr v khng th l hm hay hng.

    V d:

    total = a + b + c + d;

1. Ton t gn (assignment operator)


K thu t l p tr nh

1. Ton t gn (assignment operator)

Chuyn i kiu trong cu lnh gn

  • i vi cu lnh gn, gi tr ca biu thc bn phi du = c t ng chuyn thnh kiu d liu ca bin bn tri du =

    V d:

    int i=100;

    double d = 123.456;

  • Nu thc thi lnh i = d; th i = 123 (chuyn i kiu mt mt thng tin).

  • Nu thc thi lnh d = i; th d =100.0 (chuyn i kiu khng mt mt thng tin).


K thu t l p tr nh

1. Ton t gn (assignment operator)

Chuyn i kiu trong cu lnh gn

  • Khi chuyn i t kiu d liu c min gi tr nh sang kiu d liu c min gi tr ln hn:charintlongfloatdouble, th vic chuyn i kiu ny l khng mt mt thng tin

  • Khi chuyn i t kiu d liu c min gi tr ln sang kiu d liu c min gi tr nh hn:doublefloatlongintchar, th vic chuyn i kiu ny l mt mt thng tin


K thu t l p tr nh

2. Ton t s hc (arithmetic operators)


K thu t l p tr nh

  • Khi t s v mu s ca php chia l s nguyn th l php chia nguyn nn phn d ca php chia nguyn b ct b.

    V d: 5/2 cho kt qu l 2.

  • Ton t ly phn d % (modulus operator) ch p dng vi s nguyn.

2. Ton t s hc (arithmetic operators)


K thu t l p tr nh

3. Ton t gn phc hp


K thu t l p tr nh

3. Ton t gn phc hp

#include <iostream.h>

void main ()

{

int a, b=3;

a = b;

a+=2; // tng ng vi a=a+2

cout << a;

}


K thu t l p tr nh

4. Ton t ++ v -- (increment and decrement operators)

  • Ton t tng (++) v ton t gim (--) c tc dng lm tng hoc gim 1 gi tr lu trong bin.

  • V d:

    a++;//tng ng vi a+=1; v a=a+1

    a--;//tng ng vi a-=1; v a=a-1


K thu t l p tr nh

4. Ton t ++ v -- (increment and decrement operators)

Ton t tng/gim c 2 dng:

  • Tin t (prefix): Ton t ++/-- t trc ton hng, hnh ng tng/gim trn ton hng c thc hin trc, sau gi tr mi ca ton hng s tham gia nh tr ca biu thc.

  • V d:

    B=3;A=++B;Kt qu: A cha gi tr 4, B cha gi tr 4


K thu t l p tr nh

4. Ton t ++ v -- (increment and decrement operators)

  • Hu t (postfix): Ton t ++/-- t sau ton hng, gi tr trong ton hng c tng/gim sau khi tnh ton.

  • V d:

    B=3;A=B++;Kt qu: A cha gi tr 3, B cha gi tr 4


K thu t l p tr nh

4. Ton t ++ v -- (increment and decrement operators)

V d:

int x = 100;

int n,m;

n = ++x + 1; // n s c gi tr l 102 (1)

n = x++ + 1; // n s c gi tr l 101 (2)

  • Sau lnh (1), (2) th x c gi tr l 101

    m = --x + 1; // m s c gi tr l 100 (3)

    m = x-- + 1; // m s c gi tr 101 (4)

  • Sau lnh (3), (4) th x c gi tr l 99


K thu t l p tr nh

4. Ton t ++ v -- (increment and decrement operators)

  • Khi cc ton t s hc xut hin trong mt biu thc, th u tin thc hin nh sau:


K thu t l p tr nh

5. Ton t quan h & lun l (relational & logical operators)

  • Ton t quan h c nh tr l true hoc false.


K thu t l p tr nh

5. Ton t quan h & lun l (relational & logical operators)

  • Ton t lun l:

  • Bng chn tr:


K thu t l p tr nh

5. Ton t quan h & lun l (relational & logical operators)

  • u tin ca ton t quan h v lun l:


K thu t l p tr nh

5. Ton t quan h & lun l (relational & logical operators)

V d biu thc:

(10>9 && 8!=7) || (6<=5 || 5>4)

c nh tr nh sau:


K thu t l p tr nh

5. Ton t ? (? operator)

  • Ton t ? l mt ton t ba ngi do phi c ba ton hng.

  • Dng tng qut ca ton t ? l:

    Exp1 ? Exp2 : Exp3;

  • Exp1, Exp2, v Exp3 l cc biu thc.

  • ngha:

    • Nu Exp1 ng th Exp2 c nh tr v n tr thnh gi tr ca biu thc.

    • Ngc li, nu Exp1 sai, Exp3 c nh tr v tr thnh gi tr ca biu thc.


K thu t l p tr nh

5. Ton t ? (? operator)

V d:

X = 10

Y = X > 9 ? 100*X : 200*X

V X>9 l true nn gi tr ca biu thc s l 1000. Vy y s c gi tr l 1000.

V d:

int m = 1, n = 2, p =3;

int min =(m < n ? (m < p ? m : p) : (n < p ? n : p));


K thu t l p tr nh

5. Ton t ? (? operator)


K thu t l p tr nh

5. Ton t ? (? operator)


K thu t l p tr nh

6. Ton t sizeof

  • sizeof l ton t mt ngi m tr v s byte ca kiu d liu chim trong b nh. Ty mi trng (h iu hnh, loi CPU,...) m mi kiu d liu c s byte khc nhau.

  • C php:

    sizeof(operand)

  • operand: c th l tn kiu d liu, bin, biu thc.


K thu t l p tr nh

7. Ton t du phy (comma operator)

  • Ton t comma buc cc biu thc cng vi nhau.

  • Biu thc bn tri ca ton t comma lun lun c nh tr nh void, biu thc bn phi c nh tr v tr thnh gi tr ca biu thc.

  • Dng tng qut ca ton t comma:

    (exp_1, exp_2, ..., exp_n)


K thu t l p tr nh

7. Ton t du phy (comma operator)

  • Cc biu thc c nh tr t tri sang phi, biu thc cui cng (exp_n) c nh tr v tr thnh gi tr ca ton b biu thc.

  • V d:

    x = (y=3, y+1);

    Y c gn gi tr 3, sau x c gn gi tr y+1 l 4.


K thu t l p tr nh

8. u tin ca cc ton t


K thu t l p tr nh

4. CC CU TRC IU KHIN


K thu t l p tr nh

1.Cu trc IF

  • Dng 1:

    • C php:

      if(expression)

      statement;

  • ngha:

    Expression c nh tr. Nu

    kt qu l true th statementc thc thi, ngc li, khng lm g c.


K thu t l p tr nh

1. Cu trc IF

V d: Vit chng trnh nhp vo mt s nguyn a. In ra mn hnh kt qu a c phi l s dng khng.

#include <iostream.h>

#include <conio.h>

int main()

{

int a;

cout << "Input a = "; cin>>a;

if(a>=0)

cout << a << " is a positive.;

getch();

return 0;

}


K thu t l p tr nh

1. Cu trc IF

Lu c php

  • Dng 2:

    • C php:

      if (expression)

      statement1;

      else

      statement2;

  • ngha:

  • Nu Expression c nh l true th statement1 c thc thi.

  • Ngc li, th statement2 c thc thi.


K thu t l p tr nh

1. Cu trc IF

V d: Vit chng trnh nhp vo mt s nguyn a. In ra mn hnh kt qu kim tra a l s m hay dng.

#include <iostream.h>

#include <conio.h>

int main()

{

int a;

cout << "Input a = "; cin >> a;

if(a>=0)

cout << a << " is a positive.;

else

cout << a << " is a negative.;

getch(); return 0;

}


K thu t l p tr nh

1. Cu trc IF

Lu :

  • Ta c th s dng cc cu lnh ifelse lng nhau. Khi dng ifelse lng nhau th else s kt hp vi if gn nht cha c else.

  • Nu cu lnh if bn trong khng c else th phi t trong cp du {}


K thu t l p tr nh

2. Cu trc switch

  • Cu trc switch l mt cu trc la chn c nhiu nhnh, c s dng khi c nhiu la chn.

  • C php:

    switch(expression)

    {

    case value_1: statement_1; [break;]

    case value_n: statement_n; [break;]

    [default : statement;]

    }


K thu t l p tr nh

2. Cu trc switch


K thu t l p tr nh

2. Cu trc switch

  • Gii thch:

    • Expression s c nh tr.

    • Nu gi tr ca expression bng value_1 th thc hin statement_1 v thot.

    • Nu gi tr ca expression khc value _1 th so snh vi value_2, nu bng value_2 th thc hin statement_2 v thot., so snh ti value_n.

    • Nu tt c cc php so snh u sai th thc hin statement ca default.


K thu t l p tr nh

2. Cu trc switch

  • Lu :

    • Expression trong switch() phi c kt qu l gi tr kiu s nguyn (int, char, long).

    • Cc gi tr sau case phi l hng nguyn.

    • Khng bt buc phi c default.

    • Khi thc hin lnh tng ng ca case c gi tr bng expression, chng trnh thc hin lnh break thot khi cu trc switch.


K thu t l p tr nh

2. Cu trc switch

V d: Nhp vo mt s nguyn, chia s nguyn ny cho 2 ly phn d. Kim tra nu phn d bng 0 th in ra thng bo l s chn, nu s d bng 1 th in thng bo l s l.

#include <iostream.h>

#include <conio.h>

void main () {

int n, remainder;

cout<<Input an number: "; cin>>n; remainder = (n % 2);

switch(remainder)

{

case 0: cout << n << is an even."; break;

case 1: cout << n << is an odd."; break;

}

getch(); }


K thu t l p tr nh

3. Lnh break

  • Lnh break dng thot khi mt cu trc iu khin m khng ch n biu thc iu kin c nh tr.

  • Khi break c thc hin bn trong 1 cu trc lp, iu khin (control flow) t ng nhy n lnh u tin ngay sau cu trc lp .

  • Khng s dng lnh break bn ngoi cc cu trc lp nh while, do...while, for hay cu trc switch.


K thu t l p tr nh

3. Lnh break

V d: c vo mt mt khu ngi dng ti a attempts ln

for (i=0; i<attempts ; ++i)

{

cout<<"Input a password:;cin >> passWord;

if (check(passWord)) //kim tra mt khu

break; // thot khi vng lp

cout <<"Password is wrong!\n";

}


K thu t l p tr nh

4. Lnh continue

  • Lnh continue dng kt thc vng lp hin ti v bt u vng lp tip theo.

  • Lnh continue ch c dng trong thn cc cu trc lp nh for, while, do...while.

  • Cu lnh continue thng i km vi cu lnh if.


K thu t l p tr nh

4. Lnh continue

V d: mt vng lp thc hin c mt s, x l n nhng b qua nhng s m, v dng khi s nhp vo l s 0.

do

{

cin >> num;

if (num < 0) continue;

// process num here

} while(num != 0);


K thu t l p tr nh

5. Cu trc for

  • C php:

    for (Exp1; Exp2; Exp3)

    statement;

  • ngha:

  • Exp1: l biu thc khi to c thc hin.

  • Exp2: l biu thc iu kin

  • Exp3: biu thc iu khin lp


K thu t l p tr nh

5. Cu trc for

V d: Vit chng trnh tnh tng cc s nguyn t 1 n n.

#include <iostream.h>

#include <conio.h>

void main()

{

int i, n, sum;

cout<<Input a number:; cin >> n;

sum = 0;

for (i=1 ; i<=n ; i++)

sum += i;

cout<<Sum from 1 to << n << is: << sum;

getch();

}


K thu t l p tr nh

5. Cu trc for

  • C/C++ cho php Exp1 l mt nh ngha bin

    V d: for(int i=1; i<=n; ++i)

  • Bt k biu thc no trong 3 biu thc ca vng lp for u c th rng

    V d: for(; i != 0;) statement;

  • Xa tt c cc biu thc trong vng lp for s cho mt vng lp v tn.

    V d:

    for (;;) statement;


K thu t l p tr nh

6. Cu trc while

  • C php:

    while(expression)

    statement;

  • ngha:

  • B1: Expression c nh tr

  • B2: Nu kt qu l true th statement thc thi v quay li B1

  • B3: Nu kt qu l false th thot khi vng lp while.


K thu t l p tr nh

6. Cu trc while

V d: Vit chng trnh tnh tng cc s nguyn t 1 ti n.

#include<iostream.h>

#include<conio.h>

void main ()

{

int i, n, sum;

cout<<Input n= ; cin >> n;

i = 1; sum = 0;

while(i<=n)

{

sum += i; i++;

}

getch();

}


K thu t l p tr nh

7. Cu trc do while

  • C php:

    do {

    statement;

    }while(expression);

  • ngha:

  • Statement c thc hin

  • Expression c nh tr.

  • Nu expression l true th quay li bc 1

  • Nu expression l false th thot khi vng lp.


K thu t l p tr nh

7. Cu trc do while

V d 1: Vit chng trnh in dy s nguyn t 1 n 10.

#include <iostream.h>

#include <conio.h>

void main ()

{

int i;

clrscr();

cout<<"Display one to ten: ";

i=1;

do

{

cout << setw(3) << i;

i+=1;

} while(i<=10);

getch();

}


K thu t l p tr nh

Vng lp while v do . While

  • while(iu kin )

  • {

  • //thn vng lp

  • }

  • Nu iu kin ng th thn vng lp c thc hin, nu k sai th kt thc vng lp

  • Trong hm while th thn vng lp c th KHNG C thc thi ln no c

  • do

  • {

  • //thn vng lp

  • }while(iu kin);

  • Thn vng t nht c thc thi 1 ln

  • Hm do while s thc thi xong ri mi kim tra iu kin

Thng thng ta s dng while, do while thit lp vng lp vnh cu thc cc dng lnh lin tc no .

ta s kt hp vi t kha break thot khi vng lp vnh cu.


  • Login