矩阵的数学运算

时间:2019-06-25 21:47:13

标签: c++

我正在编写一个程序,以字符串的形式对矩阵进行数学运算,以创建幂函数,我想将乘法函数重复多次等于指数,但是当我这样做时,输出为[ 0 0 0; 0 0 0; 0 0 0] 我应该如何重复乘法功能?

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <math.h>
#include <complex>

using namespace std;

string power(float matrix1[100][100], int m1, int n1, int x);

int main()
{
    string s1; //first matrix
    char op; //operator
    string s2; //second matrix
    int p; //for power operation
    int m1 = 0; //number of rows of first matrix
    int n1 = 0; //number of columns of first matrix
    int m2 = 0; //number of rows of second matrix
    int n2 = 0; //number of columns of second matrix

    getline(cin, s1);

    for (int i1 = 0; i1 < s1.size(); i1++)
    {

        if (s1[i1] == ';')
            m1++;

    }
    for (int i2 = 0; i2 < s1.size(); i2++)
    {
        if (s1[i2] == ' ')
            n1++;
    }
    m1 = m1 + 1;
    n1 = (n1 / m1) + 1;

    s1.erase(0, 1);
    s1.erase(s1.size() - 1, 1); //to remove brackets

    for (int i4 = 0; i4 < s1.size(); i4++)
    {
        if (s1[i4] == ';')
        {
            s1[i4] = ' ';
        }
    }

    float matrix1[100][100];
    for (int i5 = 0; i5 < m1; i5++)
    {
        string token1;
        float matrix1[100][100];
        for (int i6 = 0; i6 < n1; i6++)
        {
            token1 = s1.substr(0, s1.find(' '));
            matrix1[i5][i6] = atof(token1.c_str());
            s1.erase(0, s1.find(' ') + 1);
        }
    }

    int x; //exponent
    cin >> op;
    if (op == '^' && m1 == n1)
    {
        cin >> x;
        if (x >= 2)

        {
            cout << "[" + power(matrix1, m1, n1, x) + "]";
        }

        return 0;

    }
}

string power(float matrix1[100][100], int m1, int n1, int x)
{
    string str;
    float matrix3[100][100];

    for (int i = 0; i < m1; i++)
    {
        for (int j = 0; j < n1; j++)
        {

            matrix3[i][j] = matrix1[i][0] * matrix1[0][j];

            if (m1 > 1)
            {
                for (int k = 0; k < m1; k++)
                {
                    matrix3[i][j] = matrix3[i][j]
                            + (matrix1[i][k + 1] * matrix1[k + 1][j]); //the end of multiplication function
                    if (x > 2) //repetition of the multiplication function according to the exponent
                        for (int l = 3; l < x; l++)

                            matrix3[i][j] = (matrix3[i][0] * matrix1[0][j])
                                    + (matrix3[i][k + 1] * matrix1[k + 1][j]);

                }

                ostringstream ss;
                ss << matrix3[i][j];

                if ((i < m1) && (j == 0))

                    str = str + ss.str();

                else if ((i < m1 - 1) && (j == n1 - 1))
                {
                    str = str + ' ' + ss.str() + ';';
                }
                else
                    str = str + ' ' + ss.str();
            }
        }

    }
    return str;
}

0 个答案:

没有答案