Lập trình cấu trúc
Trang 1LẬP TRÌNH CẤU TRÚC
(structured programming)
NỘI DUNG:
khiển) trong ngôn ngữ C.
1
Khái niệm lập trình cấu trúc
khai và đảm bảo tính đúng đắn của chương trình
development)
• Thiết kế môđun (Modular design)
2
• Đi từ cái chung đến cái riêng, từ kết luận đến chi tiết, từ tổng
thể đến đơn vị
• Nhóm các câu lệnh lại với nhau tạo ra những môđun có quan
hệ với nhau Nói cách khác: chương trình được phân tách
thành các phần chức năng quan hệ logic với nhau.
chương trình hơn.
3
Triển khai chương trình từ trên xuống trong ngôn ngữ C:
/* 4. Khai báo nguyên mẫu các hàm */
function prototype
/* 5 Khai báo các biến toàn cục */
{ Khai báo các biến;
Tập lệnh;
}
/* 7. “Các chương trình con”- các hàm */
Trang 21 /* Fig 6.22: fig06_22.c
2 Double-subscripted array example */
3 #include <stdio.h>
4 #define STUDENTS 3
5 #define EXAMS 4
6
7 /* function prototypes */ 8 int minimum( const int grades[][ EXAMS ], int pupils, int tests ); 9 int maximum( const int grades[][ EXAMS ], int pupils, int tests ); 10 double average( const int setOfGrades[], int tests ); 11 void printArray( const int grades[][ EXAMS ], int pupils, int tests ); 12
13 /* function main begins program execution */ 14 int main() 15 {
16 int student; /* counter */ 17
18 /* initialize student grades for three students (rows) */ 19 const int studentGrades[ STUDENTS ][ EXAMS ] = 20 { { 77, 68, 86, 73 }, 21 { 96, 87, 89, 78 }, 22 { 70, 90, 86, 81 } }; 23
Ví dụ: 3 sinh viên tương ứng với 3 hàng điểm sau: { { 77, 68, 86, 73 }, { 96, 87, 89, 78 }, { 70, 90, 86, 81 } }; Tìm điểm cao nhất, thấp nhất, tính điểm trung bình của mỗi sinh viên 24 /* output array studentGrades */ 25 printf( "The array is:\n" ); 26 printArray( studentGrades, STUDENTS, EXAMS ); 27
28 /* determine smallest and largest grade values */ 29 printf( "\n\nLowest grade: %d\nHighest grade: %d\n", 30 minimum( studentGrades, STUDENTS, EXAMS ), 31 maximum( studentGrades, STUDENTS, EXAMS ) ); 32
33 /* calculate average grade for each student */ 34 for ( student = 0; student <= STUDENTS - 1; student++ ) { 35 printf( "The average grade for student %d is %.2f\n", 36 student, average( studentGrades[ student ], EXAMS ) ); 37 } /* end for */ 38
39 return ; /* indicates successful termination */ 40
41 } /* end main */ 42
43 /* Find the minimum grade */ 44 int minimum( const int grades[][ EXAMS ], int pupils, int tests ) 45 {
46 int i; /* counter */ 47 int j; /* counter */ 48 int lowGrade = 100; /* initialize to highest possible grade */ 49
50 /* loop through rows of grades */ 51 for ( i = 0; i < pupils; i++ ) { 52
53 /* loop through columns of grades */ 54 for ( j = 0; j < tests; j++ ) { 55
56 if ( grades[ i ][ j ] < lowGrade ) { 57 lowGrade = grades[ i ][ j ]; 58 } /* end if */ 59
60 } /* end inner for */ 61
62 } /* end outer for */ 63
64 return lowGrade; /* return minimum grade */ 65
66 } /* end function minimum */ 67
68 /* Find the maximum grade */ 69 int maximum( const int grades[][ EXAMS ], int pupils, int tests ) 70 {
71 int i; /* counter */ 72 int j; /* counter */ 73 int highGrade = 0; /* initialize to lowest possible grade */ 74
75 /* loop through rows of grades */ 76 for ( i = 0; i < pupils; i++ ) { 77
78 /* loop through columns of grades */ 79 for ( j = 0; j < tests; j++ ) { 80
81 if ( grades[ i ][ j ] > highGrade ) { 82 highGrade = grades[ i ][ j ]; 83 } /* end if */ 84
85 } /* end inner for */ 86
87 } /* end outer for */ 88
89 return highGrade; /* return maximum grade */ 90
91 } /* end function maximum */ 92
93 /* Determine the average grade for a particular student */ 94 double average( const int setOfGrades[], int tests )
95 {
96 int i; /* counter */
97 int total = 0; /* sum of test grades */
98
Trang 3100 f o r ( i = 0; i < t e s t s ; i + + ) {
101 t o t a l + = s e t O f G r a d e s [ i ] ;
102 } / * e n d f o r * /
103
104 r e t u r n ( d o u b l e ) t o t a l / t e s t s ; / * a v e r a g e * /
105
106 } / * e n d f u n c t i o n a v e r a g e * /
107
108 / * P r i n t t h e a r r a y * /
109 v o i d p r i n t A r r a y ( c o n s t i n t g r a d e s [ ] [ E X A M S ] , i n t p u p i l s , i n t t e s t s )
110 {
111 i n t i ; / * c o u n t e r * /
112 i n t j ; / * c o u n t e r * /
113
114 / * o u t p u t c o l u m n h e a d s * /
115 p r i n t f ( " [ 0 ] [ 1 ] [ 2 ] [ 3 ] " ) ;
116
117 / * o u t p u t g r a d e s i n t a b u l a r f o r m a t * /
118 f o r ( i = 0; i < p u p i l s ; i + + ) {
119
120 / * o u t p u t l a b e l f o r r o w * /
121 p r i n t f ( " \ n s t u d e n t G r a d e s [ % d ] ", i ) ;
122
The array is:
[0] [1] [2] [3]
studentGrades[0] 77 68 86 73 studentGrades[1] 96 87 89 78 studentGrades[2] 70 90 86 81 Lowest grade: 68
Highest grade: 96 The average grade for student 0 is 76.00 The average grade for student 1 is 87.50 The average grade for student 2 is 81.75
123 /* output grades for one student */
124 for ( j = 0; j < tests; j++ ) {
125 printf( "%-5d", grades[ i ][ j ] );
126 } /* end inner for */
127
128 } /* end outer for */
129
10
chỉ với 3 cấu trúc điều khiển cơ bản:
• Tuần tự (Sequential)
• Lựa chọn (hay Rẽ nhánh) (Selecttion)
• Lặp (Repetition)
11
Programming)
• Trong OOP điểm trọng tâm không phải là mã (code) mà là cái chúng ta muốn mã thực hiện trên đó (đặt trọng tâm vào đối tượng!)
• Mô tả đối tượng gồm các thuộc tính thuộc hai nhóm:
– Nhóm tính chất thứ nhất: khuôn dạng, sự tổ chức, kiến trúc (ví dụ: tên gọi, kích thước, mầu sắc, …
– Nhóm tính chất thứ 2: sự vận động, sự tương tác với các đối tượng khác (ví dụ: cách tạo lập, hủy bỏ, sự cập nhật (thay đổi kích thước, mầu sắc, ), hình thức tổ hợp với các đối tượng khác
• Trong OOP sử dụng các khái niệm cơ bản: đối tượng(object), lớp (class), tính kế thừa (inheritance), phương thức (methods), and tính đa hình (polymorphism).
Trang 4 Mã nguồn cho lập trình cấu trúc
-Bắt đầu chương trình
var1
var2
var3
function1 { }
function2 { }
function3 { }
main { }
- Kết thúc chương trình
Object Oriented Programming:
- B ắt đầu chương trình
object { varA varB functionA { } functionB { } }
varC varD functionC { } main { }
- K ết thúc chương trình
13
trình bắt đầu từ cách nhìn bài toán ở mức khái niệm, thiết kế chương trình trước khi tiến hành mã hóa (viết code)
và đảm bảo tính đúng đắn của chương trình.
14
Cấu trúc lệnh
Thuật toán
Chương trình
− Các thao tác được điều khiển bởi các lệnh
− Các đối tượng chịu thao tác được mô tả thông qua các Cấu trúc dữ liệ
Bài toán
15
T F
ifstatement (single selection)
T F
if…elsestatement (double selection)
T F
đa lựa chọn
(multiple selection)
T F
T F
.
Cấu trúc lựa chọn
Seq uenc e
.
Cấu trúc lệnh
Trang 5Lặp với điều kiện trước
T F
do while statement
T
F
for statement
Cấu trúc lệnh
Cấu trúc lặp
while statement
Lặp với điều kiện sau Lặp với số lần lặp cho trước
17
Cấu trúc lựa chọn trong C
true
false grade >= 60 print “Passed”
18
if ( grade >= 60 )
printf( "Passed\n");
else
printf( "Failed\n");
true false
grade >= 60
19
Sử dụng Toán tử điều kiện ( ?: )
trị nếu false)
printf( "%s\n", grade >= 60 ? "Passed" : "Failed" );
grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” );
Trang 6int product = 2;
while ( product <= 1000 )
product = 2 * product;
product <= 1000 product = 2 * product
true
false
21
Lặp được kiểm soát bằng biến đếm (Counter)
• Lặp đến khi “biến đếm” đạt được giá trị định trước
• Số lần lặp xác định: số lần lặp biết trước
• Ví dụ: Tính điểm trung bình của 10 sinh viên (giả sử điểm là số nguyên trong khoảng từ 0 đến 100).
• Viết giả lệnh: Đọc vào từ bàn phím 10 số nguyên mô tả điểm của sinh viên và tính trung bình cộng
Đặt total bằng 0 Đặt biến đếm counter bằng 1 While counter nhỏ hơn hoặc bằng 10 Input điểm của sinh viên tiếp theo Cộng điểm vừa đọc vào total Tăng biến counter lên 1 Tính điểm trung bình: lấy total chia cho 10 Print kết quả điểm trung bình
1 /* Fig 3.8: fig03_08.c
2 Class average program with sentinel-controlled repetition */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 int counter; /* number of grades entered */
9 int grade; /* grade value */
10 int total; /* sum of grades */
11
12 float average; /* number with decimal point for average */
13
14 /* initialization phase */
15 total = 0; /* initialize total */
16 counter = 0; /* initialize loop counter */
17
18 /* processing phase */
19 /* get first grade from user */
20 printf( "Enter grade, -1 to end: " ); /* prompt for input */
21 scanf( "%d", &grade ); /* read grade from user */
22
23 /* loop while sentinel value not yet read from user */
24 while ( grade != -1 ) {
25 total = total + grade; /* add grade to total */
26 counter = counter + 1; /* increment counter */
27
28 printf( "Enter grade, -1 to end: " ); /* prompt for input */
29 scanf("%d", &grade); /* read next grade */
30 } /* end while */
31
32 /* termination phase */
33 /* if user entered at least one grade */
34 if ( counter != 0 ) {
35
36 /* calculate average of all grades entered */
37 average = ( float ) total / counter;
38
39 /* display average with two digits of precision */
40 printf( "Class average is %.2f\n", average );
41 } /* end if */
42 else { /* if no grades were entered, output message */
43 printf( "No grades were entered\n" );
44 } /* end else */
45
46 return ; /* indicate program ended successfully */
47
48 } /* end function main */
Trang 7Program Output
Enter grade, -1 to end: 94
Enter grade, -1 to end: 97
Enter grade, -1 to end: 88
Enter grade, -1 to end: 70
Enter grade, -1 to end: 64
Enter grade, -1 to end: 83
Enter grade, -1 to end: 89
Enter grade, -1 to end: -1
Class average is 82.50
Enter grade, -1 to end: -1
No grades were entered
25
Lặp kiểm soát bởi biến điều khiển (cờ):
Ví dụ vấn đề đặt ra là:
• Viết chương trình tính điểm trung bình của một lớp học mà số điểm
đưa vào là tuỳ ý và khác nhau mỗi khi chạy chương trình.
• Không biết trước số sinh viên
• Làm thế nào để biết chương trình sẽ kết thúc ?
Sử dụng giá trị điều khiển (lính gác)
• Cũng có thể gọi là giá trị tín hiệu, giá trị cờ (flag)
• Thể hiện “kết thúc lặp, tính toán xong.”
• Kết thúc lặp khi người dùng nhập vào đúng giá trị cờ
• Giá trị cờ là giá trị được chọn trước, ngoài phạm vi các giá trị vào thông thường, chẳng hạn giá trị cờ đặt là: -1 thì kết thúc lặp.
26
Viết giả lệnh với 3 pha: Khởi tạo, Nhập và tính tổng điêm, Tính và in
giá trị trung bình
Gán cho total giá trị 0; Gán cho biến đếm counter giá trị 0
Input điểm đầu tiên ( có thể là cờ)
While người dùng chưa dựng cờ
Cộng điểm vào biến total
Tăng biến đếm lần nhập điểm lên 1
Input điểm của sinh viên tiếp theo (có thể là cờ)
If counter khác 0
Tính giá trị trung bình, chia total cho biến counter
Hiển thị giá trị trung bình
else
Hiển thị “Bạn đã không vào điểm”
1 /* Fig 3.8: fig03_08.c
2 Class average program with sentinel-controlled repetition */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 int counter; /* number of grades entered */
9 int grade; /* grade value */
10 int total; /* sum of grades */
11
12 float average; /* number with decimal point for average */
13
14 /* initialization phase */
15 total = 0; /* initialize total */
16 counter = 0; /* initialize loop counter */
17
18 /* processing phase */
19 /* get first grade from user */
20 printf( "Enter grade, -1 to end: " ); /* prompt for input */
21 scanf( "%d", &grade ); /* read grade from user */
22
23 /* loop while sentinel value not yet read from user */
24 while ( grade != -1 ) {
25 total = total + grade; /* add grade to total */
26 counter = counter + 1; /* increment counter */
27
Trang 829 scanf("%d", &grade); /* read next grade */
30 } /* end while */
31
32 /* termination phase */
33 /* if user entered at least one grade */
34 if ( counter != 0 ) {
35
36 /* calculate average of all grades entered */
37 average = ( float ) total / counter;
38
39 /* display average with two digits of precision */
40 printf( "Class average is %.2f\n", average );
41 } /* end if */
42 else { /* if no grades were entered, output message */
43 printf( "No grades were entered\n" );
44 } /* end else */
45
46 return ; /* indicate program ended successfully */
47
48 } /* end function main */
Enter grade, -1 to end: 75 Enter grade, -1 to end: 94 Enter grade, -1 to end: 97 Enter grade, -1 to end: 88 Enter grade, -1 to end: 70 Enter grade, -1 to end: 64 Enter grade, -1 to end: 83 Enter grade, -1 to end: 89 Enter grade, -1 to end: -1 Class average is 82.50 Enter grade, -1 to end: -1
No grades were entered
30
Bản chất của việc lặp được kiểm soát bởi biến đếm:
trị và có thể tăng lên hoặc giảm xuống
kiểm soát
31
Lệnh lặp for trong C
Trang 9 Lệnh lặp For có thể được viết thành lặp while như sau:
Khởi tạo ban đầu;
while ( Kiểm tra điều kiện tiếp tục lặp ) {
Các lệnh;
tăng biến điều khiển (biến đếm);
}
Khởi tạo ban đầu và việc thay đổi biến đếm
• Sử dụng dấu “,” trong danh sách biến điều khiển
• Ví dụ:
for (int i = 0, j = 0; j + i <= 10; j++, i++)
printf( "%d\n", j + i );
Trong đó: i, j cùng được khởi tạo ban đầu, làm điều kiện thoát khỏi vòng
lặp for, và được tăng giá trị sau mỗi lần lặp.
33
• Khởi tạo ban đầu, tiếp tục lặp lại, và việc thay đổi biến điều khiển có thể là các biểu thức số học Nếu x = 2 và y = 10
for ( j = x; j <= 4 * x * y; j += y / x )
thì viết tương đương:
for ( j = 2; j <= 80; j += 5 )
• Thay đổi biến điều khiển: tăng hoặc giảm.
• Nếu điều kiện tiếp tục lặp ngay từ ban đầu nhận giá trị false
– Khối lệnh trong vòng lặp không được thực hiện
– Cần kiểm soát xử lý với các lệnh tiếp sau lệnh for
34
switch
• Được sử dụng khi một biến hoặc biểu thức được kiểm tra tất cả các
giá trị có thể, mỗi giá trị tương ứng với những hành động khác nhau.
• Dãy các nhãn case và một trường hợp default khi value nằm ngoài tập
giá trị.
switch ( value ){
case '1':
C âu lệnh; break;
case '2':
C âu lệnh; break;
default:
C âu lệnh; break;
}
• break; thoát khỏi lệnh
35
true
false
.
case a case a action(s) break
case b case b action(s) break false
false case z case z action(s) break
true
true
default action(s)
Trang 102 Counting letter grades */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main()
7 {
8 int grade; /* one grade */
9 int aCount = 0; /* number of As */
10 int bCount = 0; /* number of Bs */
11 int cCount = 0; /* number of Cs */
12 int dCount = 0; /* number of Ds */
13 int fCount = 0; /* number of Fs */
14
15 printf( "Enter the letter grades.\n" );
16 printf( "Enter the EOF character to end input.\n" );
17
18 /* loop until user types end-of-file key sequence */
19 while ( ( grade = getchar() ) != EOF ) {
20
21 /* determine which grade was input */
22 switch ( grade ) { /* switch nested in while */
23
24 case 'A': /* grade was uppercase A */
25 case 'a': /* or lowercase a */
26 ++aCount; /* increment aCount */
27 break; /* necessary to exit switch */
28
29 case'B': /* grade was uppercase B */
30 case'b': /* or lowercase b */
31 ++bCount; /* increment bCount */
32 break; /* exit switch */
33
34 case'C': /* grade was uppercase C */
35 case'c': /* or lowercase c */
36 ++cCount; /* increment cCount */
37 break; /* exit switch */
38
39 case'D': /* grade was uppercase D */
40 case'd': /* or lowercase d */
41 ++dCount; /* increment dCount */
42 break; /* exit switch */
43
44 case'F': /* grade was uppercase F */
45 case'f': /* or lowercase f */
46 ++fCount; /* increment fCount */
47 break; /* exit switch */
48
49 case'\n': /* ignore newlines, */
50 case'\t': /* tabs, */
51 case' ': /* and spaces in input */
52 break; /* exit switch */
53
54 default: /* catch all other characters */
55 printf( "Incorrect letter grade entered." );
56 printf( " Enter a new grade.\n" );
57 break; /* optional; will exit switch anyway */
58 } /* end switch */
59
60 } /* end while */
61
62 /* output summary of results */
63 printf( "\nTotals for each letter grade are:\n" );
64 printf( "A: %d\n", aCount ); /* display number of A grades */
65 printf( "B: %d\n", bCount ); /* display number of B grades */
66 printf( "C: %d\n", cCount ); /* display number of C grades */
67 printf( "D: %d\n", dCount ); /* display number of D grades */
68 printf( "F: %d\n", fCount ); /* display number of F grades */
69
70 return ; /* indicate program ended successfully */
71
72 } /* end function main */
Enter the letter grades
Enter the EOF character to end input
a b c C A d f C E Incorrect letter grade entered Enter a new grade D
A b
^Z Totals for each letter grade are:
A: 3 B: 2 C: 3 D: 2 F: 1