矩阵相乘
来源:互联网 发布:排课系统算法 编辑:程序博客网 时间:2024/05/01 20:21
// test2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix(int x,int y);
~Matrix();
void Print();
void Input();
Matrix(const Matrix &s);
friend Matrix operator * (const Matrix &s1,const Matrix &s2) ; //要将*重载为该类的友元函数
private:
int m,n;
double **data;
};
/*------------构造函数-----------------*/
Matrix::Matrix(int x,int y)
{
m=x;
n=y;
data=new double* [m];
int i(0),j(0);
for(;i!=m;i++)
data[i]=new double [n];
for(i=0;i!=m;i++)
for(j=0;j!=n;j++)
data[i][j]=0;
}
/*------------析构函数-----------------*/
Matrix::~Matrix()
{
for(int j=0;j!=m;j++)
delete[] data[j];
}
/*------------输出矩阵-----------------*/
void Matrix::Print()
{
int i,j;
for(i=0;i!=m;i++)
{
for(j=0;j!=n;j++)
{
cout<<data[i][j]<<" ";
}
cout<<endl;
}
}
/*------------输入矩阵-----------------*/
void Matrix::Input()
{
for(int i=0;i!=m;i++)
for(int j=0;j!=n;j++)
cin>>data[i][j];
}
m=src.m;
n=src.n;
int i,j;
data=new double* [m];
for(i=0;i<m;i++)
data[i]=new double[n];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
data[i][j]=src.data[i][j];
}
{
if(s1.n!=s2.m)
{
cout<<"两矩阵无法相乘!"<<endl;
exit(0);
}
Matrix result(s1.m,s2.n);
for(int i=0;i!=s1.m;i++)
for(int j=0;j!=s2.n;j++)
for(int k=0;k!=s1.n;k++)
result.data[i][j]+=s1.data[i][k]*s2.data[k][j];
return result;
}
{
int x(0),y(0);
cout<<"请输入矩阵1的行:";
cin>>x;
cout<<"请输入矩阵1的列:";
cin>>y;
Matrix A(x,y);
cout<<"请输入矩阵1的元素,元素个数为:"<<x<<"*"<<y<<endl;
A.Input();
cout<<"请输入矩阵2的行:";
cin>>x;
cout<<"请输入矩阵2的列:";
cin>>y;
Matrix B(x,y);
cout<<"请输入矩阵2的元素,元素个数为:"<<x<<"*"<<y<<endl;
B.Input();
Matrix C=A*B; //Matrix C= operator*(A,B);也可以
C.Print();
system("pause");
return 0;
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix(int x,int y);
~Matrix();
void Print();
void Input();
Matrix(const Matrix &s);
friend Matrix operator * (const Matrix &s1,const Matrix &s2) ; //要将*重载为该类的友元函数
private:
int m,n;
double **data;
};
/*------------构造函数-----------------*/
Matrix::Matrix(int x,int y)
{
m=x;
n=y;
data=new double* [m];
int i(0),j(0);
for(;i!=m;i++)
data[i]=new double [n];
for(i=0;i!=m;i++)
for(j=0;j!=n;j++)
data[i][j]=0;
}
/*------------析构函数-----------------*/
Matrix::~Matrix()
{
for(int j=0;j!=m;j++)
delete[] data[j];
}
/*------------输出矩阵-----------------*/
void Matrix::Print()
{
int i,j;
for(i=0;i!=m;i++)
{
for(j=0;j!=n;j++)
{
cout<<data[i][j]<<" ";
}
cout<<endl;
}
}
/*------------输入矩阵-----------------*/
void Matrix::Input()
{
for(int i=0;i!=m;i++)
for(int j=0;j!=n;j++)
cin>>data[i][j];
}
/*------------拷贝构造函数-----------------*/
Matrix::Matrix( const Matrix &src)
{m=src.m;
n=src.n;
int i,j;
data=new double* [m];
for(i=0;i<m;i++)
data[i]=new double[n];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
data[i][j]=src.data[i][j];
}
/*------------乘法运算符重载-----------------*/
Matrix operator * (const Matrix &s1,const Matrix &s2){
if(s1.n!=s2.m)
{
cout<<"两矩阵无法相乘!"<<endl;
exit(0);
}
Matrix result(s1.m,s2.n);
for(int i=0;i!=s1.m;i++)
for(int j=0;j!=s2.n;j++)
for(int k=0;k!=s1.n;k++)
result.data[i][j]+=s1.data[i][k]*s2.data[k][j];
return result;
}
{
int x(0),y(0);
cout<<"请输入矩阵1的行:";
cin>>x;
cout<<"请输入矩阵1的列:";
cin>>y;
Matrix A(x,y);
cout<<"请输入矩阵1的元素,元素个数为:"<<x<<"*"<<y<<endl;
A.Input();
cout<<"请输入矩阵2的行:";
cin>>x;
cout<<"请输入矩阵2的列:";
cin>>y;
Matrix B(x,y);
cout<<"请输入矩阵2的元素,元素个数为:"<<x<<"*"<<y<<endl;
B.Input();
Matrix C=A*B; //Matrix C= operator*(A,B);也可以
C.Print();
system("pause");
return 0;
}
【过程中曾出现过如下问题】:http://bbs.csdn.net/topics/390804720?page=1#post-397524154
0 0
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘
- 矩阵相乘!
- 矩阵相乘
- 为什么printf只能用_cdecl调用约定
- openwrt 配置 wifidog
- MyPoint类
- 活动安排_贪心算法
- 位运算(二进制位编号顺序)
- 矩阵相乘
- Linux生成动态链接库的-fPIC参数问题
- poj 2135 Farm Tour
- Double
- mysql存储过程
- webpy+uploadify实现文件异步上传
- 归并排序
- QFE23xx揭秘:最强大的Qualcomm RF360前端芯片——更多频带,更多模式,更小的PCB尺寸
- spss Clementine12 破解方法