You are on page 1of 48

Trịnh Tấn Đạt

Tuần 2
http://sites.google.com/site/ttdat88
Email: trinhtandat@sgu.edu.vn

9/11/2019 1
Nội dung
 Ôn tập Struct
 Hướng dẫn cách viết struct MyArray
 Hướng dẫn include các tập tin
 Sử dụng thư viện myArray cho bài toán Sort và Search
 Bài tập

9/11/2019 2
Thư viện trong C/C++
 Một chương trình có thể được chia nhỏ làm nhiều file, mỗi file chứa
một nhóm những hàm liên quan tới một phần của chương trình.
 Một số hàm có thể được dùng trong nhiều chương trình khác nhau
-> thư viện hàm.
 Một thư viện hàm gồm 2 phần:
 File header có đuôi .h chứa prototype các hàm có thể dùng được của thư
viện.
 File mã nguồn có đuôi .cpp chứa nội dung các hàm, hoặc file .obj, .lib
nếu đã được dịch ra các dạng tương ứng
 Dùng thư viện hàm trong một file mã nguồn:
 #include <ten_file.h> /* trong đường dẫn mặc định */
 #include "ten_file.h" /* cùng thư mục với file dịch */
 Dẫn hướng #include có tác dụng như chèn nội dung file được khai
báo vào file đang dịch ở vị trí xuất hiện

9/11/2019 3
Thư viện trong C/C++
❖ Trong file myArray.h
 Để tránh lỗi khi bị #include nhiều lần , thêm vào đầu và cuối
 #pragma once
 # ifndef __MYARRAY__
#define __MYARRAY_H__
/* Nội dung file myArray .h*/
# endif
 Sử dụng file myArray.h
#include "myArray.h" /* .h cùng thư mục tạo project */
#include <myArray.h > /* .h trong thư mục thư viện*/

9/11/2019 4
Thư viện trong C/C++
 Ví dụ: Xử lý xung đột thư viện khi chúng
ta #include nhiều thư viện
 Có một file A.h.

// File A.h

Source code A

9/11/2019 5
Thư viện trong C/C++
 Có các file B.h và C.h và 2 file này đều cần nội dung của
file A.h, vì thế #include "A.h" vào file B.h và C.h.

9/11/2019 6
Thư viện trong C/C++

9/11/2019 7
Thư viện trong C/C++
 Chúng ta có thể hình dung file main.cpp như sau:

9/11/2019 8
Thư viện trong C/C++
 Để khắc phục lỗi này thì tôi sử dụng chỉ
thị #ifndef, #define vào trong file A.h.
 Hoặc dùng #pragma once

// File A.h
#pragma once

Source code A

9/11/2019 9
9/11/2019 10
Thư viện trong C/C++
 dientich.h // dinh nghia header file
#ifndef __DIENTICH_H__
#define __DIENTICH_H__
#include <stdio.h>
#include <math.h>
double dt_tron(double r);
double dt_elip(double r1, double r2);
double dt_vuong(double l);
double dt_chu_nhat(double l1, double l2);
#endif
9/11/2019 11
Thư viện trong C/C++
Hoặc dùng #pragma once
 dientich.h // dinh nghia header file
#pragma once
#include <stdio.h>
#include <math.h>
double dt_tron(double r);
double dt_elip(double r1, double r2);
double dt_vuong(double l);
double dt_chu_nhat(double l1, double l2);
9/11/2019 12
Thư viện trong C/C++
 dientich.cpp // dinh nghia source file cpp
#include "dientich.h"
const double PI = 3.1415;
double dt_tron(double r)
{ return r*r*PI; }
double dt_elip(double r1, double r2)
{ return r1*r2*PI; }
double dt_vuong(double l)
{ return l*l; }
double dt_chu_nhat(double l1, double l2)
{ return l1*l2; }

9/11/2019 13
9/11/2019 14
9/11/2019 15
9/11/2019 16
9/11/2019 17
9/11/2019 18
9/11/2019 19
9/11/2019 20
Struct: myArray
 Tạo project mới trong Visual Studio.
 Tạo header file MyArray.h và các source file
MyArray.cpp, MainProg.cpp
 Tạo file input.txt có nội dung như sau và lưu vào thư mục
chứa project
 Dòng đầu tiên là số phần tử của mảng, n.
 Dòng thứ hai, bao gồm n số nguyên, mỗi số cách nhau ít
nhất một khoảng trắng.

9/11/2019 21
Struct: myArray

9/11/2019 22
9/11/2019 23
9/11/2019 24
#pragma once
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
// Dinh nghia cau truc Array
myArray.h
struct MyArray {
int *a;
int num;
};
int initArray(MyArray &); // khoi tao empty Array
MyArray readArrayFromFile(char *); // Doc du lieu tu file text va luu tru vao
Array
int writeToFile(MyArray, char *); // Ghi du lieu Array ra file
int printArray(MyArray); // in Array ra man hinh
int deleteArray(MyArray); // Xoa Array (don dep vung nho')
MyArray copyArray(MyArray); // Copy full Array
MyArray copyArray(MyArray, int, int); // Copy 1 phan cua Array dua vao index
left to right
9/11/2019 25
9/11/2019 26
myArray.cpp
#include "myArray.h"
int initArray(MyArray &ma) {
//..
}

MyArray readArrayFromFile(char *st) {


// …
}

9/11/2019 27
Ví dụ
int initArray(MyArray &ma) {
ma.a = ;
ma.num = ;
return 0 ;
}

9/11/2019 28
MyArray readArrayFromFile(char *st) {
FILE *fp;
MyArray temp;
int buf;
initArray(temp);
fopen_s(&fp, st, "rt"); // Visual studio: dung fopen_s thay vi fopen de mo file
if (!fp) {
printf("Cannot open file...");
return temp;
}
fscanf_s(fp, "%d", &temp.num); // Visual studio: dung fscanf_s thay vi fscanf de doc tung dong` trong file
if (temp.num <= 0) {// B ạ n ph ả i ki ể m tra tính h ợ p l ệ c ủ a s ố ph ầ n t ử
fclose(fp);
temp.num = 0;
printf("Data wrong...");
return temp;
}
temp.a = new int[temp.num];
if (!temp.a) {// Ki ể m tra có đ ủ b ộ nh ớ đ ể t ạ o đư ợ c m ả ng t ạ m hay không
fclose(fp);
temp.num = 0;
printf("Not enough memory for allocating...");
return temp;
}
for (int i = 0;i < temp.num;i++) {// Đ ọ c t ừ ng ph ầ n t ử vào m ả ng t ạ m đã t ạ o
// … code here
// … code here
}
fclose(fp); // Đóng tập tin
return temp; / Trả k ết quả
}

9/11/2019 29
Ví dụ:
int writeToFile(MyArray as, char *st) {
FILE *fp;
fopen_s(&fp, st, "wt");
if (!fp) {
printf("Cannot create new file to write data...");
return -1;
}
fprintf(fp, "%d\n", as.num);
for (int i = 0;i < as.num;i++)
fprintf(fp, "%d ", as.a[i]);
fclose(fp);
return 0;
}
9/11/2019 30
Ví dụ:
int printArray(MyArray ma)
{
// code here
return 0;
}

9/11/2019 31
Ví dụ:
int deleteArray(MyArray ma)
{
// code here
return 0;
}

9/11/2019 32
Ví dụ:
MyArray copyArray(MyArray as) {
MyArray ad;
initArray(ad);
if (as.num > 0 && as.a != NULL)
ad.a = new int[as.num];
if (ad.a != NULL)
{
ad.num = as.num;
for (int i = 0;i < as.num;i++)
ad.a[i] = as.a[i];
}
return ad;
}
9/11/2019 33
Ví dụ:
MyArray copyArray(MyArray as, int left, int right) {
MyArray ad;
initArray(ad);
ad.a = new int[right - left + 1];
if (ad.a != NULL)
{
ad.num = right - left + 1;
for (int i = 0;i < ad.num;i++)
ad.a[i] = as.a[i + left];
}
return ad;
}

9/11/2019 34
#include "myArray.h“
int main() {

char file_in[] = "File2.txt";


char *input = file_in;

MyArray ma;
MyArray mb;

initArray(ma);
initArray(mb);

ma = readArrayFromFile(input);
printf("\n Input:\n ");
printArray(ma);
mb = copyArray(ma);
printArray(mb);

deleteArray(ma);
deleteArray(mb);

system("pause");
return 0;
9/11/2019
} 35
Sơ đồ liên kết các tập tin

9/11/2019 36
9/11/2019 37
9/11/2019 38
9/11/2019 39
9/11/2019 40
mySort.h
#pragma once
#include "myArray.h"

int check(MyArray, int, int); // kiem tra hai so left va


right de sap xep tang dan
int swap(MyArray, int, int); // doi cho hai so swap
values
int interchangeSort(MyArray); // ham sap xep dua vao
ky thuat doi cho truc tiep

9/11/2019 41
#include "mySort.h“
int check(MyArray ad, int i, int j) {
// … mySort.cpp
}
int swap(MyArray ad, int i, int j) {
// . .
}
int interchangeSort(MyArray ad) {
int i, j;
for (i = 0;i < ad.num - 1;i++)
for (j = i + 1;j < ad.num;j++)
if (check(ad, i, j))
swap(ad, i, j);
return 0;
} 9/11/2019 42
Ví dụ:
int check(MyArray ad, int i, int j) {
if (ad.a[i] <= ad.a[j])
return 0;
else
return 1;
}

9/11/2019 43
Ví dụ
int swap(MyArray ad, int i, int j) {
int temp = ad.a[i];
ad.a[i] = ad.a[j];
ad.a[j] = temp;
return 0;
}

9/11/2019 44
#include "myArray.h"
#include "mySort.h“
int main() {
char file_in[] = "File2.txt";
char *input = file_in; Main.cpp
MyArray ma;
MyArray mb;
initArray(ma);
initArray(mb);
ma = readArrayFromFile(input);
printf("\n Input:\n ");
printArray(ma);
mb = copyArray(ma);

interchangeSort(mb);
printf("\n \n Mang sau khi sap xep tang dan: \n");
printArray(mb);

deleteArray(ma);
deleteArray(mb);

system("pause");
return 0;
}
9/11/2019 45
Bài tập 1:
 Tạo file header cho mySearch.h và source file
mySearch.cpp
 Cài đặt hai thuật toán tìm kiếm tuyến tính và tìm kiếm
nhị phân

 Hint (hint_Search folder) : xem gợi ý trong file


mySearch.h và mySearch.cpp

9/11/2019 46
Bài tâp 2:
 Trong file mySort.h và mySort.cpp
 Cài đặt thêm các thuật toán sắp xếp đã được học trong
lớp lý thuyết.
 Ví dụ:
 Bubble Sort
 Insert Sort
 Select Sort
 Quick Sort
 Merge Sort
 ….

9/11/2019 47
Bài tập 3
 Trong file myArray.h và myArray.cpp
 Hãy viết hàm tìm
a. Phần tử trung vị (median) của mảng.
b. Phần tử lớn nhất (max) của mảng.
c. Phần tử nhỏ nhất (min) của mảng.
d. Các phần tử là số nguyên tố

9/11/2019 48

You might also like