Professional Documents
Culture Documents
Cc
Cc
Cc
MỤC LỤC
I, Cấu trúc......................................................................................................................................2
1) Switch case..........................................................................................................................2
2) Enum...................................................................................................................................2
3) Struct...................................................................................................................................2
4) Pointer.................................................................................................................................4
5) Malloc , calloc, realloc.......................................................................................................7
6) Array...................................................................................................................................7
7) Read_Write........................................................................................................................8
8) String...................................................................................................................................9
9) Class in C++.......................................................................................................................9
10) Operator overloading......................................................................................................10
11) Template...........................................................................................................................13
12) Inheritance........................................................................................................................15
13) Friend in class..................................................................................................................17
14) Phép toán BIT..................................................................................................................22
II, HÀM..................................................................................................................................22
1) Chuyển số sang hệ nhị phân...........................................................................................22
2) Xử lí xâu kí tự...................................................................................................................22
III, NOTE................................................................................................................................23
1) Bảng key Read write file.................................................................................................23
2) Bảng mã ASCII................................................................................................................24
3) Cách sử lý dấu xuống dòng.............................................................................................24
IV, Các dạng bt thường gặp..................................................................................................24
1) Tìm UCLN, BCNN...........................................................................................................24
1
Lập trình C/C++ ~ VU DINH LUC 2
I, Cấu trúc
1) Switch case
int main(){
int grade = 5;
switch (grade){
case 4: printf("exellent\n");break;
case 3:
printf("good\n");break;
default:
printf("illegal grade\n");break;
}
}
2) Enum
int main(){
3) Struct
#include <stdio.h>
#include <string.h>
struct born{
int date;
int month;
int year;
};
typedef struct born born;
struct sinhvien{
char name[50];
born bd;
float gpa;
};
2
Lập trình C/C++ ~ VU DINH LUC 3
sv nhap(){
sv x;
getchar();
printf("Name: "); gets(x.name);
printf("Date: "); scanf("%d", &x.bd.date);
printf("Month: "); scanf("%d", &x.bd.month);
printf("Year: "); scanf("%d", &x.bd.year);
printf("GPA: "); scanf("%f", &x.gpa);
return x;
}
void xuat(sv x){
float m =x.gpa;
printf("\n-------------------------------------\n");
printf("Name: %s\n",x.name);
printf("Date of birth: %d/%d/%d\n", x.bd.date, x.bd.month, x.bd.year);
printf("GPA: %.2f\n", x.gpa);
if (m==0) printf("Rank: F\n");
else if (m>0&&m<1) printf("Rank: D\n");
else if (m>=1&&m<2) printf("Rank: C\n");
else if (m>=2&&m<3) printf("Rank: B\n");
else if (m>=3&&m<=4) printf("Rank: A\n");
else printf("Du lieu sai vui long nhap lai!\n");
}
int main()
{
// sv svtest = {"vudinhluc",3.33}; // Gán giá trị cho struc thông thường
// printf("sv test: %s, %.2lf\n\n", svtest.name,svtest.gpa);
// hoac strcpy(svtest.name, "Vu Dinh Luc");
3
Lập trình C/C++ ~ VU DINH LUC 4
printf("\n==============================================\n");
printf("Sinh vien %d:\n",i+1);
sv x = nhap();
xuat(x);
}
printf("##################################################\n\n\n");
return 0;
}
4) Pointer
#include <stdio.h>
#include <stdlib.h>
void tang(int *a) // hàm tăng 2 có tham số là 1 con trỏ kiểu int có tên là a
{
*a+=100; // toán tử giải tham chiếu
}
int main()
{
/// ############################### BASIC POINTER
################################
int a = 100, b = 100; // khai bao gia tri bien
int *pta, *ptb ; // khai bao con tro kieu int a
pta = &a; // khoi tao gia tri cho con tro
ptb = &b;
printf("Value: a= %d, b=%d\n",a,b);
printf("Location: pta = %d, ptb = %d\n", &a,&b);
*ptb = 200; // thay doi gia tri bien bang con tro
tang(&a);
tang(&b);
printf("Value after change a=%d, b=%d\n", *pta,*ptb); // giải tham chiếu (*pta mang
giá trị tại vi tri pta)
swap(&a,&b);
printf("Location after swapping: a = %d, b = %d\n", a,b);
4
Lập trình C/C++ ~ VU DINH LUC 5
int n = 5;
for (int i=0;i<n;i++)
{
printf("%d ", ptr[i]);
}
for (int i=0; i<n;i++)
{
scanf("%d", &ptr[i]);
}
for (int i;i<n;i++)
{
printf("%d ", ptr[i]);
}
printf("\n");
for (int i=0;i<n;i++)
{
*(ptr+i) = 10*i;
}
for (int i=0;i<n;i++)
{
5
Lập trình C/C++ ~ VU DINH LUC 6
/* xin cấp phát thêm cho vùng nhớ có sẵn của malloc và calloc mà không xóa bỏ giá
trị có sẵn*/
// for example:
int a = 100;
int *ptr = &a; // con trỏ ptr lưu vị trí của biến a
int **ptr2 = &ptr; // con trỏ ptr2 lưu vị trí của biến ptr mang vị trí của biến a
6
Lập trình C/C++ ~ VU DINH LUC 7
#define N 10
// const int N = 10;
int main()
{
// KHAI BÁO: <type> [tên mảng][số lượng phần tử];
int a[10]; //=> mảng a gồm 10 phần tử
int b[10] = {1,2,3,4,5,6,7,8,9,10}; // {1,2,3,4,5,6,7,8,9,10}
int b2[10] = {1,2,3,4,5}; //=> {1,2,3,4,5,0,0,0,0,0}
int c[10] = {0}; //=> {0,0,0,0,0,0,0,0,0,0}
int d[] = {1,2,3,4,5,6,7,8,9,10};
7
Lập trình C/C++ ~ VU DINH LUC 8
printf("\n");
return 0;
}
7) Read_Write
int main()
{
// CÚ PHÁP:
// FILE *f;
// f = fopen ("ten_file", "mode");
//!!!!! LƯU Ý : - Nếu chỉ đọc tên file thì file input phải cùng thư mục với file
chương trình và file output
// - fscan() sẽ đọc từng dòng một
// - dùng "a" thay vì "w" để ghi dữ liệu mà không xóa dữ liệu cũ
if((f == NULL)||(g == NULL)){ // Kiểm tra file input output có đọc được không
printf("Cannot open file !\n");}
else{
int a;
fscanf(f,"%d",&a); // đọc 1 số ở input.txt rồi ghi ở output.txt
fprintf(g,"So ban nhap la:%d\n",a);
int a1; // Đọc nhiều dữ liệu theo nhiều kiểu cùng lúc
int a2;
char xau[100];
fscanf(f,"%d %d %s", &a1,&a2,&xau);
fprintf(g,"%d %d %s\n", a1,a2,xau);
int x; // Đọc dữ liệu với số lượng cho tước
for (int i=0;i<10;i++)
{
fscanf(f,"%d ",&x);
fprintf(g,"%d ",x);
}
fprintf(g,"\n");
8
Lập trình C/C++ ~ VU DINH LUC 9
8) String
// chuỗi kết thúc bằng ký tự null kh mang độ dài của xâu
char a[10] = {'a','b','\0'};
printf("do dai xau la: %d\n", strlen(a));
int n;
n=2; //scanf("Moi ban nhap n:%d",&n);
// scanf("\n"); // sau khi scanf() muốn gets() hay fgets() thì phải đọc dấu enter
để tránh trôi lệnh
gets(a);
9) Class in C++
#include <iostream>
#include <strings.h>
using namespace std;
class Student {
private:
char name[30];
char mssv[8];
float GPA;
public:
Student(){
name[0] = '\0';
mssv[0] = '\0';
GPA = 0;
}
Student(char name[30]){
strcpy_s(this->name, 30, name);
strcpy_s(this->mssv, 8, '\0');
GPA = 0;
}
Student(char mssv[8]){
9
Lập trình C/C++ ~ VU DINH LUC 10
strcpy_s(this->mssv, 8, mssv);
strcat_s(this->name, 30, '\0');
GPA = 0;
}
Student(float GPA){
this->GPA = GPA;
strcpy_s(this->mssv, 8, '\0');
strcat_s(this->name, 30, '\0');
}
Student(char name[30], char mssv[8], float GPA){
this->GPA = GPA;
strcpy_s(this->mssv, 8, mssv);
strcat_s(this->name, 30, name);
}
char* getname(){return name;}
char* getmssv(){return mssv;}
float getGPA(){return GPA;}
~Student(){cout << "Destructor" << endl;}
};
int main(){
Student s0;
Student s1("20233513");
printf("%s", s1.getmssv());
}
10
Lập trình C/C++ ~ VU DINH LUC 11
// 2. Parameterized contructor
Circle(float xc, float yc, float rc)
{
cout << "Parameterized contructor\n";
x = xc;
y = yc;
r = rc;
}
// 3. Copy contructor
Circle(const Circle& c)
{
cout << "Copy contructor\n";
x = c.x;
y = c.y;
r = c.r;
}
// Destructor
~Circle()
{
cout << "Destructor\n";
}
float getX();
float getY();
float getR();
// Operator
Circle operator +(Circle &c)
{
Circle res;
res.x = (this->x + c.x) / 2;
res.y = (this->y + c.y) / 2;
res.r = this->r + c.r;
return res;
}
Circle operator -(Circle &c)
{
Circle res;
11
Lập trình C/C++ ~ VU DINH LUC 12
12
Lập trình C/C++ ~ VU DINH LUC 13
11) Template
#include <iostream>
using namespace std;
template < class T>
int compare (T a, T b)
{
return a>b?1:0;
}
template <class T>
T sum (T a, T b)
{
return a+b;
}
template <class A, class B, class C>
C func(A va, B vb)
{
C vc;
return vc;
}
template<class A>
void swap(A *a, A *b)
{
A temp =*a;
*a=*b;
*b=temp;
cout<<*a<<" "<<*b<<endl;
}
template <class T>
T nmax(T a,T b)
{
return a>b?a:b;
}
template <class T>
T nmax(T a,T b, T c)
{
T d = nmax<T>(a,b);
return nmax(d,c);
}
template <class T>
T nmax(T arr[], int N)
{
int M=arr[0];
for (int i=0;i<N;i++)
{
M=nmax(M,arr[i]);
13
Lập trình C/C++ ~ VU DINH LUC 14
}
return M;
}
template <class T>
class Array
{
private:
T* arr;
int N;
public:
Array(){arr=NULL; N=0;}
14
Lập trình C/C++ ~ VU DINH LUC 15
// cout<<c<<endl<<d<<endl;
// int ar[]={1,2,3,4,5,6,7,8,9,10};
// int n=sizeof(ar)/sizeof(int);
// cout<<nmax(ar,n)<<endl;
char ca = 'a';
char cb = 'b';
swap(ca,cb);
printf("%c %c", ca,cb);
return 0;
}
12) Inheritance
#include <iostream>
#include <string.h>
using namespace std;
class Employee {
private:
float salary;
char* name;
public:
Employee(){salary = 0; name[0] = '\0';}
Employee(float s, char* n){salary = s; strcpy_s(name, 99, n);}
Employee(const Employee& E){
salary = E.salary;
strcpy_s(name, 99, E.name);
}
15
Lập trình C/C++ ~ VU DINH LUC 16
}
// ~Worker();
void doWork() {}
void show(){printf("%.2f", getsalary());}
};
class Shape {
public:
void draw(){
cout << "Shape::draw\n";
}
void erase(){cout << "Shape::erase\n";}
void redraw(){erase() ; draw();}
};
int main(){
// Worker w1;
// w1.doWork();
// w1.getsalary();
// w1.pay();
// w1.show();
// Employee e1 = w1; // có thể ép kiểu mẹ thành con
// Worker w2 = e1; //=> ERROR
Circle c1;
c1.draw();
c1.erase();
return 0;
}
16
Lập trình C/C++ ~ VU DINH LUC 17
//class String
//{
// private:
// char *str;
// public:
// String()
// {
// str = NULL;
// }
// String (const char *s)
// {
// str = new char[strlen(s) + 1];
// strcpy(str, s);
// }
// ~String()
// {
// if (str) delete[] str;
// }
//};
class Fraction
{
private:
int x, y;
public:
Fraction()
{
x = 0;
y = 0;
}
// Fraction(int xi, int yi)
// {
// x = xi;
// y = yi;
// }
// Fraction(int xi, int yi): x(xi), y(yi)
// {
//
// }
Fraction(int x, int y)
{
this->x = x;
this->y = y;
}
17
Lập trình C/C++ ~ VU DINH LUC 18
Fraction(const Fraction& f)
{
x = f.x;
y = f.y;
}
};
class Square
{
private:
float xA, yA;
float xB, yB;
float xC, yC;
float xD, yD;
};
class Line
{
private:
float a, b;
public:
Line()
{
a = 0;
b = 0;
}
Line(float ai, float bi)
{
a = ai;
b = bi;
}
Line(const Line& l)
{
a = l.a;
b = l.b;
}
bool parallel(Line l)
{
return (a == l.a && b != l.b);
18
Lập trình C/C++ ~ VU DINH LUC 19
}
};
class Circle
{
private: // properties
float x, y, r;
public: // methods
// contructor
// 1. default contructor
Circle()
{
cout << "default contructor\n";
x = 0;
y = 0;
r = 0;
}
// 2. parameterized contructor
Circle(float xc, float yc, float rc)
{
cout << "parameterized contructor\n";
x = xc;
y = yc;
r = rc;
}
// 3. copy contructor
Circle(const Circle& c)
{
cout << "copy contructor\n";
x = c.x;
y = c.y;
r = c.r;
}
// Destructor
~Circle()
{
cout << "Destructor\n";
}
float getX();
float getY();
19
Lập trình C/C++ ~ VU DINH LUC 20
float getR();
class Shape
{
private:
20
Lập trình C/C++ ~ VU DINH LUC 21
Circle c;
public:
void display()
{
cout << c.r;
}
};
int main()
{
Circle c1;
c1.display();
c1.set(3.0, 4.0, 2.0);
c1.move(1.0, 1.0);
c1.scale(2.0);
c1.display();
Circle *p = &c1;
p->move(-2.0, -2.0);
p->scale(0.5);
p->display();
Circle c3(c2);
c3.display();
return 0;
}
21
Lập trình C/C++ ~ VU DINH LUC 22
II, HÀM
- Copy:
Strncpy(strmoi, strcu, 2); => copy 2 kí tự sang str mới
- Nối:
Strcat(a,b);=>nối b vào a
22
Lập trình C/C++ ~ VU DINH LUC 23
III, NOTE
1) Bảng key Read write file
2) Bảng mã ASCII
23
Lập trình C/C++ ~ VU DINH LUC 24
Int n;
Fscanf(f, “%d\n”, &n);
Gfets(s, 1000, f);
// hoặc fgetc(f);
24