├── BTH1 ├── 01.cpp ├── 02.cpp ├── 03.cpp ├── 04.cpp ├── 05.cpp └── Readme.txt ├── BTH2 ├── 01.cpp ├── 02.cpp └── Readme.txt ├── BTH3 ├── 01.cpp ├── 02.cpp ├── 03.cpp └── Readme.txt ├── BTH4 ├── 01.cpp ├── 02.cpp ├── 03.cpp ├── 04.cpp ├── 05.cpp └── Readme.txt ├── BTH6 ├── 01.cpp ├── 02.cpp ├── 03.cpp ├── 04.cpp ├── 05.cpp ├── 06.cpp ├── 07.cpp ├── 08.cpp ├── 09.cpp ├── 10.cpp ├── 11.cpp └── Readme.txt ├── BTH7 ├── 06 │ ├── 06.cpp │ ├── Cow.wav │ ├── Goat.wav │ ├── README.txt │ └── Sheep.wav └── Readme.txt ├── README.md ├── Thi thực hành cuối kỳ ├── 20521008.cpp └── Readme.txt └── Đề cương môn học.pdf /BTH1/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Fraction { 5 | int numerator, denominator; 6 | 7 | public: 8 | int getNumerator() { 9 | return this->numerator; 10 | } 11 | int getDenominator() { 12 | return this->denominator; 13 | } 14 | 15 | void simplify(); 16 | 17 | void input(); 18 | void output(); 19 | }; 20 | 21 | int main() 22 | { 23 | Fraction a; 24 | a.input(); 25 | 26 | cout << "Ket qua rut gon la "; 27 | a.output(); 28 | 29 | system("pause"); 30 | } 31 | 32 | void Fraction::input() { 33 | cout << "Tu so la: "; 34 | cin >> numerator; 35 | cout << "Mau so la: "; 36 | cin >> denominator; 37 | while (denominator == 0) { 38 | cout << " Mau so phai khac 0. Mau so moi la: "; 39 | cin >> denominator; 40 | } 41 | cout << endl; 42 | } 43 | void Fraction::output() { 44 | simplify(); 45 | if (numerator % denominator == 0) 46 | cout << numerator / denominator; 47 | else cout << numerator << "/" << denominator; 48 | cout << endl; 49 | } 50 | 51 | void Fraction::simplify() { 52 | for (int i = abs(numerator); i >= 1; i--) 53 | if (numerator % i == 0 && denominator % i == 0) { 54 | numerator /= i; 55 | denominator /= i; 56 | if ((numerator < 0 && denominator < 0) || (numerator > 0 && denominator < 0)) { 57 | numerator *= -1; 58 | denominator *= -1; 59 | } 60 | break; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /BTH1/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Fraction { 5 | int numerator, denominator; 6 | 7 | public: 8 | int getNumerator() { 9 | return this->numerator; 10 | } 11 | int getDenominator() { 12 | return this->denominator; 13 | } 14 | 15 | void input(); 16 | void output(); 17 | 18 | Fraction operator-(Fraction); 19 | }; 20 | 21 | void compare(Fraction, Fraction); 22 | 23 | void main() 24 | { 25 | Fraction a, b; 26 | 27 | cout << "\nPhan so thu nhat: " << endl; 28 | a.input(); 29 | 30 | cout << "\nPhan so thu hai: " << endl; 31 | b.input(); 32 | 33 | cout << endl; 34 | compare(a, b); 35 | cout << endl; 36 | 37 | system("pause"); 38 | } 39 | 40 | void Fraction::input() { 41 | cout << " Tu so la: "; 42 | cin >> numerator; 43 | cout << " Mau so la: "; 44 | cin >> denominator; 45 | while (denominator == 0) { 46 | cout << "Mau so phai khac 0. Mau so moi la: "; 47 | cin >> denominator; 48 | } 49 | } 50 | void Fraction::output() { 51 | if (numerator % denominator == 0) 52 | cout << numerator / denominator; 53 | else cout << numerator << "/" << denominator << " "; 54 | } 55 | Fraction Fraction::operator-(Fraction x) { 56 | Fraction result; 57 | result.numerator = numerator * x.denominator - x.numerator * denominator; 58 | result.denominator = x.denominator * denominator; 59 | return result; 60 | } 61 | 62 | void compare(Fraction x, Fraction y) { 63 | Fraction subtractResult = x - y; 64 | int temp = subtractResult.getNumerator() * subtractResult.getDenominator(); 65 | 66 | if (temp > 0) { 67 | cout << "Phan so lon nhat la "; 68 | x.output(); 69 | } 70 | else if (temp < 0) { 71 | cout << "Phan so lon nhat la "; 72 | y.output(); 73 | } 74 | else cout << "Hai phan so bang nhau"; 75 | 76 | cout << endl; 77 | } 78 | -------------------------------------------------------------------------------- /BTH1/03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Fraction { 5 | int numerator, denominator; 6 | 7 | public: 8 | Fraction() {} 9 | 10 | int getNumerator() { 11 | return this->numerator; 12 | } 13 | int getDenominator() { 14 | return this->denominator; 15 | } 16 | 17 | void input(); 18 | void output(); 19 | 20 | void simplify(); 21 | 22 | Fraction operator+(Fraction); 23 | Fraction operator-(Fraction); 24 | Fraction operator*(Fraction); 25 | Fraction operator/(Fraction); 26 | }; 27 | 28 | void getTotalOf(Fraction x, Fraction y); 29 | void getSubtractionOf(Fraction x, Fraction y); 30 | void getMultiplicationOf(Fraction x, Fraction y); 31 | void getDivisionOf(Fraction x, Fraction y); 32 | 33 | int main() 34 | { 35 | Fraction a, b; 36 | cout << "Nhap phan so thu nhat:" << endl; 37 | a.input(); 38 | cout << "Nhap phan so thu hai:" << endl; 39 | b.input(); 40 | 41 | system("cls"); 42 | 43 | cout << "Tong cua hai phan so la "; 44 | getTotalOf(a, b); 45 | cout << "Hieu cua hai phan so la "; 46 | getSubtractionOf(a, b); 47 | cout << "Tich cua hai phan so la "; 48 | getMultiplicationOf(a, b); 49 | cout << "Thuong cua hai phan so la "; 50 | getDivisionOf(a, b); 51 | 52 | system("pause"); 53 | } 54 | 55 | void Fraction::input() { 56 | cout << " Tu so la: "; 57 | cin >> numerator; 58 | cout << " Mau so la: "; 59 | cin >> denominator; 60 | while (denominator == 0) { 61 | cout << "Mau so phai khac 0. Mau so moi la: "; 62 | cin >> denominator; 63 | } 64 | } 65 | void Fraction::output() { 66 | if (numerator % denominator == 0) 67 | cout << numerator / denominator; 68 | else cout << numerator << "/" << denominator << " "; 69 | cout << endl; 70 | } 71 | void Fraction::simplify() { 72 | for (int i = abs(numerator); i >= 1; i--) 73 | if (numerator % i == 0 && denominator % i == 0) { 74 | numerator /= i; 75 | denominator /= i; 76 | if ((numerator < 0 && denominator < 0) || (numerator > 0 && denominator < 0)) { 77 | numerator *= -1; 78 | denominator *= -1; 79 | } 80 | break; 81 | } 82 | } 83 | Fraction Fraction::operator+(Fraction x) { 84 | Fraction temp; 85 | temp.numerator = numerator * x.denominator + x.numerator * denominator; 86 | temp.denominator = x.denominator * denominator; 87 | temp.simplify(); 88 | return temp; 89 | } 90 | Fraction Fraction::operator-(Fraction x) { 91 | Fraction temp; 92 | temp.numerator = numerator * x.denominator - x.numerator * denominator; 93 | temp.denominator = x.denominator * denominator; 94 | temp.simplify(); 95 | return temp; 96 | } 97 | Fraction Fraction::operator*(Fraction x) { 98 | Fraction temp; 99 | temp.numerator = numerator * x.numerator; 100 | temp.denominator = x.denominator * denominator; 101 | temp.simplify(); 102 | return temp; 103 | } 104 | Fraction Fraction::operator/(Fraction x) { 105 | Fraction temp; 106 | temp.numerator = numerator * x.denominator; 107 | temp.denominator = denominator * x.numerator; 108 | temp.simplify(); 109 | return temp; 110 | } 111 | 112 | void getTotalOf(Fraction x, Fraction y) { 113 | Fraction temp; 114 | temp = x + y; 115 | temp.output(); 116 | } 117 | void getSubtractionOf(Fraction x, Fraction y) { 118 | Fraction temp; 119 | temp = x - y; 120 | temp.output(); 121 | } 122 | void getMultiplicationOf(Fraction x, Fraction y) { 123 | Fraction temp; 124 | temp = x * y; 125 | temp.output(); 126 | } 127 | void getDivisionOf(Fraction x, Fraction y) { 128 | if (y.getNumerator() == 0) 129 | cout << "khong ton tai" << endl; 130 | else { 131 | Fraction temp; 132 | temp = x / y; 133 | temp.output(); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /BTH1/04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Date { 5 | int day, month, year; 6 | 7 | public: 8 | Date() {} 9 | Date(int day, int month, int year) { 10 | this->day = day; 11 | this->month = month; 12 | this->year = year; 13 | } 14 | 15 | void input(); 16 | void output(); 17 | 18 | bool isLeapYear(); 19 | bool isValidDate(); 20 | 21 | Date getNextDate(); 22 | }; 23 | 24 | int main(){ 25 | Date date; 26 | cout << "Nhap ngay thang nam: " << endl; 27 | date.input(); 28 | 29 | cout << "Ngay tiep theo la "; 30 | date.getNextDate().output(); 31 | 32 | system("pause"); 33 | } 34 | 35 | void Date::input() { 36 | cout << " Nhap ngay: "; 37 | cin >> day; 38 | cout << " Nhap thang: "; 39 | cin >> month; 40 | cout << " Nhap nam: "; 41 | cin >> year; 42 | 43 | if (!isValidDate()) { 44 | system("cls"); 45 | cout << "Ngay thang nam khong hop le, vui long nhap lai" << endl; 46 | input(); 47 | } 48 | } 49 | void Date::output() { 50 | cout << day << "/" << month << "/" << year << endl; 51 | } 52 | 53 | bool Date::isLeapYear(){ 54 | if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 55 | return true; 56 | return false; 57 | } 58 | bool Date::isValidDate() { 59 | if (year < 1 || day < 1) 60 | return false; 61 | if (month < 1 || month > 12) 62 | return false; 63 | if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day > 31)) 64 | return false; 65 | if ((month == 4 || month == 6 || month == 9 || month == 11) && (day > 30)) 66 | return false; 67 | if ((isLeapYear() && month == 2 && day > 29) || (!isLeapYear() && month == 2 && day > 28)) 68 | return false; 69 | return true; 70 | } 71 | 72 | Date Date::getNextDate() { 73 | Date tempDate(++this->day, this->month, this->year); 74 | 75 | if (tempDate.day > 31) { 76 | tempDate.day = 1; 77 | tempDate.month++; 78 | if (tempDate.month > 12) { 79 | tempDate.month = 1; 80 | tempDate.year++; 81 | } 82 | } 83 | else if (tempDate.day > 30) { 84 | if ((tempDate.month == 4 || tempDate.month == 6 || tempDate.month == 9 || tempDate.month == 11)) 85 | { 86 | tempDate.day = 1; 87 | tempDate.month++; 88 | } 89 | } 90 | else if (tempDate.month == 2) { 91 | if (isLeapYear()) { 92 | if (tempDate.day > 29) 93 | { 94 | tempDate.day = 1; 95 | tempDate.month++; 96 | } 97 | } 98 | else { 99 | if (tempDate.day > 28) 100 | { 101 | tempDate.day = 1; 102 | tempDate.month++; 103 | } 104 | } 105 | } 106 | 107 | return tempDate; 108 | } 109 | -------------------------------------------------------------------------------- /BTH1/05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Student { 6 | string name; 7 | double mathGrade, literatureGrade; 8 | 9 | public: 10 | void input(); 11 | double getAverageGrade(); 12 | }; 13 | 14 | int main() 15 | { 16 | Student student; 17 | cout << "Nhap thong tin hoc sinh: " << endl; 18 | student.input(); 19 | 20 | cout << "Diem trung binh: " << student.getAverageGrade() << endl; 21 | system("pause"); 22 | } 23 | 24 | void Student::input() { 25 | cout << " Ho va ten: "; 26 | cin.ignore(); 27 | getline(cin, name); 28 | cout << " Diem toan: "; 29 | cin >> mathGrade; 30 | cout << " Diem van: "; 31 | cin >> literatureGrade; 32 | while (mathGrade < 0 || mathGrade > 10 || literatureGrade > 10 || literatureGrade < 0) 33 | { 34 | system("cls"); 35 | cout << endl << "Diem so khong hop le, hay nhap lai thong tin hoc sinh." << endl; 36 | input(); 37 | } 38 | } 39 | double Student::getAverageGrade() { 40 | return (mathGrade + literatureGrade) / 2; 41 | } 42 | -------------------------------------------------------------------------------- /BTH1/Readme.txt: -------------------------------------------------------------------------------- 1 | 1. Viết chương trình nhập vào một phân số, rút gọn phân số và xuất kết quả. 2 | 2. Viết chương trình nhập vào hai phân số, tìm phân số lớn nhất và xuất kết quả. 3 | 3. Viết chương trình nhập vào hai phân số. Tính tổng, hiệu, tích, thương giữa chúng và xuất kết quả. 4 | 4. Viết chương trình nhập vào một ngày. Tìm ngày kế tiếp và xuất kết quả. 5 | 5. Viết chương trình nhập họ tên, điểm toán, điểm văn của một học sinh. Tính điểm trung bình và xuất kết quả. -------------------------------------------------------------------------------- /BTH2/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class PhanSo { 5 | int numerator, denominator; 6 | public: 7 | int getNumerator(); 8 | void set(int, int); 9 | 10 | void input(); 11 | void output(); 12 | 13 | void simplify(); 14 | 15 | PhanSo operator+(PhanSo); 16 | PhanSo operator-(PhanSo); 17 | PhanSo operator*(PhanSo); 18 | PhanSo operator/(PhanSo); 19 | }; 20 | 21 | void getTotalOf(PhanSo, PhanSo); 22 | void getTubtractionOf(PhanSo, PhanSo); 23 | void getMultiplicationOf(PhanSo, PhanSo); 24 | void getDivisionOf(PhanSo, PhanSo); 25 | 26 | void print(PhanSo&); 27 | 28 | int main() 29 | { 30 | PhanSo a, b; 31 | cout << "Nhap phan so thu nhat" << endl; 32 | a.input(); 33 | cout << "Nhap phan so thu hai" << endl; 34 | b.input(); 35 | 36 | getTotalOf(a, b); 37 | getTubtractionOf(a, b); 38 | getMultiplicationOf(a, b); 39 | getDivisionOf(a, b); 40 | 41 | cout << endl; 42 | system("pause"); 43 | } 44 | 45 | int PhanSo::getNumerator() { 46 | return numerator; 47 | } 48 | void PhanSo::set(int tu, int mau) { 49 | this->numerator = tu; 50 | this->denominator = mau; 51 | } 52 | 53 | void PhanSo::input() { 54 | cout << " Tu so la: "; 55 | cin >> numerator; 56 | cout << " Mau so la: "; 57 | cin >> denominator; 58 | while (denominator == 0) { 59 | cout << "Mau so phai khac 0. \n Mau so moi la: "; 60 | cin >> denominator; 61 | } 62 | system("cls"); 63 | } 64 | void PhanSo::output() { 65 | if (numerator % denominator == 0) 66 | cout << numerator / denominator; 67 | else cout << numerator << "/" << denominator << " "; 68 | } 69 | 70 | void PhanSo::simplify() { 71 | for (int i = numerator; i >= 1; i--) 72 | if (numerator % i == 0 && denominator % i == 0) { 73 | numerator /= i; 74 | denominator /= i; 75 | if ((numerator < 0 && denominator < 0) || (numerator > 0 && denominator < 0)) { 76 | numerator *= -1; 77 | denominator *= -1; 78 | } 79 | break; 80 | } 81 | } 82 | 83 | PhanSo PhanSo::operator+(PhanSo x) { 84 | PhanSo temp; 85 | temp.numerator = numerator * x.denominator + x.numerator * denominator; 86 | temp.denominator = x.denominator * denominator; 87 | temp.simplify(); 88 | return temp; 89 | } 90 | PhanSo PhanSo::operator-(PhanSo x) { 91 | PhanSo temp; 92 | temp.numerator = numerator * x.denominator - x.numerator * denominator; 93 | temp.denominator = x.denominator * denominator; 94 | temp.simplify(); 95 | return temp; 96 | } 97 | PhanSo PhanSo::operator*(PhanSo x) { 98 | PhanSo temp; 99 | temp.numerator = numerator * x.numerator; 100 | temp.denominator = x.denominator * denominator; 101 | temp.simplify(); 102 | return temp; 103 | } 104 | PhanSo PhanSo::operator/(PhanSo x) { 105 | PhanSo temp; 106 | temp.numerator = numerator * x.denominator; 107 | temp.denominator = x.numerator * denominator; 108 | temp.simplify(); 109 | return temp; 110 | } 111 | 112 | void getTotalOf(PhanSo x, PhanSo y) { 113 | cout << "Tong cua hai phan so la "; 114 | PhanSo tot = x + y; 115 | tot.output(); 116 | } 117 | void getTubtractionOf(PhanSo x, PhanSo y) { 118 | cout << "\nHieu cua hai phan so la "; 119 | PhanSo sub = x - y; 120 | sub.output(); 121 | } 122 | void getMultiplicationOf(PhanSo x, PhanSo y) { 123 | cout << "\nTich cua hai phan so la "; 124 | PhanSo mul = x * y; 125 | mul.output(); 126 | } 127 | void getDivisionOf(PhanSo x, PhanSo y) { 128 | cout << "\nThuong cua hai phan so la "; 129 | if (y.getNumerator() == 0) 130 | cout << "khong ton tai" << endl; 131 | else { 132 | PhanSo div = x / y; 133 | div.output(); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /BTH2/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Candidate { 6 | string name, ID; 7 | int birthDay, birthMonth, birthYear; 8 | double mathScore, literatureScore, englishScore; 9 | 10 | public: 11 | void import(); 12 | void print(); 13 | void checkValidScore(double&); 14 | bool isScoreTotalAbove15() { 15 | if (mathScore + literatureScore + englishScore > 15) 16 | return true; 17 | return false; 18 | } 19 | }; 20 | 21 | void listImport(Candidate[], int); 22 | void printMatchedCandidates(Candidate[], int); 23 | 24 | int main() 25 | { 26 | int quantity; 27 | cout << "So thi sinh la "; 28 | cin >> quantity; 29 | system("cls"); 30 | 31 | Candidate* list = new Candidate[quantity]; 32 | listImport(list, quantity); 33 | 34 | printMatchedCandidates(list, quantity); 35 | 36 | system("pause"); 37 | } 38 | 39 | void Candidate::import() { 40 | cin.ignore(); 41 | cout << "Ho va ten thi sinh: "; 42 | getline(cin, name); 43 | cout << " Ma thi sinh: "; 44 | getline(cin, ID); 45 | cout << " Ngay sinh: "; 46 | cin >> birthDay; 47 | cout << " Thang sinh: "; 48 | cin >> birthMonth; 49 | cout << " Nam sinh: "; 50 | cin >> birthYear; 51 | 52 | cout << " Diem toan: "; 53 | cin >> mathScore; 54 | checkValidScore(mathScore); 55 | cout << " Diem van: "; 56 | cin >> literatureScore; 57 | checkValidScore(literatureScore); 58 | cout << " Diem tieng Anh: "; 59 | cin >> englishScore; 60 | checkValidScore(englishScore); 61 | 62 | system("cls"); 63 | } 64 | void Candidate::print() { 65 | cout << " Ho va ten thi sinh: " << name << endl; 66 | cout << " Ngay sinh: " << birthDay << "/" << birthMonth << "/" << birthYear << endl; 67 | cout << " Diem toan: " << mathScore << endl; 68 | cout << " Diem van: " << literatureScore << endl; 69 | cout << " Diem tieng Anh: " << englishScore << endl; 70 | cout << endl; 71 | } 72 | void Candidate::checkValidScore(double& x) { 73 | while (x > 10 || x < 0) { 74 | cout << "Diem so khong hop le. Gia tri diem moi la: "; 75 | cin >> x; 76 | } 77 | } 78 | 79 | void listImport(Candidate arr[], int size) { 80 | for (int i = 0; i < size; i++) { 81 | cout << "Thi sinh thu " << i + 1 << ": " << endl << " "; 82 | arr[i].import(); 83 | } 84 | } 85 | void printMatchedCandidates(Candidate arr[], int size) { 86 | bool hasCandidateAbove15 = false; 87 | 88 | cout << "Cac thi sinh co tong diem lon hon 15: " << endl; 89 | for (int i = 0; i < size; i++) 90 | if (arr[i].isScoreTotalAbove15()) { 91 | hasCandidateAbove15 = true; 92 | cout << " Thi sinh thu " << i + 1 << ": " << endl; 93 | arr[i].print(); 94 | } 95 | 96 | if (hasCandidateAbove15 == false) 97 | cout << "Khong co trong danh sach" << endl; 98 | } 99 | -------------------------------------------------------------------------------- /BTH2/Readme.txt: -------------------------------------------------------------------------------- 1 | 1. Thiết lập lớp PhanSo để biểu diễn khái niệm phân số với hai thành phần dữ liệu tử số, mẫu số và các hàm thành phần cộng, trừ, nhân, chia hai phân số, các hàm thành phần xuất, nhập, định giá trị cho phân số. Viết chương trình cho phép nhập vào hai phân số, in ra kết quả các phép toán cộng, trừ, nhân, chia hai phân số kể trên. 2 | 3 | 2. Xây dựng lớp Candidate (Thí sinh) gồm các thuộc tính: mã, tên, ngày tháng năm sinh, điểm thi Toán, Văn, Anh và các phương thức cần thiết. Theo đó, hãy xây dựng chương trình: 4 | - Nhập vào n thí sinh (n do người dùng nhập). 5 | - In ra thông tin về các thí sinh có tổng điểm lớn hơn 15. 6 | -------------------------------------------------------------------------------- /BTH3/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Point { 5 | double x, y; 6 | 7 | public: 8 | Point() { 9 | x = y = 0; 10 | } 11 | 12 | double getX(); 13 | double getY(); 14 | 15 | void set(double, double); 16 | 17 | void input(); 18 | void output(); 19 | }; 20 | 21 | void printMenu(); 22 | void choose(Point&); 23 | 24 | void printPointX(Point); 25 | void printPointY(Point); 26 | void printPoint(Point); 27 | void changePoint(Point&); 28 | void move_vector(Point&); 29 | 30 | int main() { 31 | Point point; 32 | cout << "Nhap toa do cua diem: "; 33 | point.input(); 34 | 35 | choose(point); 36 | 37 | system("pause"); 38 | } 39 | 40 | double Point::getX() { 41 | return x; 42 | } 43 | double Point::getY() { 44 | return y; 45 | } 46 | void Point::set(double x, double y) { 47 | this->x = x; 48 | this->y = y; 49 | } 50 | void Point::input() { 51 | cout << "\n Hoanh do: "; 52 | cin >> x; 53 | cout << " Tung do: "; 54 | cin >> y; 55 | } 56 | void Point::output() { 57 | cout << "(" << x << "," << y << ")" << endl; 58 | } 59 | 60 | void printPointX(Point point) { 61 | cout << "Hoanh do cua diem la " << point.getX() << endl; 62 | } 63 | void printPointY(Point point) { 64 | cout << "Hoanh do cua diem la " << point.getY() << endl; 65 | } 66 | void printPoint(Point point) { 67 | cout << "Toa do cua diem la "; 68 | point.output(); 69 | } 70 | void changePoint(Point &point) { 71 | int newX, newY; 72 | cout << "Hoang do moi: "; 73 | cin >> newX; 74 | cout << "Tung do moi: "; 75 | cin >> newY; 76 | system("cls"); 77 | 78 | point.set(newX, newY); 79 | 80 | cout << "Thay doi toa do diem thanh cong. Toa do moi la "; 81 | point.output(); 82 | } 83 | void move_vector(Point& point) { 84 | double vectorX, vectorY; 85 | cout << "Hoanh do cua vector tinh tien: "; 86 | cin >> vectorX; 87 | cout << "Tung do cua vector tinh tien: "; 88 | cin >> vectorY; 89 | system("cls"); 90 | 91 | point.set(vectorX + point.getX(), vectorY + point.getY()); 92 | 93 | cout << "Tinh tien diem thanh cong. Toa do moi la "; 94 | point.output(); 95 | } 96 | 97 | void printMenu() { 98 | cout << "_______________________________" << endl; 99 | cout << " 1. Lay hoanh do" << endl; 100 | cout << " 2. Lay tung do" << endl; 101 | cout << " 3. Tinh tien diem" << endl; 102 | cout << " 4. Thay doi toa do diem" << endl; 103 | cout << " 5. Xuat toa do diem" << endl; 104 | cout << " 6. Thoat" << endl; 105 | cout << "_______________________________" << endl; 106 | } 107 | void choose(Point& point) { 108 | printMenu(); 109 | 110 | int choice; 111 | cout << "Lua chon cua ban la: "; 112 | cin >> choice; 113 | system("cls"); 114 | 115 | switch (choice) { 116 | case 1: 117 | printPointX(point); 118 | break; 119 | case 2: 120 | printPointY(point); 121 | break; 122 | case 3: 123 | move_vector(point); 124 | break; 125 | case 4: 126 | changePoint(point); 127 | break; 128 | case 5: 129 | printPoint(point); 130 | break; 131 | case 6: 132 | return; 133 | default: 134 | cout << "Lua chon khong ton tai" << endl; 135 | break; 136 | } 137 | 138 | choose(point); 139 | } 140 | -------------------------------------------------------------------------------- /BTH3/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Point { 5 | double x, y; 6 | public: 7 | void set(double, double); 8 | double getX(); 9 | double getY(); 10 | 11 | void input(); 12 | void output(); 13 | 14 | double getDistanceTo(Point); 15 | 16 | Point operator+(Point); 17 | }; 18 | 19 | class TamGiac { 20 | Point a, b, c; 21 | 22 | public: 23 | void input(); 24 | void output(); 25 | 26 | bool isValidTriangle(); 27 | 28 | void moveFollowVector(Point); 29 | Point getCentroidPoint(); 30 | }; 31 | 32 | void printMenu(); 33 | void choose(TamGiac&); 34 | 35 | void moveTriangle(TamGiac&); 36 | void printCentoroidPointOf(TamGiac); 37 | 38 | int main() { 39 | TamGiac triangle; 40 | triangle.input(); 41 | system("cls"); 42 | choose(triangle); 43 | system("pause"); 44 | } 45 | 46 | void printMenu() { 47 | cout << "_______________________________" << endl; 48 | cout << "0. Thoat" << endl; 49 | cout << "1. Nhap tam giac" << endl; 50 | cout << "2. Xuat tam giac" << endl; 51 | cout << "3. Tinh tien tam giac" << endl; 52 | cout << "4. Lay trong tam tam giac" << endl; 53 | cout << "_______________________________" << endl; 54 | } 55 | void choose(TamGiac& triangle) { 56 | printMenu(); 57 | int x; 58 | cout << "Lua chon cua ban la "; 59 | cin >> x; 60 | system("cls"); 61 | switch (x) { 62 | case 0: 63 | return; 64 | case 1: 65 | triangle.input(); 66 | break; 67 | case 2: 68 | triangle.output(); 69 | break; 70 | case 3: 71 | moveTriangle(triangle); 72 | break; 73 | case 4: 74 | printCentoroidPointOf(triangle); 75 | break; 76 | default: 77 | cout << "Lua chon khong ton tai" << endl; 78 | break; 79 | } 80 | choose(triangle); 81 | } 82 | 83 | double Point::getX() { 84 | return x; 85 | } 86 | double Point::getY() { 87 | return y; 88 | } 89 | void Point::input() { 90 | cout << "\n Hoanh do: "; 91 | cin >> x; 92 | cout << " Tung do: "; 93 | cin >> y; 94 | } 95 | void Point::output() { 96 | cout << "(" << x << "," << y << ")" << endl; 97 | } 98 | void Point::set(double x, double y) { 99 | this->x = x; 100 | this->y = y; 101 | } 102 | double Point::getDistanceTo(Point anotherPoint) { 103 | return sqrt((x - anotherPoint.getX()) * (x - anotherPoint.getX()) + (y - anotherPoint.getY()) * (y - anotherPoint.getY())); 104 | } 105 | Point Point::operator+(Point move) { 106 | Point temp; 107 | temp.x = x + move.x; 108 | temp.y = y + move.y; 109 | return temp; 110 | } 111 | 112 | void TamGiac::input() { 113 | cout << "Nhap toa do ba dinh cua tam giac: " << endl; 114 | 115 | cout << "Dinh thu nhat: "; 116 | a.input(); 117 | cout << "Dinh thu hai: "; 118 | b.input(); 119 | cout << "Dinh thu ba: "; 120 | c.input(); 121 | 122 | if (!isValidTriangle()) { 123 | system("cls"); 124 | cout << "Tam giac nay khong ton tai. Vui long nhap lai toa do ba dinh cua tam giac." << endl; 125 | input(); 126 | } 127 | } 128 | void TamGiac::output() { 129 | cout << "Toa do ba dinh cua tam giac: " << endl; 130 | cout << " Dinh thu nhat: "; 131 | a.output(); 132 | cout << " Dinh thu hai: "; 133 | b.output(); 134 | cout << " Dinh thu ba: "; 135 | c.output(); 136 | } 137 | bool TamGiac::isValidTriangle() { 138 | double edgeAB = a.getDistanceTo(b); 139 | double edgeBC = b.getDistanceTo(c); 140 | double edgeAC = a.getDistanceTo(c); 141 | 142 | if ((edgeAB + edgeBC > edgeAC) && (edgeBC + edgeAC > edgeAB) && (edgeAB + edgeAC > edgeBC)) 143 | return true; 144 | return false; 145 | } 146 | void TamGiac::moveFollowVector(Point vector) { 147 | a = a + vector; 148 | b = b + vector; 149 | c = c + vector; 150 | } 151 | Point TamGiac::getCentroidPoint() { 152 | Point result = a + b + c; 153 | result.set(result.getX() / 3, result.getY() / 3); 154 | return result; 155 | } 156 | 157 | void moveTriangle(TamGiac& triangle) { 158 | Point vectorForMoving; 159 | cout << "Nhap toa do vector tinh tien:"; 160 | vectorForMoving.input(); 161 | triangle.moveFollowVector(vectorForMoving); 162 | system("cls"); 163 | cout << "Da tinh tien tam giac theo vector (" << vectorForMoving.getX() << "," << vectorForMoving.getY() << ")." << endl; 164 | triangle.output(); 165 | } 166 | void printCentoroidPointOf(TamGiac triangle) { 167 | cout << "Toa do trong tam tam giac la "; 168 | triangle.getCentroidPoint().output(); 169 | } 170 | -------------------------------------------------------------------------------- /BTH3/03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Point { 5 | double x, y; 6 | 7 | public: 8 | void set(double, double); 9 | 10 | double getX(); 11 | double getY(); 12 | 13 | void input(); 14 | void output(); 15 | 16 | Point operator+(Point); 17 | }; 18 | 19 | class DaGiac { 20 | int vertexSize; 21 | Point vertices[]; 22 | 23 | public: 24 | void input(); 25 | void ouput(); 26 | 27 | void moveFollowVector(Point); 28 | Point getCentroidPoint(); 29 | }; 30 | 31 | void printMenu(); 32 | void choose(DaGiac&); 33 | 34 | void movePolygon(DaGiac&); 35 | void printCenteroidPointOf(DaGiac); 36 | 37 | int main() { 38 | DaGiac polygon; 39 | polygon.input(); 40 | system("cls"); 41 | choose(polygon); 42 | system("pause"); 43 | } 44 | 45 | void printMenu() { 46 | cout << "_______________________________" << endl; 47 | cout << "0. Thoat" << endl; 48 | cout << "1. Nhap da giac" << endl; 49 | cout << "2. Xuat da giac" << endl; 50 | cout << "3. Tinh tien da giac" << endl; 51 | cout << "4. Lay trong tam da giac" << endl; 52 | cout << "_______________________________" << endl; 53 | } 54 | void choose(DaGiac& polygon) { 55 | printMenu(); 56 | int choice; 57 | cout << "Nhap lua chon: "; 58 | cin >> choice; 59 | system("cls"); 60 | switch (choice) { 61 | case 0: 62 | return; 63 | case 1: 64 | polygon.input(); 65 | break; 66 | case 2: 67 | polygon.ouput(); 68 | break; 69 | case 3: 70 | movePolygon(polygon); 71 | break; 72 | case 4: 73 | printCenteroidPointOf(polygon); 74 | break; 75 | default: 76 | cout << "Lua chon khong hop le" << endl; 77 | break; 78 | } 79 | choose(polygon); 80 | } 81 | 82 | double Point::getX() { 83 | return x; 84 | } 85 | double Point::getY() { 86 | return y; 87 | } 88 | void Point::input() { 89 | cout << "\n Hoanh do: "; 90 | cin >> x; 91 | cout << " Tung do: "; 92 | cin >> y; 93 | } 94 | void Point::output() { 95 | cout << "(" << x << "," << y << ") "; 96 | } 97 | void Point::set(double x, double y) { 98 | this->x = x; 99 | this->y = y; 100 | } 101 | Point Point::operator+(Point move) { 102 | Point temp; 103 | temp.x = x + move.x; 104 | temp.y = y + move.y; 105 | return temp; 106 | } 107 | 108 | void DaGiac::input() { 109 | cout << "So luong dinh cua da giac la "; 110 | cin >> vertexSize; 111 | system("cls"); 112 | cout << "Nhap toa do dinh cua da giac: " << endl; 113 | for (int i = 0; i < vertexSize; i++) { 114 | cout << "Nhap dinh thu " << i + 1 << ": "; 115 | vertices[i].input(); 116 | } 117 | } 118 | void DaGiac::ouput() { 119 | cout << "Toa do cac dinh cua da giac la: "; 120 | for (int i = 0; i < vertexSize; i++) 121 | vertices[i].output(); 122 | cout << endl; 123 | } 124 | void DaGiac::moveFollowVector(Point vector) { 125 | for (int i = 0; i < vertexSize; i++) 126 | vertices[i] = vertices[i] + vector; 127 | } 128 | Point DaGiac::getCentroidPoint() { 129 | Point temp; 130 | temp.set(0, 0); 131 | for (int i = 0; i < vertexSize; i++) 132 | temp = temp + vertices[i]; 133 | temp.set(temp.getX() / vertexSize, temp.getY() / vertexSize); 134 | return temp; 135 | } 136 | 137 | void movePolygon(DaGiac& polygon) { 138 | Point vectorForMoving; 139 | cout << "Nhap toa do vector tinh tien:"; 140 | vectorForMoving.input(); 141 | system("cls"); 142 | polygon.moveFollowVector(vectorForMoving); 143 | cout << "Da tinh tien da giac theo vector (" << vectorForMoving.getX() << "," << vectorForMoving.getY() << ")." << endl; 144 | polygon.ouput(); 145 | } 146 | void printCenteroidPointOf(DaGiac polygon) { 147 | cout << "Toa do trong tam da giac la "; 148 | polygon.getCentroidPoint().output(); 149 | cout << endl; 150 | } 151 | -------------------------------------------------------------------------------- /BTH3/Readme.txt: -------------------------------------------------------------------------------- 1 | 1. Thiết lập lớp biểu diễn khái niệm điểm trong mặt phẳng với hai thành phần dữ liệu hoành độ và tung độ. Viết các phương thức thiết lập, các hàm thành phần cho phép thay đổi nội dung của điểm, lấy hoành độ, tung độ, tịnh tiến, nhập, xuất một điểm. 2 | 3 | 2. Viết định nghĩa lớp TamGiac để biểu diễn khái niệm tam giác trong mặt phẳng với các phương thức thiết lập, huỷ bỏ (nếu có). Các hàm thành phần nhập, xuất, tịnh tiến, lấy trọng tâm tam giác. 4 | 5 | 3. Viết định nghĩa lớp DaGiac để biểu diễn khái niệm đa giác trong mặt phẳng với các hàm thành phần tương tự như lớp TamGiac. 6 | -------------------------------------------------------------------------------- /BTH4/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Time { 5 | int hour; 6 | int minute; 7 | int second; 8 | 9 | public: 10 | Time(int hour = 0, int minute = 0, int second = 0) { 11 | this->hour = hour; 12 | this->minute = minute; 13 | this->second = second; 14 | } 15 | 16 | Time operator+(Time other_time); 17 | 18 | void input(); 19 | void output(); 20 | }; 21 | 22 | int main() { 23 | Time time1, time2; 24 | 25 | cout << "Nhap thoi diem dau tien: " << endl; 26 | time1.input(); 27 | 28 | cout << "Nhap thoi diem thu hai: " << endl; 29 | time2.input(); 30 | 31 | Time time_total = time1 + time2; 32 | 33 | cout << "Thoi diem dau tien la: "; 34 | time1.output(); 35 | 36 | cout << "Thoi diem thu hai la: "; 37 | time2.output(); 38 | 39 | cout << "Tong thoi gian la: "; 40 | time_total.output(); 41 | 42 | return 0; 43 | } 44 | 45 | Time Time::operator+(Time otherTime) { 46 | int totalSeconds = second + otherTime.second; 47 | int carryMinutes = totalSeconds / 60; 48 | totalSeconds %= 60; 49 | 50 | int totalMinutes = minute + otherTime.minute + carryMinutes; 51 | int carryHours = totalMinutes / 60; 52 | totalMinutes %= 60; 53 | 54 | int totalHours = hour + otherTime.hour + carryHours; 55 | 56 | return Time(totalHours, totalMinutes, totalSeconds); 57 | } 58 | 59 | void Time::input() { 60 | cout << "Nhap gio: "; 61 | cin >> hour; 62 | while (hour < 0) { 63 | cout << "Gio phai la so nguyen duong." << endl; 64 | cout << "Nhap lai gio: "; 65 | cin >> hour; 66 | } 67 | 68 | cout << "Nhap phut: "; 69 | cin >> minute; 70 | while (minute < 0 || minute >= 60) { 71 | cout << "Phut phai nam trong khoang (0;60)." << endl; 72 | cout << "Nhap lai phut: "; 73 | cin >> minute; 74 | } 75 | 76 | cout << "Nhap giay: "; 77 | cin >> second; 78 | while (second < 0 || second >= 60) { 79 | cout << "Giay phai nam trong khoang (0;60)." << endl; 80 | cout << "Nhap lai giay: "; 81 | cin >> second; 82 | } 83 | } 84 | void Time::output() { 85 | if (minute == 0 && second == 0) 86 | cout << hour << " gio " << endl; 87 | else if (second == 0) 88 | cout << hour << " gio " << minute << " phut " << endl; 89 | else 90 | cout << hour << " gio " << minute << " phut " << second << " giay " << endl; 91 | } 92 | -------------------------------------------------------------------------------- /BTH4/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Stack { 5 | int maxSize, currentSize; 6 | int* arr = new int[maxSize]; 7 | public: 8 | Stack() { 9 | currentSize = -1; 10 | } 11 | void importSize(); 12 | void output(); 13 | void deleteStack(); 14 | 15 | bool isEmpty(); 16 | bool isFull(); 17 | 18 | void push(int); 19 | void pop(); 20 | 21 | int getValueAtTop(); 22 | }; 23 | void addElementTo(Stack&); 24 | void showMenu(); 25 | void handleTasksFor(Stack&); 26 | 27 | int main() { 28 | Stack Stack; 29 | Stack.importSize(); 30 | showMenu(); 31 | handleTasksFor(Stack); 32 | } 33 | 34 | void addElementTo(Stack& stack) { 35 | int element; 36 | cout << "Phan tu muon them la: "; 37 | cin >> element; 38 | system("cls"); 39 | stack.push(element); 40 | } 41 | void showMenu() { 42 | cout << "_____________________________________________" << endl; 43 | cout << "1. Push (them phan tu tren dau ngan xep)" << endl; 44 | cout << "2. Pop (lay ra phan tu tren dau ngan xep" << endl; 45 | cout << "3. In ra phan tu tren dau ngan xep" << endl; 46 | cout << "4. In ra ngan xep hien tai" << endl; 47 | cout << "5. Xoa ngan xep da tao" << endl; 48 | cout << "_____________________________________________" << endl; 49 | } 50 | void handleTasksFor(Stack& stack) { 51 | int choice; 52 | cout << "Lua chon cua ban la: "; 53 | cin >> choice; 54 | system("cls"); 55 | switch (choice) { 56 | case 1: 57 | addElementTo(stack); 58 | break; 59 | case 2: 60 | stack.pop(); 61 | break; 62 | case 3: 63 | cout << "Phan tu tren dau ngan xep la " << stack.getValueAtTop() << endl; 64 | break; 65 | case 4: 66 | stack.output(); 67 | break; 68 | case 5: 69 | stack.deleteStack(); 70 | break; 71 | default: 72 | cout << "Lua chon khong hop le, hay nhap lai." << endl; 73 | break; 74 | } 75 | showMenu(); 76 | handleTasksFor(stack); 77 | } 78 | 79 | void Stack::importSize() { 80 | cout << "Cho trong toi da cua ngan xep la "; 81 | cin >> maxSize; 82 | } 83 | void Stack::output() { 84 | if (!isEmpty()) { 85 | cout << "Ngan xep hien tai la: "; 86 | for (int i = currentSize; i >= 0; i--) 87 | cout << arr[i] << " "; 88 | } 89 | else cout << "Ngan xep rong"; 90 | cout << endl; 91 | } 92 | bool Stack::isEmpty() { 93 | if (currentSize == -1) 94 | return true; 95 | return false; 96 | } 97 | bool Stack::isFull() { 98 | if (currentSize == maxSize - 1) 99 | return true; 100 | return false; 101 | } 102 | void Stack::push(int element) { 103 | if (!isFull()) { 104 | currentSize++; 105 | arr[currentSize] = element; 106 | cout << "Da them phan tu " << element << " vao ngan xep" << endl; 107 | } 108 | else cout << "Ngan xep da day" << endl; 109 | } 110 | void Stack::pop() { 111 | if (!isEmpty()) { 112 | 113 | cout << "Da lay phan tu " << arr[currentSize] << " khoi ngan xep" << endl; 114 | currentSize--; 115 | } 116 | else cout << "Ngan xep rong" << endl; 117 | } 118 | int Stack::getValueAtTop() { 119 | if (!isEmpty()) 120 | return arr[currentSize]; 121 | else { 122 | cout << "Ngan xep rong" << endl; 123 | return -1; 124 | } 125 | } 126 | void Stack::deleteStack() { 127 | currentSize = -1; 128 | cout << "Da xoa ngan xep." << endl; 129 | } 130 | -------------------------------------------------------------------------------- /BTH4/03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Stack { 6 | int currentSize = -1; 7 | int* arr = new int[1000]; 8 | 9 | public: 10 | void output(int x); 11 | void push(int); 12 | }; 13 | 14 | bool isPrime(int); 15 | void addToStack(int, int, Stack&); 16 | 17 | int main() { 18 | int number; 19 | Stack stack; 20 | cout << "So can phan tich la "; 21 | cin >> number; 22 | 23 | for (int i = 2; i < number - 1; i++) { 24 | if (number % i == 0 && isPrime(i)) 25 | addToStack(number, i, stack); 26 | } 27 | system("cls"); 28 | 29 | stack.output(number); 30 | system("pause"); 31 | } 32 | 33 | bool isPrime(int x) { 34 | if (x < 2) 35 | return false; 36 | for (int i = 2; i < x - 1; i++) 37 | if (x % i == 0) 38 | return false; 39 | return true; 40 | } 41 | 42 | void addToStack(int number, int index, Stack& stack) { 43 | while (number % index == 0) { 44 | stack.push(index); 45 | number /= index; 46 | } 47 | } 48 | 49 | void Stack::output(int x) { 50 | cout << x << " = "; 51 | for (int i = currentSize; i > 0; i--) 52 | cout << arr[i] << " * "; 53 | cout << arr[0] << endl; 54 | } 55 | 56 | void Stack::push(int addElement) { 57 | currentSize++; 58 | arr[currentSize] = addElement; 59 | } 60 | -------------------------------------------------------------------------------- /BTH4/04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Stack { 5 | int size = -1; 6 | int* arr = new int[10000000]; 7 | 8 | public: 9 | void output(int); 10 | void push(int); 11 | }; 12 | void convertToBinaryFrom(int number); 13 | void convertToOctalFrom(int number); 14 | void convertToHexaFrom(int number); 15 | 16 | void showMenu(); 17 | void handleTasksFor(int); 18 | 19 | int main() { 20 | int number; 21 | Stack stack; 22 | 23 | cout << "So can phan tich la "; 24 | cin >> number; 25 | 26 | showMenu(); 27 | handleTasksFor(number); 28 | } 29 | 30 | void showMenu() { 31 | cout << "________________________________" << endl; 32 | cout << "1. Doi sang he nhi phan" << endl; 33 | cout << "2. Doi sang he bat phan" << endl; 34 | cout << "3. Doi sang he thap luc phan" << endl; 35 | cout << "________________________________" << endl; 36 | } 37 | void handleTasksFor(int number) { 38 | int choice; 39 | cout << "Lua chon cua ban la: "; 40 | cin >> choice; 41 | system("cls"); 42 | 43 | switch (choice) { 44 | case 1: 45 | convertToBinaryFrom(number); 46 | break; 47 | case 2: 48 | convertToOctalFrom(number); 49 | break; 50 | case 3: 51 | convertToHexaFrom(number); 52 | break; 53 | default: 54 | cout << "Lua chon khong hop le, hay nhap lai." << endl; 55 | break; 56 | } 57 | 58 | showMenu(); 59 | handleTasksFor(number); 60 | } 61 | 62 | void convertToBinaryFrom(int number) { 63 | if (number == 0) 64 | cout << number << " = " << 0; 65 | else { 66 | Stack binary; 67 | int print = number; 68 | while (number > 0) { 69 | int temp = number % 2; 70 | binary.push(temp); 71 | number /= 2; 72 | } 73 | binary.output(print); 74 | } 75 | cout << " (2)" << endl; 76 | } 77 | void convertToOctalFrom(int number) { 78 | if (number == 0) 79 | cout << number << " = " << 0; 80 | else { 81 | Stack octal; 82 | int print = number; 83 | while (number > 0) { 84 | int temp = number % 8; 85 | octal.push(temp); 86 | number /= 8; 87 | } 88 | octal.output(print); 89 | } 90 | cout << " (8)" << endl; 91 | } 92 | void convertToHexaFrom(int number) { 93 | if (number == 0) 94 | cout << number << " = " << 0; 95 | else { 96 | Stack hexa; 97 | int print = number; 98 | while (number > 0) { 99 | int temp = number % 16; 100 | hexa.push(temp); 101 | number /= 16; 102 | } 103 | hexa.output(print); 104 | } 105 | cout << " (16)" << endl; 106 | } 107 | 108 | void Stack::output(int number) { 109 | cout << number << " = "; 110 | for (int i = size; i >= 0; i--) { 111 | if (arr[i] < 10) 112 | cout << arr[i]; 113 | else cout << char(arr[i] + 55); 114 | } 115 | } 116 | void Stack::push(int newElement) { 117 | size++; 118 | arr[size] = newElement; 119 | } 120 | -------------------------------------------------------------------------------- /BTH4/05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Queue { 5 | int size; 6 | int* arr = new int[size]; 7 | int head, tail; 8 | public: 9 | Queue() { 10 | this->head = -1; 11 | this->tail = -1; 12 | } 13 | int get_max_size() { 14 | return size; 15 | } 16 | void input(); 17 | void output(); 18 | void EnQueue(int); 19 | void DeQueue(); 20 | bool isEmpty(); 21 | bool isFull(); 22 | int getCurrentSize(); 23 | void deleteQueue(); 24 | }; 25 | 26 | void removeElementFrom(Queue&); 27 | void getCurrentSizeOf(Queue&); 28 | void showMenu(); 29 | void handleTaskFor(Queue&); 30 | 31 | int main() { 32 | Queue queue; 33 | queue.input(); 34 | handleTaskFor(queue); 35 | system("pause"); 36 | } 37 | 38 | void addElementTo(Queue& a) { 39 | if (a.isFull()) 40 | cout << "Hang doi da day" << endl; 41 | else { 42 | int x; 43 | cout << "Phan tu muon them la: "; 44 | cin >> x; 45 | system("cls"); 46 | a.EnQueue(x); 47 | cout << "Da them" << endl; 48 | } 49 | } 50 | void removeElementFrom(Queue& a) { 51 | system("cls"); 52 | if (a.isEmpty()) 53 | cout << "Hang doi rong" << endl; 54 | else { 55 | a.DeQueue(); 56 | cout << "Da xoa phan tu khoi hang doi" << endl; 57 | } 58 | } 59 | void getCurrentSizeOf(Queue& queue) { 60 | if (queue.isEmpty()) 61 | cout << "Hang doi rong" << endl; 62 | else cout << "Hang doi hien tai co " << queue.getCurrentSize() << " phan tu" << endl; 63 | } 64 | void showMenu() { 65 | cout << "=======================================" << endl; 66 | cout << "1. EnQueue (them 1 phan tu vao cuoi hang doi)" << endl; 67 | cout << "2. DeQueue (xoa phan tu o dau hang doi)" << endl; 68 | cout << "3. In ra hang doi hien tai" << endl; 69 | cout << "4. In ra so phan tu trong hang doi" << endl; 70 | cout << "5. Xoa hang doi hien tai" << endl; 71 | cout << "=======================================" << endl; 72 | } 73 | void handleTaskFor(Queue& queue) { 74 | showMenu(); 75 | int choice; 76 | cout << "Lua chon cua ban la: "; 77 | cin >> choice; 78 | system("cls"); 79 | switch (choice) { 80 | case 1: 81 | addElementTo(queue); 82 | break; 83 | case 2: 84 | removeElementFrom(queue); 85 | break; 86 | case 3: 87 | queue.output(); 88 | break; 89 | case 4: 90 | getCurrentSizeOf(queue); 91 | break; 92 | case 5: 93 | queue.deleteQueue(); 94 | break; 95 | default: 96 | cout << "Lua chon khong hop le, vui long nhap lai" << endl; 97 | break; 98 | } 99 | handleTaskFor(queue); 100 | } 101 | 102 | void Queue::input() { 103 | cout << "So phan tu toi da cua hang doi: "; 104 | cin >> size; 105 | } 106 | void Queue::output() { 107 | if (isEmpty()) 108 | cout << "Hang doi rong"; 109 | else { 110 | cout << "Hang doi hien tai la: "; 111 | for (int i = head; i <= tail; i++) 112 | cout << arr[i] << " "; 113 | } 114 | cout << endl; 115 | } 116 | void Queue::EnQueue(int add_element) { 117 | if (head == -1) 118 | head++; 119 | tail++; 120 | arr[tail] = add_element; 121 | } 122 | void Queue::DeQueue() { 123 | head++; 124 | } 125 | bool Queue::isEmpty() { 126 | if (getCurrentSize() == 0) 127 | return true; 128 | return false; 129 | } 130 | bool Queue::isFull() { 131 | if (getCurrentSize() == size) 132 | return true; 133 | return false; 134 | } 135 | int Queue::getCurrentSize() { 136 | return (tail - head + 1); 137 | } 138 | void Queue::deleteQueue() { 139 | cout << "Da xoa hang doi hien tai" << endl; 140 | head = tail = -1; 141 | } 142 | -------------------------------------------------------------------------------- /BTH4/Readme.txt: -------------------------------------------------------------------------------- 1 | 1. Viết định nghĩa lớp biểu diễn khái niệm thời gian với các thành phần dữ liệu giờ, phút, giây với các thao tác thích hợp. 2 | 3 | 2. Viết định nghĩa lớp Stack để biểu diễn khái niệm một Stack các số nguyên với thao tác tương ứng. 4 | 5 | 3. Viết chương trình phân tích một số thành thừa số nguyên tố rồi in ra theo thứ tự ngược sử dụng Stack ở câu trên. 6 | Ví dụ: Nhập vào: 750 7 | In ra: 750 = 5 * 5 * 5 * 3 * 2 8 | 9 | 4. Viết chương trình đổi một số sang hệ thập lục phân, hệ bát phân, hệ nhị phân sử dụng Stack ở câu trên. 10 | 11 | 5. Viết định nghĩa lớp Queue để biểu diễn khái niệm hàng đợi các số nguyên với thao tác tương ứng. 12 | -------------------------------------------------------------------------------- /BTH6/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ComplexNumber { 5 | double real, imaginary; 6 | 7 | public: 8 | friend istream& operator>>(istream& is, ComplexNumber& a); 9 | friend ostream& operator<<(ostream& os, ComplexNumber& a); 10 | 11 | ComplexNumber operator+(ComplexNumber a); 12 | ComplexNumber operator-(ComplexNumber a); 13 | ComplexNumber operator*(ComplexNumber a); 14 | ComplexNumber operator/(ComplexNumber a); 15 | 16 | bool operator==(ComplexNumber a); 17 | bool operator!=(ComplexNumber a); 18 | bool operator>(ComplexNumber a); 19 | bool operator>=(ComplexNumber a); 20 | bool operator<(ComplexNumber a); 21 | bool operator<=(ComplexNumber a); 22 | }; 23 | 24 | int main() { 25 | ComplexNumber a, b, c; 26 | bool result; 27 | cout << "So thuc thu nhat: " << endl; 28 | cin >> a; 29 | cout << "So thuc thu hai: " << endl; 30 | cin >> b; 31 | system("cls"); 32 | 33 | cout << "a = " << a << endl; 34 | cout << "b = " << b << endl; 35 | cout << endl; 36 | 37 | c = a + b; 38 | cout << "a + b = " << c << endl; 39 | c = a - b; 40 | cout << "a - b = " << c << endl; 41 | c = a * b; 42 | cout << "a * b = " << c << endl; 43 | c = a / b; 44 | cout << "a : b = " << c << endl; 45 | cout << endl; 46 | 47 | if (a == b) 48 | cout << a << " = " << b << endl; 49 | if (a != b) 50 | cout << a << " != " << b << endl; 51 | if (a >= b) 52 | cout << a << " >= " << b << endl; 53 | if (a <= b) 54 | cout << a << " <= " << b << endl; 55 | if (a > b) 56 | cout << a << " > " << b << endl; 57 | if (a < b) 58 | cout << a << " < " << b << endl; 59 | cout << endl; 60 | 61 | system("pause"); 62 | } 63 | 64 | 65 | istream& operator>>(istream& is, ComplexNumber& a) { 66 | cout << " Phan thuc: "; 67 | is >> a.real; 68 | cout << " Phan ao: "; 69 | is >> a.imaginary; 70 | return is; 71 | } 72 | 73 | ostream& operator<<(ostream& os, ComplexNumber& a) { 74 | if (a.imaginary > 0) 75 | cout << a.real << " + " << a.imaginary << "i"; 76 | else if (a.imaginary == 0) 77 | cout << a.real; 78 | else cout << a.real << " - " << -a.imaginary << "i"; 79 | return os; 80 | } 81 | 82 | ComplexNumber ComplexNumber::operator+(ComplexNumber a) { 83 | ComplexNumber result; 84 | result.real = real + a.real; 85 | result.imaginary = imaginary + a.imaginary; 86 | return result; 87 | } 88 | 89 | ComplexNumber ComplexNumber::operator-(ComplexNumber a) { 90 | ComplexNumber result; 91 | result.real = real - a.real; 92 | result.imaginary = imaginary - a.imaginary; 93 | return result; 94 | } 95 | 96 | ComplexNumber ComplexNumber::operator*(ComplexNumber a) { 97 | ComplexNumber result; 98 | result.real = real * a.real - imaginary * a.imaginary; 99 | result.imaginary = real * a.imaginary + imaginary * a.real; 100 | return result; 101 | } 102 | 103 | ComplexNumber ComplexNumber::operator/(ComplexNumber a) { 104 | ComplexNumber result; 105 | result.real = (real * a.real + imaginary * a.imaginary) / sqrt(a.real * a.real + a.imaginary * a.imaginary); 106 | result.imaginary = (real * a.imaginary - imaginary * a.real) / sqrt(a.real * a.real + a.imaginary * a.imaginary); 107 | return result; 108 | } 109 | 110 | bool ComplexNumber::operator==(ComplexNumber a) { 111 | double module = real * real + imaginary * imaginary; 112 | double moduleA = a.real * a.real + a.imaginary * a.imaginary; 113 | if (module == moduleA) 114 | return true; 115 | return false; 116 | } 117 | 118 | bool ComplexNumber::operator!=(ComplexNumber a) { 119 | double module = real * real + imaginary * imaginary; 120 | double moduleA = a.real * a.real + a.imaginary * a.imaginary; 121 | if (module != moduleA) 122 | return true; 123 | return false; 124 | } 125 | 126 | bool ComplexNumber::operator>(ComplexNumber a) { 127 | double module = real * real + imaginary * imaginary; 128 | double moduleA = a.real * a.real + a.imaginary * a.imaginary; 129 | if (module > moduleA) 130 | return true; 131 | return false; 132 | } 133 | 134 | bool ComplexNumber::operator>=(ComplexNumber a) { 135 | double module = real * real + imaginary * imaginary; 136 | double moduleA = a.real * a.real + a.imaginary * a.imaginary; 137 | if (module >= moduleA) 138 | return true; 139 | return false; 140 | } 141 | 142 | bool ComplexNumber::operator<(ComplexNumber a) { 143 | double module = real * real + imaginary * imaginary; 144 | double moduleA = a.real * a.real + a.imaginary * a.imaginary; 145 | if (module < moduleA) 146 | return true; 147 | return false; 148 | } 149 | 150 | bool ComplexNumber::operator<=(ComplexNumber a) { 151 | double module = real * real + imaginary * imaginary; 152 | double moduleA = a.real * a.real + a.imaginary * a.imaginary; 153 | if (module <= moduleA) 154 | return true; 155 | return false; 156 | } 157 | -------------------------------------------------------------------------------- /BTH6/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class fraction { 5 | private: 6 | int numerator, denominator; 7 | public: 8 | fraction(int newnumerator = 0, int newdenominator = 1); 9 | friend istream& operator>>(istream& is, fraction& a); 10 | friend ostream& operator<<(ostream& os, fraction& a); 11 | 12 | float Value(); 13 | void simplity(); 14 | 15 | friend fraction operator+(fraction a, fraction b); 16 | friend fraction operator-(fraction a, fraction b); 17 | friend fraction operator*(fraction a, fraction b); 18 | friend fraction operator/(fraction a, fraction b); 19 | 20 | friend bool operator==(fraction a, fraction b); 21 | friend bool operator!=(fraction a, fraction b); 22 | friend bool operator>(fraction a, fraction b); 23 | friend bool operator>=(fraction a, fraction b); 24 | friend bool operator<(fraction a, fraction b); 25 | friend bool operator<=(fraction a, fraction b); 26 | }; 27 | 28 | int main(){ 29 | fraction a, b, c; 30 | bool ketqua; 31 | cout << " Nhap PS thu nhat: \n"; 32 | cin >> a; 33 | cout << " Nhap PS thu hai: \n"; 34 | cin >> b; 35 | c = 5 + b - a * 3 + b / 2; 36 | cout << " 5 + " << b << " - " << a << " x 3 + " << b << " : 2 = " << c; 37 | ketqua = 3 > a; 38 | if (ketqua) 39 | cout << "\n (3 > " << a << ") is true" << endl; 40 | else cout << "\n (3 > " << a << ") is false" << endl; 41 | return 0; 42 | } 43 | 44 | fraction::fraction(int newnumerator, int newdenominator){ 45 | numerator = newnumerator; 46 | denominator = newdenominator; 47 | } 48 | istream& operator>>(istream& is, fraction& a){ 49 | cout << " Nhap tu so: "; 50 | is >> a.numerator; 51 | cout << " Nhap mau so: "; 52 | is >> a.denominator; 53 | while (a.denominator == 0){ 54 | cout << " Nhap lai mau so: "; 55 | cin >> a.denominator; 56 | } 57 | return is; 58 | } 59 | ostream& operator<<(ostream& os, fraction& a){ 60 | a.simplity(); 61 | if (a.denominator != 1 && a.numerator != 0) 62 | os << a.numerator << "/" << a.denominator; 63 | else os << a.numerator; 64 | return os; 65 | } 66 | 67 | float fraction::Value(){ 68 | float value; 69 | value = 1.0 * this->numerator / this->denominator; 70 | return value; 71 | } 72 | 73 | void fraction::simplity(){ 74 | int gcd; 75 | int a = this->numerator; 76 | int b = this->denominator; 77 | if (a == 0) return; 78 | while (a != b){ 79 | if (a < b) b = b - a; 80 | else a = a - b; 81 | } 82 | gcd = a; 83 | this->numerator = this->numerator / gcd; 84 | this->denominator = this->denominator / gcd; 85 | } 86 | 87 | fraction operator+(fraction a, fraction b){ 88 | fraction tong; 89 | tong.numerator = a.numerator * b.denominator + a.denominator * b.numerator; 90 | tong.denominator = a.denominator * b.denominator; 91 | return tong; 92 | } 93 | 94 | fraction operator-(fraction a, fraction b){ 95 | fraction hieu; 96 | hieu.numerator = a.numerator * b.denominator - a.denominator * b.numerator; 97 | hieu.denominator = a.denominator * b.denominator; 98 | return hieu;} 99 | 100 | fraction operator*(fraction a, fraction b){ 101 | fraction nhan; 102 | nhan.numerator = a.numerator * b.numerator; 103 | nhan.denominator = a.denominator * b.denominator; 104 | return nhan; 105 | } 106 | 107 | fraction operator/(fraction a, fraction b){ 108 | fraction chia; 109 | chia.numerator = a.numerator * b.denominator; 110 | chia.denominator = a.denominator * b.numerator; 111 | return chia; 112 | } 113 | 114 | bool operator==(fraction a, fraction b){ 115 | if (a.Value() == b.Value()) return true; 116 | return false; 117 | } 118 | 119 | bool operator!=(fraction a, fraction b){ 120 | if (a.Value() != b.Value()) return true; 121 | return false; 122 | } 123 | 124 | bool operator>(fraction a, fraction b){ 125 | if (a.Value() > b.Value()) return true; 126 | return false; 127 | } 128 | 129 | bool operator>=(fraction a, fraction b){ 130 | if (a.Value() >= b.Value()) return true; 131 | return false; 132 | } 133 | 134 | bool operator<(fraction a, fraction b){ 135 | if (a.Value() < b.Value()) return true; 136 | return false; 137 | } 138 | 139 | bool operator<=(fraction a, fraction b){ 140 | if (a.Value() <= b.Value()) return true; 141 | return false; 142 | } -------------------------------------------------------------------------------- /BTH6/03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class CTimeSpan{ 5 | private: 6 | int h, m, s; 7 | public: 8 | CTimeSpan(int newh = 0, int newm = 0, int news = 0); 9 | friend istream& operator>>(istream& is, CTimeSpan& a); 10 | friend ostream& operator<<(ostream& os, CTimeSpan& a); 11 | 12 | int Chuyen_s(); 13 | void Settime(); 14 | CTimeSpan operator+(CTimeSpan b); 15 | CTimeSpan operator-(CTimeSpan b); 16 | bool operator==(CTimeSpan b); 17 | bool operator!=(CTimeSpan b); 18 | bool operator>(CTimeSpan b); 19 | bool operator<(CTimeSpan b); 20 | bool operator<=(CTimeSpan b); 21 | bool operator>=(CTimeSpan b); 22 | }; 23 | 24 | int main(){ 25 | CTimeSpan time1, time2, sum, sub; 26 | bool SoSanh; 27 | cout << "\n Nhap gio thu nhat"; 28 | cin >> time1; 29 | cout << "\n Nhap gio thu hai"; 30 | cin >> time2; 31 | cout << "\n Tong hai gio: "; 32 | sum = time1 + time2; 33 | cout << sum; 34 | cout << "\n Hieu hai gio: "; 35 | sub = time1 - time2; 36 | cout << sub; 37 | 38 | SoSanh = (time1 == time2); 39 | cout << "\n Bang nhau: " << SoSanh; 40 | 41 | SoSanh = (time1 != time2); 42 | cout << "\n Khac nhau: " << SoSanh; 43 | 44 | SoSanh = (time1 > time2); 45 | cout << "\n Gio thu nhat > gio thu hai: " << SoSanh; 46 | 47 | SoSanh = (time1 >= time2); 48 | cout << "\n Gio thu nhat >= gio thu hai: " << SoSanh; 49 | 50 | SoSanh = (time1 < time2); 51 | cout << "\n Gio thu nhat < gio thu hai: " << SoSanh; 52 | 53 | SoSanh = (time1 <= time2); 54 | cout << "\n Gio thu nhat <= gio thu hai: " << SoSanh; 55 | 56 | cout << "\n Gio thu nhat sau khi duoc chuan hoa: "; 57 | cout << time1; 58 | cout << "\n Gio thu hai sau khi duoc chuan hoa: "; 59 | cout << time2; 60 | return 0; 61 | } 62 | 63 | CTimeSpan::CTimeSpan(int newh, int newm, int news){ 64 | h = newh; 65 | m = newm; 66 | s = news; 67 | } 68 | 69 | istream& operator>>(istream& is, CTimeSpan& a){ 70 | cout << "\n Nhap gio: "; 71 | is >> a.h; 72 | cout << " Nhap phut: "; 73 | is >> a.m; 74 | cout << " Nhap giay: "; 75 | is >> a.s; 76 | return is; 77 | } 78 | 79 | ostream& operator<<(ostream& os, CTimeSpan& a){ 80 | a.Settime(); 81 | os << "\n Gio: "; 82 | os << a.h << "h " << a.m << "m " << a.s << "s"; 83 | return os; 84 | } 85 | 86 | 87 | int CTimeSpan::Chuyen_s(){ 88 | int time; 89 | time = h * 3600 + m * 60 + s; 90 | return time; 91 | } 92 | 93 | void CTimeSpan::Settime(){ 94 | int time = Chuyen_s(); 95 | h = abs(time / 3600); 96 | m = abs((time - h * 3600) / 60); 97 | s = abs(time - h * 3600 - m * 60); 98 | } 99 | 100 | 101 | CTimeSpan CTimeSpan::operator+(CTimeSpan b){ 102 | CTimeSpan tong; 103 | tong.h = h + b.h; 104 | tong.m = m + b.m; 105 | tong.s = s + b.s; 106 | tong.Settime(); 107 | return tong; 108 | } 109 | 110 | CTimeSpan CTimeSpan::operator-(CTimeSpan b){ 111 | CTimeSpan hieu; 112 | hieu.h = h - b.h; 113 | hieu.m = m - b.m; 114 | hieu.s = s - b.s; 115 | hieu.Settime(); 116 | return hieu; 117 | } 118 | 119 | bool CTimeSpan::operator==(CTimeSpan b){ 120 | if (Chuyen_s() == b.Chuyen_s()) return true; 121 | return false; 122 | } 123 | 124 | bool CTimeSpan::operator!=(CTimeSpan b){ 125 | if (Chuyen_s() != b.Chuyen_s()) return true; 126 | return false; 127 | } 128 | 129 | bool CTimeSpan::operator>(CTimeSpan b){ 130 | if (Chuyen_s() > b.Chuyen_s()) return true; 131 | return false; 132 | } 133 | 134 | bool CTimeSpan::operator<(CTimeSpan b){ 135 | if (Chuyen_s() < b.Chuyen_s()) 136 | return true; 137 | return false; 138 | } 139 | 140 | bool CTimeSpan::operator<=(CTimeSpan b){ 141 | if (Chuyen_s() <= b.Chuyen_s()) return true; 142 | return false; 143 | } 144 | 145 | bool CTimeSpan::operator>=(CTimeSpan b){ 146 | if (Chuyen_s() >= b.Chuyen_s()) return true; 147 | return false; 148 | } -------------------------------------------------------------------------------- /BTH6/04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class CTime { 5 | private: 6 | int Hour, Min, Sec; 7 | public: 8 | CTime(int newh = 0, int newm = 0, int news = 0); 9 | friend istream& operator>>(istream& is, CTime& a); 10 | friend ostream& operator<<(ostream& os, CTime& a); 11 | 12 | int Chuyen_s(); 13 | void Settime(); 14 | CTime operator+(int news); 15 | CTime operator-(int news); 16 | CTime& operator++(); //++a 17 | CTime operator++(int); //a++ 18 | CTime& operator--(); 19 | CTime operator--(int); 20 | CTime operator-(CTime& b); 21 | }; 22 | 23 | int main(){ 24 | CTime a, b, c; 25 | cout << " Thoi diem thu nhat."; 26 | cin >> a; 27 | cout << "\n Thoi diem thu hai."; 28 | cin >> b; 29 | 30 | cout << a << " - " << b << " = "; 31 | c = a - b; 32 | cout << c << "\n"; 33 | 34 | cout << a << " + 5s = "; 35 | a = a + 5; 36 | cout << a << "\n"; 37 | 38 | cout << b << " - 15s = "; 39 | b = b - 15; 40 | cout << b << "\n"; 41 | 42 | ++a; 43 | cout << a << "\n"; 44 | b--; 45 | cout << b; 46 | } 47 | 48 | CTime::CTime(int newh, int newm, int news){ 49 | Hour = newh; 50 | Min = newm; 51 | Sec = news; 52 | } 53 | 54 | istream& operator>>(istream& is, CTime& a){ 55 | cout << "\n Hay nhap vao mot thoi diem trong ngay."; 56 | cout << "\n Nhap gio: "; 57 | is >> a.Hour; 58 | cout << " Nhap phut: "; 59 | is >> a.Min; 60 | cout << " Nhap giay: "; 61 | is >> a.Sec; 62 | a.Settime(); 63 | return is; 64 | } 65 | 66 | ostream& operator<<(ostream& os, CTime& a){ 67 | os << a.Hour << "h " << a.Min << "m " << a.Sec << "s "; 68 | return os; 69 | } 70 | 71 | int CTime::Chuyen_s(){ 72 | int time; 73 | time = Hour * 3600 + Min * 60 + Sec; 74 | return time; 75 | } 76 | 77 | void CTime::Settime(){ 78 | int time = Chuyen_s(); 79 | while (time < 0) 80 | time += 24 * 3600; 81 | while (time > 24 * 3600) 82 | time -= 24 * 3600; 83 | Hour = time / 3600; 84 | time = time - Hour * 3600; 85 | Min = time / 60; 86 | Sec = time - Min * 60; 87 | } 88 | 89 | CTime CTime::operator+(int news){ 90 | CTime tong(Hour, Min, Sec); 91 | tong.Sec += news; 92 | if (tong.Sec > 60 || tong.Sec < 0) tong.Settime(); 93 | return tong; 94 | } 95 | CTime CTime::operator-(int news){ 96 | CTime hieu(Hour, Min, Sec); 97 | hieu.Sec -= news; 98 | if (hieu.Sec > 60 || hieu.Sec < 0) hieu.Settime(); 99 | return hieu; 100 | } 101 | 102 | CTime& CTime::operator++(){ 103 | Sec++; 104 | if (Sec > 60 || Sec < 0) Settime(); 105 | return *this; 106 | } 107 | 108 | CTime CTime::operator++(int){ 109 | CTime t = *this; 110 | Sec++; 111 | if (Sec > 60 || Sec < 0) Settime(); 112 | return t; 113 | } 114 | 115 | CTime& CTime::operator--(){ 116 | Sec--; 117 | if (Sec > 60 || Sec < 0) Settime(); 118 | return *this; 119 | } 120 | 121 | CTime CTime::operator--(int){ 122 | CTime t = *this; 123 | Sec--; 124 | if (Sec > 60 || Sec < 0) Settime(); 125 | return t; 126 | } 127 | 128 | CTime CTime::operator-(CTime& b){ 129 | CTime hieu; 130 | hieu.Hour = Hour - b.Hour; 131 | hieu.Min = Min - b.Min; 132 | hieu.Sec = Sec - b.Sec; 133 | hieu.Settime(); 134 | return hieu; 135 | } -------------------------------------------------------------------------------- /BTH6/05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class CDate{ 5 | private: 6 | int Day, Month, Year; 7 | bool LeapYear = false; 8 | int months[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; 9 | public: 10 | CDate(int newday = 1, int newmonth = 1, int newyear = 1); 11 | friend istream& operator>>(istream& is, CDate& date); 12 | friend ostream& operator<<(ostream& os, CDate date); 13 | void Check(); 14 | CDate operator+(int newday); 15 | CDate operator-(int newday); 16 | CDate& operator++(); 17 | CDate operator++(int); 18 | CDate& operator--(); 19 | CDate operator--(int); 20 | 21 | int operator-(CDate b); 22 | }; 23 | 24 | int main(){ 25 | CDate a, b; 26 | int day; 27 | cout << "\n Nhap vao thoi diem thu nhat"; 28 | cin >> a; 29 | cout << a; 30 | cout << "\n Nhap vao thoi diem thu hai"; 31 | cin >> b; 32 | cout << b; 33 | cout << "\n " << a << " + 5 = "; 34 | a = a + 5; 35 | cout << a; 36 | cout << "\n " << b << " - 8 = "; 37 | b = b - 8; 38 | cout << b << "\n"; 39 | 40 | a++; 41 | cout << a << "\n"; 42 | --b; 43 | cout << b << "\n"; 44 | 45 | cout << a << " - " << b << " = "; 46 | day = a - b; 47 | cout << day << " ngay."; 48 | return 0; 49 | } 50 | 51 | CDate::CDate(int newday, int newmonth, int newyear){ 52 | Day = newday; 53 | Month = newmonth; 54 | Year = newyear; 55 | 56 | } 57 | 58 | istream& operator>>(istream& is, CDate& date){ 59 | cout << "\n Ngay: "; 60 | is >> date.Day; 61 | cout << " Thang: "; 62 | is >> date.Month; 63 | while (date.Month < 1 || date.Month >12){ 64 | cout << " Mot nam co 12 thang."; 65 | cout << "\n Thang: "; 66 | is >> date.Month; 67 | } 68 | cout << " Nam: "; 69 | is >> date.Year; 70 | while (date.Year < 1){ 71 | cout << " Vui long nhap nam sau cong nguyen, nam > 0."; 72 | cout << "\n Ban da nhap sai, vui long nhap lai."; 73 | cout << "\n Nam: "; 74 | is >> date.Year; 75 | } 76 | if (date.Year % 400 == 0 || (date.Year % 4 == 0 && date.Year % 100 != 0)){ 77 | date.LeapYear = true; 78 | date.months[1] = 29; 79 | } 80 | while (date.Day > date.months[date.Month - 1] || date.Day < 0){ 81 | cout << "\n Thang " << date.Month << " co " << date.months[date.Month - 1] << " ngay."; 82 | cout << "\n Ban da nhap sai, vui long nhap lai."; 83 | cout << "\n Ngay: "; 84 | is >> date.Day; 85 | } 86 | return is; 87 | } 88 | 89 | ostream& operator<<(ostream& os, CDate date){ 90 | os << date.Day << "/" << date.Month << "/" << date.Year; 91 | return os; 92 | } 93 | 94 | void CDate::Check(){ 95 | while (Day > months[Month - 1]){ 96 | Day -= months[Month - 1]; 97 | Month++; 98 | if (Month == 13){ 99 | Year++; 100 | if (Year % 400 == 0 || (Year % 4 == 0 && Year % 100 != 0)){ 101 | this->LeapYear = true; 102 | this->months[1] = 29; 103 | } 104 | else{ 105 | LeapYear = false; 106 | months[1] = 28; 107 | } 108 | Month = 1; 109 | } 110 | } 111 | while (Day < 1){ 112 | Month--; 113 | if (Month == 0){ 114 | Year--; 115 | Month = 12; 116 | if (Year % 400 == 0 || (Year % 4 == 0 && Year % 100 != 0)){ 117 | this->LeapYear = true; 118 | this->months[1] = 29; 119 | } 120 | else{ 121 | LeapYear = false; 122 | months[1] = 28; 123 | } 124 | } 125 | Day += months[Month - 1]; 126 | } 127 | } 128 | 129 | CDate CDate::operator+(int newday){ 130 | CDate tong(Day, Month, Year); 131 | tong.Day += newday; 132 | tong.Check(); 133 | return tong; 134 | } 135 | 136 | CDate CDate::operator-(int newday){ 137 | CDate hieu(Day, Month, Year); 138 | hieu.Day -= newday; 139 | hieu.Check(); 140 | return hieu; 141 | } 142 | 143 | CDate& CDate::operator++(){ 144 | *this = *this + 1; 145 | return *this; 146 | } 147 | 148 | CDate CDate::operator++(int){ 149 | CDate t = *this; 150 | *this = *this + 1; 151 | return t; 152 | } 153 | 154 | CDate& CDate::operator--(){ 155 | *this = *this - 1; 156 | return *this; 157 | } 158 | 159 | CDate CDate::operator--(int){ 160 | CDate t = *this; 161 | *this = *this - 1; 162 | return t; 163 | } 164 | 165 | int CDate::operator-(CDate b){ 166 | //tinh so ngay 167 | 168 | int time = 0; 169 | int year = 0; 170 | CDate temp; 171 | 172 | //tinh dau nam toi a 173 | temp.Year = Year; 174 | while (temp.Month != Month){ 175 | time += months[temp.Month - 1]; 176 | temp.Month++; 177 | } 178 | time += Day - temp.Day + 1; 179 | 180 | //tinh b toi cuoi nam 181 | temp = b; 182 | time = time + b.months[b.Month - 1] - temp.Day + 1; 183 | while (temp.Month != 12){ 184 | time += b.months[temp.Month]; 185 | temp.Month++; 186 | } 187 | 188 | if (Year == b.Year){ 189 | if (Month < b.Month) time = time - 2; 190 | if (months[1] == 29) time = 366 - time; 191 | else time = 365 - time; 192 | if (time < 0) time = 0 - time; 193 | return time; 194 | } 195 | 196 | int MaxYear; 197 | if (Year > b.Year){ 198 | year = Year - (b.Year + 1); 199 | MaxYear = Year; 200 | } 201 | else{ 202 | year = b.Year - Year + 1; 203 | MaxYear = b.Year + 1; 204 | time -= 2; 205 | } 206 | 207 | int* arryear = new int[year]; 208 | 209 | for (int i = year - 1; i >= 0; i--) 210 | arryear[i] = MaxYear - year + i; 211 | 212 | int timeyear = 0; 213 | for (int j = 0; j < year; j++){ 214 | if (arryear[j] % 400 == 0 || (arryear[j] % 4 == 0 && arryear[j] % 100 != 0)) 215 | timeyear += 366; 216 | else timeyear += 365; 217 | } 218 | if (Year > b.Year) 219 | time += timeyear; 220 | else time = timeyear - time; 221 | return time; 222 | } -------------------------------------------------------------------------------- /BTH6/06.cpp: -------------------------------------------------------------------------------- 1 | #include ; 2 | #include 3 | using namespace std; 4 | 5 | class CString { 6 | string s; 7 | public: 8 | CString(){ 9 | s = ""; 10 | } 11 | friend istream& operator>>(istream& is, CString& a); 12 | friend ostream& operator<<(ostream& os, CString& a); 13 | void setString(string value); 14 | CString operator+(CString value); 15 | bool operator!=(CString value); 16 | ~CString() {} 17 | }; 18 | 19 | int main() { 20 | CString s1, s2; 21 | cin >> s1 >> s2; 22 | CString total = s1 + s2; 23 | cout << s1 << " + " << s2 << " = " << total << endl; 24 | if (s1 != s2) 25 | cout << s1 << " != " << s2 << endl; 26 | else cout << s1 << " = " << s2 << endl; 27 | } 28 | 29 | istream& operator>>(istream& is, CString& a) { 30 | cout << "Xau nhap vao la "; 31 | is >> a.s; 32 | return is; 33 | } 34 | ostream& operator<<(ostream& os, CString& a) { 35 | os << a.s; 36 | return os; 37 | } 38 | 39 | void CString::setString(string value) { 40 | this->s = value; 41 | } 42 | CString CString::operator+(CString value) { 43 | CString result; 44 | result.s = s + value.s; 45 | return result; 46 | } 47 | bool CString::operator!=(CString value) { 48 | if (s.compare(value.s) != 0) 49 | return true; 50 | return false; 51 | } -------------------------------------------------------------------------------- /BTH6/07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class DaThuc 5 | { 6 | private: 7 | int bac = 0; 8 | int* arr = NULL; 9 | public: 10 | DaThuc(int newbac = 0); 11 | DaThuc(const DaThuc& x); 12 | friend istream& operator>>(istream& is, DaThuc& a); 13 | friend ostream& operator<<(ostream& os, DaThuc a); 14 | 15 | DaThuc operator+(DaThuc& b); 16 | DaThuc operator-(DaThuc b); 17 | DaThuc operator*(DaThuc b); 18 | 19 | int operator[](int i); 20 | 21 | DaThuc& operator++(); 22 | DaThuc operator++(int); 23 | DaThuc& operator--(); 24 | DaThuc operator--(int); 25 | ~DaThuc(); 26 | }; 27 | 28 | int main(){ 29 | DaThuc a, b, cong, tru, nhan; 30 | cout << "\n Nhap da thuc thu nhat F1(x)."; 31 | cin >> a; 32 | cout << " F1(x) = " << a; 33 | cout << "\n Nhap da thuc thu hai F2(x)."; 34 | cin >> b; 35 | cout << " F2(x) = " << b; 36 | 37 | cout << "\n F1(x) + F2(x) = "; 38 | cong = a + b; 39 | cout << cong; 40 | cout << "\n F1(x) - F2(x) = "; 41 | tru = a - b; 42 | cout << tru; 43 | cout << "\n F1(x) * F2(x) = "; 44 | nhan = a * b; 45 | cout << nhan; 46 | cout << "\n He so cua x^1 cua F1(x): " << a[1] << "\n"; 47 | 48 | a++; 49 | cout << a; 50 | 51 | --b; 52 | cout << b; 53 | return 0; 54 | } 55 | DaThuc::DaThuc(int newbac){ 56 | bac = newbac; 57 | arr = new int[bac + 1]; 58 | for (int i = 0; i <= bac; i++){ 59 | arr[i] = 0; 60 | } 61 | } 62 | 63 | DaThuc::DaThuc(const DaThuc& x) 64 | { 65 | bac = x.bac; 66 | arr = new int[bac + 1]; 67 | for (int i = 0; i <= bac; i++){ 68 | arr[i] = x.arr[i]; 69 | } 70 | } 71 | 72 | istream& operator>>(istream& is, DaThuc& a){ 73 | cout << "\n Hay nhap bac cua da thuc: "; 74 | is >> a.bac; 75 | a.arr = new int[a.bac + 1]; 76 | for (int i = 0; i <= a.bac; i++){ 77 | cout << " He so cua x^" << i << " : "; 78 | is >> a.arr[i]; 79 | } 80 | return is; 81 | } 82 | ostream& operator<<(ostream& os, DaThuc a){ 83 | for (int i = a.bac; i > 0; i--){ 84 | os << a.arr[i] << "x^" << i << " + "; 85 | } 86 | os << a.arr[0]; 87 | os << "\n"; 88 | return os; 89 | } 90 | 91 | DaThuc DaThuc::operator+(DaThuc& b){ 92 | DaThuc tong; 93 | if (bac > b.bac){ 94 | tong.bac = bac; 95 | } 96 | else{ 97 | tong.bac = b.bac; 98 | } 99 | delete[]tong.arr; 100 | tong.arr = new int[tong.bac + 1]; 101 | for (int i = 0; i <= tong.bac; i++){ 102 | if (i <= bac && i <= b.bac) { 103 | tong.arr[i] = arr[i] + b.arr[i]; 104 | } 105 | else{ 106 | if (i > bac) tong.arr[i] = b.arr[i]; 107 | else 108 | if (i > b.bac) tong.arr[i] = arr[i]; 109 | } 110 | } 111 | return tong; 112 | } 113 | 114 | DaThuc DaThuc::operator-(DaThuc b){ 115 | //a-b 116 | DaThuc hieu; 117 | if (bac > b.bac){ 118 | hieu.bac = bac; 119 | } 120 | else{ 121 | hieu.bac = b.bac; 122 | } 123 | hieu.arr = new int[hieu.bac + 1]; 124 | for (int i = 0; i <= hieu.bac; i++){ 125 | if (i <= bac && i <= b.bac){ 126 | hieu.arr[i] = arr[i] - b.arr[i]; 127 | } 128 | else{ 129 | if (i > bac) hieu.arr[i] = -b.arr[i]; 130 | else 131 | if (i > b.bac) hieu.arr[i] = arr[i]; 132 | } 133 | } 134 | return hieu; 135 | } 136 | 137 | DaThuc DaThuc::operator*(DaThuc b){ 138 | DaThuc nhan; 139 | nhan.bac = bac + b.bac; 140 | nhan.arr = new int[nhan.bac + 1]; 141 | for (int k = 0; k <= nhan.bac; k++){ 142 | nhan.arr[k] = 0; 143 | } 144 | for (int i = 0; i <= bac; i++) 145 | for (int j = 0; j <= b.bac; j++){ 146 | nhan.arr[i + j] += arr[i] * b.arr[j]; 147 | } 148 | return nhan; 149 | } 150 | 151 | int DaThuc::operator[](int i){ 152 | return arr[i]; 153 | } 154 | 155 | DaThuc& DaThuc::operator++(){ 156 | arr[0] = arr[0] + 1; 157 | return *this; 158 | } 159 | 160 | DaThuc DaThuc::operator++(int){ 161 | DaThuc t(*this); 162 | arr[0]++; 163 | return t; 164 | } 165 | 166 | DaThuc& DaThuc::operator--(){ 167 | arr[0]--; 168 | return *this; 169 | } 170 | 171 | DaThuc DaThuc::operator--(int){ 172 | DaThuc t(*this); 173 | arr[0]--; 174 | return t; 175 | } 176 | 177 | DaThuc::~DaThuc() 178 | {} -------------------------------------------------------------------------------- /BTH6/08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class CVector{ 5 | private: 6 | int n; 7 | int* arr; 8 | public: 9 | friend class CMatrix; 10 | CVector(int newn = 0); 11 | ~CVector(); 12 | //CVector& operator=(CVector vt); 13 | friend istream& operator>>(istream& is, CVector& vt); 14 | friend ostream& operator<<(ostream& os, CVector vt); 15 | }; 16 | 17 | class CMatrix{ 18 | private: 19 | int row, col; 20 | int** arr; 21 | public: 22 | CMatrix(int r = 1, int c = 1); 23 | ~CMatrix(); 24 | //CMatrix& operator=(CMatrix mt); 25 | friend istream& operator>>(istream& is, CMatrix& mt); 26 | friend ostream& operator<<(ostream& os, CMatrix mt); 27 | 28 | CMatrix operator*(CMatrix& b); 29 | CMatrix operator*(CVector& vt); 30 | 31 | }; 32 | 33 | int main(){ 34 | CVector vt1; 35 | cout << " Nhap vao mot vecto X\n"; 36 | cin >> vt1; 37 | cout << " X = " << vt1; 38 | CMatrix mt1, mt2, mt3, tich; 39 | cout << "\n Nhap vao ma tran thu nhat A\n"; 40 | cin >> mt1; 41 | cout << mt1; 42 | cout << "\n Nhap vao ma tran thu hai B\n"; 43 | cin >> mt2; 44 | cout << mt2; 45 | cout << "A * B = \n"; 46 | tich = mt1 * mt2; 47 | cout << tich << "\n"; 48 | 49 | cout << "\n Nhap vao ma tran thu ba C\n"; 50 | cin >> mt3; 51 | cout << mt3 << "\n"; 52 | cout << " C * X = \n"; 53 | tich = mt3 * vt1; 54 | cout << tich; 55 | return 0; 56 | } 57 | CVector::CVector(int newn) :n(newn){ 58 | arr = new int[n]; 59 | for (int i = 0; i < n; i++){ 60 | arr[i] = 0; 61 | } 62 | } 63 | 64 | CVector::~CVector(){ 65 | } 66 | 67 | istream& operator>>(istream& is, CVector& vt){ 68 | cout << " Nhap vao so chieu cua khong gian: "; 69 | is >> vt.n; 70 | delete[]vt.arr; 71 | vt.arr = new int[vt.n]; 72 | for (int i = 0; i < vt.n; i++) 73 | { 74 | cout << "x" << i + 1 << " = "; 75 | is >> vt.arr[i]; 76 | } 77 | return is; 78 | } 79 | ostream& operator<<(ostream& os, CVector vt){ 80 | os << "("; 81 | for (int i = 0; i < vt.n - 1; i++){ 82 | os << vt.arr[i] << ", "; 83 | } 84 | os << vt.arr[vt.n - 1] << ")"; 85 | return os; 86 | } 87 | CMatrix::CMatrix(int r, int c) :row(r), col(c){ 88 | arr = new int* [row]; 89 | int i, j; 90 | for (i = 0; i < row; i++) 91 | arr[i] = new int[col]; 92 | for (i = 0; i < row; i++) 93 | { 94 | for (j = 0; j < col; j++) 95 | { 96 | arr[i][j] = 0; 97 | } 98 | } 99 | } 100 | CMatrix::~CMatrix(){ 101 | } 102 | 103 | istream& operator>>(istream& is, CMatrix& mt){ 104 | for (int i = 0; i < mt.col; i++) 105 | delete[]mt.arr[i]; 106 | delete[]mt.arr; 107 | 108 | cout << " Nhap so hang: "; 109 | is >> mt.row; 110 | cout << " Nhap so cot: "; 111 | is >> mt.col; 112 | 113 | mt.arr = new int* [mt.col]; 114 | int i, j; 115 | for (i = 0; i < mt.row; i++) 116 | mt.arr[i] = new int[mt.col]; 117 | for (i = 0; i < mt.row; i++) 118 | { 119 | for (j = 0; j < mt.col; j++) 120 | { 121 | cout << "a[" << i << "][" << j << "] = "; 122 | is >> mt.arr[i][j]; 123 | } 124 | } 125 | return is; 126 | } 127 | ostream& operator<<(ostream& os, CMatrix mt){ 128 | for (int i = 0; i < mt.row; i++){ 129 | for (int j = 0; j < mt.col; j++){ 130 | os << mt.arr[i][j] << "\t"; 131 | } 132 | cout << "\n"; 133 | } 134 | return os; 135 | } 136 | 137 | CMatrix CMatrix::operator*(CMatrix& b){ 138 | if (col != b.row) return CMatrix(); 139 | CMatrix tich(row, b.col); 140 | for (int i = 0; i < tich.row; i++) 141 | for (int j = 0; j < tich.col; j++) 142 | for (int k = 0; k < col; k++) 143 | tich.arr[i][j] += arr[i][k] * b.arr[k][j]; 144 | return tich; 145 | } 146 | CMatrix CMatrix::operator*(CVector& vt){ 147 | if (col != 1) return CMatrix(); 148 | CMatrix tich(row, vt.n); 149 | for (int i = 0; i < tich.row; i++) 150 | for (int j = 0; j < vt.n; j++){ 151 | tich.arr[i][j] = arr[i][0] * vt.arr[j]; 152 | } 153 | return tich; 154 | } -------------------------------------------------------------------------------- /BTH6/09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class INTERGER { 5 | private: 6 | int x; 7 | public: 8 | INTERGER(int newx = 0); 9 | 10 | ~INTERGER(); 11 | INTERGER& operator++(); 12 | INTERGER operator++(int); 13 | INTERGER& operator--(); 14 | INTERGER operator--(int); 15 | friend istream& operator>>(istream& is, INTERGER& a); 16 | friend ostream& operator<<(ostream& os, INTERGER a); 17 | friend INTERGER operator+(INTERGER a, INTERGER b); 18 | friend INTERGER operator-(INTERGER a, INTERGER b); 19 | friend INTERGER operator*(INTERGER a, INTERGER b); 20 | friend INTERGER operator/(INTERGER a, INTERGER b); 21 | friend INTERGER operator%(INTERGER a, INTERGER b); 22 | }; 23 | 24 | int main(){ 25 | INTERGER a, b; 26 | cout << "a = "; 27 | cin >> a; 28 | cout << "b = "; 29 | cin >> b; 30 | cout << "a + 1 = " << a++ << endl; 31 | cout << "b - 1 = " << --b << endl; 32 | cout << a << " x " << b << " + 9 - " << a << " = " << a * b + 9 - a << "\n"; 33 | cout << a << " / " << b << " = " << a / b << "\n"; 34 | cout << a << " % " << b << " = " << a % b << endl; 35 | return 0; 36 | } 37 | INTERGER::INTERGER(int newx) :x(newx) 38 | { 39 | } 40 | 41 | INTERGER::~INTERGER() 42 | { 43 | } 44 | 45 | INTERGER& INTERGER::operator++() 46 | { 47 | x++; 48 | return *this; 49 | } 50 | 51 | INTERGER INTERGER::operator++(int) 52 | { 53 | INTERGER t = *this; 54 | x++; 55 | return t; 56 | } 57 | 58 | INTERGER& INTERGER::operator--() 59 | { 60 | --x; 61 | return *this; 62 | } 63 | 64 | INTERGER INTERGER::operator--(int) 65 | { 66 | INTERGER t(x); 67 | x--; 68 | return t; 69 | } 70 | 71 | istream& operator>>(istream& is, INTERGER& a) 72 | { 73 | 74 | is >> a.x; 75 | return is; 76 | } 77 | 78 | ostream& operator<<(ostream& os, INTERGER a) 79 | { 80 | os << a.x; 81 | return os; 82 | } 83 | 84 | INTERGER operator+(INTERGER a, INTERGER b) 85 | { 86 | return INTERGER(a.x + b.x); 87 | } 88 | 89 | INTERGER operator-(INTERGER a, INTERGER b) 90 | { 91 | return INTERGER(a.x - b.x); 92 | } 93 | 94 | INTERGER operator*(INTERGER a, INTERGER b) 95 | { 96 | return INTERGER(a.x * b.x); 97 | } 98 | 99 | INTERGER operator/(INTERGER a, INTERGER b) 100 | { 101 | return INTERGER(a.x / b.x); 102 | } 103 | 104 | INTERGER operator%(INTERGER a, INTERGER b) 105 | { 106 | return INTERGER(a.x % b.x); 107 | } -------------------------------------------------------------------------------- /BTH6/10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class MYINT { 5 | private: 6 | int x; 7 | public: 8 | MYINT(int newx = 0); 9 | 10 | ~MYINT(); 11 | MYINT& operator++(); 12 | MYINT operator++(int); 13 | MYINT& operator--(); 14 | MYINT operator--(int); 15 | friend istream& operator>>(istream& is, MYINT& a); 16 | friend ostream& operator<<(ostream& os, MYINT a); 17 | friend MYINT operator+(MYINT a, MYINT b); 18 | friend MYINT operator-(MYINT a, MYINT b); 19 | friend MYINT operator*(MYINT a, MYINT b); 20 | friend MYINT operator/(MYINT a, MYINT b); 21 | friend MYINT operator%(MYINT a, MYINT b); 22 | }; 23 | int main(){ 24 | MYINT a, b; 25 | cout << "a = "; 26 | cin >> a; 27 | cout << "b = "; 28 | cin >> b; 29 | cout << "a + 1 = " << a++ << endl; 30 | cout << "b - 1 = " << --b << endl; 31 | cout << a << " - " << b << " = " << a - b << "\n"; 32 | cout << a << " + " << b << " = " << a + b << "\n"; 33 | cout << a << " x " << b << " = " << a * b << "\n"; 34 | cout << a << " / " << b << " = " << a / b << "\n"; 35 | cout << a << " % " << b << " = " << a % b << endl; 36 | return 0; 37 | } 38 | MYINT::MYINT(int newx) :x(newx) 39 | { 40 | 41 | } 42 | 43 | MYINT::~MYINT() 44 | { 45 | 46 | } 47 | 48 | MYINT& MYINT::operator++() 49 | { 50 | x--; 51 | return *this; 52 | } 53 | 54 | MYINT MYINT::operator++(int) 55 | { 56 | MYINT t = *this; 57 | x--; 58 | return t; 59 | } 60 | 61 | MYINT& MYINT::operator--() 62 | { 63 | ++x; 64 | return *this; 65 | } 66 | 67 | MYINT MYINT::operator--(int) 68 | { 69 | MYINT t(x); 70 | x++; 71 | return t; 72 | } 73 | 74 | istream& operator>>(istream& is, MYINT& a) 75 | { 76 | is >> a.x; 77 | return is; 78 | } 79 | 80 | ostream& operator<<(ostream& os, MYINT a) 81 | { 82 | os << a.x; 83 | return os; 84 | } 85 | 86 | MYINT operator+(MYINT a, MYINT b) 87 | { 88 | return MYINT(a.x - b.x); 89 | } 90 | 91 | MYINT operator-(MYINT a, MYINT b) 92 | { 93 | return MYINT(a.x + b.x); 94 | } 95 | 96 | MYINT operator*(MYINT a, MYINT b) 97 | { 98 | return MYINT(a.x * b.x); 99 | } 100 | 101 | MYINT operator/(MYINT a, MYINT b) 102 | { 103 | return MYINT(a.x / b.x); 104 | } 105 | 106 | MYINT operator%(MYINT a, MYINT b) 107 | { 108 | return MYINT(a.x % b.x); 109 | } -------------------------------------------------------------------------------- /BTH6/11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class general 5 | { 6 | public: 7 | general(); 8 | 9 | ~general(); 10 | }; 11 | 12 | general A; 13 | int main() { 14 | cout << "Hello, world.\n"; 15 | return 0; 16 | } 17 | 18 | general::general() 19 | { 20 | cout << "Entering the Hello program saying...\n"; 21 | } 22 | general::~general() 23 | { 24 | cout << "Then exiting..."; 25 | } 26 | -------------------------------------------------------------------------------- /BTH6/Readme.txt: -------------------------------------------------------------------------------- 1 | 1. Làm lại bài số phức với một phương thức thiết lập duy nhất cho phép quan điểm một số thực như một số phức đặc biệt (phần ảo bằng 0). Định nghĩa các phép toán +, -, *, /, = =, !=, >, >=, <, <= trên số phức. Định nghĩa phép toán << và >> để xuất và nhập dữ liệu vào số phức. 2 | 3 | 2. Làm lại bài phân số với các phương thức thiết lập cho phép sử dụng một số nguyên như một phân số đặc biệt (mẫu số bằng 1). Định nghĩa các phép toán +, -, *, /, = =, != , >, >=, <, <= trên phân số. Định nghĩa phép toán << và >> để xuất và nhập dữ liệu vào phân số. 4 | 5 | 3. Định nghĩa lớp dữ liệu CTimeSpan để biểu diễn khái niệm khoảng thời gian, các hàm thành phần và các phép toán cần thiết (+, -, ==, !=, >, >=, <, <=). 6 | 7 | 4. Định nghĩa lớp CTime biểu diễn khái niệm thời điểm có các thành phần giờ phút giây. Định nghĩa các phép toán +, - (cộng, trừ thêm một số nguyên giây), - (phép trừ hai CTime để được một CTimSpan), ++, -- (thêm bớt một giây). Phép toán <<, >> để xuất, nhập dữ liệu loại CTime. 8 | 9 | 5. Định nghĩa lớp CDate biểu diễn khái niệm ngày, tháng, năm với các phép toán +, - (cộng, trừ thêm một số ngày), ++, -- (thêm bớt một ngày), - (khoảng cách giữa hai CDate tính bằng ngày). Phép toán <<, >> để xuất, nhập dữ liệu loại CDate. 10 | 11 | 6. Hãy định nghĩa lớp CString biểu diễn khái niệm chuỗi ký tự với các phương thức thiết lập, huỷ bỏ, các hàm thành phần và các phép toán cần thiết (+, gán, so sánh hai chuỗi). 12 | 13 | 7. Định nghĩa lớp biểu diễn khái niệm đa thức có bậc bất kỳ với các hàm thành phần và phép toán cần thiết. 14 | 15 | 8. Định nghĩa lớp CVector biểu diễn khái niệm vector trong không gian có số chiều bất kỳ với các hàm thành phần và các phép toán cần thiết. 16 | Định nghĩa lớp CMatrix biểu diễn khái niệm ma trận có kích thước bất kỳ với các hàm thành phần và các phép toán cần thiết. 17 | Viết hàm tính tích của một ma trận và một vector. Tích của hai ma trận. 18 | 19 | 9. Hãy định nghĩa lớp INTEGER có thể hoạt động như để mỗi INTEGER giống hệt như một 'int' của ngôn ngữ C/C++. 20 | 21 | 10. Hãy định nghĩa lớp MYINT có hoạt động như kiểu dữ liệu 'int' nhưng phép cộng hai MYINT hoạt động như phép trừ hai int và ngược lại. 22 | 23 | 11. Cho đoạn chương trình sau: 24 | #include 25 | main(){ cout << "Hello, world.\n"; } 26 | Hãy sửa lại chương trình trên, nhưng không sửa chữa gì hàm main, để chương trình có thể tạo ra kết xuất: 27 | Entering the Hello program saying... 28 | Hello, world. 29 | Then exiting... 30 | 31 | -------------------------------------------------------------------------------- /BTH7/06/06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int random(int, int); 7 | 8 | class animal { 9 | protected: 10 | int childAmount; 11 | int milkAmount; 12 | public: 13 | virtual string animalName() = 0; 14 | virtual void talk() = 0; 15 | void print(); 16 | }; 17 | 18 | class cow : public animal { 19 | public: 20 | cow() { 21 | childAmount = random(1, 5); 22 | milkAmount = random(0, 20); 23 | } 24 | string animalName() { 25 | return "BO"; 26 | } 27 | void talk(); 28 | }; 29 | 30 | class sheep : public animal { 31 | public: 32 | sheep() { 33 | childAmount = random(1, 5); 34 | milkAmount = random(0, 5); 35 | } 36 | string animalName() { 37 | return "COU"; 38 | } 39 | void talk(); 40 | }; 41 | 42 | class goat : public animal { 43 | public: 44 | goat() { 45 | childAmount = random(1, 5); 46 | milkAmount = random(0, 10); 47 | } 48 | string animalName() { 49 | return "DE"; 50 | } 51 | void talk(); 52 | }; 53 | 54 | void animalTalk(animal**, int); 55 | void animalExport(animal**, int); 56 | 57 | int main() { 58 | animal* arr[3]; 59 | arr[0] = new cow; 60 | arr[1] = new sheep; 61 | arr[2] = new goat; 62 | animalTalk(arr, 3); 63 | system("pause"); 64 | animalExport(arr, 3); 65 | system("pause"); 66 | } 67 | 68 | int random(int min, int max) { 69 | static bool first = true; 70 | if (first) { 71 | srand(time(NULL)); 72 | first = false; 73 | } 74 | return min + rand() % ((max + 1) - min); 75 | } 76 | 77 | void animal::print() { 78 | cout << animalName() << endl; 79 | cout << " So con sau mot lua sinh la " << childAmount << endl; 80 | cout << " So lit sua thu duoc la " << milkAmount << endl; 81 | } 82 | 83 | void cow::talk() { 84 | cout << " Bo keu: TAU DOI QUA !" << endl; 85 | PlaySound(TEXT("Cow.wav"), NULL, SND_SYNC); 86 | } 87 | void sheep::talk() { 88 | cout << " Cuu keu: TAU CUNG DOI QUA !" << endl; 89 | PlaySound(TEXT("Sheep.wav"), NULL, SND_SYNC); 90 | } 91 | void goat::talk() { 92 | cout << " De keu: BO MAY DOI ROI DAY !" << endl; 93 | PlaySound(TEXT("Goat.wav"), NULL, SND_SYNC); 94 | } 95 | 96 | void animalTalk(animal** arr, int size) { 97 | cout << "Hom nay bi chu bo doi, bon gia suc bat dau keu la am i" << endl; 98 | Sleep(1000); 99 | for (int i = 0; i < 3; i++) 100 | arr[i]->talk(); 101 | } 102 | void animalExport(animal** arr, int size) { 103 | system("cls"); 104 | for (int i = 0; i < 3; i++) 105 | arr[i]->print(); 106 | } -------------------------------------------------------------------------------- /BTH7/06/Cow.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Object-Oriented-Programming-UIT/0fe34e757fe365b7acbff8401a3ebd92951fede0/BTH7/06/Cow.wav -------------------------------------------------------------------------------- /BTH7/06/Goat.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Object-Oriented-Programming-UIT/0fe34e757fe365b7acbff8401a3ebd92951fede0/BTH7/06/Goat.wav -------------------------------------------------------------------------------- /BTH7/06/README.txt: -------------------------------------------------------------------------------- 1 | Đặt tệp Cow.wav, Goat.wav và Sheep.wav vào cùng thư mục với tệp mã nguồn 2 | 3 | 1 - Trong Visual Studio, nhấp chuột phải vào project đang mở và chọn "Properties". 4 | 2 - Đi theo đường dẫn Configuration Properties đến Linker và Input 5 | 3 - Bên phải mục "Additional Dependencies", nhấp vào và điền "winmm.lib" vào 6 | 4 - Chọn "Ok" và đợi 15 giây 7 | 8 | -------------------------------------------------------------------------------- /BTH7/06/Sheep.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Object-Oriented-Programming-UIT/0fe34e757fe365b7acbff8401a3ebd92951fede0/BTH7/06/Sheep.wav -------------------------------------------------------------------------------- /BTH7/Readme.txt: -------------------------------------------------------------------------------- 1 | Giảng viên yêu cầu làm được bài cuối cùng kèm theo tiếng động vật kêu thật là đạt điểm tuyệt đối cho cả bài thực hành 7 nên không có bài làm cho những bài tập còn lại. 2 | ____________________________ 3 | 4 | 1. Giả sử Công ty có hai loại nhân viên: Nhân viên văn phòng và Nhân viên sản xuất. Viết chương trình quản lý và tính lương cho từng nhân viên của công ty: 5 | Mỗi nhân viên cần quản lý các thông tin sau: Họ tên, ngày sinh, lương. Công ty cần tính lương cho nhân viên như sau: 6 | - Đối với nhân viên sản xuất: 7 | Lương = lương căn bản + số sản phẩm * 5.000 8 | - Đối nhân viên văn phòng: 9 | lương = số ngày làm việc * 100.000 10 | 11 | 2. Xây dựng các loại đối tượng sinh viên, học sinh, công nhân, nghệ sĩ, ca sĩ. Viết chương trình cho phép nhập vào một trong các loại đối tượng kể trên. In thông tin đối tượng đó. 12 | 13 | 3. Tạo một danh sách các đối tượng, mỗi đối tượng thuộc một trong các loại: sinh viên, học sinh, công nhân, nghệ sĩ, ca sĩ. Viết chương trình cho phép nhập danh sách kể trên, in thông tin của từng đối tượng trong danh sách. 14 | 15 | 4. Xây dựng các loại đối tượng sách, sách giáo khoa, tiểu thuyết, tạp chí. Viết chương trình cho phép quản lý một danh sách các loại đối tượng kể trên. 16 | 17 | 5. Xây dựng lớp DaGiac thể hiện khái niệm đa giác với các thao tác cần thiết (nhập, xuất, tịnh tiến). Dùng kế thừa xây dựng các lớp tứ giác, tam giác. Viết chương trình cho phép nhập vào một tam giác hoặc tứ giác. Xuất và thực hiện các thao tác tịnh tiến hình đã nhập. 18 | 19 | 6. Một nông trại chăn nuôi có 3 loại gia súc: bò, cừu, và dê. Mỗi loại gia súc đều có thể sinh con, cho sữa và phát ra tiếng kêu riêng của chúng. Khi đói, các gia súc sẽ phát ra tiếng kêu để đòi ăn. Sau một thời gian chăn nuôi, người chủ nông trại muốn thống kê xem trong nông trại có bao nhiêu gia súc ở mỗi loại, tổng số lit sữa mà tất cả các gia súc của ông đã cho. Áp dụng kế thừa, xây dựng chương trình cho phép người chủ nông trại nhập vào số lượng gia súc ban đầu ở mỗi loại. 20 | a. Một hôm người chủ nông trại đi vắng, tất cả gia súc trong nông trại đều đói. Hãy cho biết những tiếng kêu nghe được trong nông trại. 21 | b. Chương trình sẽ đưa ra thống kê các thông tin người chủ mong muốn (nêu trên) sau một lứa sinh và một lược cho sữa của tất cả gia súc. Biết rằng: 22 | - Tất cả gia súc ở mỗi loại đều sinh con. 23 | - Số lượng sinh của mỗi gia súc là ngẫu nhiên. 24 | - Tất cả gia súc ở mỗi loại đều cho sữa. 25 | - Số lit sữa mỗi gia súc cho là ngẫu nhiên nhưng trong giới hạn sau: 26 | • Bò: 0 – 20 lít. 27 | • Cừu: 0 – 5 lít. 28 | • Dê: 0 – 10 lít. 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lập trình hướng đối tượng UIT - Tất cả bài thực hành 2 | Tất cả bài thực hành môn lập trình hướng đối tượng - Trường Đại học Công nghệ Thông tin 3 | ____________________________ 4 | Bài làm chỉ mang tính chất tham khảo cho các bạn sinh viên khác. Ngoài ra, đây là môn học tiên quyết cho sự nghiệp phát triển phần mềm, do đó bắt buộc phải hiểu thật sâu và chắc hầu hết các tính chất lẫn khái niệm xuất hiện trong quá trình học môn này. 5 | ____________________________ 6 | Để học tốt môn này, tác giả khuyên các bạn cần chuẩn bị trước: 7 | - Hiểu rõ cách triển khai một chương trình tổng quát đầy đủ các thành phần bằng ngôn ngữ C++. 8 | - Nắm vững kiến thức về những kiểu dữ liệu cơ sở, mảng một chiều và kiến thức căn bản về con trỏ. 9 | - Có thể định nghĩa kiểu dữ liệu mới bằng kiểu cấu trúc (struct). 10 | ____________________________ 11 | ## Một số lưu ý về việc đặt tên cần nhớ: 12 | 13 | - Hạn chế đặt tên theo ngôn ngữ sở tại (chẳng hạn như tiếng Việt), nên sử dụng tiếng Anh để tránh người khác đọc hiểu sai nghĩa. Ngoài ra, hãy đặt tên có nghĩa, không nên quá dài và tránh sử dụng những từ dễ bị nhầm lẫn hoặc đồng âm. Lưu ý rằng, viết tắt tên chính là một nghệ thuật và phải thực hiện đúng cách để tối ưu cho việc đọc hiểu. 14 | Ví dụ: 15 | ```console 16 | string phoneNumber; 17 | string phoneNum; 18 | int naturalNumber; 19 | ``` 20 | 21 | - Tên biến và tên hàm nên viết theo dạng lạc đà (CamelCase) hoặc dạng rắn (snake_case). Ở đây, tác giả sử dụng dạng lạc đà. Ví dụ: 22 | ```console 23 | string phoneNumber; 24 | string phoneNum; 25 | int arraySize; 26 | ``` 27 | - Tên mảng được đặt theo tên phần tử của mảng ở dạng số nhiều. Quy tắt này được áp dụng trong lập trình hướng đối tượng hoặc với kiểu lập trình sử dụng struct. 28 | Ví dụ: 29 | ```js 30 | Student students[100]; 31 | Dog asianDogs[100]; 32 | ``` 33 | 34 | - Viết hoa chữ cái đầu mỗi từ trong tên class và struct. 35 | Ví dụ: 36 | ```js 37 | class Student {}; 38 | struct Dog {}; 39 | class AsianMan {}; 40 | ``` 41 | 42 | - Đối với biến và hàm của kiều dữ liệu bool, hãy đặt tên để ngầm hiểu câu trả lời là Yes hoặc No. 43 | Ví dụ: 44 | ```js 45 | bool isVerified; 46 | bool didHomework; 47 | bool hasFiveDogs; 48 | bool isValidAccount(); 49 | ``` 50 | 51 | - Viết hoa tất cả các ký tự trong tên của hằng số và mỗi từ trong tên phân tách bởi dấu gạch chân. 52 | Ví dụ: 53 | ```js 54 | const double PI = 3.14; 55 | const int MAX_SIZE = 10000; 56 | ``` 57 | 58 | ## Ngoài ra: 59 | - Mỗi hàm chỉ thực hiện duy nhất một chức năng. Tuyệt đối không gộp chức năng tính toán logic và nhập/xuất vào một chung một hàm. 60 | 61 | - Hàm main() chỉ được dùng để gọi hàm khác, khai báo những biến được nêu trong yêu cầu và gọi lệnh nhập/xuất cơ bản. Ngoài ra, tuyệt đối không thực hiện thêm chức năng nào khác. 62 | ____________________________ 63 | ### Để tối ưu cho việc triển khai chương trình đa lựa chọn dạng menu, tác giả cung cấp cho các bạn một mẫu menu bên dưới. 64 | 65 | ```js 66 | #include 67 | using namespace std; 68 | 69 | class YourClass { 70 | int yourVariable; 71 | 72 | public: 73 | void input(); 74 | }; 75 | 76 | void showMenu(); 77 | void handleChoiceFor(YourClass& yourObject); 78 | 79 | int main() { 80 | YourClass yourObject; 81 | yourObject.input(); 82 | 83 | handleChoiceFor(yourObject); 84 | } 85 | 86 | void YourClass::input() { 87 | cout << "Value of your variable is "; 88 | cin >> yourVariable; 89 | } 90 | 91 | void showMenu() { 92 | cout << "-----------------------------" << endl; 93 | cout << " (0) Exit." << endl; 94 | cout << " (1) Do something usefull." << endl; 95 | cout << " (2) Do something usefull." << endl; 96 | cout << " (3) Do something usefull." << endl; 97 | cout << "-----------------------------" << endl; 98 | } 99 | void handleChoiceFor(YourClass& yourObject) { 100 | showMenu(); 101 | 102 | int choice; 103 | cout << "Your choice: "; 104 | cin >> choice; 105 | system("cls"); 106 | 107 | switch (choice) 108 | { 109 | case 0: 110 | return; 111 | case 1: 112 | // Call the function for selection (1) only 113 | break; 114 | case 2: 115 | // Call the function for selection (2) only 116 | break; 117 | case 3: 118 | // Call the function for selection (3) only 119 | break; 120 | default: 121 | cout << "Invalid choice. Please try again!" << endl; 122 | break; 123 | } 124 | 125 | handleChoiceFor(yourObject); 126 | } 127 | ``` 128 | -------------------------------------------------------------------------------- /Thi thực hành cuối kỳ/20521008.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Object-Oriented-Programming-UIT/0fe34e757fe365b7acbff8401a3ebd92951fede0/Thi thực hành cuối kỳ/20521008.cpp -------------------------------------------------------------------------------- /Thi thực hành cuối kỳ/Readme.txt: -------------------------------------------------------------------------------- 1 | Xây dựng 2 lớp CCVinHome và CCBcon được kế thừa từ lớp ChungCu. Biết rằng ChungCu bao gồm các thuộc tính như sau: 2 | Tên chung cư (ten), 3 | Số tầng (soTang), 4 | Diện tích chung cư (dienTich), 5 | Tên người quản lý (tenQL), 6 | Tên đại diện sở hữu (soHuu) 7 | 8 | Lớp CCVinHome thì tên đại diện người sở hữu sẽ là “Vinhomes”. Lớp CCBcon thì tên đại diện người sở hữu sẽ là “Bcons”. Vào cuối tuần tất cả các quản lý tòa nhà đề phải gửi phiếu thông báo về tình trạng tòa nhà cho ban quản lý chung cư khu vực (guiThongBao), tiêu đề của thông báo là: “Tên người quản lý – Tên chung cư – Tên đại diện sở hữu”. Ví dụ: “Nguyễn Văn A – Chung cư UITer – Vinhomes”. 9 | 10 | 1. Áp dụng kế thừa, đa hình xây dựng chương trình với các lớp phù hợp. Cho phép ban quản lý chung cư khu vực nhập vào số lượng chung cư Vinhomes và Bcons. 11 | 12 | 2. Ban quản lý chung cư khu vực Làng Đại Học (khu vực chỉ có chung cư Vinhomes và Bcons) vào cuối tuần sẽ nhận được thông báo về tình trạng của các chung cư. Danh sách các tiêu đề mà ban quản lý chung cư cuối tuần nhận được sẽ là? 13 | Ví dụ: Có 2 chung cư: 1 chung cư Vinhomes và 1 chung cư Bcons. 14 | “Nguyễn Văn A – Chung cư UITer – Vinhomes 15 | Nguyễn Văn B – Chung cư USSHer – Bcons” 16 | 17 | 3. Tất cả các chung cư trong Làng Đại Học đều là chung cư cho thuê (sẽ phải trả tiền phòng mỗi tháng). Mỗi tầng của một chung cư đều có 6 phòng diện tích bằng nhau. Tiền thuê phòng mỗi tháng của các phòng được tính dựa theo diện tích của chung cư và đại diện sở hữu chung cư. Vậy số tiền mỗi tháng ban quản lý chung cư Làng Đại Học thu về sẽ là bao nhiêu? Biết rằng: 18 | Vinhomes: 19 | Diện tích > 600 m^2 : 10 – 15 triệu / phòng 20 | Diện tích <= 600 m^2: 6 – 10 triệu / phòng 21 | Bcons: 22 | Diện tích > 600 m^2 : 8 – 12 triệu / phòng 23 | Diện tích <= 600 m^2: 5 – 8 triệu / phòng 24 | 25 | Lưu ý: Tiền thuê phòng của các phòng thuộc một chung cư sẽ là như nhau. 26 | Ví dụ: Chung cư Vinhomes có 12 tầng, diện tích 700 m^2 thì số tiền thuê phòng của chung cư đó sẽ là hàm tienPhong() được tính theo công thức: random(10 → 15) * 12 *6 27 | -------------------------------------------------------------------------------- /Đề cương môn học.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phanxuanquang/Object-Oriented-Programming-UIT/0fe34e757fe365b7acbff8401a3ebd92951fede0/Đề cương môn học.pdf --------------------------------------------------------------------------------