vector.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 69 b5912e5f899f
parent 54 c064ce9f40f5
child 74 829976007e62
permissions -rw-r--r--
Useless cross_getch hack removed cross_getch provides a version of getch for every os. The Windows Part does not work under wine, but it works on the target platform. committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * DEREFERNCING CLASSES
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 */
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
#include <cstdio>
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <cstdlib>
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <cmath>
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <ctime>
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#define MAX 6
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
namespace algebra {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    class Vector {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
      private:
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
        int x;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
        int y;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
      public:
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
       ~Vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        void vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        double abs ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    };
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    Vector::Vector (int xx, int yy) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        x = xx;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    Vector::~Vector () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        return Vector (x + v.X (), y + v.Y ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
        return Vector (x - v.X (), y - v.Y ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
    double Vector::abs () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
        return sqrt (x * x + y * y);
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    void Vector::vector () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
        printf ("(%.3d, %.3d)", x, y);
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
}
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
using namespace algebra;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
int main (int argc, char **argv)
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
{
54
c064ce9f40f5 Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 45
diff changeset
    75
    Vector **v;
43
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
    Vector result;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
    srand (time (NULL));
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
    if ((MAX % 2) != 0) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
        printf ("MAX %% 2 != 0\n");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
        return EXIT_FAILURE;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
    for (int i = 0; i < MAX; i++) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
        v[i] = new Vector (GETRANDOM (100), GETRANDOM (100));
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
    for (int i = 0; i < MAX; i += 2) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
        v[i + 1]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
        printf ("+");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
        v[i]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
        printf ("=");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
        result = *v[i + 1] + *v[i];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
        result.vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
        printf (" = %3.2f\n", result.abs ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   101
    printf ("\n");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   103
    for (int i = 0; i < MAX; i += 2) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
        v[i + 1]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
        printf ("-");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   106
        v[i]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
        printf ("=");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
        result = *v[i + 1] - *v[i];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
        result.vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   110
        printf (" = %3.2f\n", result.abs ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   111
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   112
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   113
    for (int i = 0; i < MAX; i++)
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   114
        delete v[i];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   120
    return EXIT_SUCCESS;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   121
}