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
. 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] = -
解决方案
在得到一位非常友善的用户的帮助后,我可以得出以下结论:
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;
}
}
这个模块不介意向量是正的还是负的。它只是做减法。
所以,为了解决这个问题,我使用了另一个模块:
无效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];
}
}
这个改变了第二个向量的数字符号,如果它是正则使其为负,如果它为负则为正。(这由第一个向量位置上的 0 或 1 决定)。
最后,它所做的是检查更大向量的符号并将其分配给输出向量 W。
剩下的唯一部分是检查两个向量中的哪个更大,因为当我测试它时,在某些情况下,它会给我一个错误的答案。
例如:
[+][9][1][2][3][4][5][6][7][8][9]
[+][9][1][1][2][3][4][5][6][7][8]
会给:
[+][8][1][1][1][1][1][1][1][1]
但它不会进行操作,因为它认为第二个向量更大。
翻译: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++;
}
}
}
推荐阅读
- flutter - 网页的颤振计时器功能?
- kubernetes - 如何在“livenessProbe”中放置两个prot?
- oracle - 无法为模块 DBD::Oracle 加载 Oracle.dll:load_file:%1 在 C:/Perl64/lib/DynaLoader.pm 第 201 行不是有效的 Win32 应用程序
- ios - 动态分配 ViewController 到 Navigate
- python - Django admin inline 有 2 个外键指向同一模型
- encryption - 用于加密的 Webauthn
- tensorflow - 将中间层的输出作为模型训练的输入
- matlab - 如何访问和修改 matlab 工作区中的变量值?
- python - 如何在谷歌colab中安装web3
- javascript - 如何定义包含泛型类型变量的类型数组