Bạn cứ đem mấy bài bạn làm rồi viết lại hết, dùng class, struct. Ban sẽ hiểu ngay vấn đề . Giờ mình tạm lấy 1 bài mình làm rồi chuyển qua class bạn sẽ thấy ngay vấn đề. Bài này là tính ra ma trận xoắn ốc.
Phiên bản không class :
PHP Code:
#include <iostream>
#include <iomanip>
/*Biến cục bộ đây*/
int width, height;
int Grid[20][20];
/*Các hàm xử lý đây*/
void calculate_magic();
void construct_grid();
void print_result();
void read_input_from_user();
int main(){
read_input_from_user();
construct_grid();
calculate_magic();
print_result();
return 0;
}
void read_input_from_user(){
cout << "[ Enter width length ]" << endl;
cout << "[ -width : ";
cin >> width;
cout << "heigth: ";
cin >> height;
}
void construct_grid(){
width += 2;
height += 2;
for (int x = 0; x < width; x++){
for (int y = 0; y < height; y++){
if (x*(x - width + 1)*y*(y - height + 1) == 0)
Grid[x][y] = -1;
else
Grid[x][y] = 0;
}
}
}
void print_result(){
cout << "\n\n\n RESULT : \n";
for (int x = 1; x < width - 1; x++){
for (int y = 1; y < height - 1; y++){
cout << setw(5) << Grid[x][y];
}
cout << endl << endl;
}
}
void calculate_magic()
{
int row_step = 0, col_step = 1,
r = 1, c = 1,
mark = 1;
do{
do{
Grid[r][c] = mark++;
r += row_step;
c += col_step;
}
while(r >= 1 && r < width - 1 && c >= 1
&& c < height - 1 && Grid[r+row_step][c+col_step] == 0);
if(row_step == 0){
row_step = col_step - row_step;
col_step = 0;
}
else{
col_step = col_step - row_step;
row_step = 0;
}
}while(Grid[r][c] == 0);
}
Bây h viết lại thành class xem nó có gì khác :
PHP Code:
#include <iostream>
#include <iomanip>
/*Tạo 1 class Board cho nó oai^^*/
class Board{
/*Các hàm thành phần bây h khai báo ở đây*/
public :
void calculate_magic();
void construct_grid();
void print_result();
void read_input_from_user();
/*Các dữ liệu cục bộ bây h cho vào đây, và bây giờ chỉ*/
/*có các hàm trong class này được đụng tới nó thôi,
/*cái này gọi là sự đóng gói (encapsulation)*/
private:
int width, height;
int Grid[20][20];
};
/*Các hàm thành phần thêm class scope : "Board:: "*/
void Board::read_input_from_user(){
cout << "[ Enter width length ]" << endl;
cout << "[ -width : ";
cin >> width;
cout << "heigth: ";
cin >> height;
}
void Board::construct_grid(){
width += 2;
height += 2;
for (int x = 0; x < width; x++){
for (int y = 0; y < height; y++){
if (x*(x - width + 1)*y*(y - height + 1) == 0)
Grid[x][y] = -1;
else
Grid[x][y] = 0;
}
}
}
void Board::print_result(){
cout << "\n\n\n RESULT : \n";
for (int x = 1; x < width - 1; x++){
for (int y = 1; y < height - 1; y++){
cout << setw(5) << Grid[x][y];
}
cout << endl << endl;
}
}
void Board::calculate_magic(){
int row_step = 0, col_step = 1,
r = 1, c = 1,
mark = 1;
do{
do{
Grid[r][c] = mark++;
r += row_step;
c += col_step;
}
while(r >= 1 && r < width - 1 && c >= 1
&& c < height - 1 && Grid[r+row_step][c+col_step] == 0);
if(row_step == 0){
row_step = col_step - row_step;
col_step = 0;
}
else{
col_step = col_step - row_step;
row_step = 0;
}
}while(Grid[r][c] == 0);
}
int main(){
Board myGrid; /*Tạo 1 đối tượng myGrid*/
/*Gọi các hàm xử lý bằng syntax (tên_đối_tượng.hàm_nào_đó() )*/
myGrid.read_input_from_user();
myGrid.construct_grid();
myGrid.calculate_magic();
myGrid.print_result();
return 0;
}
Khác thì nó còn khác nhiều lắm, nhưng cơ bản là như vậy, bạn cứ học tiếp đi rồi thắc mắc gì hãy post lên hỏi.