Advertisement
Guest User

new

a guest
Feb 22nd, 2020
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <iomanip>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. // Abstract Base Class **************************
  13. class Shape {
  14. public:Shape(string = "transparent");
  15. string randColor(void);
  16. string getColor(void);
  17. void setColor(string argColor);
  18. virtual double calculateArea(void) = 0;
  19. virtual double calculatePerimeter(void) = 0;
  20.  
  21.  
  22. private:string color;
  23. };
  24.  
  25. class Polygon : public Shape {
  26. public:Polygon(string argColor= "", int argSides = 0, double argLength= 0.0);
  27. double calculateArea(void);
  28. double calculatePerimeter(void);
  29. double calculateInteriorAngle(void);
  30. string FindName(void);
  31. void printPoly(void);
  32. int getSides(void);
  33. void setSides(int argSides);
  34. double getLength(void);
  35. void setLength(double argLength);
  36.  
  37. private:int sides;
  38. double length;
  39. };
  40.  
  41. class Node {
  42.  
  43. public:
  44. Polygon* item;
  45. Node* nextPtr;
  46. Node* prevPtr;
  47. };
  48.  
  49. double PI = 3.1415926;
  50.  
  51. int main(void) {
  52. srand(time(0));
  53. string colors[6] = { "Red", "Orange", "Green","Blue", "Indigo","Violet" };
  54.  
  55.  
  56. Node* head;
  57. Node* curNode = new Node();
  58. head = curNode;
  59.  
  60. for (int i = 0; i < 3;i++) {
  61.  
  62. string randColor = colors[rand() % 5]; //random color
  63. int randSides = rand() % 8 + 2; //random sides 3-10
  64. int randLength = rand() % 5 + 1; //random Length 1-5
  65.  
  66. //pop. the first node
  67. curNode->item = new Polygon(randColor, randSides, randLength);
  68. curNode->nextPtr = new Node();
  69. curNode = curNode->nextPtr;
  70.  
  71. }
  72.  
  73. curNode = head;
  74.  
  75. while (curNode->nextPtr != nullptr) {
  76. curNode->item->printPoly();
  77. curNode = curNode->nextPtr;
  78. }
  79.  
  80. while (curNode->prevPtr != nullptr) {
  81. curNode = curNode->prevPtr;
  82. delete curNode->nextPtr->item;
  83. delete curNode->nextPtr;
  84. curNode->nextPtr = nullptr;
  85.  
  86. }
  87. curNode = head;
  88.  
  89. while (curNode->nextPtr != nullptr) {
  90. curNode->item->printPoly();
  91. curNode = curNode->nextPtr;
  92. }
  93.  
  94.  
  95.  
  96.  
  97. system("pause");
  98. return(0);
  99.  
  100. }
  101.  
  102. //Base Class *********************************
  103. Shape::Shape(string argColor) {setColor(argColor);}
  104. //maybe*************************************************************************?????????????
  105. string Shape::randColor(void) {
  106. string colors[6] = { "Red", "Orange", "Green","Blue", "Indigo","Violet" };
  107. string color = colors[rand() % 5];
  108. return color;
  109.  
  110. }
  111.  
  112. void Shape::setColor(string argColor) {
  113. color = argColor;
  114. }
  115.  
  116. string Shape::getColor(void) {return(color);}
  117.  
  118. //Poly Construct *********************************
  119. Polygon::Polygon(string argColor, int argSides, double argLength) : Shape(argColor) {
  120. setSides(argSides);
  121. setLength(argLength);
  122. }
  123.  
  124. double Polygon::getLength(void) {return(length);}
  125.  
  126. void Polygon::setLength(double argLength) {length = argLength;}
  127.  
  128. int Polygon::getSides(void) {return(sides);}
  129.  
  130. void Polygon::setSides(int argSides) {
  131. if (argSides < 3) { argSides = 3; }
  132. sides = argSides;
  133. }
  134.  
  135. double Polygon::calculateArea(void) {
  136. cout << fixed << setprecision(2);
  137. double n = getSides();
  138. double length = getLength();
  139. double area = length * length * n / (4 * tan(PI / n));
  140. return area;
  141. }
  142.  
  143. double Polygon::calculatePerimeter(void) {
  144. double perimeter = getLength() * getSides();
  145. return perimeter;
  146. }
  147.  
  148. double Polygon::calculateInteriorAngle(void) {
  149. double n = getSides();
  150. double angle = (180) * (1 - (2 / n));
  151. return angle;
  152. }
  153.  
  154. string Polygon::FindName(void) {
  155. int side = getSides();
  156. string name;
  157.  
  158. if (side == 3) {
  159. name = "triangle";
  160. }
  161. else if (side == 4) {
  162. name = "square";
  163. }
  164. else if (side == 5) {
  165. name = "pentagon";
  166. }
  167. else if (side == 6) {
  168. name = "hexagon";
  169. }
  170. else if (side == 7) {
  171. name = "heptagon";
  172. }
  173. else if (side == 8) {
  174. name = "octagon";
  175. }
  176. else if (side == 9) {
  177. name = "nonagon";
  178. }
  179. else { name = "decagon"; }
  180.  
  181. return name;
  182. }
  183.  
  184. void Polygon::printPoly(void) {
  185. cout << this->getColor() << " " << this->FindName() << " " << "has " << this->getSides() << " sides \nSide length: " << this->getLength()
  186. << "\tPerimeter: " << this->calculatePerimeter() << "\tArea: " << this->calculateArea() << "\tInterior Angles: " << this->calculateInteriorAngle() << "\370\n" << endl;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement