author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:50:39 +0200 | |
changeset 73 | d0578510aea1 |
parent 54 | c064ce9f40f5 |
child 74 | 829976007e62 |
permissions | -rw-r--r-- |
43 | 1 |
/** |
2 |
* DEREFERNCING CLASSES |
|
3 |
*/ |
|
4 |
||
5 |
#include <cstdio> |
|
6 |
#include <cstdlib> |
|
7 |
#include <cmath> |
|
8 |
#include <ctime> |
|
9 |
||
10 |
#define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0)) |
|
11 |
#define MAX 6 |
|
12 |
||
54
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
13 |
static int refCounter = 0; |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
14 |
|
43 | 15 |
namespace algebra { |
16 |
class Vector { |
|
17 |
private: |
|
18 |
int x; |
|
19 |
int y; |
|
20 |
||
21 |
public: |
|
22 |
Vector (int xx = 0, int yy = 0); |
|
54
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
23 |
Vector (const Vector &); |
43 | 24 |
~Vector (); |
25 |
||
45
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
43
diff
changeset
|
26 |
int X () { return x; }; |
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
43
diff
changeset
|
27 |
int Y () { return y; }; |
43 | 28 |
|
45
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
43
diff
changeset
|
29 |
Vector operator+ (Vector &); |
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
43
diff
changeset
|
30 |
Vector operator- (Vector &); |
43 | 31 |
|
32 |
void vector (); |
|
33 |
double abs (); |
|
34 |
}; |
|
35 |
||
36 |
Vector::Vector (int xx, int yy) { |
|
37 |
x = xx; |
|
38 |
y = yy; |
|
54
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
39 |
|
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
40 |
refCounter++; |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
41 |
} |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
42 |
|
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
43 |
Vector::Vector (const Vector & copy) |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
44 |
: x (copy.x), y (copy.y) { |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
45 |
fprintf (stderr, "Warning Copy Constructor\n"); |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
46 |
refCounter++; |
43 | 47 |
} |
48 |
||
49 |
Vector::~Vector () { |
|
50 |
fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y); |
|
54
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
51 |
refCounter--; |
43 | 52 |
} |
53 |
||
45
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
43
diff
changeset
|
54 |
Vector Vector::operator+ (Vector & v) { |
43 | 55 |
return Vector (x + v.X (), y + v.Y ()); |
56 |
} |
|
57 |
||
45
7197576fedcf
pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents:
43
diff
changeset
|
58 |
Vector Vector::operator- (Vector & v) { |
43 | 59 |
return Vector (x - v.X (), y - v.Y ()); |
60 |
} |
|
61 |
||
62 |
double Vector::abs () { |
|
63 |
return sqrt (x * x + y * y); |
|
64 |
} |
|
65 |
||
66 |
void Vector::vector () { |
|
67 |
printf ("(%.3d, %.3d)", x, y); |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
using namespace algebra; |
|
72 |
||
73 |
int main (int argc, char **argv) |
|
74 |
{ |
|
54
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
75 |
Vector **v; |
43 | 76 |
Vector result; |
77 |
||
78 |
srand (time (NULL)); |
|
79 |
||
80 |
if ((MAX % 2) != 0) { |
|
81 |
printf ("MAX %% 2 != 0\n"); |
|
82 |
return EXIT_FAILURE; |
|
83 |
} |
|
84 |
||
54
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
85 |
v = new Vector *[MAX]; |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
86 |
|
43 | 87 |
for (int i = 0; i < MAX; i++) { |
88 |
v[i] = new Vector (GETRANDOM (100), GETRANDOM (100)); |
|
89 |
} |
|
90 |
||
91 |
for (int i = 0; i < MAX; i += 2) { |
|
92 |
v[i + 1]->vector (); |
|
93 |
printf ("+"); |
|
94 |
v[i]->vector (); |
|
95 |
printf ("="); |
|
96 |
result = *v[i + 1] + *v[i]; |
|
97 |
result.vector (); |
|
98 |
printf (" = %3.2f\n", result.abs ()); |
|
99 |
} |
|
100 |
||
101 |
printf ("\n"); |
|
102 |
||
103 |
for (int i = 0; i < MAX; i += 2) { |
|
104 |
v[i + 1]->vector (); |
|
105 |
printf ("-"); |
|
106 |
v[i]->vector (); |
|
107 |
printf ("="); |
|
108 |
result = *v[i + 1] - *v[i]; |
|
109 |
result.vector (); |
|
110 |
printf (" = %3.2f\n", result.abs ()); |
|
111 |
} |
|
112 |
||
113 |
for (int i = 0; i < MAX; i++) |
|
114 |
delete v[i]; |
|
115 |
||
54
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
116 |
delete [] v; |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
117 |
|
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
118 |
fprintf (stderr, "Objects remaining on exit: %d\n", refCounter); |
c064ce9f40f5
Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
45
diff
changeset
|
119 |
|
43 | 120 |
return EXIT_SUCCESS; |
121 |
} |