C++ 中的矩阵操作

C++C++Beginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习如何使用 C++ 编程对给定的二维数组执行各种矩阵操作,如加法、减法和乘法。你还将学习如何有效地操作和使用数组。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/BasicsGroup -.-> cpp/arrays("`Arrays`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/switch("`Switch`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/IOandFileHandlingGroup -.-> cpp/files("`Files`") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("`Code Formatting`") subgraph Lab Skills cpp/operators -.-> lab-96242{{"`C++ 中的矩阵操作`"}} cpp/arrays -.-> lab-96242{{"`C++ 中的矩阵操作`"}} cpp/conditions -.-> lab-96242{{"`C++ 中的矩阵操作`"}} cpp/switch -.-> lab-96242{{"`C++ 中的矩阵操作`"}} cpp/for_loop -.-> lab-96242{{"`C++ 中的矩阵操作`"}} cpp/output -.-> lab-96242{{"`C++ 中的矩阵操作`"}} cpp/user_input -.-> lab-96242{{"`C++ 中的矩阵操作`"}} cpp/files -.-> lab-96242{{"`C++ 中的矩阵操作`"}} cpp/code_formatting -.-> lab-96242{{"`C++ 中的矩阵操作`"}} end

创建 C++ 程序文件

~/project 目录下创建一个名为 matrix_operations.cpp 的新文件。可以通过在终端中运行以下命令来完成。

touch ~/project/matrix_operations.cpp

包含所需库并设置主函数

在我们的 C++ 程序中,第一步是包含用于输入/输出操作和清除控制台的库。之后,为你的 C++ 程序设置主函数。你可以使用以下代码作为模板。

#include<iostream.h>
#include<conio.h>

int main()
{
    clrscr();
    // 你的代码写在这里
    getch();
    return 0;
}

矩阵加法

在这一步中,我们将创建一个用于执行两个矩阵加法的案例。C++ 程序会要求用户输入两个矩阵的阶数或维度(必须是 m*no*p)。

如果矩阵的维度不相同,程序将返回错误信息。然后输入矩阵的元素并进行加法运算。最终的代码应如下所示。

//ADDITION
case 1:
    cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
    cin>>m;
    cout<<"* \n";
    cin>>n;
    cout<<"Enter the order of matrix b (must be o*p): "<<endl;
    cin>>o;
    cout<<"* \n";
    cin>>p;
    if (m==o&&n==p)
    {
        cout<<"Addition possible "<<endl;
    }
    else
    {
        cout<<"Addition not possible ";
        l=0;
    }

    if(l)
    {
        cout<<"\n\nEnter the elements of matrix 1: "<<endl;
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
                cin>>a[i][j];
        }
        cout<<"Elements of matrix 1 are: ";
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<a[i][j]<<" ";
        }
        cout<<"\nEnter the elements of matrix 2: "<<endl;
        for (i=1;i<=o;i++)
        {
            for (j=1;j<=p;j++)
                cin>>b[i][j];
        }
        cout<<"Elements of matrix 2 are: ";
        for (i=1;i<=o;i++)
        {
            cout<<endl;
            for (j=1;j<=p;j++)
                cout<<b[i][j]<<" ";
        }
        cout<<"\n\n\nAddition:\nc=a+b=";
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
            {
                c[i][j]=a[i][j]+b[i][j];
            }
        }
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<c[i][j]<<" ";
        }
    }
    break;

矩阵减法

在这一步中,我们将创建一个用于执行两个矩阵减法的案例。C++ 程序会要求用户输入两个矩阵的阶数或维度(必须是 m*no*p)。

如果矩阵的维度不相同,程序将返回错误信息。然后输入矩阵的元素并进行减法运算。最终的代码应如下所示。

//SUBTRACTION
case 2:
    cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
    cin>>m;
    cout<<"* \n";
    cin>>n;
    cout<<"Enter the order of matrix b (must be o*p): "<<endl;
    cin>>o;
    cout<<"* \n";
    cin>>p;
    if (m==o&&n==p)
    {
        cout<<"Subtracion possible "<<endl;
    }
    else
    {
        cout<<"Subtraction not possible ";
        l=0;
    }

    if(l)
    {
        cout<<"\n\nEnter the elements of matrix 1: "<<endl;
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
                cin>>a[i][j];
        }
        cout<<"Elements of matrix 1 are: ";
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<a[i][j]<<" ";
        }
        cout<<"\nEnter the elements of matrix 2: "<<endl;
        for (i=1;i<=o;i++)
        {
            for (j=1;j<=p;j++)
                cin>>b[i][j];
        }
        cout<<"Elements of matrix 2 are: ";
        for (i=1;i<=o;i++)
        {
            cout<<endl;
            for (j=1;j<=p;j++)
                cout<<b[i][j]<<" ";
        }
        cout<<"\n\n\nSubtraction:\nc=a-b=";
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
            {
                c[i][j]=a[i][j]-b[i][j];
            }
        }
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<c[i][j]<<" ";
        }
    }
    break;

矩阵乘法

在这一步中,我们将创建一个用于执行两个矩阵乘法的案例。C++ 程序会要求用户输入两个矩阵的阶数或维度(必须是 m*no*p)。

如果矩阵的维度不兼容,程序将返回错误信息。然后输入矩阵的元素并进行乘法运算。最终的代码应如下所示。

//MULTIPLICATION
case 3:
    cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
    cin>>m;
    cout<<"* \n";
    cin>>n;
    cout<<"Enter the order of matrix b (must be o*p): "<<endl;
    cin>>o;
    cout<<"* \n";
    cin>>p;
    if (n==o)
    {
        cout<<"Multiplication possible "<<endl;
    }
    else
    {
        cout<<"Multiplication not possible ";
        l=0;
    }

    if(l)
    {
        cout<<"\n\nEnter the elements of matrix 1: "<<endl;
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=n;j++)
                cin>>a[i][j];
        }
        cout<<"Elements of matrix 1 are: ";
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=n;j++)
                cout<<a[i][j]<<" ";
        }
        cout<<"\nEnter the elements of matrix 2: "<<endl;
        for (i=1;i<=o;i++)
        {
            for (j=1;j<=p;j++)
                cin>>b[i][j];
        }
        cout<<"Elements of matrix 2 are: ";
        for (i=1;i<=o;i++)
        {
            cout<<endl;
            for (j=1;j<=p;j++)
                cout<<b[i][j]<<" ";
        }
        cout<<"\n\n\nMultiplication:\nc=aXb=";
        for (i=1;i<=m;i++)
        {
            for (j=1;j<=p;j++)
            {
                c[i][j]=0;
                for (int k=1;k<=n;k++)
                {
                    c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
                }
            }
        }
        for (i=1;i<=m;i++)
        {
            cout<<endl;
            for (j=1;j<=p;j++)
                cout<<c[i][j]<<" ";
        }
    }
    break;

执行 C++ 程序

你可以通过在终端中使用以下命令编译并执行 matrix_operations.cpp 文件:

g++ ~/project/matrix_operations.cpp -o matrix && ./matrix

你可能需要将 matrix_operations.cpp 替换为你为 C++ 程序文件使用的名称。

假设你的代码输出了预期的结果,你应该能够看到一个提示,要求输入案例编号(1 表示加法,2 表示减法,3 表示乘法)。在你输入选择后,程序将根据所选操作要求你输入矩阵。

完整的 C++ 程序代码

以下是 matrix_operations.cpp 程序文件的完整代码。

#include<iostream.h>
#include<conio.h>

int main()
{
    clrscr();
    int a[50][50],b[50][50],c[50][50],i,j,m,n,o,p,l=1,r;
    cout<<"Enter case:\n";
    cin>>r;
    switch(r)
    {
        //ADDITION
        case 1:
            cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
            cin>>m;
            cout<<"*\n";
            cin>>n;
            cout<<"Enter the order of matrix b (must be o*p): "<<endl;
            cin>>o;
            cout<<"*\n";
            cin>>p;
            if (m==o&&n==p)
            {
                cout<<"Addition possible "<<endl;
            }
            else
            {
                cout<<"Addition not possible ";
                l=0;
            }

            if(l)
            {
                cout<<"\n\nEnter the elements of matrix 1: "<<endl;
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                        cin>>a[i][j];
                }
                cout<<"Elements of matrix 1 are: ";
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<a[i][j]<<" ";
                }
                cout<<"\nEnter the elements of matrix 2: "<<endl;
                for (i=1;i<=o;i++)
                {
                    for (j=1;j<=p;j++)
                        cin>>b[i][j];
                }
                cout<<"Elements of matrix 2 are: ";
                for (i=1;i<=o;i++)
                {
                    cout<<endl;
                    for (j=1;j<=p;j++)
                        cout<<b[i][j]<<" ";
                }
                cout<<"\n\n\nAddition:\nc=a+b=";
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                    {
                        c[i][j]=a[i][j]+b[i][j];
                    }
                }
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<c[i][j]<<" ";
                }
            }
            break;

        //SUBTRACTION
        case 2:
            cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
            cin>>m;
            cout<<"*\n";
            cin>>n;
            cout<<"Enter the order of matrix b (must be o*p): "<<endl;
            cin>>o;
            cout<<"*\n";
            cin>>p;
            if (m==o&&n==p)
            {
                cout<<"Subtracion possible "<<endl;
            }
            else
            {
                cout<<"Subtraction not possible ";
                l=0;
            }

            if(l)
            {
                cout<<"\n\nEnter the elements of matrix 1: "<<endl;
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                        cin>>a[i][j];
                }
                cout<<"Elements of matrix 1 are: ";
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<a[i][j]<<" ";
                }
                cout<<"\nEnter the elements of matrix 2: "<<endl;
                for (i=1;i<=o;i++)
                {
                    for (j=1;j<=p;j++)
                        cin>>b[i][j];
                }
                cout<<"Elements of matrix 2 are: ";
                for (i=1;i<=o;i++)
                {
                    cout<<endl;
                    for (j=1;j<=p;j++)
                        cout<<b[i][j]<<" ";
                }
                cout<<"\n\n\nSubtraction:\nc=a-b=";
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                    {
                        c[i][j]=a[i][j]-b[i][j];
                    }
                }
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<c[i][j]<<" ";
                }
            }
            break;

        //MULTIPLICATION
        case 3:
            cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
            cin>>m;
            cout<<"*\n";
            cin>>n;
            cout<<"Enter the order of matrix b (must be o*p): "<<endl;
            cin>>o;
            cout<<"*\n";
            cin>>p;
            if (n==o)
            {
                cout<<"Multiplication possible "<<endl;
            }
            else
            {
                cout<<"Multiplication not possible ";
                l=0;
            }

            if(l)
            {
                cout<<"\n\nEnter the elements of matrix 1: "<<endl;
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=n;j++)
                        cin>>a[i][j];
                }
                cout<<"Elements of matrix 1 are: ";
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=n;j++)
                        cout<<a[i][j]<<" ";
                }
                cout<<"\nEnter the elements of matrix 2: "<<endl;
                for (i=1;i<=o;i++)
                {
                    for (j=1;j<=p;j++)
                        cin>>b[i][j];
                }
                cout<<"Elements of matrix 2 are: ";
                for (i=1;i<=o;i++)
                {
                    cout<<endl;
                    for (j=1;j<=p;j++)
                        cout<<b[i][j]<<" ";
                }
                cout<<"\n\n\nMultiplication:\nc=aXb=";
                for (i=1;i<=m;i++)
                {
                    for (j=1;j<=p;j++)
                    {
                        c[i][j]=0;
                        for (int k=1;k<=n;k++)
                        {
                            c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
                        }
                    }
                }
                for (i=1;i<=m;i++)
                {
                    cout<<endl;
                    for (j=1;j<=p;j++)
                        cout<<c[i][j]<<" ";
                }
            }
            break;
        // default case
        default:
            cout<<"Wrong choice";
    }
    getch();
    return 0;
}

总结

在本实验中,你学习了如何使用 C++ 编程对给定的二维数组执行各种矩阵操作。你还学习了如何操作和使用数组。现在你应该对 C++ 编程中矩阵的工作原理有了很好的理解。

您可能感兴趣的其他 C++ 教程