Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vector>
- using namespace std;
- //1b - 5
- //II -29
- //каркас примом
- //29 and 5
- class Graph
- {
- public:
- vector<vector<int>> graph;
- Graph()
- {
- cout << "Empty Graph was created";
- }
- Graph(int Vertex)
- {
- //vector<vector<int>> tbl;
- }
- void LoadGraph(string grstr);
- void SpecGraph(string spstr);
- void DeleteGraph();
- void SaveGraph(string resultName);
- void AddVertex();
- void RemoveVertex();
- };
- void Graph::LoadGraph(string fileName)
- {
- ifstream fin(fileName);
- vector<int> vertexInRow;
- string str;
- int k = 0;
- int s = 0;
- while (fin.peek() != EOF)
- {
- getline(fin, str);
- size_t dotFind = str.find(":");
- string sstr = str.substr(0, dotFind);
- string vert = "";
- vertexInRow.push_back(atoi(sstr.c_str()));
- for (int i = dotFind + 2; i < str.size(); i++)
- {
- if (str[i] != ' ')
- {
- vert += str[i];
- i++;
- }
- vertexInRow.push_back(atoi(vert.c_str()));
- vert = "";
- }
- graph.push_back(vertexInRow);
- vertexInRow.clear();
- }
- }
- void Graph::SpecGraph(string spstr)
- {
- graph.clear();
- vector<int> rowsVec;
- int n;
- cout << "Введите количесвто вершин: ";
- cin >> n;
- for (int i = 1; i < n + 1; i++)
- {
- rowsVec.push_back(i);
- for (int j = 1; j < n + 1; j++)
- {
- if (i != j)
- rowsVec.push_back(j);
- }
- graph.push_back(rowsVec);
- rowsVec.clear();
- }
- }
- void Graph::DeleteGraph()
- {
- graph.clear();
- }
- void Graph::SaveGraph(string resultName)
- {
- ofstream fout(resultName);
- for (int i = 0; i < graph.size(); i++)
- {
- fout << graph[i][0] << ": " ;
- for (int j = 1; j < graph[i].size(); j++)
- fout << graph[i][j] << " ";
- fout << endl;
- }
- }
- void Graph::AddVertex()
- {
- int n;
- bool mistake1 = false;
- vector<int> newVertex;
- cout << "Введите новую вершину: ";
- cin >> n;
- for (int i = 0; i < graph.size(); i++)
- {
- if (graph[i][0] == n)
- {
- cout << "Такая вершина уже есть в графе" << endl;
- mistake1 = true;
- }
- }
- if (!mistake1)
- {
- newVertex.push_back(n);
- int k;
- int vert;
- bool mistake2 = false;
- cout << "Введите количество смежных вершин: ";
- cin >> k;
- cout << "Введите " << k << " вершины" << endl;
- for (int i = 0; i < k; i++)
- {
- cin >> vert;
- newVertex.push_back(vert);
- /*if (!mistake2)
- {
- cin >> vert;
- newVertex.push_back(vert);
- for (int j = 0; j < graph.size(); j++)
- if (graph[j][0] != vert)
- mistake2 = true;
- for (int j = 0; j < newVertex.size() - 1; j++)
- if (vert == newVertex[j])
- mistake2 = true;
- }
- else
- cout << "Вершины были заданы неверно" << endl;*/
- }
- graph.push_back(newVertex);
- for (int i = 0; i < graph.size() - 1; i++)
- {
- for (int j = 1; j < graph[graph.size() - 1].size(); j++)
- {
- if (graph[i][0] == graph[graph.size() - 1][j])
- graph[i].push_back(graph[graph.size() - 1][0]);
- }
- }
- }
- }
- void Graph::RemoveVertex()
- {
- int n;
- cout << "Введите вершину: ";
- cin >> n;
- bool mistake = true;
- for (int i = 0; i < graph.size(); i++)
- if (graph[i][0] == n)
- mistake = false;
- if (!mistake)
- {
- graph.erase(graph.begin() + n);
- }
- }
- int main()
- {
- int n = 1;
- setlocale(LC_ALL,"RUS");
- string strGraph = "graph.txt";
- string strResult = "result.txt";
- string specGraph = "Create full graph";
- Graph *workGraph = NULL;
- workGraph = new Graph(n);
- workGraph->LoadGraph(strGraph);
- //workGraph->SpecGraph(specGraph);
- //workGraph->AddVertex();
- //workGraph->AddVertex();
- workGraph->RemoveVertex();
- workGraph->SaveGraph(strResult);
- workGraph->DeleteGraph();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement