鱼C论坛

 找回密码
 立即注册
查看: 2871|回复: 2

矩阵如何随机生成数字,自己编写的矩阵类

[复制链接]
发表于 2018-3-15 14:24:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. [code]#ifndef CMATRIX_H
  2. #define CMATRIX_H
  3. class CMatrix{
  4. public:
  5.         CMatrix(int r,int c);
  6.         ~CMatrix(){};
  7.         void create();
  8.         void output();
  9.         CMatrix operator +(CMatrix &M1);
  10.         CMatrix operator -(CMatrix &M1);
  11.         CMatrix operator *(CMatrix &M1);
  12.         CMatrix tran();
  13. private:
  14.         int row;
  15.         int col;
  16.         int **arr;
  17. };
  18. #endif

  19. #include<iostream>
  20. #include"CMatrix.h"
  21. using namespace std;
  22. CMatrix::CMatrix(int r,int c){
  23.         row=r;
  24.         col=c;
  25.         arr=new int*[r];
  26.         for(int i=0;i<row;i++){
  27.                 arr[i]=new int[c];
  28.         }
  29.         for(i=0;i<row;i++){
  30.                 for(int j=0;j<col;j++){
  31.                         arr[i][j]=0;
  32.                 }
  33.         }

  34. }
  35. void CMatrix::create(){
  36.         cout<<"请输入矩阵的值"<<endl;
  37.         for(int i=0;i<row;i++){
  38.                 for(int j=0;j<col;j++){
  39.                         cin>>arr[i][j];
  40.                 }
  41.         }
  42. }
  43. void CMatrix::output(){
  44.         cout<<"打印矩阵"<<endl;
  45.         for(int i=0;i<row;i++){
  46.                 for(int j=0;j<col;j++){
  47.                         cout<<arr[i][j]<<"\t";
  48.                 }
  49.                 cout<<endl;
  50.         }


  51. }
  52. CMatrix CMatrix::operator +(CMatrix &M1){
  53.         CMatrix M2(row,col);
  54.         if(row!=M1.row||col!=M1.col){
  55.                 cout<<"不符合矩阵相加规则"<<endl;
  56.        
  57.         }else{
  58.         for(int i=0;i<row;i++){
  59.                 for(int j=0;j<col;j++){
  60.                         M2.arr[i][j]=arr[i][j]+M1.arr[i][j];
  61.                 }
  62.         }
  63.         }
  64.         return M2;



  65. }
  66. CMatrix CMatrix::operator -(CMatrix &M1){
  67.         CMatrix M2(row,col);
  68.         if(row!=M1.row||col!=M1.col){
  69.                 cout<<"不符合矩阵相减规则"<<endl;
  70.        
  71.         }else{
  72.         for(int i=0;i<row;i++){
  73.                 for(int j=0;j<col;j++){
  74.                         M2.arr[i][j]=arr[i][j]-M1.arr[i][j];
  75.                 }
  76.         }
  77.         }
  78.         return M2;


  79. }
  80. CMatrix CMatrix::operator *(CMatrix &M1){
  81.         CMatrix M2(row,M1.col);
  82.         if(col!=M1.row){
  83.                 cout<<"不符合乘法规则"<<endl;
  84.         }else{
  85.                 for(int i=0;i<row;i++){[code]#include<iostream>
  86. #include<stdlib.h>
  87. #include"CMatrix.h"
  88. using namespace std;
  89. int main(){
  90.         int x,y;
  91.         cout<<"输入行和列"<<endl;
  92.         cin>>x>>y;
  93.         CMatrix m1(x,y);
  94.         m1.create();
  95.         m1.output();
  96.         cout<<"矩阵的转置"<<endl;
  97.         m1.tran().output();
  98.         CMatrix m2(x,y);
  99.         cout<<"输入矩阵B的值"<<endl;
  100.         m2.create();
  101.         m2.output();
  102.         CMatrix m3(y,x);
  103.         cout<<"输入矩阵C的值"<<endl;
  104.         m3.create();
  105.         m3.output();
  106.         cout<<"矩阵的加法"<<endl;
  107.         CMatrix M4=m1+m2;
  108.         M4.output();
  109.         cout<<"矩阵的减法"<<endl;
  110.         CMatrix M5=m1+m2;
  111.         M5.output();
  112.         cout<<"矩阵的乘法"<<endl;
  113.         CMatrix M6=m1*m3;
  114.         M6.output();
  115.         system("pause");
  116.         return 0;
  117. }
复制代码

                        for(int j=0;j<M1.col;j++){
                                for(int k=0;k<col;k++){
                                        M2.arr[i][j]+=arr[i][k]*M1.arr[k][j];
                                }
                        }
                }
       
        }
        return M2;


}
CMatrix CMatrix::tran(){
        CMatrix M2(col,row);
        for(int i=0 ;i<col;i++){
                for(int j=0;j<row;j++){
                        M2.arr[i][j]=arr[j][i];
                }
        }
        return M2;



}[/code][/code]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-3-15 14:25:42 | 显示全部楼层
想要矩阵自动生成数字
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-15 15:36:36 | 显示全部楼层
用rand函数即可,百度一下用法,你应该就会了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 22:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表