You are on page 1of 3

INT2202: Hng dn Bi thc hnh 2 V phong cch lp trnh: Mi dng ch cha mt lnh V d phong cch ti: cout <<

; Nhap he so a: ; cin >> a; V d phong cch tt: cout << Nhap he so a: ; cin >> a; V ngn ng C++: ( tng trnh by y ch mang tnh gi ) Cu 1 [eq.cpp] Hm tnh cn bc hai: sqrt (xem hng dn s dng trn http://cplusplus.com/reference/clibrary/cmath/sqrt/ - ch m v d ca h l m C) Cu 2 [frame.cpp] Trong bi ny bn nn dng kiu string nh ngha sn tng: dng 1 v dng 5 in ging nhau, dng 2 v dng 4 in ging nhau. C bn l m xem mi dng cn in bao nhiu k t sao * bao nhiu du cch Cch nhp xu dng hm getline() // xem hng dn trn http://cplusplus.com/reference/string/getline/ xc nh s k t trong xu nhp vo, dng phng thc length() ca kiu string // xem hng dn trn http://cplusplus.com/reference/string/string/length/ // Chuong trinh demo cach su dung bien kieu string #include <iostream> using namespace std; int main(){ string name = ""; cout << "Moi ban nhap ten: "; getline(cin, name); cout << "Hello, " << name << "!" << endl; cout << "Ten ban co " << name.length() << " ki tu." << endl; cin.ignore(80, \n); return 0; } Cu 3 [mtable.cpp] tng: dng cc vng lp lng nhau (nn dng lnh for v c cn m r rng) ging ct c th dng k t tab \t hoc hm setw() // xem hng dn trn http://cplusplus.com/reference/iostream/manipulators/setw/ // Chuong trinh demo dung setw va setf de dong hang du lieu #include <iostream> #include <iomanip>

using namespace std; int main(){ int i = 2; char c = 'k'; double d = 3.14159; cout << "In gia tri cac bien, can phai: " << endl; cout.setf(ios::right); cout << setw(10) << i << endl << setw(10) << c << endl << setw(10) << d << endl; cout << "\nIn gia tri cac bien, can trai: " << endl; cout.unsetf(ios::right); cout.setf(ios::left); cout << setw(10) << i << endl << setw(10) << c << endl << setw(10) << d << endl;

cin.ignore(80, \n); return 0; }

Cu 4 [savings.cpp] Ch l ht 1 nm th tin li s nhp vo gc. Nh vy sau mi nm ng An li c m = m * (1 + r) Sau y nm, ng An thu c s tin m * (1 + r) * (1 + r) * * (1 + r) // y ln (1 + r) tm c gi tr y ph hp, bn cn lp ti khi no s tin thu c ln hn hoc bng p. Cu 5 [zodiac.cpp] Bn cn tm c cng thc no so snh xem ngy d1/m1 ng trc hay sau ngy d2/m2. Gi s bn vit c hm int compareDate(int d1, int m1, int d2, int m2); tr v 1 nu d1/m1 ng sau d2/m2 tr v 0 nu d1/m1 trng d2/m2 tr v -1 nu d1/m1 ng trc d2/m2 th cng vic cn li l so snh vi cc cn tm mt khong (cung hong o) ph hp cho ngy thng ngi dng nhp vo. Cu 6 [calculator.cpp] Nn dng switch kim tra php ton nhp vo l g v c x l ph hp