TRƯỜNG ĐẠI HỌC CÔNG NGHIỆP THÀNH PHỐ HỒ CHÍ MINH
Kỹ thuật lập trình Programming Fundamentals
Ngô Hữu Dũng
Bài 1 – Giới thiệu
Blog: http://monktlt.blogspot.com/ Group: https://www.facebook.com/groups/monktlt
2 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Nội dung
Giải thuật đệ quy Quy nạp toán học Lập trình đệ quy Kiểu dữ liệu cấu trúc Kiểu cấu trúc - struct Kiểu hợp - union
Biến động và kiểu con trỏ
Biến động và tĩnh Kiểu con trỏ - pointer
Kiểu tập tin và vào ra dữ liệu
Các thao tác với tập tin
3 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Tài liệu Slide bài giảng Brian W. Kernighan and Dennis M. Ritchie. The C Programming Language
(Second Edition). Prentice-Hall. Englewood Cliffs, New Jersey, 1988.
Bản dịch tiếng Việt: Giáo trình Ngôn ngữ lập trình C Bài tập thực hành Tham khảo thêm
Phạm Văn Ất. Kỹ thuật lập trình C. NXB Khoa học và Kỹ thuật, 1995 Randal E.Bryant and David R.O’Hallaron. Computer’s Perspective, 2001 Bjarne Stroustrup. The C++ Programming Language, AT&T Labs Murray Hill, New
Jersey Addison-Wesley, 1997.
Andy Oram and Greg Wilson, Beautiful Code, 2007 cplusplus.com
4 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Lịch trình
Tuần
Nội dung
Lý thuyết Thực hành
Thảo luận
Tự học
2 4
1 Quy nạp toán học và Lập trình đệ quy 2 2 4
3 2 4
4 2 3 6
Kiểu cấu trúc và ứng dụng
5 3 2 (Bài tập) 5
6 2 3 6
7 2 3 6
8 2 3 6
9 2 3 5
Biến động và kiểu con trỏ
10 3 2 (Bài tập) 5
11 2 3 5
12 2 3 5
13 2 (Bài tập) 5
Kiểu tập tin, vào/ra dữ liệu
14 2 3 5
15 2 4
5 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Kiểm tra đánh giá
Kiểm tra thường kỳ: 20% Kiểm tra giữa kỳ: 30% Kiểm tra cuối kỳ: 50%
Số tín chỉ: 4 (60 tiết)
Lý thuyết: 24 Thảo luận: 6 Thực hành: 30
6 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Ôn tập qua một số ví dụ
1. #include
2. #include
/*Tập tin header*/ /*Tập tin header*/
//Chương trình chính
3. int main() 4. { 5.
//Khai báo biến
6.
7.
8.
9.
10.
int x, y, z; x = 5; y = 10; z = x + y; printf("Tong la %d", z); getch(); return 0;
11. 12. }
7 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
/* PI là hằng số */
Hằng số
1. #include
2. #define PI 3.14
3. int main()
4. {
5.
6.
// Bán kính hình tròn // Diện tích hình tròn
7.
8.
9.
10.
11.
int r; float S; printf("Nhap ban kinh hinh tron: "); scanf("%d", &r); S = PI * r * r; printf("Dien tich hinh tron = %f\n", S); printf("Chu vi hinh tron = %f", 2*r*PI); return 0;
12. 13. }
8 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Hoán vị – Dùng biến tạm
1. #include
// Khai báo thư viện
2. int main() 3. { 4.
// temp là biến tạm
5.
6.
7.
// Hoán vị dùng biến tạm
8.
9.
10.
int x, y, temp; x = 10;y = 60; printf("x = %d, y = %d.\n",x, y); temp = x; x = y; y = temp; printf("Hoan vi: x = %d, y = %d.",x, y); return 0;
11. 12. }
9 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Hoán vị – Không dùng biến tạm
1. #include
// Khai báo thư viện
2. int main() 3. { 4.
// Không dùng biến tạm
5.
6.
7.
8.
9.
10.
int x, y; x = 10;y = 60; printf("x = %d, y = %d.\n",x, y); x = x + y; y = x - y; x = x - y; printf("Hoan vi: x = %d, y = %d.",x, y); return 0;
11. 12. }
10 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Toán tử quan hệ
1. #include
// Khai báo thư viện
2. int main() 3. { 4.
5.
6.
7.
8.
9.
10.
int a, b, c = 10; printf("Nhap a:"); scanf("%d",&a); printf("Nhap b:"); scanf("%d",&b); if (a<=b) printf("a nho hon hoac bang b.\n"); if (a>b) printf("a lon hon b.\n"); if (a==c) printf("a bang c.\n"); if (a!=c) printf("a khac c.\n"); return 0;
11. 12. }
11 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Toán tử luận lý
1. #include
2. int main()
3. {
4.
5.
6.
int a, b = 10; printf("a = "); scanf("%d",&a); if (a >= 10 && a < 100)
7.
printf("a là số có 2 chữ số.\n");
8.
else
9.
printf("a không phải là số có 2 chữ số.\n");
10.
(a > b || a < b)?printf("a khác b."):printf("a bằng b."); return 0;
11. 12. }
12 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Toán tử tăng, giảm
1. #include
2. int main()
3. {
4.
5.
// Tăng trước
6.
7.
// Tăng sau
8.
9.
// Giảm trước
10.
11.
// Giảm sau
12.
int a, b = 10; a = ++b; printf("a = %d, b = %d\n", a, b); a = b++; printf("a = %d, b = %d\n", a, b); a = --b * 2; printf("a = %d, b = %d\n", a, b); a = b-- * 2; printf("a = %d, b = %d\n", a, b); return 0;
13. 14. } 13
Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Toán tử trên bit
#include
int main()
{
// 0011 0100 // 0001 1101
// Phép AND 0001 0100
// Phép OR 0011 1101
// Phép XOR 0010 1001
// Phép NOT 1100 1011
// Dịch trái 1101 0000
// Dịch phải 0000 1101
int a = 52; int b = 29; int c; c = a & b; printf("%d & %d = %d\n", a, b, c); c = a | b; printf("%d | %d = %d\n", a, b, c); c = a ^ b; printf("%d ^ %d = %d\n", a, b, c); c = ~a; printf("~%d = %d\n", a, c); c = a << 2; printf("%d << 2 = %d\n", a, c); c = a >> 2; printf("%d >> 2 = %d\n", a, c); return 0;
}
14 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Toán tử gán
int a = 5, b; b = a++; printf("b = a++; a = %d, b = %d. \n", a, b); b += a; printf("b += a; a = %d, b = %d. \n", a, b); b -= a; printf("b -= a; a = %d, b = %d. \n", a, b); b *= a; printf("b *= a; a = %d, b = %d. \n", a, b); b /= a; printf("b /= a; a = %d, b = %d. \n", a, b); b = 40; b %= a; printf("b %= a; a = %d, b = %d. \n", a, b); b &= a; printf("b &= a; a = %d, b = %d. \n", a, b); b |= a; printf("b |= a; a = %d, b = %d. \n", a, b); b ^= a; printf("b ^= a; a = %d, b = %d. \n", a, b); return 0;
1. #include
2. int main()
3. {
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16. }
15 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Mảng – dãy số
1. #include
2. int main()
3. {
4.
// Dãy số gồm 5 phần tử
5.
6.
// Phần tử đầu tiên: mang[0]
7.
8.
9.
10.
11.
int mang[5]; int i = 3; mang[0] = 1; mang[1] = mang[0] + 1; mang[2] = mang[0] + mang[1]; mang[i++] = mang[i-2] + mang[i-1]; mang[i] = mang[i-2] + mang[i-1]; printf("%d",mang[4]); return mang[4];
mang[0]
mang[1]
mang[2]
mang[3]
mang[4]
12. 13. }
1
?
?
?
?
Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng 16
Mảng (tiếp theo)
1. #include
2. int main()
3. {
4.
5.
6.
7.
8.
9.
10.
float diemky1[3] = {5.9, 8.6, 7.4}; float diemky2[] = {6.3, 8.5, 6.3}; float toan, ly, hoa; toan = (diemky1[0] + diemky2[0])/2; ly = (diemky1[1] + diemky2[1])/2; hoa = (diemky1[2] + diemky2[2])/2; printf("Diem tong ket: %.1f",(toan+ly+hoa)/3); return 0;
11. 12. }
17 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Mảng đa chiều
Học kỳ 1 (cột 0) Học kỳ 2 (cột 1)
Toán (hàng 0) diem[0][0]=5.9
diem[0][0]=6.3
Lý (hàng 1)
diem[0][0]=8.6
diem[0][0]=8.5
Hóa (hàng 2)
diem[0][0]=6.4
diem[0][0]=5.3
1. #include
2. int main()
3. {
4.
5.
6.
7.
8.
9.
10.
float diem[3][2] = {{5.9,6.3},{8.6,8.5},{6.4,5.3}}; float toan, ly, hoa, tongket; toan=(diem[0][0]+diem[0][1])/2; ly=(diem[1][0]+diem[1][1])/2; hoa=(diem[2][0]+diem[2][1])/2; tongket = (toan+ly+hoa)/3; printf("Diem tong ket: %.1f.",tongket); return 0;
11. 12. }
Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng 18
'C'
'h'
'a'
'o'
'!'
'\0'
Chuỗi ký tự
1. #include
2. #include
3. int main()
4. {
5.
6.
7.
8.
9.
10.
11.
12.
char ten [30]; char gioithieu[50] = "Toi ten la "; char chao[6] = {'C','h','a','o','!','\0'}; printf("Nhap ten cua ban: "); scanf("%[^\n]s",&ten); strcat(gioithieu,ten); printf("%s \n%s \n", chao, gioithieu); printf("Ten cua toi co %d ky tu.",strlen(ten)); return 0;
13. 14. }
Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng 19
Kiểu liệt kê - enum
1. #include
2. enum week {SUN, MON, TUE, WED, THU, FRI, SAT};
3. int main()
4. {
5.
6.
7.
8.
9.
10.
11.
enum week today; int date=5; today = TUE; printf("Hôm nay là "); (today==0)?printf("CN"):printf("thứ %d",today+1); printf("\nNgày hẹn là "); if (date==SAT||date==SUN)
12.
printf("cuối tuần.");
13.
else
14.
printf("ngày trong tuần.");
return 0;
15. 16. }
20 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
else if
enum week today=WED; printf("Hom nay la "); if (today==MON) printf("thu hai."); else if (today==TUE)
printf("thu ba.");
else if (today==WED)
printf("thu tu.");
else if (today==THU)
printf("thu nam.");
else if (today==FRI)
printf("thu sau.");
else printf("cuoi tuan."); return 0;
1. #include
2. enum week {SUN, MON=2, TUE, WED, THU, FRI, SAT};
3. int main()
4. {
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18. }
21 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
// Lấy thời gian hiện tại
// Week day từ 0 đến 6.
time_t t = time(NULL); struct tm *time = localtime(&t); // Đưa vào tm struct int today = time->tm_wday; printf("Hom nay la "); switch (today) {
case 1: printf("thu hai.");break; case 2: printf("thu ba.");break; case 3: printf("thu tu.");break; case 4: printf("thu nam.");break; case 5: printf("thu sau.");break; default: printf("cuoi tuan.");
} return 0;
Switch
1. #include
2. #include
3. int main()
4. {
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19. }
22
Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Switch (tiếp theo)
1. #include
2. int main()
3. {
4.
5.
6.
int m = 1; switch(m) {
7.
8.
9.
10.
case 0: printf("Zero\n"); case 1: printf("One\n"); case 2: printf("Two\n"); case 3: printf("Three\n");
11.
12.
} printf("m = %d\n", m); return 0;
13. 14. }
23 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
For
1. #include
2. int main()
3. {
4.
5.
6.
7.
int a[10] = {1,2,3,4,5,-6,7,-8,9,10}; int i, tong = 0; for (i=0; i<5; i++) {
8.
tong = tong + a[i];
9.
10.
} printf("Tong = %d", tong); return 0;
11. 12. }
24 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
For (tiếp theo)
1. #include
2. #include
3. int main()
4. {
5.
6.
7.
8.
char string[20]="Lap trinh C",c;
int i,j;
for (i=0,j=strlen(string)-1;i
9.
10.
11.
c=string[i];
string[i]=string[j];
string[j]=c;
12.
13.
}
printf("%s",string);
return 0;
14.
15. }
25 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
While
1. #include
2. int main()
3. {
4.
5.
6.
7.
int a[10] = {1,2,3,4,5,-6,7,-8,9,10};
int i = 0, tong = 0;
while (i<8)
{
8.
9.
tong = tong + a[i];
i++;
10.
11.
}
printf("Tong = %d",tong);
return 0;
12.
13. }
26 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Do while
1. #include
2. int main()
3. {
4.
5.
int a;char c;
do{
6.
7.
printf("Nhap vao mot so chan: ");
scanf("%d",&a);
8.
9.
}while (a%2!=0);
do{
10.
scanf("%c",&c);
11.
}while (c!='e');
return 0;
12.
13. }
27 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Break
1. #include
2. #define n 10
3. int main()
4. {
5.
6.
7.
8.
int i, tong = 0;
int a[n] = {1,2,3,4,5,-6,7,-8,9,10};
for (i = 0;i < n;i++)
{
9.
if (a[i]<0)
10.
11.
break;
tong = tong + a[i];
12.
13.
}
printf("Tong = %d",tong);
return 0;
14.
15. }
28 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Continue
1. #include
2. #define n 10
3. int main()
4. {
5.
6.
7.
8.
int i, tong = 0;
int a[n] = {1,2,3,4,5,-6,7,-8,9,10};
for (i = 0;i < n;i++)
{
9.
if (a[i]<0)
10.
continue;
11.
tong = tong + a[i];
12.
13.
}
printf("Tong = %d",tong);
return 0;
14.
15. }
29 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Hết bài 1
Giới thiệu
Nội dung
Tài liệu
Lịch trình
Kiểm tra
Blog: http://monktlt.blogspot.com/
Group: https://www.facebook.com/groups/monktlt
Ôn tập qua ví dụ
30 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
9.
10.
11.
c=string[i]; string[i]=string[j]; string[j]=c;
12.
13.
} printf("%s",string); return 0;
14. 15. }
25 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
While
1. #include
2. int main()
3. {
4.
5.
6.
7.
int a[10] = {1,2,3,4,5,-6,7,-8,9,10}; int i = 0, tong = 0; while (i<8) {
8.
9.
tong = tong + a[i]; i++;
10.
11.
} printf("Tong = %d",tong); return 0;
12. 13. }
26 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Do while
1. #include
2. int main()
3. {
4.
5.
int a;char c; do{
6.
7.
printf("Nhap vao mot so chan: "); scanf("%d",&a);
8.
9.
}while (a%2!=0); do{
10.
scanf("%c",&c);
11.
}while (c!='e'); return 0;
12. 13. }
27 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Break
1. #include
2. #define n 10
3. int main()
4. {
5.
6.
7.
8.
int i, tong = 0; int a[n] = {1,2,3,4,5,-6,7,-8,9,10}; for (i = 0;i < n;i++) {
9.
if (a[i]<0)
10.
11.
break; tong = tong + a[i];
12.
13.
} printf("Tong = %d",tong); return 0;
14. 15. }
28 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Continue
1. #include
2. #define n 10
3. int main()
4. {
5.
6.
7.
8.
int i, tong = 0; int a[n] = {1,2,3,4,5,-6,7,-8,9,10}; for (i = 0;i < n;i++) {
9.
if (a[i]<0)
10.
continue;
11.
tong = tong + a[i];
12.
13.
} printf("Tong = %d",tong); return 0;
14. 15. }
29 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng
Hết bài 1 Giới thiệu
Nội dung
Tài liệu
Lịch trình
Kiểm tra
Blog: http://monktlt.blogspot.com/ Group: https://www.facebook.com/groups/monktlt
Ôn tập qua ví dụ
30 Kỹ thuật lập trình | DHTH11C | HK1 | 2016-2017 Ngô Hữu Dũng