首页 > 其他分享 >矩阵

矩阵

时间:2023-07-31 23:45:02浏览次数:34  
标签:cout int 矩阵 column ++ 100 line

请使用C++实现矩阵的各种运算
1、矩阵创建
2、矩阵相加
3、矩阵相减
4、矩阵相乘
5、数字乘矩阵
6、矩阵上叠加
7、矩阵左右叠加
8、矩阵转置
9、矩阵旋转
10、矩阵求逆
11、矩阵输出

using namespace std;
int line; int column;
int line2; int column2;
int matrix2[100][100];
int matrix[100][100];
int result[100][100];
void Creat(int x) {
    int line; int column;//行与列
    cout << "请输入行与列";
    cout << "行:";
    cin >> line;
    cout << "列:";
    cin >> column;
    int k = 0;
    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            k++;
            cout << k << " ";
        }
        cout << endl;

    }
    
}
void add(int x) {//矩阵相加
    cout << "请输入第一个矩阵的行与列";
  
    cout << "行(100):";
    cin >> line;
    cout << "列(100):";
    cin >> column;
    int matrix[100][100];
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix[i][j] = 0;
        }
    }
    
    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin>>matrix[i][j];
        }
        cout << endl;

    }
    cout << "请输入第二个矩阵的行与列";
  
    cout << "行:";
    cin >> line2;
    cout << "列:";
    cin >> column2;
    int matrix2[100][100];
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix2[i][j] = 0;
        }
    }
    for (int i = 0; i < line2; i++) {
        for (int j = 0; j < column2; j++) {
            cout << "请输入数字:"<<i+1<<"行"<<j+1<<"列";
            cin >> matrix2[i][j];
        }
        cout << endl;

    }
    if (line == line2 && column == column2) {
        for (int i = 0; i < line2; i++) {
            for (int j = 0; j < column2; j++) {

                cout<< matrix[i][j] + matrix2[i][j];
            }
            cout << endl;

        }
    }
    else
    {
        cout << "无法进行运算,因为两个矩阵的行与列不相同";
    }
}
void subtract(int x) {
    cout << "请输入第一个矩阵的行与列";
    
    cout << "行(100):";
    cin >> line;
    cout << "列(100):";
    cin >> column;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix[i][j] = 0;
        }
    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix[i][j];
        }
        cout << endl;

    }
    cout << "请输入第二个矩阵的行与列";
    
    cout << "行:";
    cin >> line2;
    cout << "列:";
    cin >> column2;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix2[i][j] = 0;
        }
    }
    for (int i = 0; i < line2; i++) {
        for (int j = 0; j < column2; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix2[i][j];
        }
        cout << endl;

    }
    if (line == line2 && column == column2) {
        for (int i = 0; i < line2; i++) {
            for (int j = 0; j < column2; j++) {

                cout << matrix[i][j] - matrix2[i][j];
            }
            cout << endl;

        }
    }
    else
    {
        cout << "无法进行运算,因为两个矩阵的行与列不相同";
    }
}
void multiply(int x) {

    cout << "请输入第一个矩阵的行与列";

    cout << "行(100):";
    cin >> line;
    cout << "列(100):";
    cin >> column;
   
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix[i][j] = 0;
             result[i][j]=0;
        }
    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix[i][j];
        }
        cout << endl;

    }
    cout << "请输入第二个矩阵的行与列";
   
    cout << "行:";
    cin >> line2;
    cout << "列:";
    cin >> column2;
  
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix2[i][j] = 0;
        }
    }
    for (int i = 0; i < line2; i++) {
        for (int j = 0; j < column2; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix2[i][j];
        }
    

    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column2; j++) {
            for (int k = 0; k < line; k++) {
                result[i][j]+= matrix[i][k]* matrix2[k][j];
         }
        }
    }
    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column2; j++) {
            cout << result[i][j]<<" ";
        }cout << endl;
    }
   
}
void scalar_multiply(int x) {
    cout << "请输入矩阵的行与列";

    cout << "行数(100):";
    cin >> line;
    cout << "列数(100):";
    cin >> column;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix[i][j] = 0;
        }
    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix[i][j];
        }
        cout << endl;

    }
    cout << "请输入数字";
    int num;
    cin >> num;
    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << matrix[i][j] * num<<" ";
        }cout << endl;
    }


}
void superpositiontop(int x) {
    cout << "请输入第一个矩阵的行与列";

    cout << "行(100):";
    cin >> line;
    cout << "列(100):";
    cin >> column;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix[i][j] = 0;
            result[i][j] = 0;
        }
    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix[i][j];
        }
        cout << endl;

    }
    cout << "请输入第二个矩阵的行与列";

    cout << "行:";
    cin >> line2;
    cout << "列:";
    cin >> column2;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix2[i][j] = 0;
        }
    }
    for (int i = 0; i < line2; i++) {
        for (int j = 0; j < column2; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix2[i][j];
        }


    }
    if (line != line2) {
        cout << "无法叠加";
    }
    else {
        for (int i = 0; i < line; i++) {
            for (int j = 0; j < column2; j++) {
                cout << matrix[i][j] << " ";
            }cout << endl;
        }

        for (int i = 0; i < line; i++) {
            for (int j = 0; j < column2; j++) {
                cout << matrix2[i][j] << " ";
            }cout << endl;
        }
    }
}
void superpositionlef(int x) {
    cout << "请输入第一个矩阵的行与列";

    cout << "行(100):";
    cin >> line;
    cout << "列(100):";
    cin >> column;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix[i][j] = 0;
            result[i][j] = 0;
        }
    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix[i][j];
        }
        cout << endl;

    }
    cout << "请输入第二个矩阵的行与列";

    cout << "行:";
    cin >> line2;
    cout << "列:";
    cin >> column2;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix2[i][j] = 0;
        }
    }
    for (int i = 0; i < line2; i++) {
        for (int j = 0; j < column2; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix2[i][j];
        }


    }
    if (column != column2) {
        cout << "无法叠加";
    }
    else {
        for (int i = 0; i < line; i++) {
            for (int j = 0; j < column2; j++) {
                cout << matrix[i][j] << " ";
            }cout << endl;
        }

        for (int i = 0; i < line; i++) {
            for (int j = 0; j < column2; j++) {
                cout << matrix2[i][j] << " ";
            }cout << endl;
        }
    }
}
void  transposition(int x) {
    cout << "请输入矩阵的行与列";

    cout << "行(100):";
    cin >> line;
    cout << "列(100):";
    cin >> column;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix[i][j] = 0;
            result[i][j] = 0;
        }
    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix[i][j];
        }
        cout << endl;

    }
    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << matrix[j][i] << " ";
        }cout << endl;
    }
}
void rotation(int x) {
    cout << "请输入矩阵的行与列";

    cout << "行数(100):";
    cin >> line;
    cout << "列数(100):";
    cin >> column;

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            matrix[i][j] = 0;
            result[i][j] = 0;
        }
    }

    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << "请输入数字:" << i + 1 << "行" << j + 1 << "列";
            cin >> matrix[i][j];
        }
        cout << endl;

    }
    for (int i = 0; i < line;i++ ) {
        int k = line - 1;
        for (int j = 0; j < column; j++) {
          
            result[i][j] = matrix[k][i];
            k--;
        }
    }
    for (int i = 0; i < line; i++) {
        for (int j = 0; j < column; j++) {
            cout << result[i][j] << " ";

        }cout << endl;
    }
}
void change(int x){}
int main()
{
    int x;
    cout << "1.创建矩阵" << endl << "2.矩阵相加" << endl << "3.矩阵相减" << endl << "4.矩阵相乘" << endl << "5.数字乘矩阵" << endl
        << "6.矩阵上叠加" << endl << "7.矩阵左右叠加" << endl << "8.矩阵转置" << endl << "9.矩阵旋转" << endl << "10.矩阵求逆" << endl << "11.矩阵输出";
    if (x == 1)
    {
        Creat(1);
    }if (x == 2) {
        add(2);
    
    }if (x == 3) 
    {
        subtract(3); 
    }if (x == 4) 
    {
        multiply(4);
    }if (x == 5) 
    {
        scalar_multiply(5); 
    }if (x == 6) {
        superpositiontop(6);
    }if (x == 7) {
        superpositionlef(7);
    }if (x == 8) {
        transposition(8);
    }if (x == 9) {
        rotation(9);
    }if (x == 10) {
        change(10);
    }
    if (x == 11) {

    }
    
}

标签:cout,int,矩阵,column,++,100,line
From: https://www.cnblogs.com/yiyulhb/p/17595293.html

相关文章

  • 【暑假例题】20230727 矩阵基本运算(C++)
    题目请使用C++实现矩阵的各种运算矩阵创建矩阵相加矩阵相减矩阵相乘数字乘矩阵矩阵上叠加矩阵左右叠加矩阵转置矩阵旋转矩阵求逆矩阵输出题目分析矩阵创建这里只需注意由于我们需要通过不同的函数对数组进行操作,所以我们需要将数组存储在容器或者使用指针防止数......
  • 矩阵的基本运算
    题目:矩阵的基本运算1、矩阵创建2、矩阵相加3、矩阵相减4、矩阵相乘5、数字乘矩阵6、矩阵上叠加7、矩阵左右叠加8、矩阵转置9、矩阵旋转10、矩阵求逆11、矩阵输出运用知识1.动态内存分配是一种在程序运行时动态地分配内存空间的机制。与静态内存分配相比,动态内存分配......
  • python 矩阵自我复制
    Python矩阵自我复制实现指南作为一名经验丰富的开发者,我将带领你学习如何实现"Python矩阵自我复制"。在本文中,我们将使用Python编程语言来实现这个功能。首先,让我们来看一下整个实现的流程。实现流程下面是实现"Python矩阵自我复制"的步骤:步骤描述1创建一个矩阵2......
  • python 矩阵 第一列
    如何实现Python矩阵第一列作为一名经验丰富的开发者,我很乐意教会刚入行的小白如何实现Python矩阵的第一列。下面是详细的步骤和相应的代码示例。步骤步骤一:创建一个矩阵首先,我们需要创建一个矩阵。在Python中,我们可以使用列表(List)来表示矩阵。下面是一个示例矩阵:matrix......
  • 矩阵的基本运算
    矩阵的基本运算1、矩阵创建2、矩阵相加3、矩阵相减4、矩阵相乘5、数字乘矩阵6、矩阵上叠加7、矩阵左右叠加8、矩阵转置9、矩阵旋转10、矩阵求逆11、矩阵输出主要运用的知识点:掌握二维指针的动态分配内存下面举一个简单的例子来理解二维指针的动态分配内存#include......
  • numpy-线代和矩阵
    numpy-线代和矩阵目录numpy-线代和矩阵创建(转换)矩阵矩阵运算np.linalg线代函数库np.matlib矩阵函数库参考资料创建(转换)矩阵一般我们先创建数组,然后将其转化为矩阵np.mat(data,dtype=None)data:数据或者数组dtype:数据格式importnumpyasnparr1=np.array(......
  • 秩为 1 的矩阵特有的性质
    特征值为:任意两行或两列都成比例秩为1的矩阵一定可以相似对角化上述定理的详细解释:秩为1的矩阵的一些性质|荒原之梦考研网(公益)......
  • 【矩阵论】含hadamard积求导和优化问题
    本篇使用的符号说明,考虑优化问题\[\min\limits_X\|A\circX-B\|_F^2,\tag{1}\]其中\(A,X,B\inM_{m,n}\)。自然的想法是对其求导找闭式解,由于\(F\)-范数的平方可以看作对每个位置的平方求和,于是\((1)\)可以向量化写成以下形式,\[\min\limits_X\|\operatorname{vec}(A)\odot\o......
  • 代码随想录算法训练营第二天| LeetCode 977.有序数组的平方 ,209.长度最小的子数组 ,59.
    977.有序数组的平方     题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/    文章讲解:https://programmercarl.com/0977.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E5%B9%B3%E6%96%B9.html    视频讲解: https://www.bili......
  • HJ70 矩阵乘法计算量估算
    1.题目读题HJ70 矩阵乘法计算量估算  考查点 2.解法思路 代码逻辑 具体实现 3.总结......