一道oj刷题中见到的,对于一维数组实现矩阵的坐标变化还想了好一阵,现在总结一下。
这里重载了<<和>>运算符
//错误:一开始没有写复制构造函数,并且重载赋值函数里没有delete,所以导致溢出错误,切记切记
//vs就出现了未加载wtndll的错误,就是因为指针指向错误导致的栈溢出
#include<iostream>
using namespace std;
class integer {
protected:
int n;
};
class Vector :public integer {
protected:
int* v;
public:
Vector(int nu)
{
n = nu; v = new int[nu];
for (int i = 0; i < nu; i++)
v[i] = 0;
}
Vector(int num, int* vec) {
n = num;
v = new int[num];
for (int i = 0; i < num; i++)
{
v[i] = vec[i];
}
}
Vector() {
n = 1;
v = new int[n];
}
~Vector() { delete[]v; v = NULL; n = 0; }
};
class Matrix :public Vector {
int column, row;
public:
Matrix(int r, int c) :Vector(c* r) {
column = c; row = r;
}
Matrix() { column = 1; row = 1; }
Matrix(const Matrix& m) {
column = m.column; row = m.row; v = new int[column * row];
for (int i = 0; i < column * row; i++) {
v[i] = m.v[i];
}
}
friend Matrix operator+(const Matrix& m1, const Matrix& m2);
friend Matrix operator- (const Matrix& m1, const Matrix& m2);
Matrix operator* (const Matrix& m1);
Matrix& operator =(const Matrix&);
friend ostream& operator<<(ostream& output, const Matrix& m);
friend istream& operator>>(istream& input, Matrix& m);
};
Matrix operator+(const Matrix& m1, const Matrix& m2) {
if (m1.column != m2.column || m1.row != m2.row) {
cout << "matrix size not match" << endl;
exit(0);
}
else {
Matrix temp(m1.row, m1.column);
for (int i = 0; i < m1.row; i++) {
for (int j = 0; j < m1.column; j++) {
temp.v[i * m1.column + j] = m1.v[i * m1.column + j] + m2.v[i * m2.column + j];//
}
}
return temp;
}
}
Matrix operator-(const Matrix& m1, const Matrix& m2) {
if (m1.column != m2.column || m1.row != m2.row) {
cout << "matrix size not match" << endl;
exit(0);
}
else {
Matrix temp(m1.row, m1.column);
for (int i = 0; i < m1.row; i++) {
for (int j = 0; j < m1.column; j++) {
temp.v[i * m1.column + j] = m1.v[i * m1.column + j] - m2.v[i * m2.column + j];//注意此处的下标变化
}
}
return temp;
}
}
Matrix& Matrix::operator =(const Matrix& m) {
delete[]v;
column = m.column;
row = m.row;
n = m.column * m.row;
v = new int[n];
for (int i = 0; i < row * column; i++) {
v[i] = m.v[i];
}
return *this;
}
ostream& operator<<(ostream& output, const Matrix& m) {
for (int i = 0; i < m.row; i++)
{
for (int j = 0; j < m.column; j++)
{
output << m.v[i * m.column + j];
if (j != m.column - 1)
output << " ";
else
output << "\n";
}
}
return output;
}
istream& operator>>(istream& input, Matrix& m) {
for (int i = 0; i < m.row; i++) {
for (int j = 0; j < m.column; j++) {
input >> m.v[i * m.column + j];
}
}
return input;
}
Matrix Matrix::operator*(const Matrix& m1) {
if (column != m1.row)
{
cout << "matrix size not match" << endl;
exit(0);
}
else
{
int re = row;
int ce = m1.column;
Matrix result(re, ce);
for (int i = 0; i < re; i++)
{
for (int j = 0; j < ce; j++)
{
for (int k = 0; k < column; k++) {
result.v[i * ce + j] += v[i * column + k] * m1.v[k * ce + j];
}
}
}
return result;
}
}
/*int main() {
int r1, r2, c1, c2;
cout << "Input the numble of row and column of Matrix A:\n";
cin >> r1 >> c1;
Matrix A(r1, c1);
cout << "Input the elements of Matrix A:\n";
cin >> A;
cout << "Input the numble of row and column of Matrix B:\n";
cin >> r2 >> c2;
Matrix B(r2, c2);
cout << "Input the elements of Matrix B:\n";
cin >> B;
Matrix C;
cout << "A+B=" << endl;
C = A + B;
cout << C;
cout << "A-B=" << endl;
C = A - B;
cout << C;
cout << "A*B=" << endl;
C = A * B;
cout << C;
}*/
焦头烂额了好半天,都是粗心惹的祸。
写程序不能心浮气躁,要静下心来一点点调试。