vector.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 53 6b3d7e3418c1
parent 45 7197576fedcf
child 54 c064ce9f40f5
permissions -rw-r--r--
getch.c renamed to cross_getch.c 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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
namespace algebra {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    class Vector {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
      private:
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
        int x;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
        int y;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
      public:
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
        Vector (int xx = 0, int yy = 0);
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
       ~Vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
45
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents: 43
diff changeset
    23
        int X () { return x; };
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents: 43
diff changeset
    24
        int Y () { return y; };
43
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
        Vector operator+ (Vector &);
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents: 43
diff changeset
    27
        Vector operator- (Vector &);
43
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
        void vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
        double abs ();
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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    Vector::Vector (int xx, int yy) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        x = xx;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        y = yy;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    }
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
    Vector::~Vector () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
        fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y);
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
45
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents: 43
diff changeset
    42
    Vector Vector::operator+ (Vector & v) {
43
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        return Vector (x + v.X (), y + v.Y ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
45
7197576fedcf pmc: namespace algebra for vector
Emilio Largo <largo@largo.homelinux.org>
parents: 43
diff changeset
    46
    Vector Vector::operator- (Vector & v) {
43
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        return Vector (x - v.X (), y - v.Y ());
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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    double Vector::abs () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
        return sqrt (x * x + y * y);
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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    void Vector::vector () {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        printf ("(%.3d, %.3d)", x, 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
}
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
using namespace algebra;
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
int main (int argc, char **argv)
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
{
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    Vector *v[MAX];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
    Vector result;
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
    srand (time (NULL));
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
    if ((MAX % 2) != 0) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
        printf ("MAX %% 2 != 0\n");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
        return EXIT_FAILURE;
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
    for (int i = 0; i < MAX; i++) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
        v[i] = new Vector (GETRANDOM (100), GETRANDOM (100));
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
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
    for (int i = 0; i < MAX; i += 2) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
        v[i + 1]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
        printf ("+");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
        v[i]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
        printf ("=");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
        result = *v[i + 1] + *v[i];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
        result.vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
        printf (" = %3.2f\n", result.abs ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
    }
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
    printf ("\n");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
    for (int i = 0; i < MAX; i += 2) {
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
        v[i + 1]->vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
        printf ("-");
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    92
        v[i]->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
        result = *v[i + 1] - *v[i];
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
        result.vector ();
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    96
        printf (" = %3.2f\n", result.abs ());
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
    }
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
    printf ("\nCLEANUP\n");
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
    for (int i = 0; i < MAX; i++)
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
        delete v[i];
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
    return EXIT_SUCCESS;
cf8c1b5127b2 vector.cc Testcase
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
}