Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. //Daniel Hong
  2. //Assignment 5
  3. //Hoenigman
  4.  
  5.  
  6. #include "CommunicationNetwork.h"
  7. #include <iostream>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <string>
  11. using namespace std;
  12.  
  13.  
  14.  
  15. void CommunicationNetwork::addCity(string previous, string newCity)
  16. {
  17. City *temp = new City;
  18. temp -> cityName = newCity;
  19. head -> prev = temp;
  20. temp -> message = "";
  21. if (previous == "First")
  22. {
  23. temp -> next = head;
  24. temp -> prev = NULL;
  25. head = temp;
  26. }
  27. else
  28. {
  29. City *current = head;
  30. while (current != NULL)
  31. {
  32. if (current -> cityName == previous)
  33. {
  34. temp -> next = current -> next;
  35. current -> next = temp;
  36. break;
  37. }
  38. current = current -> next;
  39. }
  40. }
  41. }
  42.  
  43.  
  44.  
  45. void CommunicationNetwork::printNetwork()
  46. {
  47. City *current = head;
  48. cout << "===CURRENT PATH===" << endl;
  49. while (current != nullptr)
  50. {
  51. cout << current -> cityName << " -> ";
  52. current = current -> next;
  53. }
  54. cout << " NULL" << endl;
  55. cout << "==================" << endl;
  56. }
  57.  
  58.  
  59.  
  60. void CommunicationNetwork::transmitMsg(char *filename)
  61. {
  62. string word;
  63. ifstream file;
  64. file.open(filename);
  65. if (!file.is_open())
  66. {
  67. cout << "File not found." << endl;
  68. }
  69.  
  70. while (file >> word)
  71. {
  72. City *current = head;
  73. current -> message = word;
  74. City *sender = new City;
  75. while (current != NULL)
  76. {
  77. cout << current -> cityName << " received " << current -> message << endl;
  78. if (current -> next != NULL)
  79. {
  80. current -> next -> message = current -> message;
  81. if (current != head)
  82. {
  83. sender -> message = "";
  84. }
  85. sender = current;
  86. }
  87. current = current -> next;
  88. }
  89.  
  90. }
  91.  
  92. }
  93.  
  94.  
  95.  
  96. void CommunicationNetwork::buildNetwork()
  97. {
  98. string cities[10] =
  99. {
  100. "Los Angeles",
  101. "Phoenix",
  102. "Denver",
  103. "Dallas",
  104. "St. Louis",
  105. "Chicago",
  106. "Atlanta",
  107. "Washington, D.C.",
  108. "New York",
  109. "Boston",
  110. };
  111. City *city;
  112. City *prev = new City;
  113. for (int i = 0; i < 10; i++)
  114. {
  115. city = new City;
  116. city -> cityName = cities[i];
  117. city -> message = "";
  118. city -> next = NULL;
  119. if (i == 0)
  120. {
  121. head = city;
  122. prev = head;
  123. }
  124. else
  125. {
  126. prev -> next = city;
  127. prev = city;
  128. }
  129. }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. CommunicationNetwork::~CommunicationNetwork()
  138. {
  139. clearNetwork();
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement