From cf730f056dd7b5327250900cf816e1d96cdedd9f Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Thu, 3 May 2012 01:04:27 +0400 Subject: [PATCH] All units are complete and warnings removed. --- Figure.c | 11 +++++++- Makefile | 4 +-- Polygon.c | 42 ++++++++++++++++++++---------- Polygon.h | 11 +++++--- Rhomb.c | 76 +++++++++++++++++++++++++++++++++++++++---------------- Rhomb.h | 6 ++--- 6 files changed, 105 insertions(+), 45 deletions(-) diff --git a/Figure.c b/Figure.c index 6d5f37e..1408f23 100644 --- a/Figure.c +++ b/Figure.c @@ -4,14 +4,23 @@ void Figure_constructor (Figure *this) { // nothing here + + // warnings stub + this = this; } void Figure_destructor (Figure *this) { // nothing here + + // warnings stub + this = this; } -void Figure_copy (struct Figure *dest, const struct Figure *src) +void Figure_copy (Figure *dest, const Figure *src) { // nothing here + + // warnings stub + dest = (Figure *) src; } diff --git a/Makefile b/Makefile index f66945d..b9d63ff 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# Makefile generated by command: smake.sh -t main +# Makefile generated by command: smake.sh -t main --ldflags=-lm # This file is generated with smake.sh. # You can use this make file with instruction make to # use one of build mode: debug, profile, develop, release. @@ -20,7 +20,7 @@ CC=cc CXX=c++ CFLAGS := $(CFLAGS) CXXFLAGS := $(CXXFLAGS) -LDFLAGS := $(LDFLAGS) +LDFLAGS := -lm $(LDFLAGS) LIBS= SRC= INCLUDES= diff --git a/Polygon.c b/Polygon.c index de04a6b..4a27b00 100644 --- a/Polygon.c +++ b/Polygon.c @@ -5,11 +5,12 @@ #include "Polygon.h" /* considered to be protected */ -void Polygon_constructor (Polygon *this, const struct point *points, int npoints) +void Polygon_constructor (Polygon *this, const struct point *points, size_t npoints) { Figure_constructor ((Figure *) this); this->npoints = npoints; - this->points = strdup (points, npoints * sizeof (struct point)); + this->points = malloc (npoints * sizeof (struct point)); + memcpy (this->points, points, npoints * sizeof (struct point)); } void Polygon_destructor (Polygon *this) @@ -19,8 +20,10 @@ void Polygon_destructor (Polygon *this) void Polygon_copy (Polygon *dest, const Polygon *src) { + free (dest->points); dest->npoints = src->npoints; - dest->points = strdup (src->points, src->npoints * sizeof (struct point)); + dest->points = malloc (src->npoints * sizeof (struct point)); + memcpy (dest->points, src->points, src->npoints * sizeof (struct point)); } /* private */ @@ -32,7 +35,7 @@ static const char* __Polygon_type () static Polygon* __Polygon_clone (const Polygon *this) { Polygon *poly = malloc (sizeof (Polygon)); - memset (poly, 0, sizeof ( + memset (poly, 0, sizeof (Polygon)); Polygon_copy (poly, this); return poly; } @@ -40,12 +43,12 @@ static Polygon* __Polygon_clone (const Polygon *this) static void __Polygon_destroy (Polygon *this) { Polygon_destructor (this); - g_free (this); + free (this); } -static void __Polygon_draw (const Polygon *this) +void __Polygon_draw (const Polygon *this) { - int i = 0; + size_t i = 0; for (i = 0; i < this->npoints; i++) { @@ -54,10 +57,10 @@ static void __Polygon_draw (const Polygon *this) printf ("{%f;%f}, ", this->points[0].x, this->points[0].y); } -static double __Polygon_area (const Polygon *this) +double __Polygon_area (const Polygon *this) { - double s = 0.0, x, y; - int i = 0; + double s = 0.0; + size_t i = 0; for (i = 0; i < this->npoints; i++) { @@ -70,13 +73,13 @@ static double __Polygon_area (const Polygon *this) // S=abs(сумма{(x[i+1]-x[i])*(y[i+1]+y[i])/2}) } -static double __Polygon_max_diag (const Polygon *this) +double __Polygon_max_diag (const Polygon *this) { double diag = 0.0, tmp = 0.0, tmp1 = 0.0, tmp2 = 0.0; - int i = 0, j = 0; + size_t i = 0, j = 0; for (i = 0; i < this->npoints - 2; i++) { @@ -96,9 +99,20 @@ static double __Polygon_max_diag (const Polygon *this) } /* public */ -Polygon* Polygon_new (const struct point *points, int npoints) +Polygon* Polygon_new (const struct point *points, size_t npoints) { - Polygon *poly = g_new0 (Polygon, 1); + static struct Polygon_vtable vtable = { + __Polygon_type, + __Polygon_clone, + __Polygon_destroy, + __Polygon_draw, + __Polygon_area, + __Polygon_max_diag + }; + + Polygon *poly = malloc (sizeof (Polygon)); + memset (poly, 0, sizeof (Polygon)); Polygon_constructor (poly, points, npoints); + poly->vtable = &vtable; return poly; } diff --git a/Polygon.h b/Polygon.h index 6f44adb..183049d 100644 --- a/Polygon.h +++ b/Polygon.h @@ -1,6 +1,8 @@ #ifndef __POLYGON_H__ #define __POLYGON_H__ +#include + #include "Figure.h" typedef struct Polygon @@ -22,7 +24,7 @@ typedef struct Polygon } *vtable; /* Polygon fields */ - int npoints; + size_t npoints; struct point { @@ -32,7 +34,7 @@ typedef struct Polygon } Polygon; -Polygon* Polygon_new (const struct point *points, int npoints); +Polygon* Polygon_new (const struct point *points, size_t npoints); static inline const char* Polygon_type (const Polygon *this) { @@ -65,8 +67,11 @@ static inline double Polygon_max_diag (const Polygon *this) } /* considered to be protected */ -void Polygon_constructor (Polygon *this, const struct point *points, int npoints); +void Polygon_constructor (Polygon *this, const struct point *points, size_t npoints); void Polygon_destructor (Polygon *this); void Polygon_copy (Polygon *dest, const Polygon *src); +void __Polygon_draw (const Polygon *this); +double __Polygon_area (const Polygon *this); +double __Polygon_max_diag (const Polygon *this); #endif // __POLYGON_H__ diff --git a/Rhomb.c b/Rhomb.c index 9510730..01310e2 100644 --- a/Rhomb.c +++ b/Rhomb.c @@ -1,5 +1,25 @@ +#include +#include +#include + #include "Rhomb.h" +/* considered to be protected */ +void Rhomb_constructor (Rhomb *this, const struct point *points) +{ + Polygon_constructor ((Polygon *) this, points, 4); +} + +void Rhomb_destructor (Rhomb *this) +{ + Polygon_destructor ((Polygon *) this); +} + +void Rhomb_copy (Rhomb *dest, const Rhomb *src) +{ + Polygon_copy ((Polygon *) dest, (Polygon *) src); +} + /* private */ static const char* __Rhomb_type () { @@ -8,52 +28,64 @@ static const char* __Rhomb_type () static Rhomb* __Rhomb_clone (const Rhomb *this) { - + Rhomb *rhomb = malloc (sizeof (Rhomb)); + memset (rhomb, 0, sizeof (Rhomb)); + Rhomb_copy (rhomb, this); + return rhomb; } static void __Rhomb_destroy (Rhomb *this) { - + Rhomb_destructor (this); + free (this); } static void __Rhomb_draw (const Rhomb *this) { - + __Polygon_draw ((Polygon *) this); } static double __Rhomb_area (const Rhomb *this) { - + return __Polygon_area ((Polygon *) this); } static double __Rhomb_max_diag (const Rhomb *this) { - + return __Polygon_max_diag ((Polygon *) this); } static int __Rhomb_is_square (const Rhomb *this) { + double diag1, diag2, tmp1, tmp2; + + tmp1 = this->points[0].x - this->points[2].x; + tmp2 = this->points[0].y - this->points[2].y; + diag1 = tmp1 * tmp1 + tmp2 * tmp2; + tmp1 = this->points[1].x - this->points[3].x; + tmp2 = this->points[1].y - this->points[3].y; + diag2 = tmp1 * tmp1 + tmp2 * tmp2; + + return (diag1 == diag2); } /* public */ -Rhomb* Rhomb_new (const struct point *points, int npoints) -{ - -} - -/* considered to be protected */ -void Rhomb_constructor (Rhomb *this, const struct point *points, int npoints) -{ - -} - -void Rhomb_destructor (Rhomb *this) -{ - -} - -void Rhomb_copy (Rhomb *dest, const Rhomb *src) +Rhomb* Rhomb_new (const struct point *points) { + static struct Rhomb_vtable vtable = { + __Rhomb_type, + __Rhomb_clone, + __Rhomb_destroy, + __Rhomb_draw, + __Rhomb_area, + __Rhomb_max_diag, + __Rhomb_is_square + }; + Rhomb *rhomb = malloc (sizeof (Rhomb)); + memset (rhomb, 0, sizeof (Rhomb)); + Rhomb_constructor (rhomb, points); + rhomb->vtable = &vtable; + return rhomb; } diff --git a/Rhomb.h b/Rhomb.h index 96907bb..33e743c 100644 --- a/Rhomb.h +++ b/Rhomb.h @@ -25,13 +25,13 @@ typedef struct Rhomb } *vtable; /* derived from Polygon */ - int npoints; + size_t npoints; struct point *points; } Rhomb; -Rhomb* Rhomb_new (const struct point *points, int npoints); +Rhomb* Rhomb_new (const struct point *points); static inline const char* Rhomb_type (const Rhomb *this) { @@ -69,7 +69,7 @@ static inline int Rhomb_is_square (const Rhomb *this) } /* considered to be protected */ -void Rhomb_constructor (Rhomb *this, const struct point *points, int npoints); +void Rhomb_constructor (Rhomb *this, const struct point *points); void Rhomb_destructor (Rhomb *this); void Rhomb_copy (Rhomb *dest, const Rhomb *src);