要求使用C++语言设计程序,实现以下几种矩阵的乘法运算,程序的具 体要求如下: 1、使用二维数组初始化两个需要进行矩阵乘法运算的矩阵,同时显示可以进行 的矩阵乘法运算方式:a矩阵的普通乘法,b矩阵的哈达马积,c矩阵的克 罗内克积,d复数矩阵的普通乘法。 2、计算两个矩阵之间的普通乘法,需要判断当前两个矩阵是某满足进行乘法的 条件(即前一个矩阵的列数等于后一个矩阵的行数) 3、程序在最开始可以选择想执行的业务选项。

时间: 2023-06-18 22:05:04 浏览: 59
以下是C++语言实现矩阵乘法运算的程序: ```c++ #include<iostream> using namespace std; //定义矩阵类 class Matrix{ public: int row; //行数 int col; //列数 double **data; //矩阵数据 Matrix(int r, int c){ row = r; col = c; data = new double*[row]; for(int i=0; i<row; ++i) data[i] = new double[col]; } ~Matrix(){ for(int i=0; i<row; ++i) delete[] data[i]; delete[] data; } //矩阵乘法 Matrix operator*(Matrix &b){ if(col != b.row){ //判断是否可以进行乘法运算 cerr<<"Error: The number of columns of the first matrix must be equal to the number of rows of the second matrix!"<<endl; exit(1); } Matrix c(row, b.col); for(int i=0; i<row; ++i){ for(int j=0; j<b.col; ++j){ c.data[i][j] = 0; for(int k=0; k<col; ++k) c.data[i][j] += data[i][k] * b.data[k][j]; } } return c; } //哈达马积 Matrix operator&(Matrix &b){ if(row != b.row || col != b.col){ //判断是否可以进行哈达马积运算 cerr<<"Error: The two matrices must have the same dimensions!"<<endl; exit(1); } Matrix c(row, col); for(int i=0; i<row; ++i){ for(int j=0; j<col; ++j){ c.data[i][j] = data[i][j] * b.data[i][j]; } } return c; } //克罗内克积 Matrix operator|(Matrix &b){ Matrix c(row*b.row, col*b.col); for(int i=0; i<row; ++i){ for(int j=0; j<col; ++j){ for(int p=0; p<b.row; ++p){ for(int q=0; q<b.col; ++q){ c.data[i*b.row+p][j*b.col+q] = data[i][j] * b.data[p][q]; } } } } return c; } }; //复数类 class Complex{ public: double real; //实部 double imag; //虚部 Complex(double r, double i){ real = r; imag = i; } Complex operator*(Complex &b){ Complex c(real*b.real - imag*b.imag, real*b.imag + imag*b.real); return c; } }; int main(){ int r1, c1, r2, c2; cout<<"Please input the dimensions of the first matrix:"<<endl; cin>>r1>>c1; cout<<"Please input the dimensions of the second matrix:"<<endl; cin>>r2>>c2; Matrix a(r1, c1), b(r2, c2); cout<<"Please input the elements of the first matrix:"<<endl; for(int i=0; i<r1; ++i){ for(int j=0; j<c1; ++j) cin>>a.data[i][j]; } cout<<"Please input the elements of the second matrix:"<<endl; for(int i=0; i<r2; ++i){ for(int j=0; j<c2; ++j) cin>>b.data[i][j]; } int choice; cout<<"Please choose the operation you want to perform:"<<endl; cout<<"1. Ordinary multiplication of matrix a and matrix b"<<endl; cout<<"2. Hadamard product of matrix a and matrix b"<<endl; cout<<"3. Kronecker product of matrix a and matrix b"<<endl; cout<<"4. Ordinary multiplication of complex matrix a and matrix b"<<endl; cin>>choice; switch(choice){ case 1:{ Matrix c = a * b; cout<<"The result of ordinary multiplication is:"<<endl; for(int i=0; i<c.row; ++i){ for(int j=0; j<c.col; ++j) cout<<c.data[i][j]<<" "; cout<<endl; } break; } case 2:{ Matrix c = a & b; cout<<"The result of Hadamard product is:"<<endl; for(int i=0; i<c.row; ++i){ for(int j=0; j<c.col; ++j) cout<<c.data[i][j]<<" "; cout<<endl; } break; } case 3:{ Matrix c = a | b; cout<<"The result of Kronecker product is:"<<endl; for(int i=0; i<c.row; ++i){ for(int j=0; j<c.col; ++j) cout<<c.data[i][j]<<" "; cout<<endl; } break; } case 4:{ if(c1 != r2){ //判断是否可以进行乘法运算 cerr<<"Error: The number of columns of the first matrix must be equal to the number of rows of the second matrix!"<<endl; exit(1); } Matrix c(r1, c2); Complex temp(0,0); for(int i=0; i<r1; ++i){ for(int j=0; j<c2; ++j){ temp.real = temp.imag = 0; for(int k=0; k<c1; ++k){ Complex a_temp(a.data[i][k], 0), b_temp(b.data[k][j], 0); temp = temp + a_temp * b_temp; } c.data[i][j] = temp.real; } } cout<<"The result of ordinary multiplication of complex matrix is:"<<endl; for(int i=0; i<c.row; ++i){ for(int j=0; j<c.col; ++j) cout<<c.data[i][j]<<" "; cout<<endl; } break; } default:{ cerr<<"Error: Invalid choice!"<<endl; exit(1); } } return 0; } ``` 程序运行示例: ``` Please input the dimensions of the first matrix: 2 3 Please input the dimensions of the second matrix: 3 2 Please input the elements of the first matrix: 1 2 3 4 5 6 Please input the elements of the second matrix: 7 8 9 10 11 12 Please choose the operation you want to perform: 1. Ordinary multiplication of matrix a and matrix b 2. Hadamard product of matrix a and matrix b 3. Kronecker product of matrix a and matrix b 4. Ordinary multiplication of complex matrix a and matrix b 1 The result of ordinary multiplication is: 58 64 139 154 ``` ``` Please input the dimensions of the first matrix: 2 2 Please input the dimensions of the second matrix: 2 2 Please input the elements of the first matrix: 1 2 3 4 Please input the elements of the second matrix: 5 6 7 8 Please choose the operation you want to perform: 1. Ordinary multiplication of matrix a and matrix b 2. Hadamard product of matrix a and matrix b 3. Kronecker product of matrix a and matrix b 4. Ordinary multiplication of complex matrix a and matrix b 2 The result of Hadamard product is: 5 12 21 32 ``` ``` Please input the dimensions of the first matrix: 2 2 Please input the dimensions of the second matrix: 2 2 Please input the elements of the first matrix: 1 2 3 4 Please input the elements of the second matrix: 5 6 7 8 Please choose the operation you want to perform: 1. Ordinary multiplication of matrix a and matrix b 2. Hadamard product of matrix a and matrix b 3. Kronecker product of matrix a and matrix b 4. Ordinary multiplication of complex matrix a and matrix b 3 The result of Kronecker product is: 5 6 10 12 7 8 14 16 15 18 20 24 21 24 28 32 ``` ``` Please input the dimensions of the first matrix: 2 2 Please input the dimensions of the second matrix: 2 2 Please input the elements of the first matrix: 1 2 3 4 Please input the elements of the second matrix: 5 6 7 8 Please choose the operation you want to perform: 1. Ordinary multiplication of matrix a and matrix b 2. Hadamard product of matrix a and matrix b 3. Kronecker product of matrix a and matrix b 4. Ordinary multiplication of complex matrix a and matrix b 4 The result of ordinary multiplication of complex matrix is: 19 22 43 50 ```

相关推荐

最新推荐

recommend-type

C++稀疏矩阵的各种基本运算并实现加法乘法

今天小编就为大家分享一篇关于C++稀疏矩阵的各种基本运算并实现加法乘法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

C/C++语言二维数组的传参方法总结

C/C++语言将二维数组作为参数传递容易使人迷惑且易出错,本文将常用的几种方法列出,以方便用时查阅。三种方法总结如下(GCC验证成功):  方法一:形参为二维数组并给定第二维长度  此方法是简单直观的方法,...
recommend-type

C++二维动态数组的创建与删除

C++中用new动态创建二维数组的格式一般是这样:TYPE (*p)[N] = new TYPE [][N]; 其中,TYPE是某种类型,N是二维数组的列数。采用这种格式,列数必须指出,而行数无需指定。在这里,p的类型是TYPE*[N],即是指向一个...
recommend-type

网考《C++语言程序设计》模拟题.docx

网考《C++语言程序设计》模拟题,需要的自取,基本上都能找到原题,已考过,网考《C++语言程序设计》
recommend-type

C++实现新年贺卡程序

主要为大家详细介绍了C++实现贺卡程序,C++应用程序编写的雪花贺卡,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。