Tin Tức

recent

Con trỏ và mảng 1 chiều


//a + i <=> &a[0 + i] ;

//*(a+i) <=> a[i]
void NhapMang(int *a, int n)
{
 //int *pa = a; // Cách 1: Lúc này địa chỉ biến pa cũng là địa chỉ biến a, dùng cho TH có con trỏ pa
 int *pa = &a[0]; // Cách 2
 for(int i = 0; i < n; i++)
 {
  printf("\nNhap a[%d]: ", i);
  //scanf("%d", &a[i]);
  //scanf("%d", &pa[i]);
  //scanf("%d", a + i);
  //scanf("%d", pa + i);
  //scanf("%d", a++);
  scanf("%d", pa++);
 }
}
// &a[i] <=> (a + i) <=> (pa + i) <=> &pa[i]
void XuatMang(int *a, int n)
{
 //int *pa = a; // Giải thích giống 
 int *pa = &a[0]; // Cách 2
 for(int i = 0; i < n; i++)
 {
  //printf("%4d", a[i]);
  //printf("%4d", pa[i]);
  //printf("%4d", *(a + i));
  //printf("%4d", *(pa + i));
  //printf("%4d", *(a++));
  printf("%4d", *(pa++));
 }
}
int main()
{ 
 int n;
 do{
  printf("\nNhap so luong phan tu cua mang: ");
  scanf("%d", &n);
  if(n < 0)
  {
   printf("\nSo luong phan tu khong hop le. Xin kiem tra lai !");
  }
 }while(n < 0);

 int *a = (int *)malloc(n * sizeof(int));
 //a = (int *)calloc(n, sizeof(int));
 //a = (int *)realloc(0, n * sizeof(int));

 NhapMang(a, n);
 XuatMang(a, n);
 free(a);

 getch();
 return 0;
}
- Hàm cấp phát mảng n số thực(xác định số lượng phần tử trước khi cấp phát)
void arrayFloatOutput(float a[], int n)
{
 for(int i = 0; i < n; i++)
 {
  printf("\n%f", a[i]);
 }
}

float* arrayFloatInput(int* n)
{
 float *a = NULL;
 int m;
 scanf("%d", &m);
 if(m <= 0)
 {
  return NULL;
 }
 *n = m;
 a = (float*)calloc(m, sizeof(float));
 if(a != NULL)
 {
  for(int i = 0; i < m; i++)
  {
   scanf("%f", &a[i]);
  }
 }
 return a;
}

int main()
{
 float *B; int nB;
 printf("Number and elements: ");
 B = arrayFloatInput(&nB);
 if(B != NULL)
 {
  // Do something here
  arrayFloatOutput(B, nB);
  // Free Allocated memory
  free(B);
 }
 getch();
 return 0;
void arrayFLoatOutput(float a[], int n)
{
 for(int i = 0; i < n; i++)
 {
  cout << a[i] << "\n";
 }
}

float* arrayFloatInput(int &n)
{
 cin >> n;
 if(n <= 0)
 {
  return NULL;
 }
 float* a = new float[n];
 if(a != NULL)
 {
  for(int i = 0; i < n; i++)
  {
   cin >> a[i];
  }
 }
 return a;
}
int main()
{
 float *B; int nB;
 printf("Number and elements: ");
 B = arrayFloatInput(nB);
 if(B != NULL)
 {
  // Do something here
  arrayFLoatOutput(B, nB);
  // Free Allocated memory
  delete[]B;
 }
- Hàm cấp phát mảng n số thực(dùng biến cấu trúc)
typedef struct{
 int n;
 float* members;
}floatArray;

void floatArrayInit(floatArray* a, int n)
{
 a->n = n;
 a->members = (float*)calloc(n, sizeof(float));
}

void floatArrayFree(floatArray* a)
{
 if(a != NULL)
 {
  if(a->members != NULL)
  {
   free(a->members);
  }
 }
}

void floatArrayInput(floatArray* a)
{
 int m;
 if(a == NULL) return;
 scanf("%d", &m);
 if(m <= 0) return;
 floatArrayInit(a, m);
 if(a->members != NULL)
 {
  for(int i = 0; i < m; i++)
  {
   scanf("%f", &(a->members[i]));
  }
 }
}

void floatArrayOutput(floatArray* a)
{
 if(a == NULL || a->members == NULL)
 {
  return;
 }
 for(int i = 0; i < a->n; i++)
 {
  printf("\n%f", a->members[i]);
 }
}

int main()
{
 floatArray B;
 printf("\nNumber and elements: ");
 floatArrayInput(&B);
 // Do something here
 printf("\nArray is: ");
 floatArrayOutput(&B);

 // Free allocated memory
 floatArrayFree(&B);

 getch();
 return 0;
typedef struct{
 int n;
 float* members;
}floatArray;

void floatArrayInit(floatArray& a, int n)
{
 a.n = n;
 a.members = new float[n];
}

void floatArrayFree(floatArray& a)
{
 
  if(a.members != NULL)
  {
   delete[]a.members;
  }
 
}

void floatArrayInput(floatArray& a)
{
 int m;
 cin >> m;
 if(m <= 0) return;
 floatArrayInit(a, m);
 if(a.members != NULL)
 {
  for(int i = 0; i < m; i++)
  {
   cin >> a.members[i];
  }
 }
}

void floatArrayOutput(floatArray& a)
{
 if(a.members == NULL)
 {
  return;
 }
 for(int i = 0; i < a.n; i++)
 {
  cout << a.members[i] << " ";
 }
}

int main()
{
 floatArray B;
 printf("\nNumber and elements: ");
 floatArrayInput(B);
 // Do something here
 printf("Array is: ");
 floatArrayOutput(B);

 // Free allocated memory
 floatArrayFree(B);

 getch();
 return 0;
- Hàm cấp phát mảng n số thực (không hỏi trước số lượng phần tử)
void floatArrOut(float a[], int n)
{
 for(int i = 0; i < n; i++)
 {
  printf("%f", a[i]);
 }
}

float* floatArrPushBack(float*a, int *n, float x)
{
 int m = (*n) + 1;
 float* anew = (float*)realloc(a, m * sizeof(float));
 if(anew != NULL)
 {
  anew[*n] = x;
  (*n)++;
 }
 return anew;
}

float * floatArrIn(int *n)
{
 float* anew, *a = NULL; 
 float x;
 *n = 0;
 while(scanf("%f", &x) > 0)
 {
  anew = floatArrPushBack(a, n, x);
  if(anew != NULL)
  {
   a = anew;
  }
 }
 return a;
}

int main()
{
 float *B;
 int nB;
 printf("Input element: \n");
 B = floatArrIn(&nB);
 // Do something here
 printf("%d element(s): \n", nB);
 floatArrOut(B, nB);
 // Free Allocated memory
 if(B != NULL)
  free(B);
 getch();
 return 0;
}
void floatArrOut(float a[], int n)
{
 for(int i = 0; i < n; i++)
 {
  cout << a[i] << " ";
 }
}

void floatArrPushBack(float *&a, int &n, float x)
{
 int m = n + 1;
 float *anew = (float*)realloc(a, m * sizeof(float));
 if(anew != NULL)
 {
  anew[n] = x;
  n++;
  a = anew;
 }
}

void floatArrIn(float*& a, int &n)
{
 float x;
 a = NULL;
 n = 0;
 while(cin >> x)
 {
  floatArrPushBack(a, n, x);
 }
}
int main()
{
 float *B; int nB;
 cout << "Input elements: " << endl;
 floatArrIn(B, nB);
 // Do something here
 cout << nB << "element(s): " << endl;
 floatArrOut(B, nB);
 // Free Alocated memory
 if(B != NULL)
  free(B);

 getch();
 return 0;
}
- Hàm cấp phát mảng các phần tử kiểu tùy ý và áp dụng cho kiểu Phân Số
// Tập tin _ARRAY1D_T_H
template <class T>
 void arrPushBack(T *&a, int &n, T x)
 {
  int m = n + 1;
  T* anew = (T*)realloc(a, m * sizeof(T));
  if(anew != NULL)
  {
   anew[n] = x; n++;
   a = anew;
  }
 } 
 template <class T>
 void arrInput(T *&a, int &n)
 {
  T x;
  a = NULL; n = 0;
  while(cin >> x)
  {
   arrPushBack(a, n, x);
  }
 }
 template <class T>
 void arrOutput(T a[], int n)
 {
  for(int i = 0; i < n; i++)
  {
   cout << a[i] << " ";
  }
 }
#endif // -------End of FILE-------------
struct PhanSo
{
 int tuso, mauso;
};

ostream& operator<< (ostream& outDev, const PhanSo& ps)
{
 if(ps.mauso == 1|| ps.tuso == 0)
  outDev << ps.tuso;
 else
  outDev << ps.tuso << "/" << ps.mauso;
 return outDev;
}
istream& operator>> (istream& inDev, PhanSo& ps)
{
 inDev >> ps.tuso >> ps.mauso;
 return inDev;
}

int main()
{
 PhanSo *B; int nB;
 cout << "Input elements of array: \n";
 arrInput(B, nB);
 // Do something here
 cout << nB << " element(s): \n";
 arrOutput(B, nB);
 // Free Alocated memory
 if(B != NULL)
  free(B);

 getch();
 return 0;
}
Nguồn : Vương trí tài
Con trỏ và mảng 1 chiều Reviewed by myblog on 22:50:00 Rating: 5

Không có nhận xét nào:

Bản Quyền Thuộc: Blogger JT © 2016. All Rights Reserved
Phát triển bởi JT

Góp Ý Cho JT Blog

Tên

Email *

Thông báo *

Được tạo bởi Blogger.