Guest User

Динамические массивы

a guest
Jun 5th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <iomanip.h>
  6.  
  7. class Matr
  8. {
  9. public:
  10. int* matr;
  11. int* matrI;
  12.  
  13. int Mrow;
  14. int Mcol;
  15. int Mlast;
  16.  
  17. Matr(int r, int c);
  18. ~Matr();
  19. void Init(void);
  20. void Print(void);
  21. void Del(int el);
  22. void Add(int ee);
  23. };
  24. ///////////////////////////////////////
  25. Matr::Matr(int r, int c)
  26. {
  27. Mrow= r; Mcol= c; Mlast= Mrow*Mcol;
  28. matr= new int [Mrow*Mcol];
  29. matrI= new int [Mrow*Mcol];
  30. }
  31. ///////////////////////////////////////
  32. Matr::~Matr()
  33. {
  34. delete [] matr;
  35. delete [] matrI;
  36. }
  37. ///////////////////////////////////////
  38. void Matr::Del(int el)
  39. {
  40. Mlast= Mrow*Mcol-el;
  41.  
  42. delete [] matrI;
  43. matrI= new int[Mlast];
  44.  
  45. for (int q=0; q< Mlast; q++)
  46. { matrI[q]= matr[q]; }
  47.  
  48. delete [] matr;
  49. matr= new int[Mlast];
  50.  
  51. for (int q=0; q< Mlast; q++)
  52. { matr[q]= matrI[q]; }
  53.  
  54. }
  55. ///////////////////////////////////////
  56. void Matr::Add(int ee)
  57. {
  58. delete [] matrI;
  59. matrI= new int[Mlast+ee];
  60.  
  61. for (int q=0; q< Mlast; q++)
  62. { matrI[q+ee]= matr[q]; }
  63. for (int q=0; q<ee; q++)
  64. { matrI[q]= '0'; }
  65.  
  66. delete [] matr;
  67. Mlast= Mlast+ee;
  68. matr= new int[Mlast];
  69. for (int q=0; q< Mlast; q++)
  70. { matr[q]= matrI[q]; }
  71. }
  72. ///////////////////////////////////////
  73. void Matr::Init(void)
  74. {
  75. randomize();
  76.  
  77. for (int q=0; q< Mrow*Mcol; q++)
  78. { matr[q]= rand()%100; }
  79. }
  80. ///////////////////////////////////////
  81. void Matr::Print(void)
  82. {
  83. int clmn=0;
  84. for (int q=0; q< Mlast; q++)
  85. {
  86. cout<<setw(3)<<matr[q]; clmn++;
  87. if (clmn==Mcol) { clmn=0; cout<<endl; }
  88. }
  89. }
  90. ///////////////////////////////////////
  91. int main (void)
  92. {
  93. int row; int col;
  94. cout<<"Input ROW= "; cin>>row;
  95. cout<<"Input COL= "; cin>>col;
  96. cout<<endl<<endl;
  97.  
  98. cout<<"Matrix is burn"<<endl<<endl;
  99. Matr Matr0(row, col);
  100. Matr0.Init();
  101. Matr0.Print();
  102. cout<<endl<<endl<<"Deleting Matrix"<<endl<<endl;
  103. Matr0.Del(5);
  104. Matr0.Print();
  105.  
  106. cout<<endl<<endl<<"Adding Matrix"<<endl<<endl;
  107. Matr0.Add(3);
  108. Matr0.Print();
  109.  
  110. getch();
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment