Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- using namespace std;
- void Pointer()
- {
- double **p = 0;
- try{
- p = new double *;
- *p = new double;
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- delete p;
- return;
- }
- **p = 2;
- cout << " **p = " << **p << endl;
- delete *p;
- delete p;
- }
- void ArrayPointer()
- {
- double **p = 0;
- int size;
- cout << "Input the size of array." << endl;
- cin >> size;
- try{
- p = new double *;
- *p = new double [size];
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- delete p;
- return;
- }
- for (int i=0; i<size; i++)
- (*p)[i]=0;
- for (int i=0; i<size; i++)
- cout << (*p)[i] << " ";
- (*p)[0]=2.0;
- (*p)[size-1]=2.0;
- cout << endl;
- for (int i=0; i<size; i++)
- cout << (*p)[i] << " ";
- delete []*p;
- delete p;
- }
- void BigArrayPointer()
- {
- double **p = 0;
- int size;
- cout << "Input the size of array." << endl;
- cin >> size;
- try{
- p = new double *[size];
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- return;
- }
- for (int i=0; i<size; i++){
- try{
- p[i]=new double ;
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- for (int j=0; j<i; j++){
- delete p[j];
- }
- delete []p;
- return;
- }
- }
- for (int i=0; i<size; i++)
- *(p)[i]=0;
- for (int i=0; i<size; i++){
- cout << *(p[i]) << " ";
- }
- *(p[0])=2.0;
- *(p[size-1])=2.0;
- cout << endl;
- for (int i=0; i<size; i++)
- cout << *(p[i]) << " ";
- for (int i=0; i<size; i++)
- delete p[i];
- delete []p;
- }
- struct Sample
- {
- char c;
- double x;
- int *p;
- };
- void SampleTask()
- {
- Sample s;
- int n;
- double x;
- char c;
- cout << "Input the number of elements." << endl;
- cin >> n;
- s.p = new int [n];
- cout << "Input integer elements of array." << endl;
- for (int i=0; i<n ; i++)
- cin >> s.p[i];
- cout << "Input the number." << endl;
- cin >> s.x;
- cout << "Input the char." << endl;
- cin >> s.c;
- system("cls");
- for (int i=0; i<n ; i++)
- cout << s.p[i] << " ";
- cout << endl << "s.c = " << s.c << " s.x = " << s.x << endl;
- delete []s.p;
- }
- void DynamicSampleTask()
- {
- Sample *a;
- int size, n;
- cout << "Input the number of elements in array of structures:" << endl;
- cin >> size;
- cout << "Input the number of elements in array:" << endl;
- cin >> n;
- try{
- a = new Sample [size];
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- return;
- }
- for (int i = 0; i<size; i++) {
- try{
- a[i].p = new int [n]; // массив указателей на структуру,указывает на массив в структуре
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- for (int j = 0; j<i; j++)
- {delete[] a[j].p;
- }
- delete []a;
- return;
- }
- }
- for (int i = 0; i<size; i++)
- {
- for (int j = 0; j<n; j++)
- a[i].p[j] = i;
- }
- for (int i = 0; i<size; i++)
- {
- for (int j = 0; j<n; j++)
- cout << a[i].p[j] << " ";
- cout << endl;
- }
- for (int i = 0; i<size; i++)
- delete []a[i].p;
- delete []a;
- }
- void DinamicPointer()
- {
- Sample **a = 0;
- int size, n;
- cout << "Input the number of elements in array of structures." << endl;
- cin >> size;
- cout << "Input the number of elements in array." << endl;;
- cin >> n;
- try{
- a = new Sample *[size];
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- return;
- }
- for (int i = 0; i<size; i++)
- try{
- a[i] = new Sample;
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- for (int j = 0; j<i; j++)
- {
- delete a[j];
- }
- delete []a;
- return;
- }
- for (int i = 0; i<size; i++)
- try{
- a[i]->p = new int [n];
- } catch (const bad_alloc&) {
- cout << "out of memory!" << endl;
- for (int j = 0; j<i; j++)
- delete[] a[j]->p;
- for (int j = 0; j<size; j++)
- delete a[j];
- delete []a;
- return;
- }
- for (int i = 0; i<size; i++)
- {
- for (int j = 0; j<n; j++)
- (*(a[i])).p[j] = i;
- }
- for (int i = 0; i<size; i++)
- {
- for (int j = 0; j<n; j++)
- cout << (*(a[i])).p[j] << " ";
- cout << endl;
- }
- for (int i = 0; i<size; i++)
- delete [](*(a[i])).p;
- for (int i = 0; i<size; i++)
- delete a[i];
- delete []a;
- }
- int main()
- {
- Pointer();
- //ArrayPointer();
- //BigArrayPointer();
- //SampleTask();
- //DynamicSampleTask();
- //DinamicPointer();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement