Professional Documents
Culture Documents
p môn m
?
â
Nh
Nh<04B1>p
ô
• Mục đích:
– chương trình nhỏ: giải thích cho
những người khác.
– chương trình lớn: cho cả người lập
trình và người khác.
Một số chú ý khi viết TLHD
int main( ) {
Life iConfig;
iConfig . initialize( );
iConfig . print( );
cout << “Xem tiep the he sau? " << endl;
while (user_says_yes( )) {
iConfig . update( );
iCconfig . print( );
cout << “Xem tiep the he sau?” <<endl;
}
}
Tinh chỉnh Top-down – Hướng
dẫn
2. Mỗi hàm chỉ thực hiện một công việc
duy nhất
3. Mỗi hàm/lớp nên che giấu thông tin
4. Tham số của hàm cần được chỉ rõ
5. Các dữ liệu mà hàm sử dụng:
Æ Giữ cho các mỗi liên hệ càng đơn giản
càng tốt. Tránh sử dụng biến toàn cục
càng nhiều càng tốt.
Æ Nếu sử dụng biến toàn cục, viết hướng
dẫn chi tiết.
1. 3. Cài đặt, kiểm thử, tinh
chỉnh
Hàm giả
void instructions() { }
bool user_says_yes() {
return(true); }
Định nghĩa lớp Life
const int maxrow = 20, maxcol = 60;
class Life {
public:
void initialize( );
void print( );
void update( );
private:
int grid[maxrow + 2][maxcol + 2];
// allows for two extra rows and columns
int neighbor_count(int row, int col);
};
int Life :: neighbor_count(int row, int col)
/* Pre: The Life object contains a configuration, and the
coordinates row and col define a cell inside its hedge.
Post: The number of living neighbors of the specified cell is
returned. */
{
int i, j;
int count = 0;
for (i = row − 1; i <= row + 1; i++)
for (j = col − 1; j <= col + 1; j++)
count += grid[i][j]; /* Increase the count if
neighbor is alive. */
count −= grid[row][col]; /* Reduce count, since cell is not its
own neighbor. */
return count;
}
void Life :: update( )
/* Pre: The Life object contains a configuration.
Post: The Life object contains the next generation of configuration. */
{
int row, col;
int new_grid[maxrow . 2][maxcol . 2];
for (row = 1; row <= maxrow; row..)
for (col = 1; col <= maxcol; col..)
switch (neighbor_count(row, col)) {
case 2:
new_grid[row][col] = grid[row][col]; // Status stays the same.
break;
case 3:
new_grid[row][col] = 1; // Cell is now alive.
break;
default:
new_grid[row][col] = 0; // Cell is now dead.
}
for (row = 1; row <= maxrow; row..)
for (col = 1; col <= maxcol; col..)
grid[row][col] = new_grid[row][col];
}
void Life :: initialize( )
/* Pre: None.
Post: The Life object contains a configuration specified by the user. */
{
int row, col;
for (row = 0; row <= maxrow . 1; row..)
for (col = 0; col <= maxcol . 1; col..)
grid[row][col] = 0;
cout << "List the coordinates for living cells." << endl;
cout << "Terminate the list with the the special pair -1 -1" << endl;
cin >> row >> col;
while (row != -1 || col != -1) {
if (row >= 1 && row <= maxrow)
if (col >= 1 && col <= maxcol)
grid[row][col] = 1;
else
cout << "Column " << col << " is out of range.“;
else
cout << "Row " << row << " is out of range." << endl;
cin >> row >> col;
}
}
print()
void Life :: print( )
/* Pre: The Life object contains a configuration.
Post: The configuration is written for the user. */
{
int row, col;
cout << "\nThe current Life configuration is:" << endl;
for (row = 1; row <= maxrow; row..) {
for (col = 1; col <= maxcol; col..)
if (grid[row][col] == 1) cout << ‘*’;
else cout << ‘ ‘;
cout << endl;
}
cout << endl;
}
Driver
• Driver: đoạn chương trình cung cấp dữ liệu cho hàm và
đánh giá kết quả.
int main ( ) // driver for neighbor_count( )
{
Life iConfig;
iConfig.initialize( );
for (row = 1; row <= maxrow; row..){
for (col = 1; col <= maxrow; col..)
cout << iConfig.neighbor_count(row,
col) ;
cout << endl;
}
}
Dò vết chương trình
• Duyệt lại cấu trúc chương trình
local x in main is 5
2. 3. Kiểu tham chiếu
Kiểu tham chiếu (reference)
• Tham chiếu là một tên khác của biến
• Được xem như một con trỏ hằng
– không được tham chiếu tới một biến khác
int ival = 1024;
refVal += 2;
refVal = someVal;
int *p = &refVal; // khởi tạo p là địa chỉ của refVal
Ví dụ
int a[ ] = { 3, 8, -4, 6 };
int &b = a[0];
int *c = &(a[3]);
int *&d = c;
int **e = &d;
Dữ liệu của mảng a[ ] sau khi thực hiện những
câu lệnh sau?
b--;
*d += b + a[1];
c = &(a[1]);
**e -= a[2];
c[0] = *d + **e + b;
d[1] = 2 * (**e);
2. 4. Truyền tham số
Truyền tham số
p
a = 18
Tham biến là con trỏ
void SwapPtr(short *,short * );
void main()
{
short x = 10, y = 2;
SwapWrong (&x, &y);
cout << x << endl;
cout << y << endl;
}
void SwapPtr(short *u,short *v )
{
short stemp;
stemp = *v; *v = *u; *u = *stemp;
}
Tham số mặc định được chỉ ra trong hàm mẫu
Tham số mặc định
Biến địa
phương
Địa chỉ Activation Frame
Activation trở về
Record
Các tham số
Khung stack cho các lời gọi
hàm
Stack
được
D D cấp
B C C C D D D phát
cho dữ
A A A A A A A D D D D D liệu
M M M M M M M M M M M M M M M
Thời gian
Cây lời gọi hàm
Bắt đầu Kết thúc
Cây gọi hàm tương M
đương
A D
B C D
// Sử dụng đệ quy
long Factorial (long n)
{
// điều kiện dừng n == 0
if (n == 0)
return 1;
else // bước đệ quy
return n * Factorial (n-1);
}
Điều kiện của đệ quy
• Giải thuật đệ quy phải thỏa mãn 2
điều kiện:
– Phải có điểm dừng
• Một processor.
• Đồng bộ trên nhiều processor:
Chia để trị
• Khử đệ quy:
3.6. Kỹ thuật quy hoạch
động
Quy hoạch động – Dynamic
Programming
• Là một kỹ thuật để tránh phải tính
toán lại cho các giải thuật đệ quy.
• Giải pháp:
Quy hoạch động từ dưới lên
int fib(int n)
{
int f[LARGE_NUMBER];
f[0] = 0; f[1] = 1;
for (int i=2; i<= n; i++)
f[i] = f[i-1] + f[i-2];
return f[n];
}
Quy hoạch động từ trên xuống
return choices[N][k];
}
C++ (.h file)
C++ (.cpp file)