Advertisement
p0lich

TG1

Sep 5th, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. //1b - 5
  9. //II -29
  10. //каркас примом
  11. //29 and 5
  12.  
  13. class Graph
  14. {
  15. public:
  16. vector<vector<int>> graph;
  17. Graph()
  18. {
  19. cout << "Empty Graph was created";
  20. }
  21.  
  22. Graph(int Vertex)
  23. {
  24. //vector<vector<int>> tbl;
  25. }
  26.  
  27. void LoadGraph(string grstr);
  28. void SpecGraph(string spstr);
  29. void DeleteGraph();
  30. void SaveGraph(string resultName);
  31. void AddVertex();
  32. void RemoveVertex();
  33. };
  34.  
  35. void Graph::LoadGraph(string fileName)
  36. {
  37. ifstream fin(fileName);
  38. vector<int> vertexInRow;
  39. string str;
  40. int k = 0;
  41. int s = 0;
  42.  
  43. while (fin.peek() != EOF)
  44. {
  45. getline(fin, str);
  46. size_t dotFind = str.find(":");
  47. string sstr = str.substr(0, dotFind);
  48. string vert = "";
  49. vertexInRow.push_back(atoi(sstr.c_str()));
  50.  
  51. for (int i = dotFind + 2; i < str.size(); i++)
  52. {
  53. if (str[i] != ' ')
  54. {
  55. vert += str[i];
  56. i++;
  57. }
  58. vertexInRow.push_back(atoi(vert.c_str()));
  59. vert = "";
  60. }
  61. graph.push_back(vertexInRow);
  62. vertexInRow.clear();
  63. }
  64. }
  65.  
  66. void Graph::SpecGraph(string spstr)
  67. {
  68. graph.clear();
  69. vector<int> rowsVec;
  70. int n;
  71. cout << "Введите количесвто вершин: ";
  72. cin >> n;
  73. for (int i = 1; i < n + 1; i++)
  74. {
  75. rowsVec.push_back(i);
  76. for (int j = 1; j < n + 1; j++)
  77. {
  78. if (i != j)
  79. rowsVec.push_back(j);
  80. }
  81. graph.push_back(rowsVec);
  82. rowsVec.clear();
  83. }
  84. }
  85.  
  86. void Graph::DeleteGraph()
  87. {
  88. graph.clear();
  89. }
  90.  
  91. void Graph::SaveGraph(string resultName)
  92. {
  93. ofstream fout(resultName);
  94. for (int i = 0; i < graph.size(); i++)
  95. {
  96. fout << graph[i][0] << ": " ;
  97. for (int j = 1; j < graph[i].size(); j++)
  98. fout << graph[i][j] << " ";
  99. fout << endl;
  100. }
  101. }
  102.  
  103. void Graph::AddVertex()
  104. {
  105. int n;
  106. bool mistake1 = false;
  107. vector<int> newVertex;
  108. cout << "Введите новую вершину: ";
  109. cin >> n;
  110. for (int i = 0; i < graph.size(); i++)
  111. {
  112. if (graph[i][0] == n)
  113. {
  114. cout << "Такая вершина уже есть в графе" << endl;
  115. mistake1 = true;
  116. }
  117. }
  118. if (!mistake1)
  119. {
  120. newVertex.push_back(n);
  121. int k;
  122. int vert;
  123. bool mistake2 = false;
  124. cout << "Введите количество смежных вершин: ";
  125. cin >> k;
  126. cout << "Введите " << k << " вершины" << endl;
  127. for (int i = 0; i < k; i++)
  128. {
  129. cin >> vert;
  130. newVertex.push_back(vert);
  131. /*if (!mistake2)
  132. {
  133. cin >> vert;
  134. newVertex.push_back(vert);
  135.  
  136. for (int j = 0; j < graph.size(); j++)
  137. if (graph[j][0] != vert)
  138. mistake2 = true;
  139.  
  140. for (int j = 0; j < newVertex.size() - 1; j++)
  141. if (vert == newVertex[j])
  142. mistake2 = true;
  143. }
  144. else
  145. cout << "Вершины были заданы неверно" << endl;*/
  146. }
  147. graph.push_back(newVertex);
  148. for (int i = 0; i < graph.size() - 1; i++)
  149. {
  150. for (int j = 1; j < graph[graph.size() - 1].size(); j++)
  151. {
  152. if (graph[i][0] == graph[graph.size() - 1][j])
  153. graph[i].push_back(graph[graph.size() - 1][0]);
  154. }
  155. }
  156. }
  157. }
  158.  
  159. void Graph::RemoveVertex()
  160. {
  161. int n;
  162. cout << "Введите вершину: ";
  163. cin >> n;
  164. bool mistake = true;
  165. for (int i = 0; i < graph.size(); i++)
  166. if (graph[i][0] == n)
  167. mistake = false;
  168. if (!mistake)
  169. {
  170. graph.erase(graph.begin() + n);
  171. }
  172. }
  173.  
  174. int main()
  175. {
  176. int n = 1;
  177. setlocale(LC_ALL,"RUS");
  178. string strGraph = "graph.txt";
  179. string strResult = "result.txt";
  180. string specGraph = "Create full graph";
  181. Graph *workGraph = NULL;
  182. workGraph = new Graph(n);
  183. workGraph->LoadGraph(strGraph);
  184. //workGraph->SpecGraph(specGraph);
  185. //workGraph->AddVertex();
  186. //workGraph->AddVertex();
  187. workGraph->RemoveVertex();
  188. workGraph->SaveGraph(strResult);
  189. workGraph->DeleteGraph();
  190.  
  191. system("pause");
  192. return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement