Lập trình hướng đối tượng với C++ - Phụ lục 4: Mã chương trình bài toán quan hệ gia đình
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
class Nguoi
{
friend class Nam;
friend class Nu;
static Nguoi* NhanDan[100];
static int SoDan;
char Ten[25];
Nam *Bo;
Nu *Me;
Nguoi *AnhChi[10], *CacEm[10], *CacCon[10];
int SoAnhChi, SoEm, SoCon;
Nguoi(char *ten, Nam *bo, Nu *me) :
Bo(bo), Me(me), SoAnhChi(0), SoEm(0), SoCon(0)
{
strcpy(Ten, ten);
}
], *CacCon[10]; int SoAnhChi, SoEm, SoCon; Nguoi(char *ten, Nam *bo, Nu *me) : Bo(bo), Me(me), SoAnhChi(0), SoEm(0), SoCon(0) { strcpy(Ten, ten); } void ThemAnhChi(Nguoi* nguoi) { AnhChi[SoAnhChi++] = nguoi; } void ThemEm(Nguoi* nguoi) { CacEm[SoEm++] = nguoi; } void ThemCon(Nguoi* nguoi) { CacCon[SoCon++] = nguoi; } public: static int LaySoDan() { return SoDan; } static Nguoi* ThemDan(Nguoi* nguoi) { return NhanDan[SoDan++] = nguoi; } static Nguoi* TaoNguoi(char* ten, int gioitinh, Nam *bo=0, Nu *me=0); static Nguoi* TimNguoi(char* ten); static void XoaDuLieu(); virtual int GioiTinh()=0; virtual int Cuoi(Nguoi*)=0; int LaCha(Nguoi *); int LaMe(Nguoi *); int LaCon(Nguoi *); int LaAnh(Nguoi *); int LaChi(Nguoi *); int LaEm(Nguoi *); int LaCo(Nguoi *); int LaDi(Nguoi *); int LaChu(Nguoi *); int LaCau(Nguoi *); int LaMo(Nguoi *); int LaBac(Nguoi *); int LaOngNoi(Nguoi *); int LaBaNoi(Nguoi *); int LaOngNgoai(Nguoi *); int LaBaNgoai(Nguoi *); int LaAnhHo(Nguoi *); int LaChiHo(Nguoi *); int LaEmHo(Nguoi *); virtual int LaVo(Nguoi*)=0; virtual int LaChong(Nguoi*)=0; char* LayTen() { return Ten; } }; Nguoi* Nguoi::NhanDan[]; int Nguoi::SoDan = 0; class Nam : public Nguoi { Nu *Vo; int LaVo(Nguoi *) { return 0; } public: Nam(char *ten, Nam *bo=0, Nu *me=0) : Nguoi(ten, bo, me), Vo(0) {} int GioiTinh() { return 1; } int Cuoi(Nguoi *vo); int LaChong(Nguoi * nguoi); }; class Nu : public Nguoi { Nam *Chong; int LaChong(Nguoi *) { return 0; } public: Nu(char *ten, Nam *bo=0, Nu *me=0) : Nguoi(ten, bo, me), Chong(0) {} int GioiTinh() { return 0; } int Cuoi(Nguoi *chong); void SinhCon(char* ten, int gioitinh); int LaVo(Nguoi * nguoi); }; Nguoi* Nguoi::TaoNguoi(char* ten, int gioitinh, Nam *bo, Nu *me) { return gioitinh ? ThemDan(new Nam(ten, bo, me)) : ThemDan(new Nu(ten, bo, me)); } Nguoi* Nguoi::TimNguoi(char* ten) { for (int i=0; i<SoDan; i++) if (strcmp(ten, NhanDan[i]->LayTen())==0) return NhanDan[i]; return 0; } void Nguoi::XoaDuLieu() { for (int i=0; i<SoDan; i++) delete NhanDan[i]; SoDan = 0; } inline int Nguoi::LaCha(Nguoi *nguoi) { return nguoi->Bo==this; } inline int Nguoi::LaMe(Nguoi *nguoi) { return nguoi->Me==this; } int Nguoi::LaCon(Nguoi *nguoi) { return nguoi->LaCha(this)||nguoi->LaMe(this); } int Nguoi::LaAnh(Nguoi *nguoi) { if (GioiTinh()==0) return 0; return nguoi->LaEm(this); } int Nguoi::LaChi(Nguoi *nguoi) { if (GioiTinh()) return 0; return nguoi->LaEm(this); } int Nguoi::LaEm(Nguoi *nguoi) { for (int i=0; i<SoAnhChi; i++) if (AnhChi[i]==nguoi) return 1; return 0; } int Nguoi::LaCo(Nguoi *nguoi) { if (GioiTinh()) return 0; for (int i=0; i<SoDan; i++) { if (NhanDan[i]->LaAnh(this)&&NhanDan[i]->LaCha(nguoi)) return 1; } return 0; } int Nguoi::LaDi(Nguoi *nguoi) { if (GioiTinh()) return 0; for (int i=0; i<SoDan; i++) if (NhanDan[i]->LaChi(this)&&NhanDan[i]->LaMe(nguoi)) return 1; return 0; } int Nguoi::LaChu(Nguoi *nguoi) { if (GioiTinh()==0) return 0; for (int i=0; i<SoDan; i++) if (LaEm(NhanDan[i])&&NhanDan[i]->LaCha(nguoi)) return 1; return 0; } int LaCau(Nguoi *nguoi) { if (GioiTinh()==0) return 0; for (int i=0; i<SoDan; i++) if (LaEm(NhanDan[i])&&NhanDan[i]->LaMe(nguoi)) return 1; return 0; } int LaMo(Nguoi *nguoi) { if (GioiTinh()) return 0; for (int i=0; i<SoDan; i++) if (LaVo(NhanDan[i])&&NhanDan[i]->LaCau(nguoi)) return 1; return 0; } int Nguoi::LaBac(Nguoi *nguoi) { for (int i=0; i<SoDan; i++) if (NhanDan[i]->LaEm(this)&&nguoi->LaCon(NhanDan[i])) return 1; return 0; } int Nguoi::LaOngNoi(Nguoi *nguoi) { if (GioiTinh()==0) return 0; for (int i=0; i<SoDan; i++) if (LaCha(NhanDan[i])&&NhanDan[i]->LaCha(nguoi)) return 1; return 0; } int Nguoi::LaBaNoi(Nguoi *nguoi) { if (GioiTinh()) return 0; for (int i=0; i<SoDan; i++) if (LaMe(NhanDan[i])&&NhanDan[i]->LaCha(nguoi)) return 1; return 0; } int Nguoi::LaOngNgoai(Nguoi *nguoi) { if (GioiTinh()==0) return 0; for (int i=0; i<SoDan; i++) if (LaCha(NhanDan[i])&&NhanDan[i]->LaMe(nguoi)) return 1; return 0; } int Nguoi::LaBaNgoai(Nguoi *nguoi) { if (GioiTinh()) return 0; for (int i=0; i<SoDan; i++) if (LaMe(NhanDan[i])&&NhanDan[i]->LaMe(nguoi)) return 1; return 0; } int Nguoi::LaAnhHo(Nguoi *nguoi) { if (GioiTinh()==0) return 0; return nguoi->LaEmHo(this); } int Nguoi::LaChiHo(Nguoi *nguoi) { if (GioiTinh()) return 0; return nguoi->LaEmHo(this); } int Nguoi::LaEmHo(Nguoi *nguoi) { for (int i=0; i<SoDan; i++) if ( LaCon(NhanDan[i])&&(NhanDan[i]->LaChu(nguoi) ||NhanDan[i]->LaDi(nguoi)||NhanDan[i]->LaCo(nguoi)) ) return 1; return 0; } int Nam::Cuoi(Nguoi *vo) { if (Vo||vo->GioiTinh()) return 0; Vo = (Nu*)vo; Vo->Cuoi(this); return 1; } inline int Nam::LaChong(Nguoi *nguoi) { return Vo==nguoi; } int Nu::Cuoi(Nguoi *chong) { if (Chong||chong->GioiTinh()==0) return 0; Chong = (Nam*)chong; Chong->Cuoi(this); return 1; } void Nu::SinhCon(char *ten, int gioitinh) { Nguoi* nguoi = TaoNguoi(ten, gioitinh, Chong, this); ThemCon(nguoi); if (Chong) Chong->ThemCon(nguoi); for (int i=0; i<SoCon; i++) { CacCon[i]->ThemEm(nguoi); nguoi->ThemAnhChi(CacCon[i]); } } inline int Nu::LaVo(Nguoi *nguoi) { return Chong==nguoi; } char qh[256]; char* QuanHe(Nguoi* A, Nguoi* B) { for (int i=1; i<=2; i++) { strcpy(qh, A->LayTen()); strcat(qh, " va "); strcat(qh, B->LayTen()); strcat(qh, " co quan he "); if (A->LaOngNoi(B)) return strcat(qh, "ong chau noi"); if (A->LaBaNoi(B)) return strcat(qh, "ba chau noi"); if (A->LaOngNgoai(B)) return strcat(qh, "ong chau ngoai"); if (A->LaBaNgoai(B)) return strcat(qh, "ba chau ngoai"); if (A->LaCha(B)) return strcat(qh, "cha con"); if (A->LaMe(B)) return strcat(qh, "me con"); if (A->LaCo(B)) return strcat(qh, "co chau"); if (A->LaDi(B)) return strcat(qh, "di chau"); if (A->LaChu(B)) return strcat(qh, "chu chau"); if (A->LaBac(B)) return strcat(qh, "bac chau"); if (A->LaAnh(B)) return strcat(qh, "anh em"); if (A->LaChi(B)) return strcat(qh, "chi em"); if (A->LaAnhHo(B)) return strcat(qh, "anh em ho"); if (A->LaChiHo(B)) return strcat(qh, "chi em ho"); if (A->LaVo(B)) return strcat(qh, "vo chong"); Nguoi* temp = A; A = B; B = temp; } strcpy(qh, A->LayTen()); strcat(qh, " va "); strcat(qh, B->LayTen()); return strcat(qh, " khong co quan he gia dinh"); } // thñ tôc nhËp d÷ liÖu tõ tÖp ®Ó t¹o c©y gia ®×nh void NhapDuLieu() { clrscr(); char s[80]; cout << "Ten tep nhap du lieu: "; cin >> s; ifstream input(s, ios::in|ios::nocreate); input.seekg(0L, ios::end ); if ( input.tellg() < 0) { cout << "Loi mo tep ! \n"; getch(); return; } input.seekg(0L, ios::beg); cout << "Dang nhap du lieu........\n"; int dong = 1; while (1) { input.getline(s, sizeof(s)); if (input.gcount()==0) break; if (strcmp(s, "")==0) { dong++; continue; } if (strcmp(s, "Tao")==0) { char ten[25]; input.getline(ten, sizeof(ten)); if (strcmp(ten, "")) { int gt; input >> gt; cout << "Tao nguoi ten " << ten << endl; if (Nguoi::TimNguoi(ten)) cout << "Da co nguoi ten la " << ten << endl; else Nguoi::TaoNguoi(ten, gt); dong += 2; continue; } } if (strcmp(s, "Cuoi")==0) { char ten1[25]; input.getline(ten1, sizeof(ten1)); char ten2[25]; input.getline(ten2, sizeof(ten2)); Nguoi* A = Nguoi::TimNguoi(ten1); Nguoi* B = Nguoi::TimNguoi(ten2); cout << "Cuoi " << ten1 << " va "<< ten2 << endl; if (A==0) cout << "Khong co nguoi ten " << ten1 << endl; if (B==0) cout << "Khong co nguoi ten " << ten2 << endl; if (A&&B&&A->Cuoi(B)==0) cout << "Khong cuoi duoc\n"; dong += 2; continue; } if (strcmp(s, "Sinh")==0) { char ten1[25]; input.getline(ten1, sizeof(ten1)); char ten2[25]; input.getline(ten2, sizeof(ten2)); if (strcmp(ten2, "")) { cout << ten1 << " sinh con "<< ten2 << endl; Nguoi* A = Nguoi::TimNguoi(ten1); if (A==0) cout << "Khong co nguoi ten " << ten1 << endl; int gt; input >> gt; if (Nguoi::TimNguoi(ten2)) cout << "Da co nguoi ten la " << ten2 << endl; else { if ( A ) if ( A->GioiTinh() ) cout << ten1 << " la nam khong sinh con duoc\n"; else ((Nu*)A)->SinhCon(ten2, gt); } dong += 3; continue; } } cout << "Loi o dong thu " << dong << endl; break; } cout << "Ket thuc nhap.\n"; getch(); } void TimQuanHe() { clrscr(); char ten1[25]; cout << "Ten nguoi thu nhat: "; gets(ten1); Nguoi *A = Nguoi::TimNguoi(ten1); if (A==0) { cout << "Khong co nguoi ten " << ten1 << endl; getch(); return; } char ten2[25]; cout << "Ten nguoi thu hai: "; gets(ten2); Nguoi *B = Nguoi::TimNguoi(ten2); if (B==0) { cout << "Khong co nguoi ten " << ten2 << endl; getch(); return; } cout << QuanHe(A, B) << endl; getch(); } void Menu() { clrscr(); cout << "\n\n Lua chon cong viec theo so\n\n"; cout << " 1. Nhap du lieu\n"; cout << " 2. Tim quan he\n"; cout << " 3. Ket thuc\n"; } void main() { int i; Menu(); do { i = getch(); switch (i) { case '1': Nguoi::XoaDuLieu(); NhapDuLieu(); Menu(); break; case '2': TimQuanHe(); Menu(); } }while (i!='3'); Nguoi::XoaDuLieu(); }
File đính kèm:
- Lập trình hướng đối tượng với C++ - Phụ lục 4 Mã chương trình bài toán quan hệ gia đình.DOC