All units are complete and warnings removed.

This commit is contained in:
Kolan Sh 2012-05-03 01:04:27 +04:00
parent 9490c1e935
commit cf730f056d
6 changed files with 105 additions and 45 deletions

View File

@ -4,14 +4,23 @@
void Figure_constructor (Figure *this) void Figure_constructor (Figure *this)
{ {
// nothing here // nothing here
// warnings stub
this = this;
} }
void Figure_destructor (Figure *this) void Figure_destructor (Figure *this)
{ {
// nothing here // 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 // nothing here
// warnings stub
dest = (Figure *) src;
} }

View File

@ -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. # This file is generated with smake.sh.
# You can use this make file with instruction make to # You can use this make file with instruction make to
# use one of build mode: debug, profile, develop, release. # use one of build mode: debug, profile, develop, release.
@ -20,7 +20,7 @@ CC=cc
CXX=c++ CXX=c++
CFLAGS := $(CFLAGS) CFLAGS := $(CFLAGS)
CXXFLAGS := $(CXXFLAGS) CXXFLAGS := $(CXXFLAGS)
LDFLAGS := $(LDFLAGS) LDFLAGS := -lm $(LDFLAGS)
LIBS= LIBS=
SRC= SRC=
INCLUDES= INCLUDES=

View File

@ -5,11 +5,12 @@
#include "Polygon.h" #include "Polygon.h"
/* considered to be protected */ /* 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); Figure_constructor ((Figure *) this);
this->npoints = npoints; 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) void Polygon_destructor (Polygon *this)
@ -19,8 +20,10 @@ void Polygon_destructor (Polygon *this)
void Polygon_copy (Polygon *dest, const Polygon *src) void Polygon_copy (Polygon *dest, const Polygon *src)
{ {
free (dest->points);
dest->npoints = src->npoints; 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 */ /* private */
@ -32,7 +35,7 @@ static const char* __Polygon_type ()
static Polygon* __Polygon_clone (const Polygon *this) static Polygon* __Polygon_clone (const Polygon *this)
{ {
Polygon *poly = malloc (sizeof (Polygon)); Polygon *poly = malloc (sizeof (Polygon));
memset (poly, 0, sizeof ( memset (poly, 0, sizeof (Polygon));
Polygon_copy (poly, this); Polygon_copy (poly, this);
return poly; return poly;
} }
@ -40,12 +43,12 @@ static Polygon* __Polygon_clone (const Polygon *this)
static void __Polygon_destroy (Polygon *this) static void __Polygon_destroy (Polygon *this)
{ {
Polygon_destructor (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++) 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); 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; double s = 0.0;
int i = 0; size_t i = 0;
for (i = 0; i < this->npoints; i++) 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}) // 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, double diag = 0.0,
tmp = 0.0, tmp = 0.0,
tmp1 = 0.0, tmp1 = 0.0,
tmp2 = 0.0; tmp2 = 0.0;
int i = 0, j = 0; size_t i = 0, j = 0;
for (i = 0; i < this->npoints - 2; i++) for (i = 0; i < this->npoints - 2; i++)
{ {
@ -96,9 +99,20 @@ static double __Polygon_max_diag (const Polygon *this)
} }
/* public */ /* 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); Polygon_constructor (poly, points, npoints);
poly->vtable = &vtable;
return poly; return poly;
} }

View File

@ -1,6 +1,8 @@
#ifndef __POLYGON_H__ #ifndef __POLYGON_H__
#define __POLYGON_H__ #define __POLYGON_H__
#include <stdlib.h>
#include "Figure.h" #include "Figure.h"
typedef struct Polygon typedef struct Polygon
@ -22,7 +24,7 @@ typedef struct Polygon
} *vtable; } *vtable;
/* Polygon fields */ /* Polygon fields */
int npoints; size_t npoints;
struct point struct point
{ {
@ -32,7 +34,7 @@ typedef struct Polygon
} 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) 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 */ /* 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_destructor (Polygon *this);
void Polygon_copy (Polygon *dest, const Polygon *src); 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__ #endif // __POLYGON_H__

76
Rhomb.c
View File

@ -1,5 +1,25 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "Rhomb.h" #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 */ /* private */
static const char* __Rhomb_type () static const char* __Rhomb_type ()
{ {
@ -8,52 +28,64 @@ static const char* __Rhomb_type ()
static Rhomb* __Rhomb_clone (const Rhomb *this) 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) static void __Rhomb_destroy (Rhomb *this)
{ {
Rhomb_destructor (this);
free (this);
} }
static void __Rhomb_draw (const Rhomb *this) static void __Rhomb_draw (const Rhomb *this)
{ {
__Polygon_draw ((Polygon *) this);
} }
static double __Rhomb_area (const Rhomb *this) static double __Rhomb_area (const Rhomb *this)
{ {
return __Polygon_area ((Polygon *) this);
} }
static double __Rhomb_max_diag (const Rhomb *this) static double __Rhomb_max_diag (const Rhomb *this)
{ {
return __Polygon_max_diag ((Polygon *) this);
} }
static int __Rhomb_is_square (const Rhomb *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 */ /* public */
Rhomb* Rhomb_new (const struct point *points, int npoints) Rhomb* Rhomb_new (const struct point *points)
{
}
/* 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)
{ {
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;
} }

View File

@ -25,13 +25,13 @@ typedef struct Rhomb
} *vtable; } *vtable;
/* derived from Polygon */ /* derived from Polygon */
int npoints; size_t npoints;
struct point *points; struct point *points;
} Rhomb; } 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) 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 */ /* 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_destructor (Rhomb *this);
void Rhomb_copy (Rhomb *dest, const Rhomb *src); void Rhomb_copy (Rhomb *dest, const Rhomb *src);