vector.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 05 Mar 2010 22:03:25 +0100
changeset 113 397270b5d21a
parent 77 49e0babccb23
permissions -rw-r--r--
sys/wait.h is the proper header, not wait.h 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
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 74
diff changeset
     2
 * vector.cc
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 74
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 74
diff changeset
     4
 *
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 74
diff changeset
     5
 * Dereferencing Classes
43
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
 */
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <cstdio>
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <cstdlib>
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <cmath>
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <ctime>
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
#define MAX 6
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
namespace algebra {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    class Vector {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
      private:
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
        int x;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
        int y;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
      public:
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
       ~Vector ();
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
        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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
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
        void vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
        double abs ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    };
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    Vector::Vector (int xx, int yy) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
        x = xx;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
    Vector::~Vector () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
        return Vector (x + v.X (), y + v.Y ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
        return Vector (x - v.X (), y - v.Y ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    }
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
    double Vector::abs () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        return sqrt (x * x + y * y);
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
    }
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
    void Vector::vector () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
        printf ("(%.3d, %.3d)", x, y);
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
    }
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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
using namespace algebra;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
int main (int argc, char **argv)
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
{
54
c064ce9f40f5 Vector and PMC ChangeLog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 45
diff changeset
    78
    Vector **v;
43
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
    Vector result;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
    srand (time (NULL));
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
    if ((MAX % 2) != 0) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
        printf ("MAX %% 2 != 0\n");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
        return EXIT_FAILURE;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
    for (int i = 0; i < MAX; i++) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
        v[i] = new Vector (GETRANDOM (100), GETRANDOM (100));
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
    for (int i = 0; i < MAX; i += 2) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
        v[i + 1]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
        printf ("+");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
        v[i]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
        printf ("=");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
        result = *v[i + 1] + *v[i];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
        result.vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   101
        printf (" = %3.2f\n", result.abs ());
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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
    printf ("\n");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   106
    for (int i = 0; i < MAX; i += 2) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
        v[i + 1]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
        printf ("-");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
        v[i]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   110
        printf ("=");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   111
        result = *v[i + 1] - *v[i];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   112
        result.vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   113
        printf (" = %3.2f\n", result.abs ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   114
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   115
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   116
    for (int i = 0; i < MAX; i++)
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   117
        delete v[i];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   123
    return EXIT_SUCCESS;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   124
}