Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <memory>
  8. #include "shape.hpp"
  9. #include "allFigures.hpp"
  10.  
  11. void task_1()
  12. {
  13.   std::list<double> list((std::istream_iterator<double>(std::cin)), std::istream_iterator<double>());
  14.   if (!std::cin.eof() && std::cin.fail())
  15.   {
  16.     throw std::runtime_error("Error reading data");
  17.   }
  18.   std::transform(list.begin(), list.end(), std::ostream_iterator<double>(std::cout, " "),
  19.                  [](double &list)
  20.                  { return list * M_PI; });
  21.   std::cout << std::endl;
  22. }
  23.  
  24.  
  25. void task_2()
  26. {
  27.   Shape::shape_ptr shape_ptr;
  28.   std::list<Shape::shape_ptr> list;
  29.   std::string line;
  30.  
  31.   while (std::getline(std::cin, line, '\n'))
  32.   {
  33.     line.erase(remove(line.begin(), line.end(), ' '), line.end());
  34.     line.erase(remove(line.begin(), line.end(), '\t'), line.end());
  35.  
  36.     if (line.empty())
  37.     {
  38.       continue;
  39.     }
  40.  
  41.     const auto openBracket = line.find_first_of('(');
  42.     const auto semicolon = line.find_first_of(';');
  43.     const auto closeBracket = line.find_first_of(')');
  44.  
  45.     if (openBracket == std::string::npos || semicolon == std::string::npos || closeBracket == std::string::npos)
  46.     {
  47.       throw std::runtime_error("Invalid data!\n");
  48.     }
  49.  
  50.     const auto shape = line.substr(0, openBracket);
  51.  
  52.     Center center{};
  53.     center.x = std::stod(line.substr(openBracket + 1, semicolon - openBracket + 1));
  54.     center.y = std::stod(line.substr(semicolon + 1, closeBracket - semicolon + 1));
  55.  
  56.     if (shape == "CIRCLE")
  57.     {
  58.       shape_ptr = std::make_shared<Circle>(center);
  59.       list.push_back(shape_ptr);
  60.     } else if (shape == "SQUARE")
  61.     {
  62.       shape_ptr = std::make_shared<Square>(center);
  63.       list.push_back(shape_ptr);
  64.     } else if (shape == "TRIANGLE")
  65.     {
  66.       shape_ptr = std::make_shared<Triangle>(center);
  67.       list.push_back(shape_ptr);
  68.     } else
  69.     {
  70.       throw std::runtime_error("Invalid shape!\n");
  71.     }
  72.   }
  73.  
  74.   std::cout << "Original: " << std::endl;
  75.   std::for_each(list.begin(), list.end(), [](const Shape::shape_ptr &ptr)
  76.   { ptr->draw(); });
  77.  
  78.   std::cout << "Left-Right: " << std::endl;
  79.   list.sort([](const Shape::shape_ptr &leftFigure, const Shape::shape_ptr &rightFigure)
  80.             { return leftFigure->isMoreLeft((const Shape &) rightFigure); });
  81.   std::for_each(list.begin(), list.end(), [](const Shape::shape_ptr &ptr)
  82.   { ptr->draw(); });
  83.  
  84.   std::cout << "Right-Left: " << std::endl;
  85.   list.reverse();
  86.   std::for_each(list.begin(), list.end(), [](const Shape::shape_ptr &ptr)
  87.   { ptr->draw(); });
  88.  
  89.   std::cout << "Top-Bottom: " << std::endl;
  90.   list.sort([](const Shape::shape_ptr &topFigure, const Shape::shape_ptr &bottomFigure)
  91.             { return topFigure->isUpper(*bottomFigure); });
  92.   std::for_each(list.begin(), list.end(), [](const Shape::shape_ptr &ptr)
  93.   { ptr->draw(); });
  94.  
  95.   std::cout << "Bottom-Top: " << std::endl;
  96.   list.reverse();
  97.   std::for_each(list.begin(), list.end(), [](const Shape::shape_ptr &ptr)
  98.   { ptr->draw(); });
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement