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