naquo

Little Quilt (doesn-t work)

Jul 13th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. //#pragma GCC optimize("Ofast")
  2. //#pragma GCC optimize("unroll-loops,no-stack-protector")
  3. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  4. #include <bits/stdc++.h>
  5.  
  6. #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  7. #define endl '\n'
  8. #define elif else if
  9. #define lli long long int
  10. #define ld long double
  11. #define vec vector
  12. #define forn(a) for(lli a=0; a<n; a++)
  13. #define all(x) x.begin(), x.end()
  14. #define presicion(x) cout<<fixed<<setprecision(x)
  15. #define dbg(x) cerr<<#x<<" = "<<x<<endl
  16. #define PI 3.14159265358979323
  17. #define sen(ang) sin((ang)*PI/180)
  18. //cout<<flush;
  19.  
  20. using namespace std;
  21.  
  22. class Drawing {
  23. protected:
  24.     int rot;
  25. public:
  26.     virtual int lines() = 0;
  27.     virtual void draw(int line) = 0;
  28.     virtual Drawing* turn() {
  29.         rot = (rot + 1) % 4;
  30.         return this;
  31.     }
  32.     void draw_all(void) {
  33.         for (int i = 0; i < lines(); i++) {
  34.             draw(i);
  35.             cout << endl;
  36.         }
  37.     }
  38.  
  39. };
  40.  
  41. class A : public Drawing {
  42. public:
  43.     int lines() override { return 2; }
  44.  
  45.     void draw(int line) override {
  46.         constexpr static char options[4][2][3] = {
  47.             {"//", "/+"},
  48.             {"\\\\", "+\\"},
  49.             {"+/", "//"},
  50.             {"\\+", "\\\\"}
  51.         };
  52.         cout << options[rot][line % 4];
  53.     }
  54. };
  55.  
  56. class B : public Drawing {
  57. public:
  58.     int lines() override { return 2; }
  59.  
  60.     void draw(int line) override {
  61.         constexpr static char options[4][2][3] = {
  62.             {"--", "--"},
  63.             {"||", "||"},
  64.             {"--", "--"},
  65.             {"||", "||"}
  66.         };
  67.         cout << options[rot][line % 4];
  68.     }
  69. };
  70.  
  71.  
  72. class Sew : public Drawing {
  73.     Drawing* a;
  74.     Drawing* b;
  75. public:
  76.     Sew(Drawing* _a, Drawing* _b) : a(_a), b(_b) {}
  77.  
  78.     bool is_error() {
  79.         return a->lines() != b->lines()
  80.             || a->lines() == 1
  81.             || b->lines() == 1;
  82.     }
  83.  
  84.     int lines() override {
  85.         if (is_error())
  86.             return 1;
  87.         return rot % 2 ? a->lines() + b->lines() : a->lines();
  88.     }
  89.  
  90.     void draw(int line) override {
  91.         if (is_error()) {
  92.             cout << "error";
  93.             return;
  94.         }
  95.  
  96.         switch (rot) {
  97.         case 0:
  98.             a->draw(line);
  99.             b->draw(line);
  100.             break;
  101.         case 1:
  102.             if (line < a->lines())
  103.                 a->draw(line);
  104.             else
  105.                 b->draw(line - a->lines());
  106.             break;
  107.         case 2:
  108.             b->draw(line);
  109.             a->draw(line);
  110.             break;
  111.         case 3:
  112.             if (line < b->lines())
  113.                 b->draw(line);
  114.             else
  115.                 a->draw(line - b->lines());
  116.             break;
  117.         }
  118.     }
  119.     Drawing* turn() override {
  120.         rot = (rot + 1) % 4;
  121.         a->turn();
  122.         b->turn();
  123.         return this;
  124.     }
  125. };
  126.  
  127. class ErrorQuilt : public Drawing {
  128. public:
  129.     int lines() override {
  130.         return 1;
  131.     }
  132.  
  133.     void draw(int line) override {
  134.         cout << "error";
  135.     }
  136. } error;
  137.  
  138. Drawing* parse(string::iterator& por_ver) {
  139.     Drawing* a;
  140.     Drawing* b;
  141.  
  142.     while (isspace(*por_ver))
  143.         por_ver++;
  144.     switch (*por_ver) {
  145.     case 's':
  146.         while (*por_ver != '(')
  147.             por_ver++;
  148.         por_ver++;
  149.         a = parse(por_ver);
  150.         while (*por_ver != ',')
  151.             por_ver++;
  152.         por_ver++;
  153.         b = parse(por_ver);
  154.         while (*por_ver != ')')
  155.             por_ver++;
  156.         por_ver++;
  157.         return new Sew(a, b);
  158.     case 't':
  159.         while (*por_ver != '(')
  160.             por_ver++;
  161.         por_ver++;
  162.         a = parse(por_ver)->turn();
  163.         while (*por_ver != ')')
  164.             por_ver++;
  165.         por_ver++;
  166.         return a;
  167.     case 'A':
  168.         por_ver++;
  169.         return new A();
  170.     case 'B':
  171.         por_ver++;
  172.         return new B();
  173.     default:
  174.         return new ErrorQuilt();
  175.     }
  176. }
  177.  
  178. int main() {
  179.     IOS;
  180.     string expr;
  181.     getline(cin, expr, ';');
  182.     int i = 1;
  183.     while (!cin.eof()) {
  184.         auto it = expr.begin();
  185.         cout << "Quilt " << i++ << ":" << endl;
  186.         Drawing* quilt = parse(it);
  187.         while (isspace(*it))
  188.             it++;
  189.         if (*it != '\0')
  190.             cout << "error" << endl;
  191.         else
  192.             quilt->draw_all();
  193.         getline(cin, expr, ';');
  194.     }
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment