Hello
A few months ago I found a code which was able to solve equations.
At first I didn't understood it at all, I tried examining the code and with some support I now understand how the operators, structs, overloads, hooking (with line 59 - via overloading),... works
I accomplished to understand the whole code, EXCEPT the mathematical and logical part - the actually most important part -.
I'd really appreciate it, if someone was able to explain me what the math-part in the constructors +,-,/,* do, and work.
How is this piece code for example, able to sum up two terms?
1 2 3 4 5 6 7 8
|
LINE operator + (LINE A,LINE B) { //assumes that A.x == 0 or B.x == 0 or A.x == B.x
LINE R;
R.a = A.a + B.a;
R.k = A.k + B.k;
if(A.x) R.x = A.x;
else R.x = B.x;
return R;
}
|
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <math.h>
struct VAR{
float i;
};
struct LINE{ //k*x+a
float a,k;
VAR* x;
LINE(){}
LINE(int a): a(a),k(0),x(0) {}
LINE(VAR& v): a(0),k(1),x(&v) {}
};
LINE operator + (LINE A,LINE B) { //assumes that A.x == 0 or B.x == 0 or A.x == B.x
LINE R;
R.a = A.a + B.a;
R.k = A.k + B.k;
if(A.x) R.x = A.x;
else R.x = B.x;
return R;
}
LINE operator - (LINE A,LINE B) { //same as +
LINE R;
R.a = A.a - B.a;
R.k = A.k - B.k;
if(A.x) R.x = A.x;
else R.x = B.x;
return R;
}
LINE operator * (LINE A,LINE B) { //assumes that A.x == 0 or B.x == 0
LINE R;
R.a = A.a * B.a;
R.k = A.k * B.a + B.k * A.a;
if(A.x) R.x = A.x;
else R.x = B.x;
return R;
}
LINE operator / (LINE A,LINE B) { //assumes that B.x == 0
LINE R;
R.a = A.a / B.a;
R.k = A.k / B.a;
R.x = A.x;
return R;
}
void operator == (LINE A,LINE B) {
LINE C = A - B;
C.x->i = -C.a/C.k;
}
int main(){
VAR x;
4*x == 2*2;
std::cout << "x = " << x.i;
std::cin.get();
return 0;
}
|
Other things which would be nice to have it explained:
- Line 12 - 14 (the last three lines of where the struct LINE is initialised),
- Some more info about the operator overloading part here,
- More explanation about the constructors used in this code;
I'd really like to understand how this code works.
Thanks a lot all!