C,
pasted
on Jun 20:
|
#include<iostream>
using namespace std;
void NhapMang(int *a, int n)
{
for (int i = 0; i < n; i++)
{
cout << "Nhap vao a[" << i << "] = ";
cin >> a[i];
//cin >> *(a + i);
//cin >> *(a++);
}
}
void XuatMang(int *a, int n)
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
//cout << *(a + i) << " ";
//cout << *(a++) << " ";
}
}
template <typename Tai>
void REALLOC(Tai *&a, int OldSize, int NewSize)
{
/*
NewSize > OldSize => tăng thêm
NewSize < OldSize => Co lại
*/
// Bước 1: Tạo mảng tạm
Tai *Tam = new Tai[OldSize];
// Bước 2: Đổ dữ liệu từ mảng a sang mảng tạm
for (int i = 0; i < OldSize; i++)
{
Tam[i] = a[i];
}
// Bước 3: Giải phóng mảng a và cấp lại size mới
delete [] a;
a = new Tai[NewSize];
// Bước 4: Xác định min giữa 2 cái để biết thêm hay xóa
int Min = OldSize < NewSize ? OldSize : NewSize;
// Bước 5 : Đổ dữ liệu từ tạm sang a
for(int i = 0; i < Min; i++)
{
a[i] = Tam[i];
}
// Bước 6: Xóa tạm
delete [] Tam;
}
void Them(int *&a, int &n, int vitrithem, int phantuthem)
{
REALLOC(a,n, n + 1);
for (int i = n - 1; i >= vitrithem; i--)
{
a[i + 1] = a[i];
}
a[vitrithem] = phantuthem;
n++;
}
void Xoa(int *&a, int &n, int vitrixoa)
{
for (int i = vitrixoa + 1; i < n; i++)
{
a[i - 1] = a[i];
}
REALLOC(a, n , n - 1);
n--;
}
int main()
{
int n;
do
{
cout << "Nhap so luong phan tu cua mang: ";
cin >> n;
if(n < 0)
{
cout << "So luong phan tu khong hop le. Xin kiem tra lai !\n";
}
} while (n < 0);
// Cấp phát
int *a = new int[n];
NhapMang(a, n);
XuatMang(a, n);
// Cơ chế realloc do ta tự tạo
//REALLOC(a,n, n + 1);
//cout << "\na[n - 1] = " << a[n - 1];
//a[n] = 69;
//cout << "\na[" << n << "] = " << a[n];
int vitrithem, phantuthem;
// 0 <= vitrithem <= n
/*do
{
cout << "\nNhap vao vi tri them(0, " << n << "): ";
cin >> vitrithem;
if (vitrithem < 0 || vitrithem > n)
{
cout << "\nVi tri them khong hop le. Xin kiem tra lai !";
}
} while (vitrithem < 0 || vitrithem > n);
cout << "\nNhap vao phan tu can them: ";
cin >> phantuthem;
Them(a,n,vitrithem,phantuthem);
cout << "\nMang sau khi them la: ";
XuatMang(a,n);*/
int vitrixoa;
// 0 <= n <= n - 1
do
{
cout << "\nNhap vao vi tri xoa(0, " << n << "): ";
cin >> vitrixoa;
if (vitrixoa < 0 || vitrixoa > n - 1)
{
cout << "\nVi tri xoa khong hop le. Xin kiem tra lai !";
}
} while (vitrixoa < 0 || vitrixoa > n - 1);
Xoa(a,n,vitrixoa);
cout << "\nMang sau khi xoa: ";
XuatMang(a,n);
// Giải phóng
delete [] a;
system("pause");
return 0;
}
|
Output:
|
Line 18: error: iostream: No such file or directory
Line 2: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
In function 'NhapMang':
Line 6: error: 'for' loop initial declaration used outside C99 mode
Line 8: error: 'cout' undeclared (first use in this function)
Line 8: error: (Each undeclared identifier is reported only once
Line 8: error: for each function it appears in.)
Line 9: error: 'cin' undeclared (first use in this function)
In function 'XuatMang':
Line 17: error: 'for' loop initial declaration used outside C99 mode
Line 19: error: 'cout' undeclared (first use in this function)
t.c: At top level:
Line 25: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
Line 59: error: expected ';', ',' or ')' before '&' token
Line 70: error: expected ';', ',' or ')' before '&' token
In function 'main':
Line 87: error: 'cout' undeclared (first use in this function)
Line 88: error: 'cin' undeclared (first use in this function)
Line 98: error: 'new' undeclared (first use in this function)
Line 98: error: expected ',' or ';' before 'int'
Line 155: error: 'delete' undeclared (first use in this function)
Line 155: error: expected expression before ']' token
|
|