Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma GCC optimize("Ofast")
- //#pragma GCC optimize("unroll-loops,no-stack-protector")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #include <bits/stdc++.h>
- #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
- #define endl '\n'
- #define elif else if
- #define lli long long int
- #define ld long double
- #define vec vector
- #define forn(a) for(lli a=0; a<n; a++)
- #define all(x) x.begin(), x.end()
- #define presicion(x) cout<<fixed<<setprecision(x)
- #define dbg(x) cerr<<#x<<" = "<<x<<endl
- #define PI 3.14159265358979323
- #define sen(ang) sin((ang)*PI/180)
- //cout<<flush;
- using namespace std;
- class Drawing {
- protected:
- int rot;
- public:
- virtual int lines() = 0;
- virtual void draw(int line) = 0;
- virtual Drawing* turn() {
- rot = (rot + 1) % 4;
- return this;
- }
- void draw_all(void) {
- for (int i = 0; i < lines(); i++) {
- draw(i);
- cout << endl;
- }
- }
- };
- class A : public Drawing {
- public:
- int lines() override { return 2; }
- void draw(int line) override {
- constexpr static char options[4][2][3] = {
- {"//", "/+"},
- {"\\\\", "+\\"},
- {"+/", "//"},
- {"\\+", "\\\\"}
- };
- cout << options[rot][line % 4];
- }
- };
- class B : public Drawing {
- public:
- int lines() override { return 2; }
- void draw(int line) override {
- constexpr static char options[4][2][3] = {
- {"--", "--"},
- {"||", "||"},
- {"--", "--"},
- {"||", "||"}
- };
- cout << options[rot][line % 4];
- }
- };
- class Sew : public Drawing {
- Drawing* a;
- Drawing* b;
- public:
- Sew(Drawing* _a, Drawing* _b) : a(_a), b(_b) {}
- bool is_error() {
- return a->lines() != b->lines()
- || a->lines() == 1
- || b->lines() == 1;
- }
- int lines() override {
- if (is_error())
- return 1;
- return rot % 2 ? a->lines() + b->lines() : a->lines();
- }
- void draw(int line) override {
- if (is_error()) {
- cout << "error";
- return;
- }
- switch (rot) {
- case 0:
- a->draw(line);
- b->draw(line);
- break;
- case 1:
- if (line < a->lines())
- a->draw(line);
- else
- b->draw(line - a->lines());
- break;
- case 2:
- b->draw(line);
- a->draw(line);
- break;
- case 3:
- if (line < b->lines())
- b->draw(line);
- else
- a->draw(line - b->lines());
- break;
- }
- }
- Drawing* turn() override {
- rot = (rot + 1) % 4;
- a->turn();
- b->turn();
- return this;
- }
- };
- class ErrorQuilt : public Drawing {
- public:
- int lines() override {
- return 1;
- }
- void draw(int line) override {
- cout << "error";
- }
- } error;
- Drawing* parse(string::iterator& por_ver) {
- Drawing* a;
- Drawing* b;
- while (isspace(*por_ver))
- por_ver++;
- switch (*por_ver) {
- case 's':
- while (*por_ver != '(')
- por_ver++;
- por_ver++;
- a = parse(por_ver);
- while (*por_ver != ',')
- por_ver++;
- por_ver++;
- b = parse(por_ver);
- while (*por_ver != ')')
- por_ver++;
- por_ver++;
- return new Sew(a, b);
- case 't':
- while (*por_ver != '(')
- por_ver++;
- por_ver++;
- a = parse(por_ver)->turn();
- while (*por_ver != ')')
- por_ver++;
- por_ver++;
- return a;
- case 'A':
- por_ver++;
- return new A();
- case 'B':
- por_ver++;
- return new B();
- default:
- return new ErrorQuilt();
- }
- }
- int main() {
- IOS;
- string expr;
- getline(cin, expr, ';');
- int i = 1;
- while (!cin.eof()) {
- auto it = expr.begin();
- cout << "Quilt " << i++ << ":" << endl;
- Drawing* quilt = parse(it);
- while (isspace(*it))
- it++;
- if (*it != '\0')
- cout << "error" << endl;
- else
- quilt->draw_all();
- getline(cin, expr, ';');
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment