You are on page 1of 6

Cc cu lnh thng gp khi lp trnh C++

(T Trung Hiu, 29-nov-2007) In d liu (output) Chng ta dng bin cout in cc biu thc ra mn hnh cout << biu thc; cout << biu thc1 << biu thc 2; V d nh cc lnh sau cout << "xin chao cac ban"; cout << "Tong " << 3+4; in v xung dng, ta dng bin endl cout << "xin chao" << endl;

Cu lnh sau y s in ra ba chui trn ba dng, mc d chng ch c vit trn mt dng cout << "thu hai" << endl << "thu ba" << endl << "thu tu" << endl;

Khai bo bin (variable declaration) Chng ta dng tn kiu trc v sau l bin int x; //khai bo x kiu nguyn int a, b; //khai bo a v b u kiu nguyn double m, n, p, q; //khai bo m, n, p, q u kiu thc kp

Chng ta c th kt hp vic khai bo v khi to gi tr double x, s = 0; //khai bo s v x u kiu double, ban u s bng 0, x cha xc nh int a=1, b=2; //khai bo a v b u kiu nguyn, ban u a=1 v b=2

Nhp d liu (input) Chng ta dng bin cin nhp s liu cho cc bin cin >> bin; cin >> bin1 >> bin2;

V d cin >> x; cin >> a >> b;

Chng ta nn thng bo cho ngi dng trc khi nhp s liu, ngi dng khng b b ng v khng bit nhp g cout << "Nhap a"; cin >> a; cout << "Nhap b"; cin >> b;

nh km (include) Chng ta thng dng nh km u chng trnh, nhng chng ta cng c th dng gia chng trnh. Ngha l vic nh km khng bt buc phi vit u. nh km cho php chng ta khai bo cc tp tin #include <tn tp tin> #include <tn tp tin.h>

V d #include <iostream> #include <cmath>

Cc th vin theo chun c ca C++ thng kt thc bng du chm v ch h v d nh cc th vin #include <stdio.h> #include <math.h>

Tuy nhin theo chun mi, cc th vin ny c vit khng cn du chm v ch h cui m bo tng thch vi nhiu phin bn C++ khc c phn m rng khng phi l h S dng khng gian tn (namespace) Trong lp trnh hng i tng, khng gian tn cho php qun l cc i tng thnh nhm ring bit. Trong mi nhm cc tn khng trng lp, nhng trong hai nhm khc nhau c th c cc tn trng lp. Khi cc bin c vit trong khng gian tn, vic s dng bin s phi dn n vic s dng mnh using namespace hoc phi dng y tn. V d chng ta mun dng bin cin, cout, endl trong khng gian tn std, ta phi dng y tn std::cin >> x; std::cout << "Xin chao ban" << std::endl; rt ngn vic vit li tn ca khng gian cha bin, ta dng mnh using trn u chng trnh using namespace std;

Sau ti mi ni trong chng trnh, ta ch vit tn ngn cin >> x; cout << "Xin chao ban" << endl;

Nu chng ta s dng th vin c, v d iostream.h ca C++, th cc bin cha c t chc thnh khng gian tn, do chng ta khng cn dng mnh using. Nhng nu chng ta s dng th vin mi, v d iostream khng c chm h cui, vic s dng khng gian tn hoc mnh using l bt buc. Tng v gim (increasement) Cho php tng gi tr mt bin ln hoc gim gi tr i x++ tng ln mt n v x += a tng ln a n v, ging vi x = x + a x-- gim gi tr ca bin x i mt n v x -= a gim gi tr ca bin x i a n v

V d x++; x += a*a; y--; y -= a*b;

Tnh cn bc hai (square root) Cho php chng ta ly cn bc hai ca mt s y = sqrt(x) ging nh y =

V d s = sqrt( p*(p-a)*(p-b)*(p-c) ) ging nh s = p( p a)( p b)( p c) x1 = ( -b + sqrt(d) ) / (2 * a) ging nh x1 =

b+ d 2a

Tnh m (exponential) Cho php chng ta ly m ca mt s no y = exp(x) ging nh

y = ex y = ax
nhng s nhanh hn vic dng pow(a, 2)

y = pow(a, x) ging nh y = a*a ging nh

y = a2

y = pow(a, 0.5) ging nh y = y = pow(a, 0.25) ging nh nh ngha hm

y=4 x

Dng nh ngha hay xc nh ngha cng nh cch thc hin mt hm. Chng ta cn phi ni tn hm, kiu tr v, cc i s v cc lnh thc hin vic tnh ton bin i u vo thnh u ra kiu-tr-v tn-hm ( cc-tham-s ) { cc-lnh } 4

V d n gin nht l hm khng tr v gi tr void main( ) { }

Ta c th nh ngha hm tong tnh tng bnh phng ca hai s truyn vo double tong(double x, double y) { x = x*x; y = y*y; return x + y; }

Hm tong c hai i s l x v y u c kiu double v tr v (return) mt gi tr kiu double bng tng bnh phng ca hai gi tr truyn vo. Hm ng (closed) l hm ch thc hin vic x l ni b trong n, khng c gi tr truyn vo v khng c gi tr tr v. void giaipt( ) { double a; cout << "nhap a:"; cin >> a; double b; cout << "nhap b:"; cin >> b; double c; cout << "nhap c:"; cin >> c; double d = b*b - 4*a*c; double x1 = ( -b - sqrt(d) ) / ( 2 * a ); double x2 = ( -b + sqrt(d) ) / ( 2 * a ); cout << "Nghiem thu nhat la " << x1 << endl; cout << "Nghiem thu hai la " << x2 << endl; } Ch thch (comment) Dng gii thch mt cu lnh hoc on lnh trong chng trnh.

Ch thch c th c vit bng ting Anh, Vit, Php hoc bt k th ting no v s khng c tnh vo chng trnh. void main( ) { int x, y; //khai bo bin x v y u kiu nguyn x = 1; //gn gi tr 1 cho bin x y = 2; //gn gi tr 2 cho bin y } Ch thch i khi c dng trang tr hoc lm dng k ngn cch gia cc hm m ngun d nhn //////////////////////////////////////////// // y l th vin cc hm ton hc //---------------------------------------int tong(int x, int y) { return x+y; } //---------------------------------------int hieu(int x, int y) { return x-y; }

Bn th ngh xem, chng trnh sau y in ra my dng? #include <iostream.h> void main( ) { cout << "Dong thu nhat" << endl; // cout << "Dong thu nhat" << endl; }