Advertisement
Guest User

Untitled

a guest
Dec 18th, 2021
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. class MatrixCombo
  9. {
  10. public:
  11. int row;
  12. int col;
  13.  
  14. int *mtrI;
  15.  
  16. MatrixCombo (int r, int c)
  17. {
  18. row= r; col= c;
  19. }
  20. ////////////
  21. ~MatrixCombo()
  22. {
  23. delete [] mtrI;
  24. cout << "Освобождаем ранее занятую память..." << endl;
  25. }
  26. ////////////////////
  27. void MakeIntMatr()
  28. {
  29. mtrI= new int [row*col]; if (mtrI==NULL) throw 11;
  30. for (int rr= 0; rr< row; rr++)
  31. {
  32. for (int cc= 0; cc< col; cc++)
  33. {
  34. mtrI[rr*col+cc]= rr*10+cc;
  35. }
  36. }
  37. }
  38. //////////////////
  39. void ChangeColumn(int c1, int c2)
  40. {
  41. if (c1<0 || c1>col) { cout << "Индекс первого столбца некорректен... c1=0" << endl; c1=0; }
  42. if (c2<0 || c2>col) { cout << "Индекс второго столбца некорректен... c2= максиммальный" << endl;
  43. c2=col-1; }
  44. if (c1==c2) { cout << "Индексы столбцов равны... c1=0 и c2= максиммальный " << endl;
  45. c1=0; c2= col-1; }
  46.  
  47. int tmp;
  48. for (int r=0; r< row; r++)
  49. {
  50. tmp= mtrI[r*col+c1];
  51. mtrI[r*col+c1]= mtrI[r*col+c2];
  52. mtrI[r*col+c2]= tmp;
  53. }
  54. }
  55.  
  56. ///////////////
  57. void OutMatr()
  58. {
  59. for (int rr= 0; rr< row; rr++)
  60. {
  61. for (int cc= 0; cc< col; cc++)
  62. {
  63. cout << mtrI[rr*col+cc] << '\t';
  64. }
  65. cout << endl;
  66. }
  67. }
  68. };
  69.  
  70. int main(int argc, char **argv)
  71. {
  72. system("chcp 1251 > nul"); // Руссификация сообщений
  73. setlocale(LC_ALL, "Russian");
  74.  
  75. MatrixCombo mc(5,7);
  76.  
  77. try { mc.MakeIntMatr(); mc.OutMatr();
  78. cout << endl << "Поменяем указанные столбцы..." << endl;
  79. int c1,c2; cin >> c1 >> c2;
  80. mc.ChangeColumn(c1,c2); mc.OutMatr(); }
  81. catch (int i)
  82. {
  83. switch (i)
  84. {
  85. case 11: cout << "Ошибка выделения памяти..." << endl; exit(1); break;
  86. }
  87. }
  88. system("pause"); // system("pause > nul");
  89. return 0;
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement