目录
首页 笔记 详情

c++编程代码例子大全(c++编程学习题库)


C++编程题目

描述:求向量之积和矩阵之积

C++编程算法题解析,C++求矩阵和向量乘法运算

实现代码:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
struct Data 
{
    char flag = 0;
    int theRows = 0;
    int theCols = 0;
    vector<vector<int>> data;
};
struct Data  operator*(struct Data  arrA, struct Data  arrB)
{//矩阵乘法
    //矩阵arrA的行数
    int rowA = arrA.data.size();
    //矩阵arrA的列数
    int colA = arrA.data[0].size();
    //矩阵arrB的行数
    int rowB = arrB.data.size();
    //矩阵arrB的列数
    int colB = arrB.data[0].size();
    //相乘后的结果矩阵
    struct Data  res;
    if (arrA.flag=='v' && arrB.flag== 'v')//如果矩阵arrA的列数不等于矩阵arrB的行数。
    {
         res.data.resize(1);
         res.data[0].resize(1);
         //向量相乘
         for (int j = 0; j < arrA.data.size(); ++j)
         {
             res.data[0][0] += arrA.data[j][0] * arrB.data[j][0];
         }
         res.flag = 's';
         res.theCols = res.theRows = 1;
         return res;
    }
    if (arrA.flag == 'v' && arrB.flag == 'm') 
    {
        if (arrA.data.size() != arrB.data.size()) 
        {
            return res;
        }
    }
    if(colA==rowB)
    {
        //设置结果矩阵的大小,初始化为为0
        res.data.resize(rowA);
        for (int i = 0; i < rowA; ++i)
        {
            res.data[i].resize(colB);
        }
        //矩阵相乘
        for (int i = 0; i < rowA; ++i)
        {
            for (int j = 0; j < colB; ++j)
            {
                for (int k = 0; k < colA; ++k)
                {
                    res.data[i][j] += arrA.data[i][k] * arrB.data[k][j];
                }
            }
        }
        if (res.data[0].size() == 1)
        {
            res.flag = 'v';
        }
        else 
        {
            res.flag = 'm';
        }
    }
    return res;
}
void inputData(struct Data&  arr)
{
    char str[20];
    cin.getline(str, 20);
    int temp=0;
    stringstream stream(str);
    stream >> arr.flag;
    if (arr.flag == 'v')
    {
        stream >> arr.theRows;
        arr.data.resize(arr.theRows);
        for (int i = 0; i < arr.theRows; i++)
        {
            arr.data[i].resize(1);
            cin >> arr.data[i][0];
        }
    }
    if (arr.flag == 'm')
    {
        stream >> arr.theRows >> arr.theCols;
        arr.data.resize(arr.theRows);
        for (int i = 0; i < arr.theRows; i++)
        {
            arr.data[i].resize(arr.theCols);
        }
        for (int i = 0; i < arr.theRows; i++)
        {
            for (int j = 0; j < arr.theCols; j++)
            {
                cin >> arr.data[i][j];
            }
        }
    }
    //cout << flag << " " << theRows << " " << theCols << endl;
}
void print(struct Data& arr)
{
    switch (arr.flag) 
    {
    case 's':
        cout << "s" << endl;
        cout << arr.data[0][0] << endl;
        break;
    case 'v':
        cout << "v" << " " << arr.data.size() << endl;
        for (int i = 0; i < arr.data.size();i++) 
        {
            cout << arr.data[i][0] << " ";
        }
        cout << endl;
        break;
    case 'm':
        cout << "m" << " " << arr.data.size() << " " << arr.data[0].size() << endl;
        for (int i = 0; i < arr.data.size(); i++)
        {
            for (int j = 0; j < arr.data[i].size(); j++)
            {
                cout << arr.data[i][j] << " ";
            }
            cout << endl;
        }
        break;
    }
}
/*

v 3
-1 1 2
v 3
2 5 2
*/
int main() 
{
    struct Data  arrA;
    struct Data  arrB;
    inputData(arrA);
    while (getchar() != '\n');
    inputData(arrB);
    struct Data result = arrA * arrB;
    if (result.data.empty()) 
    {
        cout << "error" << endl;
    }
    else 
    {
        print(result);
    }
	return 0;
}

写笔记

咨询

您的电子邮箱地址不会被公开。