Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

CÂU 1: if (index >= 0 && index < CAPACITY) {

#include <iostream> ITE = buffer + index;

typedef char* PCHAR; // xâu ký tự }

typedef const PCHAR LPCHAR; // hằng xâu ký tự return ITE;

const int CAPACITY = 1024; }

char buffer[CAPACITY] = { }; // vùng dữ liệu // Tìm ký tự c đầu tiên xuất hiện trong buffer (0.75 điểm)

PCHAR ITE = buffer; // con trỏ truy cập buffer // Trả về -1 nếu không tìm thấy

// Hàm copy xâu ký tự từ xâu src sang xâu dst (0.75 điểm) int find_first(char c) {

PCHAR copy(PCHAR dst, LPCHAR src) { for (int i = 0; i < CAPACITY; ++i) {

PCHAR ret = dst; if (buffer[i] == c) {

while ((*dst++ = *src++) != '\0'); return i;

return ret; }

} if (buffer[i] == '\0') {

// Hàm kết thúc xâu ký tự trong buffer break;

PCHAR end_string(char c = 0) { }

*ITE = c; }

*(++ITE) = 0; return -1;

return buffer; }

} // Hàm xóa dấu cách đầu tiên trong buffer (0.5 điểm)
LPCHAR remove() {

// Hàm đặt ITE vào vị trí index của buffer (0.5 điểm) for (int i = 0; i < CAPACITY; ++i) {

PCHAR seek(int index) { if (buffer[i] == ' ') {


for (int j = i; j < CAPACITY - 1; ++j) { void set_value(int v) {
buffer[j] = buffer[j + 1]; char temp[12]; // đủ lớn để chứa số nguyên
} int i = 0;
buffer[CAPACITY - 1] = 0; if (v == 0) {
return buffer; temp[i++] = '0';
} } else {
if (buffer[i] == '\0') { while (v > 0) {
break; temp[i++] = (v % 10) + '0';
} v /= 10;
} }
return buffer; }
} while (i > 0) {
*ITE++ = temp[--i];
// Hàm lấy số nguyên bắt đầu từ vị trí ITE (0.5 điểm) }
int get_value() { *ITE = '\0';
int value = 0; }
while (*ITE >= '0' && *ITE <= '9') { int main() {
value = value * 10 + (*ITE - '0'); seek(0);
++ITE; copy(buffer, LPCHAR("size("));
} set_value(CAPACITY);
return value; std::cout << end_string(')') << std::endl; // in ra: size(1024)
} seek(find_first('(') + 1);
// Hàm chuyển các chữ số của số nguyên (1 điểm) std::cout << get_value() << std::endl; // in ra: 1024
// vào buffer từ vị trí ITE }
CÂU 2: // Hàm khởi tạo Date có giá trị trong khoảng từ 01/01/1900 đến 01/01/2099 (0.5
điểm)
#include <iostream>
Date(int y, int m, int d) : year(in_range(y, 1900, 2099)), month(in_range(m, 1,
#include <string> 12)), day(in_range(d, 1, GetDaysOfMonth())) {}
#include <list>
using namespace std; // Toán tử in đối tượng ra màn hình theo định dạng dd/MM/yyyy (1 điểm)
int in_range(int v, int a, int b) { friend ostream& operator<<(ostream& out, const Date& obj) {
if (v < a) return a; return out << obj.day << '/' << obj.month << '/' << obj.year;
return v > b ? b : v; }
}

// Phương thức tăng Date thêm 1 ngày (1 điểm)


class Date { void IncreaseDay() {
int day, month, year; day++;
public: if (day > GetDaysOfMonth()) {
// Phương thức lấy số ngày của tháng day = 1;
int GetDaysOfMonth() const { month++;
if (month == 2) return year % 4 ? 28 : 29; if (month > 12) {
if (month == 4 || month == 6 || month == 9 || month == 11) return 30; month = 1;
return 31; year++;
} }
// Bổ sung các phương thức lấy các thuộc tính (0.5 điểm) }
int getDay() const { return day; } }
int getMonth() const { return month; } };
int getYear() const { return year; }

// Hàm khởi tạo Date mặc định (để Date(1900, 1, 1))


Date::Date(int y, int m, int d) : year(y), month(m), day(d) {} // Tạo thêm 3 sự kiện khác trong năm 2023
events.push_back(new History(9, 3, year, "International Women's Day"));
class History : public Date { events.push_back(new History(19, 6, year, "Father's Day"));
string name; events.push_back(new History(2, 9, year, "Labor Day"));
public:
// Hàm khởi tạo sự kiện lịch sử gồm ngày, tháng, năm và tên sự kiện (0.5 điểm) // In tất cả các sự kiện xảy ra trong tháng 9
History(int day, int month, int year, const char* name) : Date(year, month, day), cout << "Cac su kien trong thang 9/2023:" << endl;
name(name) {}
for (auto x : events) {
if (x->getMonth() == 9) {
// Toán tử in sự kiện ra màn hình (0.5 điểm)
cout << *x << endl;
friend ostream& operator<<(ostream& out, const History& obj) {
}
out << obj.getDay() << '/' << obj.getMonth() << '/' << obj.getYear() << ": "
}
<< obj.name;
return out;
// Giải phóng bộ nhớ
}
for (auto x : events) {
};
delete x;
int main() {
}
const int year = 2023;
list<History*> events; // Danh sách các sự kiện lịch sử
return 0;
events.push_back(new History(1, 1, year, "Tet Nguyen dan"));
}
CÂU 1
#include <iostream> // Tìm ký tự c đầu tiên xuất hiện trong buffer (0.75 điểm)
// Trả về -1 nếu không tìm thấy

typedef char* PCHAR; // xâu ký tự int find_first(char c) {

typedef const PCHAR LPCHAR; // hằng xâu ký tự PCHAR p = buffer;


while (*p) {

const int CAPACITY = 1024; if (*p == c)

char buffer[CAPACITY] = { }; // vùng dữ liệu return p - buffer;


p++;

PCHAR ITE = buffer; // con trỏ truy cập buffer; }


return -1;

// Hàm copy xâu ký tự từ xâu src sang xâu dst (0.75 điểm) }

PCHAR copy(PCHAR dst, LPCHAR src) {


PCHAR p = dst; // Hàm xóa dấu cách đầu tiên trong buffer (0.5 điểm)

while ((*p++ = *src++)) LPCHAR remove() {

; PCHAR p = buffer;

return dst; while (*p && *p == ' ')

} p++;
return p;

// Hàm đặt ITE vào vị trí index của buffer (0.5 điểm) }

PCHAR seek(int index) {


ITE = buffer + index; // Hàm lấy số nguyên bắt đầu từ vị trí ITE (0.5 điểm)

return ITE; int get_value() {

} int value = 0;
PCHAR p = ITE;
while (*p >= '0' && *p <= '9') { int main() {
value = value * 10 + (*p - '0'); seek(0);
p++; copy(buffer, LPCHAR("size("));
} set_value(CAPACITY);
return value;
} std::cout << end_string(')') << std::endl; // in ra: size(1024)

// Hàm chuyển các chữ số của số nguyên (1 điểm) seek(find_first('(') + 1);


// vào buffer từ vị trí ITE std::cout << get_value() << std::endl; // in ra: 1024
void set_value(int v) {
PCHAR p = ITE; return 0;
do { }
*p++ = v % 10 + '0';
v /= 10;
} while (v);
}

// Hàm kết thúc xâu ký tự trong buffer


PCHAR end_string(char c) {
*ITE = c; CÂU 2:
*(++ITE) = 0; #include <iostream>
return buffer; #include <string>
} #include <list>
using namespace std; return out;
}
class Time {
int hour, minu, sec; // Phương thức tăng Time thêm 1 giây (1 điểm)
void IncreaseSecond() {
public: sec++;
// Bổ sung các phương thức lấy các thuộc tính (0.5 điểm) if (sec >= 60) {
int getHour() const { return hour; } sec = 0;
int getMinute() const { return minu; } minu++;
int getSecond() const { return sec; } if (minu >= 60) {
minu = 0;
// Hàm khởi tạo Time có giá trị trong khoảng từ 00:00:00 đến 23:59:59 (0.5 hour++;
điểm)
if (hour >= 24) {
Time(int h = 0, int m = 0, int s = 0) {
hour = 0;
hour = (h >= 0 && h <= 23) ? h : 0;
}
minu = (m >= 0 && m <= 59) ? m : 0;
}
sec = (s >= 0 && s <= 59) ? s : 0;
}
}
}
};
// Toán tử in đối tượng ra màn hình theo định dạng HH:mm:ss (1 điểm)
friend ostream& operator<<(ostream& out, const Time& obj) {
class Task : public Time {
out << (obj.hour < 10 ? "0" : "") << obj.hour << ":"
string todo;
<< (obj.minu < 10 ? "0" : "") << obj.minu << ":"
<< (obj.sec < 10 ? "0" : "") << obj.sec;
public:
// Hàm khởi tạo việc cần làm trong ngày gồm giờ, phút và đầu việc (0.5 điểm) // In tất cả các Task sau 9 giờ tối (0.5 điểm)
Task(int hour, int minute, const char* todo) cout << "Tasks after 9 PM:" << endl;
: Time(hour, minute), todo(todo) {} for (auto x : events) {
if (x->getHour() >= 21) {
// Toán tử in việc cần làm ra màn hình (0.5 điểm) cout << *x << endl;
friend ostream& operator<<(ostream& out, const Task& obj) { }
out << obj.getHour() << ":" << obj.getMinute() << " - " << obj.todo; }
return out;
} // Giải phóng bộ nhớ
}; for (auto x : events) {
delete x;
int main() { }
const int year = 2023;
list<Task*> events; // Danh sách các Task return 0;
events.push_back(new Task(5, 30, "Tap the duc")); }

// Tạo thêm tối thiểu 3 Task cần thực hiện trong ngày (0.5 điểm)
events.push_back(new Task(9, 0, "Meeting"));
events.push_back(new Task(12, 0, "Lunch"));
events.push_back(new Task(18, 30, "Dinner"));

You might also like