Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string>
  3. #include<iostream>
  4. #include<sstream>
  5. #include<list>
  6. #include<cmath>
  7. #include<stdlib.h>
  8. //using namespace std;
  9.  
  10.  
  11. class Point{
  12. private:
  13. float x, y;
  14.  
  15. public:
  16.  
  17. Point(){}
  18.  
  19. Point(float x, float y){
  20. this->x = x;
  21. this->y = y;
  22. }
  23. void print(){
  24. cout<<"x = "<<x<<" si y = "<<y<<"\n";
  25. }
  26. float getX(){
  27. return x;
  28. }
  29. float getY(){
  30. return y;
  31. }
  32. void setX(int x){
  33. this->x = x;
  34. }
  35. void setY(int y){
  36. this->y = y;
  37. }
  38. };
  39.  
  40. class Figure{
  41. protected:
  42. list<Point*> points;
  43.  
  44. public:
  45. float getPerimeter(){
  46. double perimeter = 0;
  47. Point* start = this->points.front();
  48. list<Point*>::iterator it;
  49. for (it = this->points.begin(); it != prev(this->points.end()); it++){
  50. Point* p1 = *it;
  51. Point* p2 = *(++it);
  52. perimeter += sqrt(pow((p2->getX() - p1->getX()),2) + pow((p2->getY() - p1->getY()),2));
  53. it--;
  54. }
  55. perimeter += sqrt(pow(((*it)->getX() - start->getX()),2) + pow(((*it)->getY() - start->getY()),2));
  56.  
  57. return perimeter;
  58. }
  59.  
  60. void printPoints(){
  61. cout<<"Coordonatele sunt:\n";
  62. for (list<Point*>::iterator it = this->points.begin(); it != this->points.end(); it++)
  63. (*it)->print();
  64. }
  65.  
  66. template<class T>
  67. void printPanta(T& figure){
  68. list<Point*>::iterator it;
  69. it = figure.points.begin();
  70. Point* p1 = *it;
  71. Point* p2 = *(++it);
  72. double slope = (p2->getY() - p1->getY()) / (p2->getX() - p1->getX());
  73. if(isinf(slope)){
  74. cout<<"Dreapta e verticala\n";
  75. }
  76. else if(slope == 0){
  77. cout<<"Dreapta e orizontala\n";
  78. }
  79. else{
  80. cout<<"Panta dreptei formata de primele 2 este: "<<slope<<"\n";
  81. }
  82. }
  83.  
  84. template<class T, class C>
  85. bool hasPointsOnCircle(T& figure, C& circle){
  86. for (list<Point*>::iterator it = figure.points.begin(); it != figure.points.end(); it++){
  87. float x = (*it)->getX();
  88. float y = (*it)->getY();
  89. float ox = circle.getOrigine().getX();
  90. float oy = circle.getOrigine().getY();
  91. if( (pow(x - ox, 2) + pow(y - oy, 2)) == circle.getRadius())
  92. return true;
  93. }
  94. return false;
  95. }
  96. };
  97.  
  98.  
  99. class Triangle : public Figure{
  100. private:
  101.  
  102. public:
  103. Triangle(){
  104. this->points.push_back(new Point(0,1));
  105. this->points.push_back(new Point(-1,-1));
  106. this->points.push_back(new Point(1,-1));
  107. }
  108. void afiseaza(){
  109. cout<<"Ati creat un triunghi\n";
  110. }
  111. };
  112.  
  113.  
  114. class Trapezoid : public Figure{
  115. private:
  116.  
  117. public:
  118. Trapezoid(){
  119. this->points.push_back(new Point(1,1));
  120. this->points.push_back(new Point(0,-1));
  121. this->points.push_back(new Point(-2,-1));
  122. this->points.push_back(new Point(-1,1));
  123. }
  124. void afiseaza(){
  125. cout<<"Ati creat un trapez\n";
  126. }
  127. };
  128.  
  129.  
  130.  
  131. class Square : public Figure{
  132. private:
  133.  
  134. public:
  135. Square(){
  136. this->points.push_back(new Point(0,0));
  137. this->points.push_back(new Point(0,1));
  138. this->points.push_back(new Point(1,1));
  139. this->points.push_back(new Point(1,0));
  140. }
  141. void afiseaza(){
  142. cout<<"Ati creat un patrat\n";
  143. }
  144. };
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. class Circle{
  153. private:
  154. float radius;
  155. Point center;
  156.  
  157. Circle(float radius, Point center){
  158. this->radius = radius;
  159. this->center = center;
  160. }
  161.  
  162. public:
  163. static Circle* instance;
  164.  
  165. static Circle *getInstance(){
  166. if(!instance)
  167. instance = new Circle(1, Point(0,0));
  168. else throw 1;
  169. return instance;
  170. }
  171. void afiseaza(){
  172. cout<<"Cercul cu raza "<<radius<<" si originea in ("<<center.getX()<<","<<center.getY()<<")\n";
  173. }
  174. float getRadius(){
  175. return radius;
  176. }
  177. Point getOrigine(){
  178. return center;
  179. }
  180. };
  181.  
  182. Circle* Circle::instance = NULL;
  183.  
  184.  
  185.  
  186. int main(){
  187.  
  188. Square* p1;
  189. Triangle* t1;
  190. Trapezoid* z1;
  191. Circle* circle;
  192. Point* pct1;
  193. Figure f1;
  194.  
  195. cout<<"1. Adauga figura\n";
  196. cout<<"2. Adauga puncte\n";
  197. cout<<"3. Modifica puncte\n";
  198. cout<<"4. Afisare panta figurii\n";
  199. cout<<"5. Creeaza cerc\n";
  200. cout<<"6. E figura pe cerc ?\n";
  201. cout<<"0. Exit\n";
  202.  
  203. int key = -1;
  204. while(key != 0){
  205. cout<<"Alegeti o optiune\n";
  206. cin>>key;
  207. switch(key){
  208. case 1:
  209. int c1;
  210. cout<<"1. Square\n";
  211. cout<<"2. Triangle\n";
  212. cout<<"3. Trapezoid\n";
  213. cin>>c1;
  214. switch(c1){
  215. case 1:
  216. p1 = new Square();
  217. p1->afiseaza();
  218. break;
  219. case 2:
  220. t1 = new Triangle();
  221. t1->afiseaza();
  222. break;
  223. case 3:
  224. z1 = new Trapezoid();
  225. z1->afiseaza();
  226. break;
  227. }
  228. break;
  229. case 2:
  230. pct1 = new Point(2,2);
  231. pct1->print();
  232. break;
  233. case 3:
  234. int x;
  235. int y;
  236. cout<<"Dati noile valori pentru punct\nx= ";
  237. cin>>x;
  238. cout<<"y= ";
  239. cin>>y;
  240. pct1->setX(x);
  241. pct1->setY(y);
  242. break;
  243. case 4:
  244. int c4;
  245. cout<<"1. Square\n";
  246. cout<<"2. Triangle\n";
  247. cout<<"3. Trapezoid\n";
  248. cin>>c4;
  249. switch(c4){
  250. case 1:
  251. f1.printPanta(*p1);
  252. break;
  253. case 2:
  254. f1.printPanta(*t1);
  255. break;
  256. case 3:
  257. f1.printPanta(*z1);
  258. break;
  259. }
  260. break;
  261. case 5:
  262. try{
  263. circle = circle->getInstance();
  264. circle->afiseaza();
  265. }catch(int x){
  266. cout<<"Nu puteti instantia 2 cercuri!\n";
  267. }
  268. break;
  269. case 6:
  270. int c6;
  271. cout<<"1. Square\n";
  272. cout<<"2. Triangle\n";
  273. cout<<"3. Trapezoid\n";
  274. cin>>c6;
  275. switch(c6){
  276. case 1:
  277. if(f1.hasPointsOnCircle(*p1,*circle)){
  278. cout<<"E pe cerc\n";
  279. }
  280. else{
  281. cout<<"Nu e pe cerc\n";
  282. }
  283. break;
  284. case 2:
  285. if(f1.hasPointsOnCircle(*t1,*circle)){
  286. cout<<"E pe cerc\n";
  287. }
  288. else{
  289. cout<<"Nu e pe cerc\n";
  290. }
  291. break;
  292. case 3:
  293. if(f1.hasPointsOnCircle(*z1,*circle)){
  294. cout<<"E pe cerc\n";
  295. }
  296. else{
  297. cout<<"Nu e pe cerc\n";
  298. }
  299. break;
  300. }
  301. break;
  302. case 0:
  303. cout<<p1->getPerimeter();
  304. break;
  305. }
  306. }
  307.  
  308. return 0;
  309.  
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement