Advertisement
Guest User

Header

a guest
May 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.93 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cassert>
  6. #include <cmath>
  7. #include <array>
  8. using namespace std;
  9.  
  10. template <typename T>
  11. class List {
  12. public:
  13.     struct Node {
  14.         T value;
  15.         Node *next;
  16.         Node *previous;
  17.     };
  18.     Node *head;
  19.     int size = 0;
  20.     List() : head(NULL) {}
  21.     List(const List &b) {
  22.         Node *nodeb; Node *nodea;
  23.         nodeb = b.head;
  24.         nodea = new Node;
  25.         nodea->previous = NULL;
  26.         if (nodeb != NULL)
  27.         {
  28.             head = nodea;
  29.             nodea->value = nodeb->value;
  30.             size++;
  31.             nodeb = nodeb->next;
  32.         }
  33.         while (nodeb != NULL)
  34.         {
  35.             size++;
  36.             Node *node = new Node;
  37.             nodea->next = node;
  38.             nodea->next->previous = nodea;
  39.             nodea = nodea->next;
  40.             nodea->value = nodeb->value;
  41.             nodeb = nodeb->next;
  42.         }
  43.     }
  44.     ~List()
  45.     {
  46.         clear();
  47.     }
  48.     unsigned int getSize() const
  49.     {
  50.         return this->size;
  51.     }
  52.     bool deleteElement(int n);
  53.     bool incert(T &x, int n);
  54.     void print();
  55.     void print2();
  56.     void clear();
  57.     void push_front(T &x);
  58.     T& operator[] (const unsigned int n);
  59.     void operator=(const List &b);
  60. };
  61. template <typename T>
  62. void List<T>::operator=(const List &b)
  63. {
  64.     clear();
  65.     Node *nodeb; Node *nodea;
  66.     nodeb = b.head;
  67.     nodea = new Node;
  68.     nodea->previous = NULL;
  69.     if (nodeb != NULL)
  70.     {
  71.         head = nodea;
  72.         nodea->value = nodeb->value;
  73.         size++;
  74.         nodeb = nodeb->next;
  75.     }
  76.     while (nodeb != NULL)
  77.     {
  78.         size++;
  79.         Node *node = new Node;
  80.         nodea->next = node;
  81.         nodea->next->previous = nodea;
  82.         nodea = nodea->next;
  83.         nodea->value = nodeb->value;
  84.         nodeb = nodeb->next;
  85.     }
  86. }
  87. template <typename T>
  88. void List<T>::clear()
  89. {
  90.     Node *a;
  91.     size = 0;
  92.     while (head != NULL)
  93.     {
  94.         a = head->next;
  95.         delete[] head;
  96.         head = a;
  97.     }
  98. }
  99. template <typename T>
  100. T& List<T>::operator[] (unsigned int n)
  101. {
  102.     Node *p = head;
  103.     while (p != NULL && n != 0)
  104.     {
  105.         n--;
  106.         p = p->next;
  107.     }
  108.     assert(p != NULL);
  109.     return p->value;
  110. }
  111. template <typename T>
  112. bool List<T>::deleteElement(int n)
  113. {
  114.     Node **p = &head;
  115.     if (n < 0 || *p == NULL)
  116.         return true;
  117.     while (*p != NULL && n != 0)
  118.     {
  119.         p = &((*p)->next);
  120.         n--;
  121.         if (*p == NULL)
  122.             return false;
  123.     }
  124.     Node *b;
  125.     b = (*p)->next;
  126.     delete *p;
  127.     *p = b;
  128.     size--;
  129.     return true;
  130. }
  131. template <typename T>
  132. bool List<T>::incert(T &x, int n)
  133. {
  134.     if (n < 0)
  135.         return false;
  136.     Node **p = &head;
  137.     while (*p != NULL && n != 0)
  138.     {
  139.         p = &((*p)->next);
  140.         n--;
  141.         if (*p == NULL)
  142.             return false;
  143.     }
  144.     Node *node = new Node;
  145.     node->value = x;
  146.     node->next = *p;
  147.     node->previous = (*p)->previous;
  148.     (*p)->previous = node;
  149.     *p = node;
  150.     size++;
  151.     return true;
  152. }
  153. template <typename T>
  154. void List<T>::print()
  155. {
  156.     Node *p = head;
  157.     while (p != NULL)
  158.     {
  159.         cout << p->value << endl; // -> == (*p).
  160.         p = p->next;
  161.     }
  162. }
  163. template <typename T>
  164. void List<T>::print2()
  165. {
  166.     Node *p = head;
  167.     while (p->next != NULL)
  168.         p = p->next;
  169.     while (p->previous != NULL)
  170.     {
  171.         cout << p->value << endl;
  172.         p = p->previous;
  173.     }
  174.     cout << p->value << endl;
  175. }
  176. template <typename T>
  177. void List<T>::push_front(T &x)
  178. {
  179.     Node *p = new Node;
  180.     p->value = x;
  181.     p->next = head;
  182.     p->previous = NULL;
  183.     if (head != NULL)
  184.         head->previous = p;
  185.     head = p;
  186.     size++;
  187. }
  188.  
  189. struct base_points {
  190.     float lat;
  191.     float lon;
  192.     string id;
  193. };
  194. struct base_ways {
  195.     string id;
  196.     List<string> points;
  197.     };
  198. class map {
  199. public:
  200.     List<base_points> points;
  201.     List<base_ways> ways;
  202. };
  203.  
  204. class data_processing {
  205. public:
  206.     map currentMap;
  207.     List<string> a;
  208.     base_points temporaryPoint;
  209.     base_ways temporaryWay;
  210.     int stage = 0;
  211.     void on_element_start(string &name);
  212.     bool on_element_end(string &name);
  213.     void on_attribute(string &name, string &value);
  214.     void on_character_data(string &text);
  215. };
  216. void data_processing::on_element_start(string &name)
  217. {
  218.     //cout << "begining: " << name << endl;
  219.     a.push_front(name);
  220. }
  221. bool data_processing::on_element_end(string &name) {
  222.     if (a.head->value == name) {
  223.         //cout << "ending: " << name << endl;
  224.         if (name == "way")
  225.         {
  226.             currentMap.ways.push_front(temporaryWay);
  227.         }
  228.         a.deleteElement(0);
  229.         name = "";
  230.         return true;
  231.     }
  232.     else {
  233.         return false;
  234.     }
  235. }
  236. void data_processing::on_attribute(string &name, string &value) {
  237.     //cout << "attribute: " << name << ", value: " << value << endl;
  238.     if (a.head->value == "node")
  239.     {
  240.         if (name == "lat") {
  241.             stage++;
  242.             temporaryPoint.lat = stof(value);
  243.         }
  244.         else if (name == "lon") {
  245.             stage++;
  246.             temporaryPoint.lon = stof(value);
  247.         }
  248.         else if (name == "id") {
  249.             stage++;
  250.             temporaryPoint.id = value;
  251.         }
  252.         if (stage == 3) {
  253.             stage = 0;
  254.             currentMap.points.push_front(temporaryPoint);
  255.         }
  256.     }
  257.     if (a.head->value == "way") {
  258.         if (name == "id")
  259.             temporaryWay.id = value;
  260.     }
  261.     if (a.head->value == "nd") {
  262.         if (name == "ref")
  263.         {
  264.             temporaryWay.points.push_front(value);
  265.         }
  266.     }
  267.  
  268.     name = ""; value = "";
  269. }
  270. void data_processing::on_character_data(string &text) {
  271.     //cout << "text: " << text << endl;
  272.     text = "";
  273. }
  274.  
  275. class word_processing {
  276. public:
  277.     string elementName = "", attributeName = "", attributeValue = "", text = "";
  278.     char c = 0;
  279.     data_processing* data;
  280.     string input;
  281.     int line = 1, column = 0;
  282.     word_processing(string in, data_processing* input_data) : input(in), data(input_data) {}
  283.     void start();
  284.     void error();
  285.     void check_char(char c);
  286. };
  287. void word_processing::check_char(char c)
  288. {
  289.     if (c == '\n' || c == '\r')
  290.     {
  291.         line++;
  292.         column = 0;
  293.     }
  294.     else if (c == '\t')
  295.         column += 4;
  296.     else
  297.         column++;
  298. }
  299. void word_processing::start() {
  300.     ifstream in(input);
  301.     if (!in.is_open())
  302.     {
  303.         cout << "FILE HAD NOT BEEN  READ!\n";
  304.         return;
  305.     }
  306.     int state(-1);
  307.     bool closedTag = false;
  308.     while (c != EOF)
  309.     {
  310.         switch (state) {
  311.         case -1:
  312.             c = in.get();
  313.             check_char(c);
  314.             if (c == '<')
  315.                 state = 0;
  316.             break;
  317.         case 0://ищем имя элемета
  318.             c = in.get();
  319.             if (c == '?')
  320.             {
  321.                 state = -1;
  322.                 break;
  323.             }
  324.             check_char(c);
  325.             if (c == ' ' || c == '\n')
  326.             {
  327.                 state = 1;
  328.                 data->on_element_start(elementName);
  329.             }
  330.             else if (c == '/')
  331.             {
  332.                 if (elementName != "")
  333.                 {
  334.                     data->on_element_start(elementName);
  335.                     if (!data->on_element_end(elementName))
  336.                     {
  337.                         //cout << "error on line " << line << " column " << column - data->name.length() << "! founded </" << name << ">, expected </" << a.tail->value << ">" << endl;
  338.                         error();
  339.                         return;
  340.                     }
  341.                 }
  342.                 else
  343.                     closedTag = true;
  344.             }
  345.             else if (c == '>')
  346.             {
  347.                 if (elementName != "" && !closedTag) {
  348.                     data->on_element_start(elementName);
  349.                     elementName = "";
  350.                 }
  351.                 else if (closedTag)
  352.                 {
  353.                     closedTag = false;
  354.                     if (!data->on_element_end(elementName)) {
  355.                         error();
  356.                         return;
  357.                     }
  358.                 }
  359.                 state = 2;
  360.             }
  361.             else if (elementName == "" && !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == ':' || c == '_' || c == '?'))
  362.             {
  363.                 cout << "syntax error!" << endl;
  364.                 return;
  365.             }
  366.             else if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c <= '9' && c >= '0') || c == '?' || c == ':' || c == '_' || c == '-' || c == '.'))
  367.             {
  368.                 cout << "syntax error!" << endl;
  369.                 return;
  370.             }
  371.             else
  372.                 elementName += c;
  373.             break;
  374.         case 1://имена атрибутов
  375.             c = in.get();
  376.             check_char(c);
  377.             if (c == '/')
  378.             {
  379.                 state = 0;
  380.                 if (!data->on_element_end(elementName))
  381.                 {
  382.                     error();
  383.                     return;
  384.                 }
  385.             }
  386.             else if (c == '>')
  387.             {
  388.                 elementName = "";
  389.                 state = 2;
  390.             }
  391.             else if (c == '"')
  392.                 state = 3;
  393.             else if (c == '=' || c == ' ' || c == '\n' || c == '\r')
  394.                 attributeName = attributeName;
  395.             else if (attributeName == "" && !((c >= 'a' && c <= 'z') || c == '?' || (c >= 'A' && c <= 'Z') || c == ':' || c == '_'))
  396.             {
  397.                 cout << "syntax error!" << endl;
  398.                 return;
  399.             }
  400.             else if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c <= '9' && c >= '0') || c == '?' || c == ':' || c == '_' || c == '-' || c == '.'))
  401.             {
  402.                 cout << "syntax error!" << endl;
  403.                 return;
  404.             }
  405.             else
  406.                 attributeName += c;
  407.             break;
  408.         case 2://текст
  409.             c = in.get();
  410.             check_char(c);
  411.             if (c == '<')
  412.             {
  413.                 state = 0;
  414.                 data->on_character_data(text);
  415.             }
  416.             else
  417.                 text += c;
  418.             break;
  419.         case 3://значение атрибутов
  420.             c = in.get();
  421.             check_char(c);
  422.             if (c == '"')
  423.             {
  424.                 state = 1;
  425.                 data->on_attribute(attributeName, attributeValue);
  426.             }
  427.             else
  428.                 attributeValue += c;
  429.             break;
  430.         }
  431.     }
  432.     in.close();
  433. }
  434. void word_processing::error()
  435. {
  436.     cout << "error on line " << line << " column " << column << "!" << endl;
  437. }
  438. double cosDegrees(double x)
  439. {
  440.     return cos(x * 3.141592653 / 180);
  441. }
  442.  
  443. double sinDegrees(double x)
  444. {
  445.     return sin(x *3.141592653 / 180);
  446. }
  447.  
  448. double distanceDegrees(const pair<double, double> &latLon1, const pair<double, double> &latLon2)
  449. {
  450.     const unsigned int earthRadius = 6371000;
  451.     return earthRadius * std::acos(sinDegrees(latLon1.first) * sinDegrees(latLon2.first) + cosDegrees(latLon1.first) * cosDegrees(latLon2.first) * cosDegrees(latLon2.second - latLon1.second));
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement