zoukankan      html  css  js  c++  java
  • 矩阵运算(加减乘除) 代码参考

      1 #include <iostream>
      2 
      3 using namespace std;
      4 
      5 void add();
      6 void sub();
      7 void mul();
      8 void div();
      9 
     10 int main()
     11 {
     12     int choice=0;
     13     cout<<"本程序提供行与列均不超过100的矩阵的加减乘除操作"<<endl;
     14     cout<<"输入矩阵的格式如下:"<<endl;
     15     cout<<"1 2 3 4 5 6"<<endl
     16         <<"2 5 4 6 7 8"<<endl
     17         <<"5 8 9 7 4 6"<<endl
     18         <<"此为3行5列矩阵"<<endl<<endl;
     19     while(1)
     20     {
     21         cout<<"选择加法请按1"<<endl
     22             <<"选择减法请按2"<<endl
     23             <<"选择乘法请按3"<<endl
     24             <<"选择除法请按4"<<endl
     25             <<"结束程序请按0"<<endl;
     26         cin>>choice;
     27         if(choice==1)   add();
     28         else if(choice==2)  sub();
     29         else if(choice==3)  mul();
     30         else if(choice==4)  div();
     31         else if(choice==0)  break;
     32         else    cout<<"输入有误!请重新输入"<<endl;
     33     }
     34     return 0;
     35 }
     36 
     37 void add()
     38 {
     39     int n,m;
     40     double arr1[110][110]={0},arr2[110][110]={0},arr3[110][110]={0};
     41     cout<<"矩阵加法要求两个矩阵同型,请输入矩阵的行数和列数,用空格隔开:";
     42     cin>>n>>m;
     43     cout<<"您的第一个矩阵是:"<<endl;
     44     for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    cin>>arr1[i][j];
     45     cout<<"第二个矩阵是:"<<endl;
     46     for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    cin>>arr2[i][j];
     47     cout<<"两个矩阵相加的结果是:"<<endl;
     48     //计算与输出同时进行
     49     for(int i=0;i<n;i++)
     50     {
     51         for(int j=0;j<m;j++)
     52         {
     53             arr3[i][j]=arr1[i][j]+arr2[i][j];
     54             if(j!=0)    cout<<' ';
     55             cout<<arr3[i][j];
     56             if(j==m-1)  cout<<endl;
     57         }
     58     }
     59     cout<<endl;//格式美化
     60     return;
     61 }
     62 
     63 void sub()
     64 {
     65     int n,m;
     66     double arr1[110][110]={0},arr2[110][110]={0},arr3[110][110]={0};
     67     cout<<"矩阵减法要求两个矩阵同型,请输入矩阵的行数和列数,用空格隔开:";
     68     cin>>n>>m;
     69     cout<<"您的第一个矩阵是:"<<endl;
     70     for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    cin>>arr1[i][j];
     71     cout<<"第二个矩阵是:"<<endl;
     72     for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    cin>>arr2[i][j];
     73     cout<<"两个矩阵相减的结果是:"<<endl;
     74     //计算与输出同时进行,减少代码量
     75     for(int i=0;i<n;i++)
     76     {
     77         for(int j=0;j<m;j++)
     78         {
     79             arr3[i][j]=arr1[i][j]-arr2[i][j];
     80             if(j!=0)    cout<<' ';
     81             cout<<arr3[i][j];
     82             if(j==m-1)  cout<<endl;
     83         }
     84     }
     85     cout<<endl;//格式美化
     86     return;
     87 }
     88 
     89 void mul()
     90 {
     91     int n1,m1,n2,m2;
     92     double arr1[110][110]={0},arr2[110][110]={0},arr3[110][110]={0};
     93     cout<<"请输入第一个矩阵的行数和列数,用空格隔开:";
     94     cin>>n1>>m1;
     95     cout<<"请输入第二个矩阵的行数和列数,用空格隔开:";
     96     cin>>n2>>m2;
     97     if(m1!=n2)
     98     {
     99         cout<<"两矩阵无法相乘!"<<endl<<endl;
    100         return;
    101     }
    102     cout<<"请输入第一个矩阵:"<<endl;
    103     for(int i=0;i<n1;i++)   for(int j=0;j<m1;j++)   cin>>arr1[i][j];
    104     cout<<"请输入第二个矩阵:"<<endl;
    105     for(int i=0;i<n2;i++)   for(int j=0;j<m2;j++)   cin>>arr2[i][j];
    106     cout<<"两矩阵相乘结果为:"<<endl;
    107     //计算与输出同时进行,缩小代码量
    108     for(int i=0;i<n1;i++)
    109     {
    110         for(int j=0;j<m2;j++)
    111         {
    112             for(int k=0;k<m1;k++)    arr3[i][j]+=arr1[i][k]*arr2[k][j];
    113             if(j!=0)    cout<<' ';
    114             cout<<arr3[i][j];
    115             if(j==m2-1) cout<<endl;
    116         }
    117     }
    118     cout<<endl;//格式美化
    119     return;
    120 }
    121 
    122 void div()
    123 {
    124     //本除法运用 A*E=E*(A逆) 原理
    125     cout<<"注:矩阵相除要求除数矩阵必须是可逆方阵,请使用者输入正确的除数矩阵"<<endl;
    126     int n1,m1,n2,m2;
    127     double temp,arr1[110][110]={0},arr2[110][110]={0},arr3[110][110]={0},arr4[110][110]={0};
    128     cout<<"请输入被除矩阵的行数和列数,用空格隔开:";
    129     cin>>n1>>m1;
    130     cout<<"请输入除数矩阵的行数和列数,用空格隔开:";
    131     cin>>n2>>m2;
    132     if(m1!=n2)
    133     {
    134         cout<<"两矩阵无法相除!"<<endl<<endl;
    135         return;
    136     }
    137     cout<<"请输入被除矩阵:"<<endl;
    138     for(int i=0;i<n1;i++)   for(int j=0;j<m1;j++)   cin>>arr1[i][j];
    139     cout<<"请输入除数矩阵:"<<endl;
    140     for(int i=0;i<n2;i++)   for(int j=0;j<m2;j++)   cin>>arr2[i][j];
    141     //除数矩阵求逆处理
    142     //创造E
    143     for(int i=0;i<n2;i++)   for(int j=0;j<m2;j++)   if(i==j)    arr3[i][j]=1;
    144     //默认A与E组成新的矩阵,对对角线进行化1,并从左下往右上分两部分化0
    145     for(int k=0;k<n2;k++)
    146     {
    147         temp=arr2[k][k];
    148         for(int i=0;i<m2;i++) //该行首位非0项化1
    149         {
    150             arr2[k][i]/=temp;
    151             arr3[k][i]/=temp;
    152         }
    153         for(int i=k+1;i<n2;i++)
    154         {
    155             temp=arr2[i][k];
    156             for(int j=0;j<m2;j++)
    157             {
    158                 arr2[i][j]-=temp*arr2[k][j];
    159                 arr3[i][j]-=temp*arr3[k][j];
    160             }
    161         }
    162     }
    163     for(int k=n2-1;k>=0;k--)
    164     {
    165         temp=arr2[k][k];
    166         for(int i=0;i>=0;i--)
    167         {
    168             arr2[k][i]/=temp;
    169             arr3[k][i]/=temp;
    170         }
    171         for(int i=k-1;i>=0;i--)
    172         {
    173             temp=arr2[i][k];
    174             for(int j=0;j<m2;j++)
    175             {
    176                 arr2[i][j]-=temp*arr2[k][j];
    177                 arr3[i][j]-=temp*arr3[k][j];
    178             }
    179         }
    180     }
    181     cout<<"两矩阵相除结果为:"<<endl;
    182     for(int i=0;i<n1;i++)
    183     {
    184         for(int j=0;j<m2;j++)
    185         {
    186             for(int k=0;k<m1;k++)    arr4[i][j]+=arr1[i][k]*arr3[k][j];
    187             if(j!=0)    cout<<' ';
    188             cout<<arr4[i][j];
    189             if(j==m2-1) cout<<endl;
    190         }
    191     }
    192     cout<<endl;//格式美化
    193     return;
    194 }
  • 相关阅读:
    Coding Souls团队---电梯演讲
    第一次团队会议总结-NABCD分析
    软件工程团队项目介绍
    python进行四舍五入
    python列表元素两两比较
    Linux常用命令
    谷歌日历的正确用法--在谷歌日历中添加农历、天气、中国节假日
    Nose框架的安装
    python中staticmethod装饰器的作用
    python 3.x与python 2.7.x在语法上的区别
  • 原文地址:https://www.cnblogs.com/Conan-jine/p/12592818.html
Copyright © 2011-2022 走看看