Cc

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Lập trình C/C++ ~ VU DINH LUC 1

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(){

enum wday {mon, tue, wed, thu=50, fri, sat,sun};


printf("&d/n", sun); //=>53
printf("%d\n", mon); //=>47
enum wday m = wed; // gán stt
printf("%d\n", m);
}

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;
};

typedef struct sinhvien sv;

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");

// sv sv1; // Nhập giá trị cho struct từ bàn phím


// printf("Nhap sv1: ");
// scanf("%s %lf", &sv1.name,&sv1.gpa);
// printf("In sv sv1: %s, %.2lf\n\n", sv1.name, sv1.gpa);

// sv sv2 = {"Nguyen Trong Vu", {12,1,2005}, 1.3}; // Struct lồng


// printf("sv2: %s , %ld/%ld/%ld , %.2lf", sv2.name, sv2.bd.date, sv2.bd.month,
sv2.bd.year,sv2.gpa);

printf("NHAP DU LIEU LOP ET1-06: \n\n");


int n;
printf("So sinh vien: ");
scanf("%d", &n);

for (int i=0; i < n; i++){

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
}

void swap( int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

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

/// ############################# POINTER WITH ARRAY ################

int a[10] = {1,2,3,4,5,6,7,8,9,10};


int *ptr = a; // con trỏ ptr đang trỏ vào vị trí đầu tiên trong mảng
printf("Location of array a: %d\n", a); // gia tri cua a la vi tri dau tien trong
mang
for (int i=0; i<10;i++)
{
printf("Location of a[%d] is %d\n", i, &a[i]);
printf("%d\n", a+i); // &a[i]=a+i => vi tri thu i trong mang a

printf("Gia tri ptr ban dau: %d\n", *ptr);


++ptr; // dich vi tri con tro ptr sang phai
printf("Gia tri ptr sau do: %d\n", *ptr);
ptr+=3;
printf("Gia tri ptr sau do: %d", *ptr);

/// ##############################3 CẤP PHÁT ĐỘNG ###########################


// dataType *pointerName = (type_cast*)malloc(size_of_byte);
int *ptr = (int*)malloc(1000000 * sizeof(int)); // xin cấp phát bộ nhớ cho 1 triệu
phần tử int (4 triệu byte)

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

printf("%d ", ptr[i]);


}
printf("\n%d\n", ptr[10000]);

free(ptr); // $$giải phóng vùng nhớ


// calloc = > tương tự như malloc nhưng các giá trị vùng bộ nhớ cấp phát bằng 0
// #### realloc ########

/* 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 *ptr2 = (int*)malloc(5*sizeof(int));


for (int i=0;i<5;i++)
{
ptr2[i] = 10*i;
}

for (int i = 0; i< n ; i++)


{
printf("%d ", ptr2[i]);
}
printf("\n");
ptr2 = (int*)realloc(ptr2,10*sizeof(int));
for (int i = 0; i< 10 ; i++)
{
printf("%d ", ptr2[i]);
/// ######################### CON TRO CAP 2,3,.... ####################################

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

printf("Location: a-%d, ptr-%d", ptr,ptr2);

5) Malloc , calloc, realloc


(TRONG MỤC 4)
6) Array
#include <stdio.h>

// ######################### 1 DIMENSION ARRAY #####################

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};

// // #################### 2 DIMENSION ARRAY ####################

// KHAI BAO: <type> name[dimention 1][dimention 2];


// hàng cột
int arr2D[3][3] = {{4,2,3},{4,8,6},{7,8,7}}; // Mảng 2 chiều gồm 2 hàng 3 cột
// BAI TAP:
printf("Cac phan tu trong mang la: \n");
int sum = 0;
int min = arr2D[0][0];
int max = arr2D[0][0];
for (int row=0 ; row<3; row++)
{
for(int col=0; col<3; col++)
{
printf("%d ", arr2D[row][col]);
sum += arr2D[row][col];
if (arr2D[row][col]<min) min = arr2D[row][col];
if (arr2D[row][col]>max) max = arr2D[row][col];
}
//BAI TAP TIM SO PHAN TU
for (int i=min; i<=max; i++)
{
int cnt = 0;
for (int row=0 ; row<3; row++)
{
for(int col=0; col<3; col++)
{
if(arr2D[row][col]==i)
cnt++;
}
}
if (cnt>0)
printf("so phan tu %d la %d\n", i, cnt);
}

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ũ

// f = fopen("C:/Users/may cua luc/Máy tính/code C/28 tech/input.txt", "r"); => ĐỌC


FILE KHÁC THƯ MỤC

FILE *f; // khai báo con trỏ file tên là f


FILE *g; // khai báo con trỏ file tên g
f = fopen("input.txt","r"); // Đọc file input.txt
g = fopen("output.txt","w"); // Đọc file output.txt

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");

int n; // xử lí dấu enter


fscanf(f, "%d\n", &n);

8
Lập trình C/C++ ~ VU DINH LUC 9

//hoặc xử lí enter bằng fgetc(f);


char s[1000];
fgets(s, 1000, f);
//Xoa enter
fprintf(g,"Data : %d\n%s", n, s);

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));

// nhập xâu từ bàn phím:


scanf("%s", &a);
// gets(a); // nhập cả dấu cách
// fgets(a); // tính cả dấu enter ở cuối dòng

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) Operator overloading


#include <iostream>
using namespace std;
class Circle
{
private:
float x, y, r;
public:
// Contructor
// 1. Default contructor
Circle()
{
cout << "Default contructor\n";
x = 0;
y = 0;
r = 0;
}

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";
}

void set(float xc, float yc, float rc);

float getX();
float getY();
float getR();

void move(float vx, float vy);


void scale(float s);
void display();

// 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

res.x = (this->x + c.x) / 2;


res.y = (this->y + c.y) / 2;
res.r = this->r - c.r;
return res;
}
Circle operator !(){
return Circle(y,x,r);
}
};
void Circle::set(float xc, float yc, float rc)
{
x = xc;
y = yc;
r = rc;
}
float Circle::getX() {return x;}
float Circle::getY() {return y;}
float Circle::getR() {return r;}

void Circle::move(float vx, float vy)


{
x = x + vx;
y = y + vy;
}
void Circle::scale(float s)
{
r = r * s;
}
void Circle::display()
{
cout << getX() << " " << getY() << " " << getR() << endl;
}
int main()
{
Circle c1(2, 4, 3);
Circle c2(6, 8, 4);
Circle c3 = c1 + c2;
Circle c4 = c1 - c2;
c3.display();
c4.display();
return 0;
}

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;}

Array(T arr[], int N){


// Xin cấp phát mới cho arr
this->arr=new T[N];
this->N=N;
// thực hiện copy lần tượt phần tử của a sang arr
//this->arr=arr;//Khong lam nhu nay
for (int i=0;i<N;i++) this->arr[i]=arr[i];
}
Array(const Array& a){}
~Array(){if (arr!=NULL) delete[] arr;}
T sumall(){T s=0;for(int i=0;i<N;i++) {s=s+arr[i];}; return s;}
};
int main()
{
int aa[]={1,2,3,4,5};
int N=sizeof(aa)/sizeof(int);
Array<int> a(aa,N);
cout<<"sum="<<a.sumall()<<endl;
Array<float> b;
// int res = compare<int>(5,6);
// cout<<"res="<<res<<endl;
// res = compare<double>(3.2,1.5);
// cout<<"res float="<<res<<endl;
// res = compare<char>('a','b');
// cout<<"res char="<<res<<endl;
// int s=sum<int>(3,4);
// float sf=sum<float>(3.4,4.6);
// cout<<"s="<<s<<endl<<"sf="<<sf<<endl;
// float a=2.5, b=3.5;
// //swap(&a,&b);
// int c=nmax<int>(5,6);
// int d=nmax<int>(1,2,3);

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);
}

char* getname(){return name;}


float getsalary(){return salary;}

void pay(){printf("Pay for %s %.2f dollars\n");}


// ~Employee();
};

class Worker:public Employee{ // derived class


private:
int level;
public:
Worker(){level = 1;}
Worker(float salary, char* name, int level):Employee(salary, name){
this-> level = level;

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();}
};

class Circle:public Shape {


public:
void draw(){
cout << "Circle::draw\n";
}
void erase(){cout << "Circle::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;
}

13) Friend in class


#include <iostream>
#include <string.h>

using namespace std;

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 online(float x, float y)


{
return y == a * x + 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";
}

void set(float xc, float yc, float rc);

float getX();
float getY();

19
Lập trình C/C++ ~ VU DINH LUC 20

float getR();

void move(float vx, float vy);


void scale(float s);
void display();

// Friend class and function


friend float radiusDifference(Circle c1, Circle c2);
friend class Shape;
};

void Circle::set(float xc, float yc, float rc)


{
x = xc;
y = yc;
r = rc;
}

float Circle::getX() {return x;}


float Circle::getY() {return y;}
float Circle::getR() {return r;}

void Circle::move(float vx, float vy)


{
x = x + vx;
y = y + vy;
}
void Circle::scale(float s)
{
r = r * s;
}
void Circle::display()
{
cout << getX() << " " << getY() << " " << getR() << endl;
}

// Friend class and function


float radiusDifference(Circle c1, Circle c2)
{
return c1.getR() - c2.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 c2(3.5, 2.5, 2);


c2.display();

Circle c3(c2);
c3.display();

cout << radiusDifference(c1, c2) << endl;

Line l1(2, 3);


Line l2(2, 4);
cout << l1.online(0, 3) << endl;
cout << l1.parallel(l2);

return 0;
}

14) Phép toán BIT


int =5, b=6;
int c = a|b; ~ a or b

21
Lập trình C/C++ ~ VU DINH LUC 22

int c2 = ~a; //đảo bit


int c3 = a^b; ~ a XOR b // ( 1 nếu khác nhau, 0 nếu giống nhau)

II, HÀM

1) Chuyển số sang hệ nhị phân


Char buffer[30]; luu so nhi phan
Int a = 100;
Itoa(a, buffer ,2); 2 là hệ nhị phân
2) Xử lí xâu kí tự
-VIẾT HOA , in thường:
a= tolower(A)
B=toupper(b);

-So sánh 2 sâu:


Strcmp(“a”,”b”); => bé hơn là -1, bằng là 0, lớn hơn là 1.

- Copy:
Strncpy(strmoi, strcu, 2); => copy 2 kí tự sang str mới

- Nối:
Strcat(a,b);=>nối b vào a

- Chuỗi <-> số:


Char num1[20] = “23434”
Chả num2[20] = “-2344”
Chả num3[20] = "123asf23”;
Int n1 = atoi(num1);
Int n2 = atoi(num2);
Int n3 = atoi(num3); => 123

- Chuyển string thành in hoa


Char tu[5] = “hahaH”;
Char tuuper[5] = strupr(tu);

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

3) Cách sử lý dấu xuống dòng

Int n;
Fscanf(f, “%d\n”, &n);
Gfets(s, 1000, f);
// hoặc fgetc(f);

IV, Các dạng bt thường gặp


1) Tìm UCLN, BCNN
// Hàm tính UCLN sử dụng thuật toán Euclid
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

// Hàm tính BCNN


int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
- Kích thước con trỏ trên 64 – bit là 8
- 32 – bit là 4
- Chú ý :
- Khai báo int *s =….
- Sizeo(s) là kích thước con trỏ chứ không phải độ dài của mảng s
- “<<” dịch trái : nhân với 2^n
- “>>” dịch phải : chia với 2^n
- Ví dụ : 0xF1 (hệ 16) = 15*16^1 + 1

24

You might also like