From 708ec83e36924ff1d459556fccd462a14e528cca Mon Sep 17 00:00:00 2001 From: sakuya213 Date: Thu, 22 Jan 2015 08:50:25 +0100 Subject: [PATCH 1/4] added factory --- main/circle.h | 4 ++-- main/diamond.h | 6 +++--- main/factory.cpp | 2 ++ main/factory.h | 21 +++++++++++++++++++ main/figure.h | 6 ++++-- main/figurespatial.h | 4 ++-- main/library.h | 3 +-- main/main.cpp | 8 ++++--- main/main.vcxproj | 6 ++++-- main/main.vcxproj.filters | 18 ++++++++++------ main/pentagon.h | 4 ++-- main/point.cpp | 2 +- main/prismpentagonal.h | 6 +++--- main/prismtetragonal.h | 6 +++--- main/prismtriangular.h | 6 +++--- main/pyramidpentagonal.h | 6 +++--- main/pyramidtetragonal.h | 6 +++--- ...idtriangular.cpp => pyramidtriangular.cpp} | 2 +- ...iramidtriangular.h => pyramidtriangular.h} | 10 ++++----- main/square.h | 6 +++--- main/tetragon.h | 6 +++--- main/trapeze.h | 6 +++--- main/triangle.cpp | 9 +++++--- main/triangle.h | 6 +++--- 24 files changed, 98 insertions(+), 61 deletions(-) create mode 100644 main/factory.cpp create mode 100644 main/factory.h rename main/{piramidtriangular.cpp => pyramidtriangular.cpp} (98%) rename main/{piramidtriangular.h => pyramidtriangular.h} (59%) diff --git a/main/circle.h b/main/circle.h index 94dc8e4..e9ca195 100644 --- a/main/circle.h +++ b/main/circle.h @@ -14,8 +14,8 @@ class Circle: public Figure void setA(Point _a); void setR(double _r); Point getA(); - double getR(); - double getArea(); + virtual double getR(); + virtual double getArea(); }; diff --git a/main/diamond.h b/main/diamond.h index 254d83a..7fc9da6 100644 --- a/main/diamond.h +++ b/main/diamond.h @@ -6,12 +6,12 @@ class Diamond: public Figure { private: - Point points[3]; + Point points[4]; public: Diamond(Point _a, Point _b, Point _c, Point _d); - double getCircumference(); - double getArea(); + virtual double getCircumference(); + virtual double getArea(); }; #endif \ No newline at end of file diff --git a/main/factory.cpp b/main/factory.cpp new file mode 100644 index 0000000..350f084 --- /dev/null +++ b/main/factory.cpp @@ -0,0 +1,2 @@ +#include "factory.h" + diff --git a/main/factory.h b/main/factory.h new file mode 100644 index 0000000..825e01d --- /dev/null +++ b/main/factory.h @@ -0,0 +1,21 @@ +#ifndef factory_h +#define factory_h + +enum figure +{circle,diamond,pentagon,squre,tetragon,trapeze,triangle}; + +enum figureSpatial +{piramidtriangular,prismpentagonal,prismtetragonal,prismtriangular,pyramidpentagonal,pyramidtetragonal}; + +class factory +{ +public: + + +}; + + + + + +#endif \ No newline at end of file diff --git a/main/figure.h b/main/figure.h index cb99ec2..477ceeb 100644 --- a/main/figure.h +++ b/main/figure.h @@ -3,8 +3,10 @@ class Figure { - double getCircumference(); - double getArea(); +public: + + virtual double getCircumference(); + virtual double getArea(); }; #endif diff --git a/main/figurespatial.h b/main/figurespatial.h index b382ca7..45ab082 100644 --- a/main/figurespatial.h +++ b/main/figurespatial.h @@ -3,8 +3,8 @@ class FigureSpatial { - double getCircumference(); - double getVolume(); + virtual double getCircumference(); + virtual double getVolume(); }; #endif diff --git a/main/library.h b/main/library.h index 10d49cb..4f1bd02 100644 --- a/main/library.h +++ b/main/library.h @@ -1,7 +1,6 @@ #ifndef library_h #define library_h -#include #include #include "point.h" #include "pointspatial.h" @@ -14,7 +13,7 @@ #include "triangle.h" #include "figure.h" #include "figurespatial.h" -#include "piramidtriangular.h" +#include "pyramidtriangular.h" #include "pyramidtetragonal.h" #include "pyramidpentagonal.h" #include "prismpentagonal.h" diff --git a/main/main.cpp b/main/main.cpp index ad51233..8627d4c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,9 +1,11 @@ +#include #include "library.h" -//using namespace std; + +using namespace std; @@ -14,9 +16,9 @@ int main() Point _a(5,0); Point _b(10,0); Point _c(0,5); - Triangle test(_a,_b,_c); + Figure *test=new Triangle(_a,_b,_c); - std::cout<getCircumference()< + - + @@ -82,11 +83,12 @@ + - + diff --git a/main/main.vcxproj.filters b/main/main.vcxproj.filters index 8a9e21e..5926174 100644 --- a/main/main.vcxproj.filters +++ b/main/main.vcxproj.filters @@ -96,15 +96,18 @@ Source Files\figure spatial\figures spatial - - Source Files\figure spatial\figures spatial - Source Files\figure Source Files\figure\figures + + Source Files + + + Source Files\figure spatial\figures spatial + @@ -116,9 +119,6 @@ Header Files\figurespatial - - Header Files\figurespatial\figures - Header Files\figurespatial\figures @@ -161,5 +161,11 @@ Header Files + + Header Files + + + Header Files\figurespatial\figures + \ No newline at end of file diff --git a/main/pentagon.h b/main/pentagon.h index 2539c2f..3858cad 100644 --- a/main/pentagon.h +++ b/main/pentagon.h @@ -9,7 +9,7 @@ class Pentagon: public Figure Point points[5]; public: Pentagon(Point _a, Point _b, Point _c, Point _d, Point _e); - double getCircumference(); - double getArea(); + virtual double getCircumference(); + virtual double getArea(); }; #endif \ No newline at end of file diff --git a/main/point.cpp b/main/point.cpp index e4e466e..3605c9a 100644 --- a/main/point.cpp +++ b/main/point.cpp @@ -1,7 +1,7 @@ #include "point.h" -Point::Point(double _x, double _y){ + Point::Point(double _x, double _y){ this->x = _x; this->y = _y; } diff --git a/main/prismpentagonal.h b/main/prismpentagonal.h index 6424748..72d1a7a 100644 --- a/main/prismpentagonal.h +++ b/main/prismpentagonal.h @@ -6,10 +6,10 @@ class PrismPentagonal: public FigureSpatial { private: - PointSpatial points[9]; + PointSpatial points[10]; public: PrismPentagonal(PointSpatial _a, PointSpatial _b, PointSpatial _c, PointSpatial _d, PointSpatial _e, PointSpatial _f, PointSpatial _g, PointSpatial _h, PointSpatial _i, PointSpatial _j); - double getCircumference(); - double getVolume(); + virtual double getCircumference(); + virtual double getVolume(); }; #endif \ No newline at end of file diff --git a/main/prismtetragonal.h b/main/prismtetragonal.h index 02105b0..17fa90e 100644 --- a/main/prismtetragonal.h +++ b/main/prismtetragonal.h @@ -6,10 +6,10 @@ class PrismTetragonal: public FigureSpatial { private: - PointSpatial points[7]; + PointSpatial points[8]; public: PrismTetragonal(PointSpatial _a, PointSpatial _b, PointSpatial _c, PointSpatial _d, PointSpatial _e, PointSpatial _f, PointSpatial _g, PointSpatial _h); - double getCircumference(); - double getVolume(); + virtual double getCircumference(); + virtual double getVolume(); }; #endif diff --git a/main/prismtriangular.h b/main/prismtriangular.h index 61ce2a2..931839e 100644 --- a/main/prismtriangular.h +++ b/main/prismtriangular.h @@ -6,11 +6,11 @@ class PrismTriangular: public FigureSpatial { private: - PointSpatial points[5]; + PointSpatial points[6]; public: PrismTriangular(PointSpatial _a, PointSpatial _b, PointSpatial _c, PointSpatial _d, PointSpatial _e, PointSpatial _f); - double getCircumference(); - double getVolume(); + virtual double getCircumference(); + virtual double getVolume(); }; #endif \ No newline at end of file diff --git a/main/pyramidpentagonal.h b/main/pyramidpentagonal.h index 517b8f6..a6626b0 100644 --- a/main/pyramidpentagonal.h +++ b/main/pyramidpentagonal.h @@ -6,11 +6,11 @@ class PyramidPentagonal: public FigureSpatial { private: - PointSpatial points[5]; + PointSpatial points[6]; public: PyramidPentagonal(PointSpatial _a, PointSpatial _b, PointSpatial _c, PointSpatial _d, PointSpatial _e, PointSpatial _f); - double getCircumference(); - double getVolume(); + virtual double getCircumference(); + virtual double getVolume(); }; #endif diff --git a/main/pyramidtetragonal.h b/main/pyramidtetragonal.h index f4eddeb..d939042 100644 --- a/main/pyramidtetragonal.h +++ b/main/pyramidtetragonal.h @@ -7,11 +7,11 @@ class PyramidTetragonal: public FigureSpatial { private: - PointSpatial points[4]; + PointSpatial points[5]; public: PyramidTetragonal(PointSpatial _a, PointSpatial _b, PointSpatial _c, PointSpatial _d, PointSpatial _e); - double getCircumference(); - double getVolume(); + virtual double getCircumference(); + virtual double getVolume(); }; #endif diff --git a/main/piramidtriangular.cpp b/main/pyramidtriangular.cpp similarity index 98% rename from main/piramidtriangular.cpp rename to main/pyramidtriangular.cpp index 024ffbe..969686f 100644 --- a/main/piramidtriangular.cpp +++ b/main/pyramidtriangular.cpp @@ -1,4 +1,4 @@ -#include "piramidtriangular.h" +#include "pyramidtriangular.h" #include PyramidTriangular::PyramidTriangular(PointSpatial _a, PointSpatial _b, PointSpatial _c, PointSpatial _d){ diff --git a/main/piramidtriangular.h b/main/pyramidtriangular.h similarity index 59% rename from main/piramidtriangular.h rename to main/pyramidtriangular.h index ab5e570..7c0c362 100644 --- a/main/piramidtriangular.h +++ b/main/pyramidtriangular.h @@ -1,15 +1,15 @@ -#ifndef piramidtriangular_h -#define piramidtriangular_h +#ifndef pyramidtriangular_h +#define pyramidtriangular_h #include "figurespatial.h" #include "pointspatial.h" class PyramidTriangular: public FigureSpatial { private: - PointSpatial points[3]; + PointSpatial points[4]; public: PyramidTriangular(PointSpatial _a, PointSpatial _b, PointSpatial _c, PointSpatial _d); - double getCircumference(); - double getVolume(); + virtual double getCircumference(); + virtual double getVolume(); }; #endif diff --git a/main/square.h b/main/square.h index a9c27ba..ce54df3 100644 --- a/main/square.h +++ b/main/square.h @@ -6,10 +6,10 @@ class Square: public Figure { private: - Point points[3]; + Point points[4]; public: Square(Point _a, Point _b, Point _c, Point _d); - double getCircumference(); - double getArea(); + virtual double getCircumference(); + virtual double getArea(); }; #endif \ No newline at end of file diff --git a/main/tetragon.h b/main/tetragon.h index 4490d4f..be62e9d 100644 --- a/main/tetragon.h +++ b/main/tetragon.h @@ -6,11 +6,11 @@ class Tetragon: public Figure { private: - Point points[3]; + Point points[4]; public: Tetragon(Point _a, Point _b, Point _c, Point _d); - double getCircumference(); - double getArea(); + virtual double getCircumference(); + virtual double getArea(); }; #endif \ No newline at end of file diff --git a/main/trapeze.h b/main/trapeze.h index 2fa4059..fb3e030 100644 --- a/main/trapeze.h +++ b/main/trapeze.h @@ -6,11 +6,11 @@ class Trapeze: public Figure { private: - Point points[3]; + Point points[4]; public: Trapeze(Point _a, Point _b, Point _c, Point _d); - double getCircumference(); - double getArea(); + virtual double getCircumference(); + virtual double getArea(); }; #endif \ No newline at end of file diff --git a/main/triangle.cpp b/main/triangle.cpp index 5c860fe..93febfb 100644 --- a/main/triangle.cpp +++ b/main/triangle.cpp @@ -11,10 +11,13 @@ Triangle::Triangle(Point _a, Point _b, Point _c){ double Triangle::getCircumference() { double cir = 0; + int a,b; for(int i=0;i<3;i++){ // cir triangle + a = (i%3); + b = ((i+1)%3); cir += sqrt( - ( points[(i)%3].getX() - points[(i+1)%3].getX() ) * ( points[(i)%3].getX() - points[(i+1)%3].getX() ) + - ( points[(i)%3].getY() - points[(i+1)%3].getY() ) * ( points[(i)%3].getY() - points[(i+1)%3].getY() ) + ( points[a].getX() - points[b].getX() ) * ( points[a].getX() - points[b].getX() ) + + ( points[a].getY() - points[b].getY() ) * ( points[a].getY() - points[b].getY() ) ); } return cir; @@ -26,5 +29,5 @@ double Triangle::getArea() ( points[0].getX() - points[1].getX() ) * ( points[0].getX() - points[1].getX() ) + ( points[0].getY() - points[1].getY() ) * ( points[0].getY() - points[1].getY() ) ); - return area * area * sqrt(double(3)) /4; + return area * area * sqrt(double(3)) /((double)4); } \ No newline at end of file diff --git a/main/triangle.h b/main/triangle.h index 6934b6e..8c887af 100644 --- a/main/triangle.h +++ b/main/triangle.h @@ -6,10 +6,10 @@ class Triangle: public Figure { private: - Point points[2]; + Point points[3]; public: Triangle(Point _a, Point _b, Point _c); - double getCircumference(); - double getArea(); + virtual double getCircumference(); + virtual double getArea(); }; #endif From 1586a3458a601c52dac58d9a4923211ea52d1d37 Mon Sep 17 00:00:00 2001 From: sakuya213 Date: Fri, 23 Jan 2015 00:41:39 +0100 Subject: [PATCH 2/4] factory ended --- main/factory.cpp | 2 ++ main/factory.h | 7 ++--- main/factoryfigure.cpp | 34 +++++++++++++++++++++ main/factoryfigure.h | 35 +++++++++++++++++++++ main/factoryfiguresspatial.cpp | 56 ++++++++++++++++++++++++++++++++++ main/factoryfiguresspatial.h | 36 ++++++++++++++++++++++ main/figurespatial.h | 2 ++ main/main.cpp | 15 +++++---- main/main.vcxproj | 4 +++ main/main.vcxproj.filters | 12 ++++++++ 10 files changed, 192 insertions(+), 11 deletions(-) create mode 100644 main/factoryfigure.cpp create mode 100644 main/factoryfigure.h create mode 100644 main/factoryfiguresspatial.cpp create mode 100644 main/factoryfiguresspatial.h diff --git a/main/factory.cpp b/main/factory.cpp index 350f084..1e8fbf9 100644 --- a/main/factory.cpp +++ b/main/factory.cpp @@ -1,2 +1,4 @@ #include "factory.h" +#include "factoryfigure.h" +#include "factoryfiguresspatial.h" diff --git a/main/factory.h b/main/factory.h index 825e01d..ef7a904 100644 --- a/main/factory.h +++ b/main/factory.h @@ -1,15 +1,12 @@ #ifndef factory_h #define factory_h -enum figure -{circle,diamond,pentagon,squre,tetragon,trapeze,triangle}; -enum figureSpatial -{piramidtriangular,prismpentagonal,prismtetragonal,prismtriangular,pyramidpentagonal,pyramidtetragonal}; class factory { -public: + public: + }; diff --git a/main/factoryfigure.cpp b/main/factoryfigure.cpp new file mode 100644 index 0000000..a2a6cc8 --- /dev/null +++ b/main/factoryfigure.cpp @@ -0,0 +1,34 @@ +#include "factoryfigure.h" + + +FactoryFigure::FactoryFigure(figureType f,Point _a,Point _b,Point _c){ + if(f==triangle){ + this->a= new Triangle(_a,_b,_c); + } +} +FactoryFigure::FactoryFigure(figureType f,Point _a,Point _b,Point _c,Point _d){ + switch(f){ + case diamond: + this->a= new Diamond(_a,_b,_c,_d); + break; + case square: + this->a= new Square(_a,_b,_c,_d); + break; + case tetragon: + this->a= new Tetragon(_a,_b,_c,_d); + break; + case trapeze: + this->a= new Trapeze(_a,_b,_c,_d); + break; + } +} +FactoryFigure::FactoryFigure(figureType f,Point _a,double _r){ + if(f==circle){ + this->a= new Circle(_a,_r); + } +} +FactoryFigure::FactoryFigure(figureType f,Point _a,Point _b,Point _c,Point _d,Point _e){ + if(f==pentagon){ + this->a= new Pentagon(_a,_b,_c,_d,_e); + } +} diff --git a/main/factoryfigure.h b/main/factoryfigure.h new file mode 100644 index 0000000..d27297c --- /dev/null +++ b/main/factoryfigure.h @@ -0,0 +1,35 @@ +#ifndef factoryfigure_h +#define factoryfigure_h +#include "figure.h" +#include "point.h" +#include "triangle.h" +#include "diamond.h" +#include "square.h" +#include "tetragon.h" +#include "trapeze.h" +#include "circle.h" +#include "pentagon.h" + + +enum figureType +{ + circle, + diamond, + pentagon, + square, + tetragon, + trapeze, + triangle +}; + +class FactoryFigure +{ +public: + Figure *a; + FactoryFigure(figureType f,Point _a,Point _b,Point _c); + FactoryFigure(figureType f,Point _a,Point _b,Point _c,Point _d); + FactoryFigure(figureType f,Point _a,double _r); + FactoryFigure(figureType f,Point _a,Point _b,Point _c,Point _d,Point _e); +}; + +#endif diff --git a/main/factoryfiguresspatial.cpp b/main/factoryfiguresspatial.cpp new file mode 100644 index 0000000..620f3d2 --- /dev/null +++ b/main/factoryfiguresspatial.cpp @@ -0,0 +1,56 @@ +#include "factoryfiguresspatial.h" + + +FactoryFigureSpatial::FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d) + +{ + if ( f == pyramidtriangular ) + { + this->a = new PyramidTriangular(_a,_b,_c,_d); + } + + +} +FactoryFigureSpatial::FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d,PointSpatial _e) +{ + if ( f == pyramidtetragonal ) + { + this->a = new PyramidTetragonal(_a,_b,_c,_d,_e); + } + + + +} +FactoryFigureSpatial::FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d,PointSpatial _e,PointSpatial _f) +{ + if ( f == pyramidpentagonal ) + { + this->a = new PyramidPentagonal(_a,_b,_c,_d,_e,_f); + } + else if ( f == prismtriangular ) + { + + this->a = new PrismTriangular(_a,_b,_c,_d,_e,_f); + } + + +} +FactoryFigureSpatial::FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d,PointSpatial _e,PointSpatial _f, PointSpatial _g,PointSpatial _h) +{ + + if ( f == prismtetragonal ) + { + this->a = new PrismTetragonal(_a,_b,_c,_d,_e,_f,_g,_h); + } + +} + +FactoryFigureSpatial::FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d,PointSpatial _e,PointSpatial _f, PointSpatial _g,PointSpatial _h,PointSpatial _i,PointSpatial _j) +{ + + if ( f == prismpentagonal ) + { + this->a = new PrismPentagonal(_a,_b,_c,_d,_e,_f,_g,_h,_i,_j); + } + +} diff --git a/main/factoryfiguresspatial.h b/main/factoryfiguresspatial.h new file mode 100644 index 0000000..0c46462 --- /dev/null +++ b/main/factoryfiguresspatial.h @@ -0,0 +1,36 @@ +#ifndef factoryfigurespatial_h +#define factoryfigurespatial_h +#include "figurespatial.h" +#include "pointspatial.h" +#include "prismpentagonal.h" +#include "prismtetragonal.h" +#include "prismtriangular.h" +#include "pyramidtetragonal.h" +#include "pyramidpentagonal.h" +#include "pyramidtriangular.h" + + +enum figureSpatialType +{ + pyramidtriangular, + prismpentagonal, + prismtetragonal, + prismtriangular, + pyramidpentagonal, + pyramidtetragonal +}; + + +class FactoryFigureSpatial +{ +public: + FigureSpatial *a; + FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d); + FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d,PointSpatial _e); + FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d,PointSpatial _e,PointSpatial _f); + FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d,PointSpatial _e,PointSpatial _f, PointSpatial _g,PointSpatial _h); + FactoryFigureSpatial(figureSpatialType f,PointSpatial _a,PointSpatial _b,PointSpatial _c,PointSpatial _d,PointSpatial _e,PointSpatial _f, PointSpatial _g,PointSpatial _h,PointSpatial _i,PointSpatial _j); + +}; + +#endif \ No newline at end of file diff --git a/main/figurespatial.h b/main/figurespatial.h index 45ab082..02007e7 100644 --- a/main/figurespatial.h +++ b/main/figurespatial.h @@ -3,6 +3,8 @@ class FigureSpatial { +public: + virtual double getCircumference(); virtual double getVolume(); }; diff --git a/main/main.cpp b/main/main.cpp index 8627d4c..91f4654 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,5 +1,7 @@ #include -#include "library.h" +#include "factoryfigure.h" +#include "factoryfiguresspatial.h" + @@ -13,12 +15,13 @@ using namespace std; int main() { - Point _a(5,0); - Point _b(10,0); - Point _c(0,5); - Figure *test=new Triangle(_a,_b,_c); + PointSpatial _a(0,0,0); + PointSpatial _b(5,0,0); + PointSpatial _c(2.5,5,0); + PointSpatial _d(2.5,2.5,5); + FactoryFigureSpatial test(pyramidtriangular,_a,_b,_c,_d); - cout<getCircumference()<getCircumference()< + + @@ -84,6 +86,8 @@ + + diff --git a/main/main.vcxproj.filters b/main/main.vcxproj.filters index 5926174..f927bcc 100644 --- a/main/main.vcxproj.filters +++ b/main/main.vcxproj.filters @@ -108,6 +108,12 @@ Source Files\figure spatial\figures spatial + + Source Files\figure + + + Source Files\figure spatial + @@ -167,5 +173,11 @@ Header Files\figurespatial\figures + + Header Files\figurespatial + + + Header Files\figure + \ No newline at end of file From ec1ab15a228735cb37b46fee62525965dc4962d4 Mon Sep 17 00:00:00 2001 From: sakuya213 Date: Sat, 24 Jan 2015 01:40:25 +0100 Subject: [PATCH 3/4] added new task --- main/main.cpp | 142 ++++++++++++++++++++++++++++++++++++++++++++-- main/triangle.cpp | 15 +++-- 2 files changed, 147 insertions(+), 10 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 91f4654..d6a6dba 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,4 +1,8 @@ #include +#include +#include +#include +#include #include "factoryfigure.h" #include "factoryfiguresspatial.h" @@ -9,20 +13,150 @@ using namespace std; +char** str_split(char* a_str, const char a_delim) +{ + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + /* Count how many elements will be extracted. */ + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + /* Add space for trailing token. */ + count += last_comma < (a_str + strlen(a_str) - 1); + + /* Add space for terminating null string so caller + knows where the list of returned strings ends. */ + count++; + result = (char**)malloc(sizeof(char*) * count); + + if (result) + { + size_t idx = 0; + char* token = strtok(a_str, delim); + + while (token) + { + assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok(0, delim); + } + assert(idx == count - 1); + *(result + idx) = 0; + } + + return result; +} -int main() + +int main( int argc, char *argv[] ) { + if ( argc >= 2 ) + { + if ( !strcmp(argv[1],"triangle") ) + { + if ( !strcmp(argv[2],"area") ) + { + char **part; + Point points[3]; + + part=str_split(argv[3], ';'); + points[0] = Point(atof(part[0]),atof(part[1])); + part=str_split(argv[4], ';'); + points[1] = Point(atof(part[0]),atof(part[1])); + part=str_split(argv[5], ';'); + points[2] = Point(atof(part[0]),atof(part[1])); + cout<getArea()<getCircumference()< Date: Tue, 27 Jan 2015 13:30:24 +0100 Subject: [PATCH 4/4] example ended added 2 more figures, arguments validation --- main/main.cpp | 173 ++++++++++++++++---------------------- main/main.vcxproj | 2 + main/main.vcxproj.filters | 6 ++ main/tools.cpp | 53 ++++++++++++ main/tools.h | 5 ++ 5 files changed, 140 insertions(+), 99 deletions(-) create mode 100644 main/tools.cpp create mode 100644 main/tools.h diff --git a/main/main.cpp b/main/main.cpp index d6a6dba..6fce125 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -5,6 +5,7 @@ #include #include "factoryfigure.h" #include "factoryfiguresspatial.h" +#include "tools.h" @@ -13,61 +14,13 @@ using namespace std; -char** str_split(char* a_str, const char a_delim) -{ - char** result = 0; - size_t count = 0; - char* tmp = a_str; - char* last_comma = 0; - char delim[2]; - delim[0] = a_delim; - delim[1] = 0; - - /* Count how many elements will be extracted. */ - while (*tmp) - { - if (a_delim == *tmp) - { - count++; - last_comma = tmp; - } - tmp++; - } - - /* Add space for trailing token. */ - count += last_comma < (a_str + strlen(a_str) - 1); - - /* Add space for terminating null string so caller - knows where the list of returned strings ends. */ - count++; - - result = (char**)malloc(sizeof(char*) * count); - - if (result) - { - size_t idx = 0; - char* token = strtok(a_str, delim); - - while (token) - { - assert(idx < count); - *(result + idx++) = strdup(token); - token = strtok(0, delim); - } - assert(idx == count - 1); - *(result + idx) = 0; - } - - return result; -} - int main( int argc, char *argv[] ) { if ( argc >= 2 ) { - if ( !strcmp(argv[1],"triangle") ) + if ( !strcmp(argv[1],"triangle") && ( argc == 6) ) { if ( !strcmp(argv[2],"area") ) { @@ -80,7 +33,6 @@ int main( int argc, char *argv[] ) points[1] = Point(atof(part[0]),atof(part[1])); part=str_split(argv[5], ';'); points[2] = Point(atof(part[0]),atof(part[1])); - cout<getArea()<getCircumference()<getArea()<getCircumference()<getArea()<getCircumference()<getCircumference()< + @@ -102,6 +103,7 @@ + diff --git a/main/main.vcxproj.filters b/main/main.vcxproj.filters index f927bcc..f3bfd60 100644 --- a/main/main.vcxproj.filters +++ b/main/main.vcxproj.filters @@ -114,6 +114,9 @@ Source Files\figure spatial + + Source Files + @@ -179,5 +182,8 @@ Header Files\figure + + Header Files + \ No newline at end of file diff --git a/main/tools.cpp b/main/tools.cpp new file mode 100644 index 0000000..40e707a --- /dev/null +++ b/main/tools.cpp @@ -0,0 +1,53 @@ +#include "tools.h" +#include +#include +#include + +char** str_split(char* a_str, const char a_delim) +{ + char** result = 0; + size_t count = 0; + char* tmp = a_str; + char* last_comma = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + /* Count how many elements will be extracted. */ + while (*tmp) + { + if (a_delim == *tmp) + { + count++; + last_comma = tmp; + } + tmp++; + } + + /* Add space for trailing token. */ + count += last_comma < (a_str + strlen(a_str) - 1); + + /* Add space for terminating null string so caller + knows where the list of returned strings ends. */ + count++; + + result = (char**)malloc(sizeof(char*) * count); + + if (result) + { + size_t idx = 0; + char* token = strtok(a_str, delim); + + while (token) + { + assert(idx < count); + *(result + idx++) = strdup(token); + token = strtok(0, delim); + } + assert(idx == count - 1); + *(result + idx) = 0; + } + + return result; +} + diff --git a/main/tools.h b/main/tools.h new file mode 100644 index 0000000..ea17db0 --- /dev/null +++ b/main/tools.h @@ -0,0 +1,5 @@ +#ifndef tools_h +#define tools_h + +char** str_split(char* a_str, const char a_delim); +#endif \ No newline at end of file