pmc: namespace algebra for vector
authorEmilio Largo <largo@largo.homelinux.org>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 45 7197576fedcf
parent 44 bb6586b1c547
child 46 4b9e1ac40246
pmc: namespace algebra for vector -> the namespace prevents clashes with std::vector -> std::vector and algebra::Vector with capital V. md5: a md5 replacement ncurses: keypad function added committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
md5.c
md5rec.c
ncurses.c
pmc/cube.cc
pmc/include/cube.h
pmc/include/drawable.h
pmc/include/rectangle.h
pmc/include/surface.h
pmc/include/vector.h
pmc/main.cc
pmc/rectangle.cc
pmc/vector.cc
vector.cc
--- a/Makefile
+++ b/Makefile
@@ -29,6 +29,7 @@
 	sudoku \
 	cunit \
 	md5 \
+	md5rec \
 	varargs \
 	concatenation \
 	alpha_beta \
@@ -175,6 +176,10 @@
 
 md5: md5.o
 	@echo Linking $< ...
+	@$(CC) -o $@ $< -lssl
+
+md5rec: md5rec.o
+	@echo Linking $< ...
 	@$(CC) -o $@ $< -llsf
 
 varargs: varargs.o
new file mode 100644
--- /dev/null
+++ b/md5.c
@@ -0,0 +1,47 @@
+/**
+ * test/demos/md5.c
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <openssl/md5.h>
+
+typedef unsigned char u_char;
+
+int main (int argc, char **argv)
+{
+    char buffer[80];
+    char text[80];
+
+    unsigned char *md5_hash;
+    char result[33];
+    char byte[3];
+
+    int i, j;
+
+    if (argc != 2) {
+        printf ("Usage: %s <text>\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    /*
+     * MD5 alters the input buffer
+     */
+    strcpy (buffer, argv[1]);
+    strcpy (text, argv[1]);
+
+    md5_hash = MD5 ((u_char *) buffer, strlen (buffer), NULL);
+
+    for (i = 0, j = 0; i < 16; i++) {
+        sprintf (byte, "%02x", (md5_hash[i] & 0xFF));
+        result[j++] = byte[0];
+        result[j++] = byte[1];
+    }
+
+    result[j] = 0;
+    printf ("%s: %s\n", text, result);
+
+    return EXIT_SUCCESS;
+}
--- a/md5rec.c
+++ b/md5rec.c
@@ -1,5 +1,5 @@
 /**
- * test/demos/md5.c
+ * test/demos/md5rec.c
  * Copyright (C) 2008 Markus Broeker
  */
 
--- a/ncurses.c
+++ b/ncurses.c
@@ -34,14 +34,13 @@
     return EXIT_SUCCESS;
 }
 
-wchar_t fucnt (WINDOW * win)
+wchar_t func (WINDOW * win)
 {
     wchar_t guess;
 
     char dest[4];
 
     int maxx;
-
     int maxy;
 
     while (guess != ESCAPE) {
@@ -82,10 +81,12 @@
 
     check_locale ();
     win = initscr ();
+    keypad (win, 1);
     noecho ();
-    x = fucnt (win);
+
+    x = func (win);
+
     endwin ();
-
     printf ("\n");
 
     return EXIT_SUCCESS;
--- a/pmc/cube.cc
+++ b/pmc/cube.cc
@@ -6,8 +6,14 @@
 #include <cube.h>
 #include <cstdio>
 
+using namespace algebra;
+
 Cube::Cube (Surface * s)
 {
+    surface = s;
+    name = "Cube";
+    height = 25;
+
     P[0] = new Vector (0, 0);
     P[1] = new Vector (100, 0);
     P[2] = new Vector (100, 100);
@@ -17,16 +23,15 @@
     P[5] = new Vector (125, 25);
     P[6] = new Vector (125, 125);
     P[7] = new Vector (25, 125);
-
-    surface = s;
-    height = 25;
 }
 
-Cube::Cube (Surface * s, Vector& p1, Vector& p2, Vector& p3, Vector& p4, int h)
+Cube::Cube (Surface * s, Vector & p1, Vector & p2, Vector & p3, Vector & p4, int h)
 {
+    surface = s;
+    name = "Cube";
     height = h;
-    surface = s;
-    Vector location(h/2, h/2);
+
+    Vector location (h / 4, h / 4);
 
     P[0] = new Vector (p1);
     P[1] = new Vector (p2);
@@ -39,6 +44,21 @@
     }
 }
 
+Cube::Cube (Surface * s, Vector p[4], int h)
+{
+    surface = s;
+    name = "Cube";
+    height = h;
+
+    Vector location (h / 4, h / 4);
+
+    for (int i = 0; i < 4; i++) {
+        P[i] = new Vector (p[i]);
+        P[4 + i] = new Vector (*P[i]);
+        *P[4 + i] = *P[i] + location;
+    }
+}
+
 Cube::~Cube ()
 {
     for (int i = 0; i < 8; i++) {
@@ -58,7 +78,9 @@
 
 void Cube::show ()
 {
-	/* Implement some Sanity Checks */
+    /*
+     * Implement some Sanity Checks
+     */
     int height = surface->getHeight ();
 
     surface->drawLine (P[0]->X (), P[0]->Y (), P[1]->X (), P[1]->Y ());
--- a/pmc/include/cube.h
+++ b/pmc/include/cube.h
@@ -12,15 +12,16 @@
 
 class Cube:public Drawable {
   private:
-    Vector * P[8];
+    algebra::Vector * P[8];
     int height;
 
   public:
-    Cube (Surface *, Vector&, Vector&, Vector&, Vector&, int);
+    Cube (Surface *, algebra::Vector &, algebra::Vector &, algebra::Vector &, algebra::Vector &, int);
+    Cube (Surface *, algebra::Vector[4], int);
     Cube (Surface *);
 
     virtual ~ Cube ();
     void show ();
-    void move (Vector);
+    void move (algebra::Vector);
 };
 #endif
--- a/pmc/include/drawable.h
+++ b/pmc/include/drawable.h
@@ -11,12 +11,12 @@
 
 class Drawable:public Object {
   protected:
-    Vector anker;
+    algebra::Vector anker;
     Surface *surface;
 
   public:
-    virtual ~ Drawable () {};
-    virtual void move (Vector) = 0;
+    virtual ~ Drawable () { };
+    virtual void move (algebra::Vector) = 0;
     virtual void show () = 0;
 };
 #endif
--- a/pmc/include/rectangle.h
+++ b/pmc/include/rectangle.h
@@ -12,13 +12,14 @@
 
 class Rectangle:public Drawable {
   private:
-    Vector * P[4];
+    algebra::Vector * P[4];
 
   public:
-    Rectangle (Surface *, Vector& p1, Vector& p2, Vector& p3, Vector& p4);
+    Rectangle (Surface *, algebra::Vector & p1, algebra::Vector & p2, algebra::Vector & p3, algebra::Vector & p4);
+    Rectangle (Surface *, algebra::Vector[4]);
     Rectangle (Surface *);
     virtual ~ Rectangle ();
-    void move (Vector);
+    void move (algebra::Vector);
     void show ();
 };
 #endif
--- a/pmc/include/surface.h
+++ b/pmc/include/surface.h
@@ -20,12 +20,13 @@
 
   public:
     enum foregroundColor { BLACK, RED };
-    Surface (int w, int h, int d);
-   ~Surface ();
+      Surface (int w, int h, int d);
+     ~Surface ();
 
     int getWidth () {
         return width;
     };
+
     int getHeight () {
         return height;
     };
--- a/pmc/include/vector.h
+++ b/pmc/include/vector.h
@@ -8,27 +8,29 @@
 
 #include <object.h>
 
-class Vector:public Object {
-  public:
-    enum Mode { DEG, RAD, GRAD };
+namespace algebra {
+    class Vector:public Object {
+      public:
+        enum Mode { DEG, RAD, GRAD };
 
-    Vector (int xx = 0, int yy = 0);
-    virtual ~ Vector ();
+        Vector (int xx = 0, int yy = 0);
+        virtual ~ Vector ();
 
-    int X ();
-    int Y ();
+        int X ();
+        int Y ();
 
-    Vector operator+ (Vector&);
-    Vector operator- (Vector&);
-    double abs ();
+        Vector operator+ (Vector &);
+        Vector operator- (Vector &);
+        double abs ();
+
+        void vector ();
+        double angle (Vector &);
+        void setMode (Mode m = RAD);
 
-    void vector ();
-    double angle (Vector&);
-    void setMode (Mode m = RAD);
-
-  private:
-    Mode mode;
-    int x;
-    int y;
-};
+      private:
+        Mode mode;
+        int x;
+        int y;
+    };
+}
 #endif
--- a/pmc/main.cc
+++ b/pmc/main.cc
@@ -16,6 +16,8 @@
 
 #define MAX 3
 
+using namespace algebra;
+
 int main (int argc, char **argv)
 {
     Surface *surface;
@@ -45,7 +47,7 @@
     d[2] = new Cube (surface, p1, p2, p3, p4, height);
 
     bool running = true;
-    int x, y;
+    int i, x, y;
 
     x = y = 0;
 
@@ -60,7 +62,7 @@
 
         if (event.type == SDL_KEYDOWN) {
             surface->setColor (Surface::BLACK);
-            for (int i = 0; i < MAX; i++)
+            for (i = 0; i < MAX; i++)
                 d[i]->show ();
             surface->setColor (Surface::RED);
 
@@ -70,29 +72,29 @@
                 break;
 
             case SDLK_UP:
-                for (int i = 0; i < MAX; i++)
+                for (i = 0; i < MAX; i++)
                     d[i]->move ((Vector (x, y - STEP)));
                 break;
 
             case SDLK_DOWN:
-                for (int i = 0; i < MAX; i++)
+                for (i = 0; i < MAX; i++)
                     d[i]->move ((Vector (x, y + STEP)));
                 break;
 
             case SDLK_LEFT:
-                for (int i = 0; i < MAX; i++)
+                for (i = 0; i < MAX; i++)
                     d[i]->move ((Vector (x - STEP, y)));
                 break;
 
             case SDLK_RIGHT:
-                for (int i = 0; i < MAX; i++)
+                for (i = 0; i < MAX; i++)
                     d[i]->move ((Vector (x + STEP, y)));
                 break;
             default:
                 break;
             }
 
-            for (int i = 0; i < MAX; i++) {
+            for (i = 0; i < MAX; i++) {
                 d[i]->show ();
             }
 
@@ -100,7 +102,7 @@
         }
     }
 
-    for (int i = 0; i < MAX; i++)
+    for (i = 0; i < MAX; i++)
         delete d[i];
 
     delete surface;
--- a/pmc/rectangle.cc
+++ b/pmc/rectangle.cc
@@ -6,28 +6,44 @@
 #include <rectangle.h>
 #include <cstdio>
 
+using namespace algebra;
+
 Rectangle::Rectangle (Surface * s)
 {
+    surface = s;
+    name = "Rectangle";
+
+    anker = Vector (0, 0);
+
     P[0] = new Vector (0, 0);
     P[1] = new Vector (0, 100);
     P[2] = new Vector (100, 100);
     P[3] = new Vector (0, 100);
+}
+
+Rectangle::Rectangle (Surface * s, Vector & p1, Vector & p2, Vector & p3, Vector & p4)
+{
+    surface = s;
+    name = "Rectangle";
 
     anker = Vector (0, 0);
 
-    surface = s;
-}
-
-Rectangle::Rectangle (Surface * s, Vector& p1, Vector& p2, Vector& p3, Vector& p4)
-{
     P[0] = new Vector (p1);
     P[1] = new Vector (p2);
     P[2] = new Vector (p3);
     P[3] = new Vector (p4);
+}
+
+Rectangle::Rectangle (Surface * s, Vector p[4])
+{
+    surface = s;
+    name = "Rectangle";
 
     anker = Vector (0, 0);
 
-    surface = s;
+    for (int i = 0; i < 4; i++) {
+        P[i] = new Vector (p[i]);
+    }
 }
 
 Rectangle::~Rectangle ()
--- a/pmc/vector.cc
+++ b/pmc/vector.cc
@@ -8,6 +8,8 @@
 
 #include <cstdio>
 
+using namespace algebra;
+
 Vector::Vector (int xx, int yy)
 {
     name = "Vector";
@@ -37,12 +39,12 @@
     return y;
 }
 
-Vector Vector::operator+ (Vector& a)
+Vector Vector::operator+ (Vector & a)
 {
     return Vector (x + a.X (), y + a.Y ());
 }
 
-Vector Vector::operator- (Vector& a)
+Vector Vector::operator- (Vector & a)
 {
     return Vector (x - a.X (), y - a.Y ());
 }
@@ -57,7 +59,7 @@
     fprintf (stderr, "(%.3d, %.3d)", x, y);
 }
 
-double Vector::angle (Vector& v)
+double Vector::angle (Vector & v)
 {
     if (mode == DEG)
         return ((180.0 / M_PI) * std::acos ((v.X () * X () + v.Y () * Y ()) / (abs () * v.abs ())));
--- a/vector.cc
+++ b/vector.cc
@@ -20,11 +20,11 @@
         Vector (int xx = 0, int yy = 0);
        ~Vector ();
 
-        int X () { return x; }
-        int Y () { return y; }
+        int X () { return x; };
+        int Y () { return y; };
 
-        Vector operator+ (Vector&);
-        Vector operator- (Vector&);
+        Vector operator+ (Vector &);
+        Vector operator- (Vector &);
 
         void vector ();
         double abs ();
@@ -39,11 +39,11 @@
         fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y);
     }
 
-    Vector Vector::operator+ (Vector& v) {
+    Vector Vector::operator+ (Vector & v) {
         return Vector (x + v.X (), y + v.Y ());
     }
 
-    Vector Vector::operator- (Vector& v) {
+    Vector Vector::operator- (Vector & v) {
         return Vector (x - v.X (), y - v.Y ());
     }