1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>
#include <cassert>
#include <string>
#include <cmath>
#include <iomanip>
static const unsigned short SHAPE = 106;
static const std::string SHAPES[SHAPE] = { "balbis", "concave polygon", "constructible polygon", "convex polygon", "cyclic polygon", "equiangular polygon", "equilateral polygon", "Penrose tile", "polyform", "regular polygon", "simple polygon", "tangential polygon", "henagon", "digon", "triangle", "acute triangle", "equilateral triangle", "isosceles triangle", "obtuse triangle", "rational triangle", "right triangle", "30-60-90 triangle", "isosceles right triangle", "Kepler triangle", "scalene triangle", "quadrilateral", "cyclic quadrilateral", "square", "diamond", "kite", "parallelogram", "rhombus", "Lozenge", "rhomboid", "rhomb", "rectangle", "square", "rectagon", "quadrangle", "quadragon", "tangential quadrilateral", "trapezus", "trapezoid", "isosceles trapezoid", "pentagon", "regular pentagon", "pentagonoid", "hexagon", "Lemoine hexagon", "heptagon", "octagon", "regular octagon", "nonagon", "decagon", "regular decagon", "hendecagon", "dodecagon", "hexadecagon", "icosagon", "swastika", "star without crossing lines", "star polygon", "hexagram", "star of David", "heptagram", "octagram", "star of Lakshmi", "decagram", "pentagram", "annulus", "arbelos", "circle", "Archimedes' twin circles", "Bankoff circle", "circumcircle", "disc", "excircle", "incircle", "nine-point circle", "circular sector", "circular segment", "crescent", "lens, vesica piscis", "lune", "Reuleaux polygon", "Reuleaux triangle", "salinon", "semicircle", "tomahawk", "triquetra", "Archimedean spiral", "astroid", "cardioid", "deltoid", "ellipse", "heart", "heartagon", "various lemniscates", "oval", "Cartesian oval", "Cassini oval", "Oval of Booth", "ovoid", "superellipse", "taijitu", "tomoe, magatama" };
static const double PI = 3.14;
// Abstract base class
class shape
{
public:
shape(const std::string& sIn = "balbis");
virtual~shape();
void set(const std::string&);
std::string name()const;
virtual double area()const = 0;
protected:
unsigned short index;
};
shape::shape(const std::string& sIn) : index(0)
{
set(sIn);
}
shape::~shape()
{
}
void shape::set(const std::string& sIn)
{
index = 0;
for (unsigned short i = 0; i < SHAPE; ++i)
{
if ((index < SHAPE) && (SHAPES[i] == sIn))
index = i;
}
assert(index < SHAPE);
}
std::string shape::name()const
{
return SHAPES[index];
}
double shape::area()const
{
return 0.0;
}
// sub class inherting shape
class circle : public shape
{
public:
circle(const double rIn = 1.0);
virtual~circle();
void set(const double rIn);
virtual double area()const;
protected:
double radius;
};
circle::circle(const double rIn) : shape("circle")
{
assert(rIn > 0.0);
set(rIn);
}
circle::~circle()
{
}
void circle::set(const double rIn)
{
radius = rIn;
}
double circle::area()const
{
return PI * pow(radius, 2);
}
// sub class inherting shape
class square : public shape
{
public:
square(const double sIn = 1.0);
virtual~square();
void set(const double);
virtual double area()const;
protected:
double side;
};
square::square(const double sIn) : shape("square")
{
assert(sIn > 0.0);
set(sIn);
}
square::~square()
{
}
void square::set(const double sIn)
{
side = sIn;
}
double square::area()const
{
return pow(side, 2);
}
// sub class inherting shape
class triangle : public shape
{
public:
triangle(const double s1 = 1.0, const double s2 = 1.0, const double s3 = 1.0);
triangle(const double*);
virtual~triangle();
void set(const double, const double, const double);
void set(const double*);
double getPerimeter()const;
virtual double area()const;
protected:
double* sideLength;
};
triangle::triangle(const double s1, const double s2, const double s3) : shape("triangle")
{
assert((s1 > 0.0) && (s2 > 0.0) && (s3 > 0.0));
sideLength = new double[3];
set(s1, s2, s3);
}
triangle::triangle(const double* sIn)
{
assert((sIn[0] > 0.0) && (sIn[1] > 0.0) && (sIn[2] > 0.0));
sideLength = new double[3];
set(sIn);
}
triangle::~triangle()
{
}
void triangle::set(const double s1, const double s2, const double s3)
{
sideLength[0] = s1;
sideLength[1] = s1;
sideLength[2] = s1;
}
void triangle::set(const double* sIn)
{
sideLength[0] = sIn[0];
sideLength[1] = sIn[1];
sideLength[2] = sIn[2];
}
double triangle::getPerimeter()const
{
return sideLength[0] + sideLength[1] + sideLength[2];
}
double triangle::area()const
{
double semiP = getPerimeter() * 0.5;
return sqrt(semiP*(semiP - sideLength[0])*(semiP - sideLength[1])*(semiP - sideLength[2]));
}
// Main
bool triValTest(const double, const double, const double);
bool lessThan(shape*, shape*);
void display(std::vector<shape*>);
int main(void)
{
std::vector<shape*> figure(10);
srand((unsigned)time(NULL));
double triArr[3];
for (unsigned short i = 0; i < figure.size(); ++i)
{
switch (rand() % 3)
{
case 0: figure[i] = new circle((rand() % 100 + 1) / 10.0);
break;
case 1: figure[i] = new square((rand() % 100 + 1) / 10.0);
break;
case 2:
do
{
triArr[0] = ((rand() % 100 + 1) / 10.0);
triArr[1] = ((rand() % 100 + 1) / 10.0);
triArr[2] = ((rand() % 100 + 1) / 10.0);
} while (!(triValTest(triArr[0], triArr[1], triArr[2])));
figure[i] = new triangle((rand() % 100 + 1) / 10.0);
break;
}
}
display(figure);
std::sort(figure.begin(), figure.end(), lessThan);
std::cout << std::endl;
display(figure);
return 0;
}
bool triValTest(const double s1, const double s2, const double s3)
{
return (s1 + s2 > s3) && (s1 + s3 > s2) && (s2 + s3 > s1);
}
void display(std::vector<shape*> figure)
{
std::cout << std::fixed;
std::cout << std::setprecision(6);
std::cout << "Name \t\t Area" << std::endl;
for (unsigned short c = 0; c < figure.size(); ++c)
{
std::cout << c + 1 << ":" << figure[c]->name() << "\t" << figure[c]->area() << std::endl;
}
}
bool lessThan(shape* lVal, shape* rVal)
{
return lVal->area() < rVal->area();
}
|