Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.16 KB | None | 0 0
  1. #include "parser.h"
  2. #include <locale>
  3.  
  4.  
  5. parser::parser(int rWidth) :
  6.   width_(rWidth), head_(new buffer()), elems_(0), sumSize_(0), first_(true), writed_(false)
  7. {
  8.   if (rWidth < 25)
  9.   {
  10.     std::cerr << "incorrect width\n";
  11.     exit(EXIT_FAILURE);
  12.   }
  13.   pList temp2 = head_;
  14.   for (int i = 0; i < 3; i++)
  15.   {
  16.     pList temp(new buffer());  
  17.     temp2->next = temp;
  18.     temp2 = temp;
  19.   }
  20.   temp2->next = head_;
  21. }
  22.  
  23. parser::~parser()
  24. {
  25.   pList temp = head_;
  26.   for (int i = 0; i < 3; i++)
  27.   {
  28.     temp = head_->next;
  29.     delete head_;
  30.     head_ = temp;
  31.   }
  32.   delete temp;
  33. }
  34.  
  35. void parser::parse()
  36. {
  37.   pList iter = head_->next->next->next;
  38.   type_t t = UNDEFINED;
  39.   int i = 0;
  40.   char dot = '.';
  41.   bool numberWithDot = false;
  42.   bool wordWithMinus = false;
  43.   std::cin >> std::noskipws;
  44.   while (std::cin >> arr_[i])
  45.   {
  46.     if (t == WORD)
  47.     {
  48.       if (isalpha(arr_[i]))
  49.       {
  50.         i++;
  51.         wordWithMinus = false;
  52.         continue;
  53.       }
  54.       else if (isspace(arr_[i]))
  55.       {
  56.         save(iter, 0, i, t);
  57.         i = 0;
  58.         t = UNDEFINED;
  59.         wordWithMinus = false;
  60.         continue;
  61.       }
  62.       else if ((ispunct(arr_[i])) && (arr_[i] != '-'))
  63.       {
  64.         save(iter, 0, i, t);
  65.         if (arr_[i] == ',')
  66.         {
  67.           save(iter, i, i + 1, COMMA);
  68.         }
  69.         else
  70.         {
  71.           save(iter, i, i + 1, PUNCTUATION);
  72.         }
  73.         i = 0;
  74.         t = UNDEFINED;
  75.         wordWithMinus = false;
  76.         continue;
  77.       }
  78.       else if (arr_[i] == '-')
  79.       {
  80.         if (wordWithMinus == false)
  81.         {
  82.           wordWithMinus = true;
  83.         }
  84.         else
  85.         {
  86.           t = DASH;
  87.         }
  88.       }
  89.       else if (isdigit(arr_[i]))
  90.       {
  91.         std::cerr << "letter+digit\n";
  92.         exit(EXIT_FAILURE);
  93.       }
  94.     }
  95.  
  96.     else if (t == NUMBER)
  97.     {
  98.       if (isdigit(arr_[i]))
  99.       {
  100.         i++;
  101.         continue;
  102.       }
  103.       else if (isspace(arr_[i]))
  104.       {
  105.         if (arr_[i - 1] == dot)
  106.         {
  107.           save(iter, 0, i - 1, t);
  108.           save(iter, i - 1, i, PUNCTUATION);
  109.         }
  110.         else
  111.         {
  112.           save(iter, 0, i, t);
  113.         }
  114.         i = 0;
  115.         t = UNDEFINED;
  116.         numberWithDot = false;
  117.         continue;
  118.       }
  119.       else if ((arr_[i] == dot) && (numberWithDot == false))
  120.       {
  121.         numberWithDot = true;
  122.       }
  123.       else if ((ispunct(arr_[i])) && (arr_[i] != '-'))
  124.       {
  125.         if (arr_[i - 1] == dot)
  126.         {
  127.           std::cerr << "wrong combination of number with dot and punctuation\n";
  128.           exit(EXIT_FAILURE);
  129.         }
  130.         else
  131.         {
  132.           save(iter, 0, i, NUMBER);
  133.           if (arr_[i] == ',')
  134.           {
  135.             save(iter, i, i + 1, COMMA);
  136.           }
  137.           else
  138.           {
  139.             save(iter, i, i + 1, PUNCTUATION);
  140.           }
  141.           i = 0;
  142.           t = UNDEFINED;
  143.           numberWithDot = false;
  144.           continue;
  145.         }
  146.       }
  147.       else if (arr_[i] == '-')
  148.       {
  149.         if (i != 1)
  150.         {
  151.           save(iter, 0, i, NUMBER);
  152.         }
  153.         t = DASH;
  154.       }
  155.       else if (isalpha(arr_[i]))
  156.       {
  157.         std::cerr << "letter+digit\n";
  158.         exit(EXIT_FAILURE);
  159.       }
  160.     }
  161.  
  162.     else if (t == DASH)
  163.     {
  164.       if (arr_[i - 2] == '-')
  165.       {
  166.         if (arr_[i] == '-')
  167.         {
  168.           if ((iter->t == PUNCTUATION) || (iter->t == UNDEFINED))
  169.           {
  170.             std::cerr << "punctuation error\n";
  171.             exit(EXIT_FAILURE);
  172.           }
  173.           else
  174.           {
  175.             save(iter, i - 2, i + 1, DASH);
  176.             i = 0;
  177.             t = UNDEFINED;
  178.             wordWithMinus = false;
  179.             numberWithDot = false;
  180.             continue;
  181.           }
  182.         }
  183.  
  184.       }
  185.       else
  186.       {
  187.         std::cerr << "--\n";
  188.         exit(EXIT_FAILURE);
  189.       }
  190.  
  191.       if (arr_[i] != '-')
  192.       {
  193.         std::cerr << "wrong dash\n";
  194.         exit(EXIT_FAILURE);
  195.       }
  196.     }
  197.  
  198.     else if (t == UNDEFINED)
  199.     {
  200.       if (isspace(arr_[i]))
  201.       {
  202.         continue;
  203.       }
  204.       else if (isalpha(arr_[i]))
  205.       {
  206.         t = WORD;
  207.       }
  208.       else if (isdigit(arr_[i]) || (arr_[i] == '+') || (arr_[i] == '-'))
  209.       {
  210.         t = NUMBER;
  211.       }
  212.       else if ((ispunct(arr_[i])) && (arr_[i] != '-'))
  213.       {
  214.         if ((iter->t == UNDEFINED) || (iter->t == PUNCTUATION) || (iter->t == COMMA) || (iter->t == DASH))
  215.         {
  216.           std::cerr << "punctuation error\n";
  217.           exit(EXIT_FAILURE);
  218.         }
  219.         else if (arr_[i] == ',')
  220.         {
  221.           save(iter, 0, 1, COMMA);
  222.         }
  223.         else
  224.         {
  225.           save(iter, 0, 1, PUNCTUATION);
  226.         }
  227.         continue;
  228.       }
  229.     }
  230.  
  231.     i++;
  232.   }
  233.   if (i > 0)
  234.   {
  235.     save(iter, 0, i, t);
  236.   }
  237.   while (elems_ != 0)
  238.   {
  239.     print();
  240.   }
  241.   std::cout << std::endl;
  242. }
  243.  
  244. void parser::print()
  245. {
  246.   pList iter = head_;
  247.   std::string temp = iter->str; //current word + punctuations
  248.   elems_--;
  249.   if (iter->next->t == COMMA)
  250.   {
  251.     temp += ',';
  252.     iter = iter->next;
  253.     iter->t = UNDEFINED;
  254.     elems_--;
  255.     if (iter->next->t == DASH)
  256.     {
  257.       temp += " ---";
  258.       iter = iter->next;
  259.       iter->t = UNDEFINED;
  260.       elems_--;
  261.     }
  262.   }
  263.   else if (iter->next->t == DASH)
  264.   {
  265.     temp += " ---";
  266.     iter = iter->next;
  267.     iter->t = UNDEFINED;
  268.     elems_--;
  269.   }
  270.   else if (iter->next->t == PUNCTUATION)
  271.   {
  272.     temp += iter->next->str;
  273.     iter = iter->next;
  274.     iter->t = UNDEFINED;
  275.     elems_--;
  276.     }
  277.  
  278.   if (!first_)
  279.   {
  280.     sumSize_++;
  281.   }
  282.   sumSize_ += temp.size();
  283.   if (sumSize_ > width_)
  284.   {
  285.     std::cout << std::endl;
  286.     sumSize_ = temp.size();
  287.   }
  288.   else if (!first_)
  289.   {
  290.     std::cout << ' ';
  291.   }
  292.  
  293.   std::cout << temp;
  294.   first_ = false;
  295.   head_ = iter->next;
  296. }
  297.  
  298. void parser::save(pList & iter, int begin, int end, type_t t)
  299. {
  300.   if ((end - begin) > 20)
  301.   {
  302.     std::cerr << "too large\n";
  303.     exit(EXIT_FAILURE);
  304.   }
  305.   if ((iter->next == head_) && (writed_))
  306.   {
  307.     print();
  308.   }
  309.   iter = iter->next;
  310.   std::string temp(arr_ + begin, arr_ + end);
  311.   iter->t = t;
  312.   iter->str = temp;
  313.   writed_ = true;
  314.   elems_++;
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement