Класс "Матрица". Ввести статические методы
27.11.2015, 19:11. Показов 880. Ответов 0
Это моя программа, в ней нужно переделать методы в методы типа static
Честное слово не знаю как это делать на C++
Нужна help! нужно исправить два метода:
int matrix::check_mult(const matrix &A)const{
return N == A.M;
}
int matrix::check_sum(const matrix &A)const{
return M == A.M && N == A.N;
}
| 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
| #include "stdafx.h"
#include "Matrix.h"
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <string>
using namespace std;
namespace math{
int matrix::count = 1; //N -строчки M- столбцы
void matrix::init(int _N, int _M , const double *array_numbers){
numb = count++;
if(_N<0 || _M<0) throw error(1,numb);
if(_N==0 || _M==0){
N=M=size=0;
ptr = NULL;
}else{
N=_N;
M=_M;
ptr = new double[N*M];
size=M*N;
if(array_numbers==NULL){
for(int i=0;i<size;i++){
ptr[i]=0;
}
}else{
for(int i=0;i<size;i++){
ptr[i]=array_numbers[i];
}
}
}
printf("матрица №%d\n",numb);
}
matrix::matrix(int _N, int _M, const double *array_numbers){
init(_N, _M, array_numbers);
}
matrix::matrix(int _N, const double *array_numbers){
init(_N, _N, array_numbers);
}
matrix::matrix(int _N, int _M, double a, ...) {
init(_N, _M, &a);
}
matrix::matrix(int _N, double a, ...) {
init(_N, _N, &a);
}
matrix::matrix(const matrix& A){
init(A.N, A.M, A.ptr);
}
matrix::~matrix(){
if(ptr!=NULL) {
delete [] ptr;
}
printf("матрица №%d удалена\n",numb);
}
int matrix::check_mult(const matrix &A)const{
return N == A.M;
}
int matrix::check_sum(const matrix &A)const{
return M == A.M && N == A.N;
}
double matrix::max_value()const{
if(N==0 || M==0 || ptr==NULL) throw error(2, numb);
double max = *ptr;
for(int i=1;i<size;i++){
if(ptr[i]>=max){
max = ptr[i];}
}
return max;
}
double matrix::min_value()const{
if(N==0 || M==0 || ptr==NULL) throw error(2, numb);
double min = *ptr;
for(int i=1;i<size;i++){
if(ptr[i]<=min){
min = ptr[i];}
}
return min;
}
double *matrix::operator[](int i){
if(i<0 || i>=M) throw error(3, numb, i);
return ptr+i*M;
}
const double *matrix::operator[](int i) const{
if(i<0 || i>=M) throw error(3, numb, i);
return &ptr[i*M];
}
ostream& operator << (ostream &os, const matrix &A){
if (A.ptr){
int width = (int)os.width();
if(width==0) width = A.length();
for (int i = 0; i < A.N; i++){
for (int j = 0; j < A.M; j++){
os << setw(width) << A[i][j];
}
os << endl;
}
}
else
os << "Матрица пуста"<<endl;
return os;
}
matrix &matrix::operator = (const matrix &A){
if (N*M != A.N*A.N){
if (ptr != NULL){
delete[] ptr;
}
if (A.N == 0 || A.M == 0)
ptr = NULL;
else{
ptr = new double[A.N*A.M];
}
}
N = A.N;
M = A.M;
for(int i=0;i<N*M;i++){
ptr[i]=A.ptr[i];
}
return *this;
}
matrix &matrix::operator += (const matrix &A){
if(check_sum(A)){
for(int i=0;i<M*N;i++){
ptr[i] += A.ptr[i];
}
return *this;
}
throw error(4, numb, A.numb);
}
matrix &matrix::operator -= (const matrix &A){
if(check_sum(A)){
for(int i=0;i<M*N;i++){
ptr[i] -= A.ptr[i];
}
return *this;
}
throw error(5, numb, A.numb);
}
matrix& matrix::operator *= (const matrix& A){
if (check_mult(A)){
matrix result(N,A.M);
for (int i = 0; i < N; i++){
for (int j = 0; j < A.M; j++){
for (int k = 0; k < M; k++){
result.ptr[i*M+j] += ptr[i*M + k] * A.ptr[k*M+j];
}
}
}
*this = result;
return *this;
}
throw error(6, numb, A.numb);
}
matrix &matrix::operator *= (double k){
for (int i = 0; i < M*N; i++) {
ptr[i] *= k;
}
return *this;
}
int matrix::get_rows()const{
return N;
}
int matrix::get_cols()const{
return M;
}
int matrix::get_count()const{
return count;
}
int matrix::get_numb()const{
return numb;
}
int matrix::get_size()const{
return size;
}
int matrix::length()const{
static char str[24];
int max=0;
for(int i=0; i<size;i++){
if(cout.flags( ) & ios::scientific){
sprintf_s(str, "%e", ptr[i] );
}
else if(cout.flags( ) & ios::fixed){
sprintf_s(str, "%f", ptr[i] );
}
else{
sprintf_s(str, "%g", ptr[i] );
}
if((int)strlen(str)>max)
max=strlen(str);
}
return max+1;
}
matrix operator +(const matrix&lhs, const matrix&rhs){
matrix res(lhs);
res+=rhs;
return res;
}
matrix operator -(const matrix&lhs, const matrix&rhs){
matrix res(lhs);
res-=rhs;
return res;
}
matrix operator *(const matrix&lhs, const matrix&rhs){
matrix res(lhs);
res*=rhs;
return res;
}
matrix operator *(const matrix&lhs, double k){
matrix res(lhs);
res*=k;
return res;
}
matrix operator *(double k, const matrix&lhs){
matrix res(lhs);
res*=k;
return res;
}
error::error (int _code, int _matr1, int _matr2){
code=_code;
if(code==1){
sprintf_s(message,"Введён некорректный размер матрицы № %d\n",_matr1);
}
if(code==2){
sprintf_s(message,"Невозможно найти минимальный/максимальный элемент в матрице № %d\n",_matr1);
}
if(code==3){
sprintf_s(message,"Введён некорректный индекс матрицы № %d\n",_matr1);
}
if(code==4){
sprintf_s(message, "Невозможно сложить матрицу № %d и матрицу № %d\n",_matr2, _matr1);
}
if(code==5){
sprintf_s(message,"Невозможно вычесть из матрицы № %d матрицу № %d\n",_matr1, _matr2);
}
if(code==6){
sprintf_s(message,"Невозможно умножить матрицу № %d на матрицу № %d\n",_matr1, _matr2);
}
}
int error::err_code() const{
return code;
}
const char *error::err_mes()const{
return message;
}
} |
|
| 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
| #pragma once
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
namespace math{
class matrix
{
private:
int N, M;
double *ptr;
static int count;
int numb;
int size;
void init(int = 0, int = 0, const double * = NULL);
public:
matrix(int, int, const double * = NULL);
explicit matrix(int = 0, const double * = NULL);
matrix(int, int, double, ...);
matrix(int, double, ...);
matrix(const matrix&);
~matrix();
int check_mult(const matrix&)const;
int check_sum(const matrix&)const;
double max_value()const;
double min_value()const;
double *operator[](int);
const double *operator[](int) const;
friend ostream& operator << (ostream &, const matrix &);
matrix &operator = (const matrix&);
matrix &operator += (const matrix&);
matrix &operator -= (const matrix&);
matrix &operator *= (const matrix&);
matrix &operator *= (double);
int length()const;
int get_rows()const;
int get_cols()const;
int get_count()const;
int get_numb()const;
int get_size()const;
};
matrix operator +(const matrix&,const matrix&);
matrix operator -(const matrix&,const matrix&);
matrix operator *(const matrix&,const matrix&);
matrix operator *(const matrix&, double);
matrix operator *(double, const matrix&);
class error {
private:
char message[80];
int matr1, matr2,code;
public:
error (int, int, int k=0);
int err_code() const;
const char* err_mes()const;
};
}
[CPP]// ооп_2.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include "Matrix.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace math;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "Russian");
double array_numbers[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
double array_numbers2[] = {4,3,522,2,6,4,7,5,1,2,3,4,5,6,3,6,2,4,2,3,4,1,1,5};
try{
matrix v (1,3,array_numbers);
matrix c (3,1,array_numbers);
//matrix mm;
//matrix mm2(2,1,3.1,3.2);
//matrix mm3(2,3);
cout<<c;
//printf("%d\n",c.check_sum(v));
//cout<<v.max_value()<<endl;
//v[1][2]= 222;
//cout<<c<<endl;
//cout<<v*3;
//v *= c;
//matrix h(v);
//matrix h(3,3,number);
//c = v;
//v+c;
//v+=c;
//cout<<mm2;
//c*=v;
//cout<<c;
//cout<<cout.flags( )<<endl;
//printf("%d\n",v.length());
//matrix v(c);
//v-=c;
//cout<<c[0][0]<<endl;
//printf("%g\n",k[1][1]);
}
catch(error &err){
if(err.err_code()==1)
cerr << "Ошибка: " << err.err_mes()<<endl;
if(err.err_code()==2)
cerr << "Ошибка: " << err.err_mes()<< endl;
if(err.err_code()==3)
cerr << "Ошибка: " << err.err_mes()<< endl;
if(err.err_code()==4)
cerr << "Ошибка: " << err.err_mes()<< endl;
if(err.err_code()==5)
cerr << "Ошибка: " << err.err_mes()<< endl;
if(err.err_code()==6)
cerr << "Ошибка: " << err.err_mes()<< endl;
}
return 0;
} |
|
[/CPP]
0
|