Помогите, пожалуйста, разобраться с вылетом программы
Задание: Составить описание класса, обеспечивающего представление матрицы произвольного
размера с возможностью изменения числа строк и столбцов, вывода на
экран подматрицы любого размера и всей матрицы.
Написать программу, демонстрирующую работу с этим классом. Программа должна
содержать меню, позволяющее осуществить проверку всех методов класса.
При заходе в "change" программа вылетает и пишет:
Windows has triggered a breakpoint in Класс.exe.
This may be due to a corruption of the heap, which indicates a bug in Класс.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Класс.exe has focus.
The output window may have more diagnostic information.
А после нажатия Break: No symbols are loaded for any call stack frame. The source code cannot be displayed.
Код программы:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
| #include "stdafx.h"
#include <iostream>
using namespace std;
class matr
{
private:
int **matrix;
int rows, columns; //количество рядов и столбцов матрицы
public:
matr(int r, int c){ //Конструктор предназначен для инициализации объекта и вызывается автоматически при его создании
this -> rows = r;
this -> columns = c;
matrix = new int *[r];
for (int i = 0; i < r; i++){
this -> matrix[i] = new int [c];
}
}
~matr(){ //декструктор
for (int i = 0; i < this -> rows; i++){
delete [] this -> matrix[i];
}
delete [] this -> matrix;
}
matr(const matr &R){ //конструктор копирования
this -> rows = R.rows;
this -> columns = R.columns;
matrix = new int *[this -> rows];
for (int i = 0; i < this -> rows; i++){
this -> matrix[i] = new int [this -> columns];
}
}
void cinMatr(int r, int c){ //введение элементов в матрицу
this -> rows = r;
this -> columns = c;
cout << "Enter the elements: ";
for (int i = 0; i < this -> rows; i++){
for (int j = 0; j < this -> columns; j++){
cin >> this -> matrix[i][j];
}
}
}
void printMatr(int r, int c){//выведение элементов на экран
this -> rows = r;
this -> columns = c;
int cc = c + 1, cr = r + 1, nr = r + 1, nc = c + 1;
while (nr > this -> rows){
cout << "Enter the number of starting row: ";
cin >> nr;
}
while (nc > this -> columns){
cout << "Enter the number of starting columns: ";
cin >> nc;
}
while (cr > this -> rows || cr < nr){
cout << "Enter the number of ending row: ";
cin >> cr;
}
while (cc > this -> columns || cc < nc){
cout << "Enter the number of ending columns: ";
cin >> cc;
}
for (int i = (nr-1); i < cr; i++){
for ( int j = (nc-1); j < cc; j++ ){
cout << this -> matrix[i][j] << ' ';
}
cout << endl;
}
}
void printMatrNew(int r, int c){//выведение элементов на экран
this -> rows = r;
this -> columns = c;
for (int i = 0; i < this -> rows; i++){
for ( int j = 0; j < this -> columns; j++ ){
cout << this -> matrix[i][j] << ' ';
}
cout << endl;
}
}
matr operator +(const matr &R){
int r = max(this->rows,R.rows);
int c = max(this->columns,R.columns);
matr K(r, c);
int rr = 0;
int cc = 0;
// обнуляем всю матрицу
for (int i = 0; i < r; i++){
if (rr < R.rows){
for (int j = 0; j < c; j++){
if (cc < R.columns){
if (this -> rows < i || this -> columns < j){
K.matrix[i][j] = 0;
cc++;
}
else{
K.matrix[i][j] = R.matrix[i][j];
c++;
}
}
}
}
rr++;
}
return K;
}
matr& operator=(const matr &R){//оператор равно
if(this == &R){
return *this;
}
if(rows != R.rows || columns != R.columns){
for (int i = 0; i < this -> rows; i++){
delete [] this -> matrix[i];
}
delete [] this -> matrix;
matrix = new int *[R.rows];
for (int i = 0; i < R.rows; i++){
this -> matrix[i] = new int [R.columns];
}
this -> rows = R.rows;
this -> columns = R.columns;
}
for (int i = 0; i < this -> rows; i++){
for (int j = 0; j < this -> columns; j++){
this -> matrix[i][j] = R.matrix[i][j];
}
}
return *this;
}
};
int main ( ){
int k = 0;
do{
int a = 0, b = 0, c = 0, d = 0, e = 0;
while (a < 1){
cout << "Enter the number of rows ";
cin >> a;
}
while (b < 1){
cout << "Enter the number of colums ";
cin >> b;
}
matr myMatrix(a, b); // создаем матрицу a*b
myMatrix.cinMatr(a, b); // заполняем ее
do{
cout << "print - 1, change - 2 ";
cin >> c;
if (c == 1){
myMatrix.printMatr(a,b); // выводим
do{
cout << "Input new matrix? yes - 0, no - 1, exit - 2 ";
cin >> k;
}while (k < 0 || k > 2);
}
if (c == 2){
cout << endl << "Enter the number of rows ";
while (d < 1){
cin >> d;
}
cout << endl << "Enter the number of colums ";
while (e < 1){
cin >> e;
}
matr R(d,e);
myMatrix = R + myMatrix;
myMatrix.printMatrNew(a,b); // выводим
do{
cout << "Input new matrix? yes - 0, no - 1, exit - 2 ";
cin >> k;
}while (k < 0 || k > 2);
do{
cout << "Input new matrix? yes - 0, no - 1, exit - 2 ";
cin >> k;
}while (k < 0 || k > 2);
}
}while (k == 1);
} while (k != 2);
system ("pause");
return 0;
} |
|