简体   繁体   English

C++ 向量减法

[英]C++ Vector subtraction

I've been trying to make my vector calculator subtract.我一直在尝试让我的矢量计算器减去。 It was doing it fine with vectors that store the same amount of numbers, for example: (+) 503 - (-) 305 .对于存储相同数量的数字的向量,它做得很好,例如: (+) 503 - (-) 305 It would give me a good result on this kind of cases.在这种情况下,它会给我一个很好的结果。

But when I tried subtracting different size vectors the problems appeared.但是当我尝试减去不同大小的向量时,问题就出现了。 I tried to solve the problem by making different filters to make the program act how I want it.我试图通过制作不同的过滤器来解决这个问题,以使程序按照我的意愿行事。 But now, instead of subtracting all it does is add.但是现在,它所做的不是减去它所做的就是添加。

This is my setup:这是我的设置:

const int MAX = 102;

typedef int vector[MAX];

A(0): Puts all the values of a vector at 0. For example: A(0):将一个向量的所有值都设为 0。例如:

Before using it:使用前:

[0][3][5][0][5]

Where the first 0 means it is a positive number, if it was [1] it would mean its negative, that [3] means that there are 3 numbers stored in the vector.其中第一个 0 表示它是一个正数,如果它是 [1] 则表示它的负数,那 [3] 表示向量中存储了 3 个数字。

After using A0:使用 A0 后:

[0][1][0][0][0][0][0]...[0]

ES_MAJOR_VECTOR checks if vector Z > X. On that case returns true, else it's going to return false. ES_MAJOR_VECTOR 检查向量 Z > X。在这种情况下返回 true,否则返回 false。

SUMA_VEC basically adds Z to X and then assings the value to W. SUMA_VEC 基本上将 Z 添加到 X,然后将值分配给 W。

void RESTA_VEC(vector Z, vector X, vector W) {

    int ZigualX(0), Xmes1(0), Xactual(0), ZmajorX(0), XmajorZ(0), contador(0);
    vector copia_Z, copia_X;

    A0(copia_Z);
    A0(copia_X);

    for (int k = 0; k < MAX; k++)
    {
        copia_Z[k] = Z[k];
        copia_X[k] = X[k];
    }

    if (Z[0] == X[0])
    {
        if (Z[0] == 0)
        {
            for (int y = MAX; y >= 2; y--)
            {
                if (Z[y] < X[y])
                {//RESTA
                    Z[y] = Z[y] + 10;
                    W[y] = Z[y] - X[y];
                    X[y + 1] = X[y + 1] + 1;
                }

                if (Z[y] > X[y])
                {
                    W[y] = Z[y] - X[y];
                }

                if (Z[y] == X[y])
                {
                    W[y] = 0;
                }
            }
        }

        if (Z[0] == 1)
        {
            SUMA_VEC(Z, X, W);
        }
    }
    
    if (Z[0] != X[0])
    {
        if (ES_MAJOR_VECTOR(Z, X) == true)
        {
            for (int y = MAX; y >= 2; y--)
            {
                if (Z[y] < X[y])
                {
                    Z[y] = Z[y] + 10;
                    W[y] = Z[y] - X[y];
                    X[y + 1] = X[y + 1] + 1;
                }

                if (Z[y] > X[y])
                {
                    W[y] = Z[y] - X[y];
                }

                if (Z[y] == X[y])
                {
                    W[y] = 0;
                }
            }
        }
        else
        {
            for (int y = MAX; y >= 2; y--)
            {
                if (X[y] < Z[y])
                {
                    X[y] = X[y] + 10;
                    W[y] = X[y] - Z[y];
                    Z[y + 1] = Z[y + 1] + 1;
                }

                if (X[y] > Z[y])
                {
                    W[y] = X[y] - Z[y];
                }

                if (X[y] == Z[y])
                {
                    W[y] = 0;
                }
            }
        }
    }

    for (int h = 0; h < MAX; h++)
    {
        Z[h] = copia_Z[h];
        X[h] = copia_X[h];
    }
}

How can I solve this?我该如何解决这个问题? I've thought that I need to check: If they are positive or negative, if they're both positive I have to execute a subtraction, if they're negative I have to add Z to X and assign it to W.我认为我需要检查:如果它们是正数或负数,如果它们都是正数,我必须执行减法,如果它们是负数,我必须将 Z 添加到 X 并将其分配给 W。

If the vectors are different (Meaning one is positive and the another one is negative) I have to check which one is bigger, do the subtraction and then assign either 0 or 1 depending on the bigger vector of the two used earlier.如果向量不同(意味着一个是正的,另一个是负的)我必须检查哪个更大,做减法,然后根据前面使用的两个向量中的较大向量分配 0 或 1。

For example:例如:

+5050
-305

W[0] = +

-5050
+305

W[0] = -

After getting helped by a very kind user I could get to this conclusion:在得到一位非常友善的用户的帮助后,我可以得出以下结论:

void RESTA_ABS(vector Z, vector X, vector W) {

    int error(0);

    A0(W);
        
    if (ES_MAJOR_VECTOR(Z,X))
    {
        for (int i = MAX-1; i >= 2; i--)
        {
            if (Z[i] < X[i]) {

                Z[i] = Z[i] + 10;
                W[i] = Z[i] - X[i];
                X[i - 1] = X[i - 1] + 1;
            }           
            else
            {
                W[i] = Z[i] - X[i];
            }
        }
    }
    else
    {
        cout << "ERROR - VECTOR 2 > VECTOR 1" << endl;
    }
}

This module doesn't mind about the vector being positive or negative.这个模块不介意向量是正的还是负的。 It just does the subtraction.它只是做减法。

So, to solve this, I used another module:所以,为了解决这个问题,我使用了另一个模块:

void RESTA_VEC(vector Z, vector X, vector W) {无效RESTA_VEC(向量Z,向量X,向量W){

if (X[0] == 0)
{
    X[0] = 1;
}
else
{
    X[0] = 0;
}

RESTA_ABS(Z, X, W);

if (ES_MAJOR_VECTOR(Z, X) == true)
    {
        W[0] = Z[0];
    }
    else
    {
        W[0] = X[0];
    }

} }

This one changes the number's sign of the second vector, making it negative if its positive, and positive if its negative.这个改变了第二个向量的数字符号,如果它是正则使其为负,如果它为负则为正。 (This is determined by having a 0 or a 1 on the first vector position). (这由第一个向量位置上的 0 或 1 决定)。

And then finally, what it does is to check the sign of the greater vector and assign it to the output vector W.最后,它所做的是检查更大向量的符号并将其分配给 output 向量 W。

The only part left is checking which of the two vectors is greaters, because as I was testing it, on some cases, it would give me a wrong answer.剩下的唯一部分是检查两个向量中的哪个更大,因为当我测试它时,在某些情况下,它会给我一个错误的答案。

For example:例如:

[+][9][1][2][3][4][5][6][7][8][9]
[+][9][1][1][2][3][4][5][6][7][8]

Would give:会给:

[+][8][1][1][1][1][1][1][1][1]

But it doesn't make the opperation cause it thinks that the second vector is greater.但它不会进行操作,因为它认为第二个向量更大。

Translation: Is Z greater than X?翻译:Z是否大于X?

bool ES_MAJOR_VECTOR_SIGNE(vector Z, vector X) {

    int END(0), UP(2);

    return false;

    if (Z[1] > X[1])
    {
        return true;
    }
    else
    {
        while (END != 1)
        {
            if (Z[UP] > X[UP])
            {
                return true;
                END = 1;
            }

            UP++;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM