简体   繁体   中英

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 . 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:

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.

After using 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.

SUMA_VEC basically adds Z to X and then assings the value to 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.

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.

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) {

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).

And then finally, what it does is to check the sign of the greater vector and assign it to the output vector 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?

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++;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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