際際滷

際際滷Share a Scribd company logo
畛 bi 2 - KHTN 2010.
Xem m達 ngu畛n C++ 動畛c vi畉t trong b畉ng sau 但y (ch炭 箪 c叩c ph動董ng th畛c 畉o).
PHP Code:
#include <iostream>
using namespace std;
class ListItem{
protected:
int Index;
ListItem* nextItem;
public:
ListItem (int idx = 0){
Index = idx;
nextItem = NULL;
}
ListItem* addFront (int idxNew){
ListItem* anItem = create(idxNew);
anItem->nextItem = this;
return anItem;
}
void outFromThis (ostream& oDev){
ListItem* pItem = this;
while (pItem != NULL){
pItem->outItem(oDev);
pItem = pItem->nextItem;
}
}
virtual void outItem (ostream& outDev){
outDev << Index <<"";
}
virtual ListItem* create (int idxNew){
return new ListItem(idxNew);
}
ListItem* searchFromThis(int idx);//s畉 ci 畉t....
};
class LinkedList{
protected:
ListItem* firstItem;
public:
LinkedList (ListItem* head = NULL){
firstItem = head;
}
ListItem* add (int idxNew){
if (firstItem != NULL)
firstItem = firstItem->addFront(idxNew);
else
firstItem = new ListItem(idxNew);
return firstItem;
}
void out (ostream& oDev){
if (firstItem != NULL)
firstItem->outFromThis(oDev);
}
bool isEmpty(){
return (firstItem == NULL);
}
ListItem* search(int idx); //s畉 ci 畉t....
};
void main(){
LinkedList* myList = new LinkedList();
int indices[] = {8, 9, 9, 1, 0, 1, 0, 2};
int n = sizeof(indices)/sizeof(indices[0]);
for (int i = 0; i < n; i++)
myList->add(indices[i]);
myList->out(cout);
}
M畛i 畛i t動畛ng thu畛c l畛p ListItem c坦 tr動畛ng nextItem b畉ng NULL (ngh挑a l 畛ng) hay l tr畛 畉n m畛t phn t畛 k畉 ti畉p
c滴ng l m畛t 畛i t動畛ng thu畛c l畛p ny, t畛c l m畛i 畛i t動畛ng nh動 v畉y c滴ng l ph畉n t畛 畉u ti棚n c畛a m畛t danh s叩ch li棚n
k畉t (ph畉n t畛 畉u m畛t x但u).
a/ Kh担ng k畛 hai ph動董ng th畛c ch動a ci 畉t searchFromThis(), search() trong hai l畛p ListItem, LinkedList, h達y cho
bi畉t k畉t qu畉 ch畉y c畛a ch動董ng tr狸nh.
b/ D畛a vo c叩ch vi畉t ph動董ng th畛c outFromThis(), h達y ci 畉t ph動董ng th畛c searchFromThis() c畛a l畛p ListItem theo
h動畛ng d畉n nh動 sau:
PHP Code:
ListItem* ListItem::searchFromThis (int idx){
//B畉n vi畉t th棚m c叩c d嘆ng m達 ngu畛n t畉i 但y....
//
}
畛 t狸m v tr畉 v畛 ph畉n t畛 畉u ti棚n trong x但u (kh畛i 畉u t畛 畛i t動畛ng ang x辿t c畛a l畛p ListItem) c坦 tr動畛ng Index b畉ng
v畛i gi叩 tr畛 c畛a tham s畛 idx (tr畉 v畛 NULL n畉u t狸m kh担ng c坦).
c/ Vi畉t ph動董ng th畛c search() c畛a l畛p LinkedList 畛 t狸m v tr畉 v畛 ph畉n t畛 畉u ti棚n trong x但u (kh畛i 畉u t畛 畛i t動畛ng
ang x辿t c畛a l畛p ListItem) c坦 tr動畛ng Index b畉ng v畛i gi叩 tr畛 c畛a tham s畛 idx (tr畉 v畛 NULL n畉u t狸m kh担ng c坦).
Xem m達 ngu畛n c畛a l畛p Point (t畛a 畛 i畛m trong m畉t ph畉ng) v l畛p Polygon (a gi叩c) k畉 th畛a t畛 c叩c l畛p ListItem,
LinkedList b棚n tr棚n v hm main() 動畛c vi畉t l畉i nh動 sau:
PHP Code:
class Point:public ListItem{
double x, y;
public:
Point (int idxNew): ListItem(idxNew){}
Point(double xx = 0, double yy = 0, int idx = 0):ListItem(idx){
set(xx, yy);
}
void set(double xx, double yy){
x = xx; y = yy;
}
virtual ListItem* create(int idxNew){
return new Point(idxNew);
}
virtual void outItem(ostream& outDev){
outDev <<"(" << x << ","<<y<<")";
}
};
class Polygon: public LinkedList{
int nVer;
public:
Polygon(double xfirst = 0, double yfirst = 0):
LinkedList(new Point(xfirst, yfirst)){
nVer = 1;
}
Point* addVer(double x, double y){
Point* p = (Point*)add(nVer);
p->set(x,y);
nVer++;
return p;
}
static Polygon* read(istream& inDev);//s畉 ci 畉t ....
};
void main(){
Polygon pg(1.9, 8.4);
pg.addVer(1.9, 8.9);
pg.addVer(1.7, 6.8);
pg.addVer(2.5, 1.1);
pg.out(cout);
}
d/ H達y cho bi畉t k畉t qu畉 ch畉y c畛a ch動董ng tr狸nh.
e/ Ph動董ng th畛c read() c畛a l畛p polygon l m畛t ph動董ng th畛c t挑nh c坦 nhi畛m v畛 nh畉p d達y c叩c 畛nh c畛a a gi叩c t畛 thi畉t
b畛 nh畉p (m畛i 畛nh g畛m m畛t honh 畛 v tung 畛 l c叩c s畛 th畛c, c叩c s畛 c叩ch nhau b畛i d畉u kho畉ng tr畛ng hay k鱈 t畛
ng畉t) v tr畉 v畛 m畛t 畛i t動畛ng c畛a l畛p Polygon. H達y vi畉t m達 ngu畛n 畉y 畛 c畛a ph動董ng th畛c ny theo h動畛ng d畉n
sau:
PHP Code:
Polygon* Polygon::read(鱈tream& inDev){
Polygon* poly;
//B畉n vi畉t th棚m c叩c d嘆ng m達 ngu畛n t畉i 但y....
//..........
return poly;
}

More Related Content

What's hot (18)

Ctdl C05
Ctdl C05Ctdl C05
Ctdl C05
giang
Linq net
Linq net Linq net
Linq net
NguynSang29
Ctdl C02
Ctdl C02Ctdl C02
Ctdl C02
giang
Ctdl C01
Ctdl C01Ctdl C01
Ctdl C01
giang
Gioi Thieu
Gioi ThieuGioi Thieu
Gioi Thieu
giang
Ctdl C04
Ctdl C04Ctdl C04
Ctdl C04
giang
Fortran c董 s畛
Fortran c董 s畛Fortran c董 s畛
Fortran c董 s畛
Hajunior9x
Bi Gi畉ng Ng担n Ng畛 L畉p Tr狸nh CC++ - Con Tr畛 V S畛 H畛c 畛a Ch畛
Bi Gi畉ng Ng担n Ng畛 L畉p Tr狸nh CC++ - Con Tr畛 V S畛 H畛c 畛a Ch畛 Bi Gi畉ng Ng担n Ng畛 L畉p Tr狸nh CC++ - Con Tr畛 V S畛 H畛c 畛a Ch畛
Bi Gi畉ng Ng担n Ng畛 L畉p Tr狸nh CC++ - Con Tr畛 V S畛 H畛c 畛a Ch畛
nataliej4
Code v s董 畛 kh畛i m担n Ph動董ng ph叩p s畛 v l畉p tr狸nh Fortran
Code v s董 畛 kh畛i m担n Ph動董ng ph叩p s畛 v l畉p tr狸nh FortranCode v s董 畛 kh畛i m担n Ph動董ng ph叩p s畛 v l畉p tr狸nh Fortran
Code v s董 畛 kh畛i m担n Ph動董ng ph叩p s畛 v l畉p tr狸nh Fortran
Lee Ein
Bi t畉p 担n l畉p tr狸nh
Bi t畉p 担n l畉p tr狸nhBi t畉p 担n l畉p tr狸nh
Bi t畉p 担n l畉p tr狸nh
Thai Hoc Vu
Local sakainame 501127 ktl_tr狸nh hlmt1 a01 fall 2013 _ modules
Local sakainame   501127 ktl_tr狸nh hlmt1 a01 fall 2013 _ modulesLocal sakainame   501127 ktl_tr狸nh hlmt1 a01 fall 2013 _ modules
Local sakainame 501127 ktl_tr狸nh hlmt1 a01 fall 2013 _ modules
Tr畉n Vn Nam
Dethi c++ -lt
Dethi c++ -ltDethi c++ -lt
Dethi c++ -lt
H畛 L畛i
Nang cao c++
Nang cao c++Nang cao c++
Nang cao c++
Ti畉n Quang Phan
Ctdl C05
Ctdl C05Ctdl C05
Ctdl C05
giang
Ctdl C02
Ctdl C02Ctdl C02
Ctdl C02
giang
Ctdl C01
Ctdl C01Ctdl C01
Ctdl C01
giang
Gioi Thieu
Gioi ThieuGioi Thieu
Gioi Thieu
giang
Ctdl C04
Ctdl C04Ctdl C04
Ctdl C04
giang
Fortran c董 s畛
Fortran c董 s畛Fortran c董 s畛
Fortran c董 s畛
Hajunior9x
Bi Gi畉ng Ng担n Ng畛 L畉p Tr狸nh CC++ - Con Tr畛 V S畛 H畛c 畛a Ch畛
Bi Gi畉ng Ng担n Ng畛 L畉p Tr狸nh CC++ - Con Tr畛 V S畛 H畛c 畛a Ch畛 Bi Gi畉ng Ng担n Ng畛 L畉p Tr狸nh CC++ - Con Tr畛 V S畛 H畛c 畛a Ch畛
Bi Gi畉ng Ng担n Ng畛 L畉p Tr狸nh CC++ - Con Tr畛 V S畛 H畛c 畛a Ch畛
nataliej4
Code v s董 畛 kh畛i m担n Ph動董ng ph叩p s畛 v l畉p tr狸nh Fortran
Code v s董 畛 kh畛i m担n Ph動董ng ph叩p s畛 v l畉p tr狸nh FortranCode v s董 畛 kh畛i m担n Ph動董ng ph叩p s畛 v l畉p tr狸nh Fortran
Code v s董 畛 kh畛i m担n Ph動董ng ph叩p s畛 v l畉p tr狸nh Fortran
Lee Ein
Bi t畉p 担n l畉p tr狸nh
Bi t畉p 担n l畉p tr狸nhBi t畉p 担n l畉p tr狸nh
Bi t畉p 担n l畉p tr狸nh
Thai Hoc Vu
Local sakainame 501127 ktl_tr狸nh hlmt1 a01 fall 2013 _ modules
Local sakainame   501127 ktl_tr狸nh hlmt1 a01 fall 2013 _ modulesLocal sakainame   501127 ktl_tr狸nh hlmt1 a01 fall 2013 _ modules
Local sakainame 501127 ktl_tr狸nh hlmt1 a01 fall 2013 _ modules
Tr畉n Vn Nam
Dethi c++ -lt
Dethi c++ -ltDethi c++ -lt
Dethi c++ -lt
H畛 L畛i

Viewers also liked (16)

Actividad 3Actividad 3
Actividad 3
gloriacaravez19
Mi blog de fisioterapiaMi blog de fisioterapia
Mi blog de fisioterapia
migniegar
Fichas San Fernando 8.1 Fichas San Fernando 8.1
Fichas San Fernando 8.1
juliandavidlondono
Device Modeling Plan(List2)
Device Modeling Plan(List2)Device Modeling Plan(List2)
Device Modeling Plan(List2)
Tsuyoshi Horigome
Tarea del seminario 2Tarea del seminario 2
Tarea del seminario 2
migniegar
Coaching Certificate1
Coaching Certificate1Coaching Certificate1
Coaching Certificate1
Prasad Palav
Budget
BudgetBudget
Budget
Michael Yudkovich
Casablanca
CasablancaCasablanca
Casablanca
Michael Umphrey
Flash mxFlash mx
Flash mx
Hernan133
Descansarei - Nani Azevedo
Descansarei - Nani AzevedoDescansarei - Nani Azevedo
Descansarei - Nani Azevedo
Josiane Ap. Pontes dos Santos
亠亟舒亞仂亞亳亠从舒 亟仂从仄亠仆舒亳 亟仍 丕
亠亟舒亞仂亞亳亠从舒 亟仂从仄亠仆舒亳 亟仍 丕亠亟舒亞仂亞亳亠从舒 亟仂从仄亠仆舒亳 亟仍 丕
亠亟舒亞仂亞亳亠从舒 亟仂从仄亠仆舒亳 亟仍 丕
Airat Yusupov
Sms
SmsSms
Sms
Kumar
La prostitucionLa prostitucion
La prostitucion
545fd4f5
Em teus bra巽os laura souguellisEm teus bra巽os laura souguellis
Em teus bra巽os laura souguellis
Gleici31
Me GustaMe Gusta
Me Gusta
fernando rodriguez
Actividad 3Actividad 3
Actividad 3
gloriacaravez19
Mi blog de fisioterapiaMi blog de fisioterapia
Mi blog de fisioterapia
migniegar
Fichas San Fernando 8.1 Fichas San Fernando 8.1
Fichas San Fernando 8.1
juliandavidlondono
Device Modeling Plan(List2)
Device Modeling Plan(List2)Device Modeling Plan(List2)
Device Modeling Plan(List2)
Tsuyoshi Horigome
Tarea del seminario 2Tarea del seminario 2
Tarea del seminario 2
migniegar
Coaching Certificate1
Coaching Certificate1Coaching Certificate1
Coaching Certificate1
Prasad Palav
Flash mxFlash mx
Flash mx
Hernan133
亠亟舒亞仂亞亳亠从舒 亟仂从仄亠仆舒亳 亟仍 丕
亠亟舒亞仂亞亳亠从舒 亟仂从仄亠仆舒亳 亟仍 丕亠亟舒亞仂亞亳亠从舒 亟仂从仄亠仆舒亳 亟仍 丕
亠亟舒亞仂亞亳亠从舒 亟仂从仄亠仆舒亳 亟仍 丕
Airat Yusupov
Sms
SmsSms
Sms
Kumar
La prostitucionLa prostitucion
La prostitucion
545fd4f5
Em teus bra巽os laura souguellisEm teus bra巽os laura souguellis
Em teus bra巽os laura souguellis
Gleici31

Similar to Cau 2 (20)

Bi t畉p CTDL v GT 1
Bi t畉p CTDL v GT 1Bi t畉p CTDL v GT 1
Bi t畉p CTDL v GT 1
H畛 L畛i
Bi t畉p CTDL v GT 12
Bi t畉p CTDL v GT 12Bi t畉p CTDL v GT 12
Bi t畉p CTDL v GT 12
H畛 L畛i
Chuong5 (2)
Chuong5 (2)Chuong5 (2)
Chuong5 (2)
H畛 L畛i
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Lap trinh huong_doi_tuong_cpp_dhct_lesson07Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
xcode_esvn
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Lap trinh huong_doi_tuong_cpp_dhct_lesson08Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
xcode_esvn
Oop unit 09 l畉p tr狸nh t畛ng qu叩t
Oop unit 09 l畉p tr狸nh t畛ng qu叩tOop unit 09 l畉p tr狸nh t畛ng qu叩t
Oop unit 09 l畉p tr狸nh t畛ng qu叩t
Tr叩ng H Vi畉t
Bai tap java
Bai tap javaBai tap java
Bai tap java
Phan van giap
B叩o c叩o tu畉n 5- X但y d畛ng c叩c class li棚n quan 董n hng, s畉n ph畉m, database
B叩o c叩o tu畉n 5- X但y d畛ng c叩c class li棚n quan 董n hng, s畉n ph畉m, databaseB叩o c叩o tu畉n 5- X但y d畛ng c叩c class li棚n quan 董n hng, s畉n ph畉m, database
B叩o c叩o tu畉n 5- X但y d畛ng c叩c class li棚n quan 董n hng, s畉n ph畉m, database
Dlinh Truong
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provipSV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
huykhadeo113
際際滷 lap trinh web sieu nang cao provip
際際滷 lap trinh web sieu nang cao provip際際滷 lap trinh web sieu nang cao provip
際際滷 lap trinh web sieu nang cao provip
huykhadeo113
Java ease learning(1)
Java ease learning(1)Java ease learning(1)
Java ease learning(1)
Nguyen Xuan
ky-thuat-lap-trinh__chuong4_mang-mot-chieu - [cuuduongthancong.com].pdf
ky-thuat-lap-trinh__chuong4_mang-mot-chieu - [cuuduongthancong.com].pdfky-thuat-lap-trinh__chuong4_mang-mot-chieu - [cuuduongthancong.com].pdf
ky-thuat-lap-trinh__chuong4_mang-mot-chieu - [cuuduongthancong.com].pdf
LinhLe296944
01 ngon ngu_c#_phan_1
01 ngon ngu_c#_phan_101 ngon ngu_c#_phan_1
01 ngon ngu_c#_phan_1
htpsccbb159
01 ngon ngu_c#_phan_1
01 ngon ngu_c#_phan_101 ngon ngu_c#_phan_1
01 ngon ngu_c#_phan_1
Tran Trung Hieu
Bi 2: L畉p tr狸nh h動畛ng 畛i t動畛ng & Collection - L畉p tr狸nh winform - Gi叩o tr狸n...
Bi 2: L畉p tr狸nh h動畛ng 畛i t動畛ng & Collection - L畉p tr狸nh winform - Gi叩o tr狸n...Bi 2: L畉p tr狸nh h動畛ng 畛i t動畛ng & Collection - L畉p tr狸nh winform - Gi叩o tr狸n...
Bi 2: L畉p tr狸nh h動畛ng 畛i t動畛ng & Collection - L畉p tr狸nh winform - Gi叩o tr狸n...
MasterCode.vn
Stack &amp; queue
Stack &amp; queueStack &amp; queue
Stack &amp; queue
K畛 T担n Th畉t
Bi t畉p CTDL v GT 1
Bi t畉p CTDL v GT 1Bi t畉p CTDL v GT 1
Bi t畉p CTDL v GT 1
H畛 L畛i
Bi t畉p CTDL v GT 12
Bi t畉p CTDL v GT 12Bi t畉p CTDL v GT 12
Bi t畉p CTDL v GT 12
H畛 L畛i
Chuong5 (2)
Chuong5 (2)Chuong5 (2)
Chuong5 (2)
H畛 L畛i
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Lap trinh huong_doi_tuong_cpp_dhct_lesson07Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
xcode_esvn
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Lap trinh huong_doi_tuong_cpp_dhct_lesson08Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
xcode_esvn
Oop unit 09 l畉p tr狸nh t畛ng qu叩t
Oop unit 09 l畉p tr狸nh t畛ng qu叩tOop unit 09 l畉p tr狸nh t畛ng qu叩t
Oop unit 09 l畉p tr狸nh t畛ng qu叩t
Tr叩ng H Vi畉t
B叩o c叩o tu畉n 5- X但y d畛ng c叩c class li棚n quan 董n hng, s畉n ph畉m, database
B叩o c叩o tu畉n 5- X但y d畛ng c叩c class li棚n quan 董n hng, s畉n ph畉m, databaseB叩o c叩o tu畉n 5- X但y d畛ng c叩c class li棚n quan 董n hng, s畉n ph畉m, database
B叩o c叩o tu畉n 5- X但y d畛ng c叩c class li棚n quan 董n hng, s畉n ph畉m, database
Dlinh Truong
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provipSV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
huykhadeo113
際際滷 lap trinh web sieu nang cao provip
際際滷 lap trinh web sieu nang cao provip際際滷 lap trinh web sieu nang cao provip
際際滷 lap trinh web sieu nang cao provip
huykhadeo113
Java ease learning(1)
Java ease learning(1)Java ease learning(1)
Java ease learning(1)
Nguyen Xuan
ky-thuat-lap-trinh__chuong4_mang-mot-chieu - [cuuduongthancong.com].pdf
ky-thuat-lap-trinh__chuong4_mang-mot-chieu - [cuuduongthancong.com].pdfky-thuat-lap-trinh__chuong4_mang-mot-chieu - [cuuduongthancong.com].pdf
ky-thuat-lap-trinh__chuong4_mang-mot-chieu - [cuuduongthancong.com].pdf
LinhLe296944
01 ngon ngu_c#_phan_1
01 ngon ngu_c#_phan_101 ngon ngu_c#_phan_1
01 ngon ngu_c#_phan_1
htpsccbb159
01 ngon ngu_c#_phan_1
01 ngon ngu_c#_phan_101 ngon ngu_c#_phan_1
01 ngon ngu_c#_phan_1
Tran Trung Hieu
Bi 2: L畉p tr狸nh h動畛ng 畛i t動畛ng & Collection - L畉p tr狸nh winform - Gi叩o tr狸n...
Bi 2: L畉p tr狸nh h動畛ng 畛i t動畛ng & Collection - L畉p tr狸nh winform - Gi叩o tr狸n...Bi 2: L畉p tr狸nh h動畛ng 畛i t動畛ng & Collection - L畉p tr狸nh winform - Gi叩o tr狸n...
Bi 2: L畉p tr狸nh h動畛ng 畛i t動畛ng & Collection - L畉p tr狸nh winform - Gi叩o tr狸n...
MasterCode.vn

More from H畛 L畛i (20)

Xu ly chuoi
Xu ly chuoiXu ly chuoi
Xu ly chuoi
H畛 L畛i
T坦m t畉t c叩c hm chu畉n c畛a c
T坦m t畉t c叩c hm chu畉n c畛a cT坦m t畉t c叩c hm chu畉n c畛a c
T坦m t畉t c叩c hm chu畉n c畛a c
H畛 L畛i
Nguyen lyoop
Nguyen lyoopNguyen lyoop
Nguyen lyoop
H畛 L畛i
Lect04 functions
Lect04 functionsLect04 functions
Lect04 functions
H畛 L畛i
Ky thuatkhudequy
Ky thuatkhudequyKy thuatkhudequy
Ky thuatkhudequy
H畛 L畛i
Itt epc assignment
Itt epc assignmentItt epc assignment
Itt epc assignment
H畛 L畛i
Huong danontapc
Huong danontapcHuong danontapc
Huong danontapc
H畛 L畛i
H hai epc_baitap
H hai epc_baitapH hai epc_baitap
H hai epc_baitap
H畛 L畛i
Giaotrinhbaitapkythuatlaptrinh
GiaotrinhbaitapkythuatlaptrinhGiaotrinhbaitapkythuatlaptrinh
Giaotrinhbaitapkythuatlaptrinh
H畛 L畛i
Giao trinh ky thuat lap trinh 2
Giao trinh ky thuat lap trinh 2Giao trinh ky thuat lap trinh 2
Giao trinh ky thuat lap trinh 2
H畛 L畛i
Giao trinh c c++
Giao trinh c c++Giao trinh c c++
Giao trinh c c++
H畛 L畛i
File trong c_
File trong c_File trong c_
File trong c_
H畛 L畛i
Epc assignment
Epc assignmentEpc assignment
Epc assignment
H畛 L畛i
Epc test practical
Epc test practicalEpc test practical
Epc test practical
H畛 L畛i
De thic++ --th
De thic++ --thDe thic++ --th
De thic++ --th
H畛 L畛i
Debug trong c
Debug trong cDebug trong c
Debug trong c
H畛 L畛i
Xu ly chuoi
Xu ly chuoiXu ly chuoi
Xu ly chuoi
H畛 L畛i
T坦m t畉t c叩c hm chu畉n c畛a c
T坦m t畉t c叩c hm chu畉n c畛a cT坦m t畉t c叩c hm chu畉n c畛a c
T坦m t畉t c叩c hm chu畉n c畛a c
H畛 L畛i
Nguyen lyoop
Nguyen lyoopNguyen lyoop
Nguyen lyoop
H畛 L畛i
Lect04 functions
Lect04 functionsLect04 functions
Lect04 functions
H畛 L畛i
Ky thuatkhudequy
Ky thuatkhudequyKy thuatkhudequy
Ky thuatkhudequy
H畛 L畛i
Itt epc assignment
Itt epc assignmentItt epc assignment
Itt epc assignment
H畛 L畛i
Huong danontapc
Huong danontapcHuong danontapc
Huong danontapc
H畛 L畛i
H hai epc_baitap
H hai epc_baitapH hai epc_baitap
H hai epc_baitap
H畛 L畛i
Giaotrinhbaitapkythuatlaptrinh
GiaotrinhbaitapkythuatlaptrinhGiaotrinhbaitapkythuatlaptrinh
Giaotrinhbaitapkythuatlaptrinh
H畛 L畛i
Giao trinh ky thuat lap trinh 2
Giao trinh ky thuat lap trinh 2Giao trinh ky thuat lap trinh 2
Giao trinh ky thuat lap trinh 2
H畛 L畛i
Giao trinh c c++
Giao trinh c c++Giao trinh c c++
Giao trinh c c++
H畛 L畛i
File trong c_
File trong c_File trong c_
File trong c_
H畛 L畛i
Epc assignment
Epc assignmentEpc assignment
Epc assignment
H畛 L畛i
Epc test practical
Epc test practicalEpc test practical
Epc test practical
H畛 L畛i
De thic++ --th
De thic++ --thDe thic++ --th
De thic++ --th
H畛 L畛i
Debug trong c
Debug trong cDebug trong c
Debug trong c
H畛 L畛i

Cau 2

  • 1. 畛 bi 2 - KHTN 2010. Xem m達 ngu畛n C++ 動畛c vi畉t trong b畉ng sau 但y (ch炭 箪 c叩c ph動董ng th畛c 畉o). PHP Code: #include <iostream> using namespace std; class ListItem{ protected: int Index; ListItem* nextItem; public: ListItem (int idx = 0){ Index = idx; nextItem = NULL; } ListItem* addFront (int idxNew){ ListItem* anItem = create(idxNew); anItem->nextItem = this; return anItem; } void outFromThis (ostream& oDev){ ListItem* pItem = this; while (pItem != NULL){ pItem->outItem(oDev); pItem = pItem->nextItem; } } virtual void outItem (ostream& outDev){ outDev << Index <<""; } virtual ListItem* create (int idxNew){ return new ListItem(idxNew); } ListItem* searchFromThis(int idx);//s畉 ci 畉t.... }; class LinkedList{ protected: ListItem* firstItem; public: LinkedList (ListItem* head = NULL){ firstItem = head; } ListItem* add (int idxNew){ if (firstItem != NULL) firstItem = firstItem->addFront(idxNew); else firstItem = new ListItem(idxNew); return firstItem; } void out (ostream& oDev){ if (firstItem != NULL) firstItem->outFromThis(oDev); } bool isEmpty(){ return (firstItem == NULL); } ListItem* search(int idx); //s畉 ci 畉t.... }; void main(){
  • 2. LinkedList* myList = new LinkedList(); int indices[] = {8, 9, 9, 1, 0, 1, 0, 2}; int n = sizeof(indices)/sizeof(indices[0]); for (int i = 0; i < n; i++) myList->add(indices[i]); myList->out(cout); } M畛i 畛i t動畛ng thu畛c l畛p ListItem c坦 tr動畛ng nextItem b畉ng NULL (ngh挑a l 畛ng) hay l tr畛 畉n m畛t phn t畛 k畉 ti畉p c滴ng l m畛t 畛i t動畛ng thu畛c l畛p ny, t畛c l m畛i 畛i t動畛ng nh動 v畉y c滴ng l ph畉n t畛 畉u ti棚n c畛a m畛t danh s叩ch li棚n k畉t (ph畉n t畛 畉u m畛t x但u). a/ Kh担ng k畛 hai ph動董ng th畛c ch動a ci 畉t searchFromThis(), search() trong hai l畛p ListItem, LinkedList, h達y cho bi畉t k畉t qu畉 ch畉y c畛a ch動董ng tr狸nh. b/ D畛a vo c叩ch vi畉t ph動董ng th畛c outFromThis(), h達y ci 畉t ph動董ng th畛c searchFromThis() c畛a l畛p ListItem theo h動畛ng d畉n nh動 sau: PHP Code: ListItem* ListItem::searchFromThis (int idx){ //B畉n vi畉t th棚m c叩c d嘆ng m達 ngu畛n t畉i 但y.... // } 畛 t狸m v tr畉 v畛 ph畉n t畛 畉u ti棚n trong x但u (kh畛i 畉u t畛 畛i t動畛ng ang x辿t c畛a l畛p ListItem) c坦 tr動畛ng Index b畉ng v畛i gi叩 tr畛 c畛a tham s畛 idx (tr畉 v畛 NULL n畉u t狸m kh担ng c坦). c/ Vi畉t ph動董ng th畛c search() c畛a l畛p LinkedList 畛 t狸m v tr畉 v畛 ph畉n t畛 畉u ti棚n trong x但u (kh畛i 畉u t畛 畛i t動畛ng ang x辿t c畛a l畛p ListItem) c坦 tr動畛ng Index b畉ng v畛i gi叩 tr畛 c畛a tham s畛 idx (tr畉 v畛 NULL n畉u t狸m kh担ng c坦). Xem m達 ngu畛n c畛a l畛p Point (t畛a 畛 i畛m trong m畉t ph畉ng) v l畛p Polygon (a gi叩c) k畉 th畛a t畛 c叩c l畛p ListItem, LinkedList b棚n tr棚n v hm main() 動畛c vi畉t l畉i nh動 sau: PHP Code: class Point:public ListItem{ double x, y; public: Point (int idxNew): ListItem(idxNew){} Point(double xx = 0, double yy = 0, int idx = 0):ListItem(idx){ set(xx, yy); } void set(double xx, double yy){ x = xx; y = yy; } virtual ListItem* create(int idxNew){ return new Point(idxNew); } virtual void outItem(ostream& outDev){ outDev <<"(" << x << ","<<y<<")"; } }; class Polygon: public LinkedList{ int nVer; public: Polygon(double xfirst = 0, double yfirst = 0): LinkedList(new Point(xfirst, yfirst)){ nVer = 1; } Point* addVer(double x, double y){ Point* p = (Point*)add(nVer); p->set(x,y); nVer++; return p; } static Polygon* read(istream& inDev);//s畉 ci 畉t .... }; void main(){
  • 3. Polygon pg(1.9, 8.4); pg.addVer(1.9, 8.9); pg.addVer(1.7, 6.8); pg.addVer(2.5, 1.1); pg.out(cout); } d/ H達y cho bi畉t k畉t qu畉 ch畉y c畛a ch動董ng tr狸nh. e/ Ph動董ng th畛c read() c畛a l畛p polygon l m畛t ph動董ng th畛c t挑nh c坦 nhi畛m v畛 nh畉p d達y c叩c 畛nh c畛a a gi叩c t畛 thi畉t b畛 nh畉p (m畛i 畛nh g畛m m畛t honh 畛 v tung 畛 l c叩c s畛 th畛c, c叩c s畛 c叩ch nhau b畛i d畉u kho畉ng tr畛ng hay k鱈 t畛 ng畉t) v tr畉 v畛 m畛t 畛i t動畛ng c畛a l畛p Polygon. H達y vi畉t m達 ngu畛n 畉y 畛 c畛a ph動董ng th畛c ny theo h動畛ng d畉n sau: PHP Code: Polygon* Polygon::read(鱈tream& inDev){ Polygon* poly; //B畉n vi畉t th棚m c叩c d嘆ng m達 ngu畛n t畉i 但y.... //.......... return poly; }