Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- // Abstract Base Class **************************
- class Shape {
- public:Shape(string = "transparent");
- string randColor(void);
- string getColor(void);
- void setColor(string argColor);
- virtual double calculateArea(void) = 0;
- virtual double calculatePerimeter(void) = 0;
- private:string color;
- };
- class Polygon : public Shape {
- public:Polygon(string argColor= "", int argSides = 0, double argLength= 0.0);
- double calculateArea(void);
- double calculatePerimeter(void);
- double calculateInteriorAngle(void);
- string FindName(void);
- void printPoly(void);
- int getSides(void);
- void setSides(int argSides);
- double getLength(void);
- void setLength(double argLength);
- private:int sides;
- double length;
- };
- class Node {
- public:
- Polygon* item;
- Node* nextPtr;
- Node* prevPtr;
- };
- double PI = 3.1415926;
- int main(void) {
- srand(time(0));
- string colors[6] = { "Red", "Orange", "Green","Blue", "Indigo","Violet" };
- Node* head;
- Node* curNode = new Node();
- head = curNode;
- for (int i = 0; i < 3;i++) {
- string randColor = colors[rand() % 5]; //random color
- int randSides = rand() % 8 + 2; //random sides 3-10
- int randLength = rand() % 5 + 1; //random Length 1-5
- //pop. the first node
- curNode->item = new Polygon(randColor, randSides, randLength);
- curNode->nextPtr = new Node();
- curNode = curNode->nextPtr;
- }
- curNode = head;
- while (curNode->nextPtr != nullptr) {
- curNode->item->printPoly();
- curNode = curNode->nextPtr;
- }
- while (curNode->prevPtr != nullptr) {
- curNode = curNode->prevPtr;
- delete curNode->nextPtr->item;
- delete curNode->nextPtr;
- curNode->nextPtr = nullptr;
- }
- curNode = head;
- while (curNode->nextPtr != nullptr) {
- curNode->item->printPoly();
- curNode = curNode->nextPtr;
- }
- system("pause");
- return(0);
- }
- //Base Class *********************************
- Shape::Shape(string argColor) {setColor(argColor);}
- //maybe*************************************************************************?????????????
- string Shape::randColor(void) {
- string colors[6] = { "Red", "Orange", "Green","Blue", "Indigo","Violet" };
- string color = colors[rand() % 5];
- return color;
- }
- void Shape::setColor(string argColor) {
- color = argColor;
- }
- string Shape::getColor(void) {return(color);}
- //Poly Construct *********************************
- Polygon::Polygon(string argColor, int argSides, double argLength) : Shape(argColor) {
- setSides(argSides);
- setLength(argLength);
- }
- double Polygon::getLength(void) {return(length);}
- void Polygon::setLength(double argLength) {length = argLength;}
- int Polygon::getSides(void) {return(sides);}
- void Polygon::setSides(int argSides) {
- if (argSides < 3) { argSides = 3; }
- sides = argSides;
- }
- double Polygon::calculateArea(void) {
- cout << fixed << setprecision(2);
- double n = getSides();
- double length = getLength();
- double area = length * length * n / (4 * tan(PI / n));
- return area;
- }
- double Polygon::calculatePerimeter(void) {
- double perimeter = getLength() * getSides();
- return perimeter;
- }
- double Polygon::calculateInteriorAngle(void) {
- double n = getSides();
- double angle = (180) * (1 - (2 / n));
- return angle;
- }
- string Polygon::FindName(void) {
- int side = getSides();
- string name;
- if (side == 3) {
- name = "triangle";
- }
- else if (side == 4) {
- name = "square";
- }
- else if (side == 5) {
- name = "pentagon";
- }
- else if (side == 6) {
- name = "hexagon";
- }
- else if (side == 7) {
- name = "heptagon";
- }
- else if (side == 8) {
- name = "octagon";
- }
- else if (side == 9) {
- name = "nonagon";
- }
- else { name = "decagon"; }
- return name;
- }
- void Polygon::printPoly(void) {
- cout << this->getColor() << " " << this->FindName() << " " << "has " << this->getSides() << " sides \nSide length: " << this->getLength()
- << "\tPerimeter: " << this->calculatePerimeter() << "\tArea: " << this->calculateArea() << "\tInterior Angles: " << this->calculateInteriorAngle() << "\370\n" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement