Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include<sstream>
  5. #include "Cities.h"
  6. #include "Neighbours.h"
  7. #include "DjikstraResult.h"
  8. #include "Visited.h"
  9. using namespace std;
  10.  
  11. bool contain(Cities* head, string name) {
  12.     while (head) {
  13.         if (head->name == name) {
  14.             return true;
  15.         }
  16.         head = head->next;
  17.     }
  18.     return false;
  19. }
  20. bool containNeighbours(Neighbours* head, string name) {
  21.     while (head) {
  22.         if (head->name == name) {
  23.             return true;
  24.         }
  25.         head = head->next;
  26.     }
  27.     return false;
  28. }
  29. void addVert(DjikstraResult*& head, string name) {
  30.     if (head == nullptr) {
  31.         head = new DjikstraResult{ name,"", INT_MAX, nullptr };
  32.         return;
  33.     }
  34.     else {
  35.         DjikstraResult* tmp = head;
  36.         while (tmp->next) {
  37.             tmp = tmp->next;
  38.         }
  39.         tmp->next = new DjikstraResult{ name,"",INT_MAX, nullptr };
  40.     }
  41. }
  42. void set0forcapital(DjikstraResult* head, string name) {
  43.     while (head) {
  44.         if (name == head->vertname) {
  45.             head->waga = 0;
  46.             break;
  47.         }
  48.         head = head->next;
  49.     }
  50. }
  51. void add(Cities*& head, string name) {
  52.     if (head == nullptr) {
  53.         head = new Cities{ name, nullptr, nullptr };
  54.     }
  55.     else {
  56.         Cities* tmp = head;
  57.         while (tmp->next) {
  58.             tmp = tmp->next;
  59.         }
  60.         tmp->next = new Cities{ name, nullptr, nullptr };
  61.     }
  62. }
  63. void addToNeighbours(Neighbours*& head, string name, int dis) {
  64.     if (head == nullptr) {
  65.         head = new Neighbours{ name, dis, nullptr };
  66.         return;
  67.     }
  68.     else {
  69.         Neighbours* tmp = head;
  70.         while (tmp->next) {
  71.             tmp = tmp->next;
  72.         }
  73.  
  74.         tmp->next = new Neighbours{ name, dis, nullptr };
  75.     }
  76. }
  77. Cities* getByName(Cities* head, const string& name) {
  78.     while (head) {
  79.         if (head->name == name) {
  80.             return head;
  81.         }
  82.         head = head->next;
  83.     }
  84. }
  85. void read(const string& n, Cities*& head) {
  86.     fstream file;
  87.     file.open(n, ios::in);
  88.     if (file.good()) {
  89.         while (!file.eof()) {
  90.             string city1, city2;
  91.             int distance;
  92.             string line;
  93.             getline(file, line);
  94.             stringstream linestream;
  95.             linestream << line;
  96.             if (linestream >> city1 >> city2 >> distance) {
  97.                 if (!contain(head, city1)) {
  98.                     add(head, city1);
  99.                     Cities* city1cell = getByName(head, city1);
  100.                     addToNeighbours(city1cell->distances, city2, distance);
  101.                 }
  102.                 else {
  103.                     Cities* city1cell = getByName(head, city1);
  104.                     if (!containNeighbours(city1cell->distances, city2)) {
  105.                         addToNeighbours(city1cell->distances, city2, distance);
  106.                     }
  107.                 }
  108.                 if (!contain(head, city2)) {
  109.                     add(head, city2);
  110.                     Cities* city2cell = getByName(head, city2);
  111.                     addToNeighbours(city2cell->distances, city1, distance);
  112.                 }
  113.                 else {
  114.                     Cities* city2cell = getByName(head, city2);
  115.                     if (!containNeighbours(city2cell->distances, city1)) {
  116.                         addToNeighbours(city2cell->distances, city1, distance);
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.     }
  122.     file.close();
  123. }
  124. Neighbours* getNeighbours(Cities* head, string name) {
  125.     while (head) {
  126.         if (head->name == name) {
  127.             return head->distances;
  128.         }
  129.         head = head->next;
  130.     }
  131. }
  132. bool checkInVisited(Visited* head, string name) {
  133.     while (head) {
  134.         if (head->name == name) {
  135.             return true;
  136.         }
  137.         head = head->next;
  138.     }
  139.     return false;
  140. }
  141. bool isVisited(Visited* head, string name) {
  142.     while (head) {
  143.         if (head->name == name) {
  144.             return true;
  145.         }
  146.         head = head->next;
  147.     }
  148.     return false;
  149. }
  150. void addAtEnd(Visited*& head, string name) {
  151.     if (!head) {
  152.         head = new Visited{ name, nullptr };
  153.     }
  154.     else {
  155.         Visited* nowy = head;
  156.         while (nowy->next) {
  157.             nowy = nowy->next;
  158.         }
  159.         nowy->next = new Visited{ name, nullptr };
  160.     }
  161. }
  162. DjikstraResult* fillTab(Cities* head, string city) {
  163.     Cities* temp = head;
  164.     DjikstraResult* pathvert = nullptr;
  165.     while (temp) {
  166.         addVert(pathvert, temp->name);
  167.         temp = temp->next;
  168.     }
  169.     set0forcapital(pathvert, city);
  170.     return pathvert;
  171. }
  172. DjikstraResult* findCityWithShortestPath(DjikstraResult* pathLengths, Visited* visitedCities) {
  173.     DjikstraResult* shortest = new DjikstraResult{ "","",INT_MAX,nullptr };
  174.     while (pathLengths) {
  175.         if (!isVisited(visitedCities, pathLengths->vertname) && pathLengths->waga < shortest->waga) {
  176.             shortest = pathLengths;
  177.         }
  178.         pathLengths = pathLengths->next;
  179.     }
  180.     return shortest;
  181. }
  182. int getWeightByName(DjikstraResult* head, string name) {
  183.     while (head) {
  184.         if (head->vertname == name) {
  185.             return head->waga;
  186.         }
  187.         head = head->next;
  188.     }
  189. }
  190. void setNewWeight(DjikstraResult* head, string name, string currentName, int weight) {
  191.     while (head) {
  192.         if (head->vertname == name) {
  193.             head->waga = weight;
  194.             head->previous = currentName;
  195.             break;
  196.         }
  197.         head = head->next;
  198.     }
  199.  
  200. }
  201. void deleteNeighbours(Neighbours* head) {
  202.     if (head) {
  203.         deleteNeighbours(head->next);
  204.         delete head;
  205.         head = nullptr;
  206.     }
  207. }
  208. void deleteMap(Cities* Map) {
  209.     if (Map) {
  210.         deleteMap(Map->next);
  211.         deleteNeighbours(Map->distances);
  212.         delete Map;
  213.         Map = nullptr;
  214.     }
  215. }
  216. void deleteResults(DjikstraResult* Tab) {
  217.     if (Tab) {
  218.         deleteResults(Tab->next);
  219.         delete Tab;
  220.         Tab = nullptr;
  221.     }
  222. }
  223. void djikstra(Cities*& Map, DjikstraResult*& Tab, string capital) {
  224.     Cities* cities = Map;
  225.     DjikstraResult* pathLengths = Tab;
  226.     int citiesLength = 0;
  227.     while (cities) {
  228.         addVert(pathLengths, cities->name);
  229.         cities = cities->next;
  230.         citiesLength++;
  231.     }
  232.     set0forcapital(pathLengths, capital);
  233.     Visited* visitedCities = nullptr;
  234.     cities = Map;
  235.     for (int i = 0; i < citiesLength; i++) {
  236.         DjikstraResult* currentCity = findCityWithShortestPath(pathLengths, visitedCities);
  237.         Neighbours* neighbours = getNeighbours(cities, currentCity->vertname);
  238.  
  239.         while (neighbours) {
  240.             int newWeight = currentCity->waga + neighbours->dist;
  241.             if (newWeight < getWeightByName(pathLengths, neighbours->name)) {
  242.                 setNewWeight(pathLengths, neighbours->name, currentCity->vertname, newWeight);
  243.             }
  244.             neighbours = neighbours->next;
  245.         }
  246.         addAtEnd(visitedCities, currentCity->vertname);
  247.     }
  248.     Tab = pathLengths;
  249.    
  250. }
  251. DjikstraResult* findByName(DjikstraResult* head, string n) {
  252.     DjikstraResult* tmp = head;
  253.  
  254.     while (tmp) {
  255.         if (tmp->vertname == n) {
  256.             return tmp;
  257.         }
  258.         else {
  259.             tmp = tmp->next;
  260.         }
  261.     }
  262.  
  263.     return nullptr;
  264. }
  265. string xd(DjikstraResult* Tab, string name) {
  266.     if (Tab) {
  267.         DjikstraResult* current = findByName(Tab, name);
  268.  
  269.         if (current->previous != "") {
  270.             return xd(Tab, current->previous) + "->" + current->vertname;
  271.         }
  272.         return current->vertname;
  273.     }
  274.     return "";
  275. }
  276. void saveToTXT(DjikstraResult* Tab, const string& n, string capital) {
  277.     fstream file;
  278.     file.open(n, fstream::out);
  279.     if (file.good()) {
  280.         DjikstraResult* tmp = Tab;
  281.         while (tmp) {
  282.             if (tmp->vertname != capital) {
  283.                 string x = xd(Tab, tmp->vertname);
  284.                 file << x << ": " << tmp->waga << endl;
  285.                 tmp = tmp->next;
  286.             }
  287.             else {
  288.                 tmp = tmp->next;
  289.             }
  290.  
  291.         }
  292.         file.close();
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement