C++用类实现矩阵时出现的问题,主要是二维数组,还需要动态申请内存搞得有点晕

C++用类实现矩阵时出现的问题,主要是二维数组,还需要动态申请内存搞得有点晕,第1张

你的程序缺了太多的细节处理,

而且动态二位数组申请,访问和删除都有问题,

下面已经都给你改好了,并且在VC上编译运行通过,具体的看一下注释:

#include<iostream>

#include<stdlib.h>

#include<malloc.h>

using namespace std

class Matrix

{

public:

Matrix() {lines=0rows=0}

Matrix(int x,int y)

Matrix(Matrix &m)

int** GetPtr() {return ptr}

int GetLine(){return lines}//这里需要加一个lines返回的接口

void size()

{

ptr=new int * [rows]

for(int i = 0i <linesi++)

{

ptr[i] = new int[lines]

}

}

friend void InputLine(Matrix &a)

friend void InputRow(Matrix &a)

friend void Initi( Matrix &a )

friend void PlusMatrix(Matrix &a,Matrix &b,Matrix &c)

friend void MinusMatrix(Matrix &a,Matrix &b,Matrix &c)

void disp()

{

int i,j

cout<<"The matrix is:"<<endl

for(i=0i<rowsi++)

{

for(j=0j<linesj++)

cout<<*(ptr[i]+j)<<" "

cout<<endl

}

}

~Matrix() {}

private:

int lines,rows,**ptr

}

Matrix::Matrix(int x,int y):lines(x),rows(y)

{

cout<<"Rectangle构造函数被调用"<<endl

int i

for(i=0i<rowsi++)

{

ptr[i] = new int [lines]

}

}

Matrix::Matrix(Matrix &m)

{

lines=m.lines

rows=m.rows

int i

for(i=0i<rowsi++)

{

ptr[i]=m.ptr[i]

}

cout<<"拷贝函数被调用"<<endl

}

void InputLine(Matrix &a)

{

cout<<"Please input the number of lines:"

cin>>a.lines

}

void InputRow(Matrix &a)

{

cout<<"Please input the number of rows:"

cin>>a.rows

}

void Initi( Matrix &a )

{

int i,j

//在这里要给a动态申请内存

a.ptr=new int *[a.rows+1]

for(i=0i<a.rowsi++)

a.ptr[i]=new int [a.lines+1]

//在这里要给a动态申请内存

cout<<"Please input the member :"<<endl

for(i=0i<a.rowsi++)//注意是<,不是<=

for(j=0j<a.linesj++)//注意是<,不是<=

cin>>*(a.ptr[i]+j)

}

void PlusMatrix(Matrix &a,Matrix &b,Matrix &c)

{

int i,j

//在这里要给c动态申请内存

c.ptr=new int *[a.rows+1]

for(i=0i<a.rowsi++)

c.ptr[i]=new int [a.lines+1]

c.lines=a.lines

c.rows=a.rows

//在这里要给c动态申请内存

for(i=0i<a.rowsi++)

{

for(j=0j<a.linesj++)

*(c.ptr[i]+j)=*(a.ptr[i]+j)+*(b.ptr[i]+j)

}

}

void MinusMatrix(Matrix &a,Matrix &b,Matrix &c)

{

int i,j

for(i=0i<a.rowsi++)//注意是<,不是<=

{

for(j=0j<a.linesj++)//注意是<,不是<=

*(c.ptr[i]+j)=*(a.ptr[i]+j)-*(b.ptr[i]+j)

}

}

int main()

{

Matrix A1,A2,A3

int **p//临时指针,释放动态数组时用

int i//临时变量

int *temp

InputLine(A1)

InputRow(A1)

Initi(A1)

InputLine(A2)

InputRow(A2)

Initi(A2)

PlusMatrix(A1,A2,A3)

cout<<"sum:"<<endl

A3.disp()

MinusMatrix(A1,A2,A3)

cout<<"difference:"<<endl

A3.disp()

//正确的二维数组的释放,应该这么写

p=A1.GetPtr()

for(i=0i<A1.GetLine()i++)

{

delete p[i]

}

delete p

p=A2.GetPtr()

for(i=0i<A2.GetLine()i++)

{

delete p[i]

}

delete p

p=A3.GetPtr()

for(i=0i<A3.GetLine()i++)

{

delete p[i]

}

delete p

system("pause")

return 0

}

运行实例:

Please input the number of lines:2

Please input the number of rows:2

Please input the member :

1

2

3

4

Please input the number of lines:2

Please input the number of rows:2

Please input the member :

1

2

4

5

sum:

The matrix is:

2 4

7 9

difference:

The matrix is:

0 0

-1 -1

请按任意键继续. . .

#include <iostream>

#include <string.h>

using namespace std

class Matrix3

{

public:

Matrix3()

Matrix3(const double arr[][3])

Matrix3(const Matrix3&mat)

Matrix3&operator= (const Matrix3&mat)

~Matrix3()

Matrix3 operator+ (const Matrix3&mat) const

Matrix3 operator- (const Matrix3&mat) const

Matrix3 operator* (const Matrix3&mat) const

double Det() const

Matrix3 T () const

double Get(const int i,const int j) const { return mArray[i][j]}

void Set(const int i,const int j,const double value) { mArray[i][j] = value}

double operator() (const int i,const int j) const { return mArray[i][j]}

double&operator() (const int i,const int j) { return mArray[i][j]}

friend ostream&operator<<(ostream&o,const Matrix3&mat)

private:

double mArray[3][3]

}

Matrix3::Matrix3()

{

double arr[3][3] = {0}

memcpy(mArray,arr,sizeof(mArray))

}

Matrix3::~Matrix3()

{

}

Matrix3::Matrix3(const double arr[][3])

{

for(int i=0i<3i++)

for(int j=0j<3j++)

mArray[i][j] = arr[i][j]

//memcpy(mArray,arr,sizeof(mArray))

}

Matrix3::Matrix3(const Matrix3&mat)

{

memcpy(mArray,mat.mArray,sizeof(mArray))

}

Matrix3 Matrix3::operator+ (const Matrix3&mat) const

{

Matrix3 tmp

for(int i=0i<3i++)

for(int j=0j<3j++)

tmp.mArray[i][j] = mArray[i][j] + mat.mArray[i][j]

return tmp

}

Matrix3 Matrix3::operator- (const Matrix3&mat) const

{

Matrix3 tmp

for(int i=0i<3i++)

for(int j=0j<3j++)

tmp.mArray[i][j] = mArray[i][j] - mat.mArray[i][j]

return tmp

}

Matrix3 Matrix3::operator* (const Matrix3&mat) const

{

Matrix3 tmp

for(int i=0i<3i++)

for(int j=0j<3j++)

for(int k=0k<3k++)

tmp.mArray[i][j] += mArray[i][k] * mat.mArray[k][j]

return tmp

}

double Matrix3::Det() const

{

return mArray[0][0] * mArray[1][1] * mArray[2][2]

+ mArray[0][1] * mArray[1][2] * mArray[2][0]

+ mArray[1][0] * mArray[2][1] * mArray[0][2]

- mArray[0][2] * mArray[1][1] * mArray[2][0]

- mArray[0][1] * mArray[1][0] * mArray[2][2]

- mArray[1][2] * mArray[2][1] * mArray[0][0]

}

Matrix3 Matrix3::T () const

{

Matrix3 tmp

for(int i=0i<3i++)

for(int j=0j<3j++)

tmp.mArray[i][j] = mArray[j][i]

return tmp

}

Matrix3&Matrix3::operator= (const Matrix3&mat)

{

memcpy(mArray,mat.mArray,sizeof(mArray))

return (*this)

}

ostream&operator<<(ostream&o,const Matrix3&mat)

{

o <<"[\n"

for(int i=0i<3i++)

{

for(int j=0j<3j++)

o <<mat.mArray[i][j] <<"\t"

o <<endl

}

o <<"]"

return o

}

void main()

{

double arr[3][3] = {1,2,3,4,5,6,7,8,9}

double arr2[3][3] = {9,8,7,6,5,4,3,2,1}

Matrix3 m1(arr),m2(arr2)

cout <<"m1 = \n" <<m1 <<endl <<endl

cout <<"m2 = \n" <<m2 <<endl <<endl

cout <<"m1(1,1) = " <<m1(1,1) <<endl <<endl

m1(1,1) = 8

cout <<"set m1(1,1) = 8, then m1(1,1) = " <<m1(1,1) <<endl <<endl

cout <<"m1 = \n" <<m1 <<endl <<endl

cout <<"m1 + m2 = \n" <<m1 + m2 <<endl <<endl

cout <<"m1 - m2 = \n" <<m1 - m2 <<endl <<endl

cout <<"m1 * m2 = \n" <<m1 * m2 <<endl <<endl

cout <<"Det(m1) = " <<m1.Det() <<endl <<endl

cout <<"m1' = \n" <<m1.T() <<endl <<endl

m2 = m1

cout <<"set m2 = m1, then m2 = \n" <<m2 <<endl <<endl

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/7916909.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存