2015-05-20 20 views
0

我需要解析這種形式的一個圖形文件:解析圖形文件中使用字符串流C++

5 
0 1 0.2 3 10.1 4 0.5 -1 
1 0 1.5 -1 
2 1 100.0 3 50.2 -1 
3 -1 
4 1 10.5 2 13.9 -1 

當第一線是節點的數量。從第二行開始,

0 1 0.2 3 10.1 4 0.5 -1 

0是源節點,1是它前往的節點,0.5是邊的權重。 -1表示行結束。

我創建了一個圖形類:

#ifndef GRAPH_H 
#define GRAPH_H 
#include <vector> 
#include <iostream> 

class Graph{ 
public: 
    explicit Graph(int size=0) : vertices_(size) { }; 

    void resize(int size){ 
     vertices_.resize(size); 
     empty(); 
    } 

    void insert(int v, int n, double w){ 
     Vertex* tmp = new Vertex{ n, w, nullptr } 
     cout << " inserting!"; 
     end(v)->next = tmp; 
    } 

    void empty(){ 
     for(int i=0;i<vertices_.size();i++) 
      vertices_[i] = new Vertex{i,0,nullptr} 
    } 

    void print() { 
     for(auto& v : vertices_){ 
      Vertex* tmp = v; 
      cout<< " Node " << v->node << " has edges to: \n" 
      while(tmp->next != nullptr){ 
       cout<< " node " << tmp->node << " with weight " << tmp->weight<<endl; 
      } 
     cout<<endl; 
     } 
    } 

private: 
    struct Vertex{ // struct for vertices of graph 
     int node; 
     double weight; 
     Vertex* next; 

     Vertex (int n, double w, Vertex* v) 
    : node{ n }, weight{ w }, next{ v } { } 
    }; 

    vector<Vertex*> vertices_; 

    Vertex* end(int v){ 
     Vertex* tmp = vertices_[v]; 
     while(tmp->next != nullptr) 
      tmp = tmp->next; 

     return tmp; 
    } 
}; 
#endif 

我有這個解析器迄今:(所有的變量是合適的類型)

#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include "graph.h" 

using namespace std; 

int main(int argc, char** argv) { 
    if(argc<3){ 
     cout<<"Usage: " << argv[0] << " <graph file> <starting vertex>\n"; 
     return 0; 
    } 
    bool first = true;  // true until the first line is read (flow control) 
    stringstream parse;  // stringstream for easy parsing & conversion of strings 
    int n, s;    // holds the nodes for inserting 
    double w;    // holds the weights for edges 
    string line;    

    ifstream graph1(argv[1]); 

    Graph g; 
    while(getline(graph1,line)){ 
    if(first){ 
     n=stoi(line); 
     g.resize(n); 
     first = false; 
    } else { 
     parse << line; 
     first = true; 
     while(getline(parse,line,' ')&&line != "-1"){ 
      if(first){ 
       parse >> s; 
       g.insert(s,s,0) 
       first = false; 
      } 
      parse >> n >> w; 
      cout << s << " " << n << " " << w <<endl; 
      g.insert(s,n,w); 
     } 
    } 
} 

但是當我print()我得到一個賽格故障。我究竟做錯了什麼?

+0

你能告訴我們你的解析函數的其餘部分嗎? – refi64

+0

'\t bool first = true; \t \t \t stringstream parse; \t \t \t int n,s; \t \t \t \t \t double w; \t \t \t \t \t string line; \t \t \t \t ifstream graph1(argv [1]); \t圖g;' – zeta

+0

你能用該代碼編輯你的問題嗎? – refi64

回答

0

檢查你忘了推進臨時變量

#include <bits/stdc++.h> 
using namespace std; 

class Graph { 
    public: 
     explicit Graph(int size=0) : vertices_(size) { }; 

    void resize(int size){ 
     vertices_.resize(size); 
     empty(); 
    } 

    void insert(int v, int n, double w){ 
     end(v)->next = new Vertex{ n, w, nullptr }; 
     cout << " inserted! " << endl; 
    } 

    void empty(){ 
     for(int i = 0; i < vertices_.size(); i++) 
      vertices_[i] = new Vertex{ i, 0, nullptr}; 
    } 

    void print() { 
     for(auto& v : vertices_){ 
      Vertex* tmp = v; 
      cout<< " Node " << v->node << " has edges to: \n"; 
      while(tmp->next != nullptr) { 
       if(tmp->node != v->node) 
        cout<< " Node " << tmp->node << " with weight " << tmp->weight<<endl; 
       tmp = tmp->next; // You forgot this line 
      } 
     cout<<endl; 
     } 
    } 

private: 
    struct Vertex{ // struct for vertices of graph 
     int node; 
     double weight; 
     Vertex* next; 

     Vertex (int n, double w, Vertex* v) 
    : node{ n }, weight{ w }, next{ v } { } 
    }; 

    vector<Vertex*> vertices_; 

    Vertex* end(int v){ 
     Vertex* tmp = vertices_[v]; 
     while(tmp->next != nullptr) 
      tmp = tmp->next; 

     return tmp; 
    } 
}g; 


int main() 
{ 
    int N,x,y; 
    double w; 
    cin >> N; 
    g.resize(N); 
    for(int i = 0; i < N; i++) 
    { 
     cin >> x; 
     while(cin >> y, y != -1) 
     { 
      cin >> w; 
      cout << "Edge from " << x << " to " << y << " with cost " << w << endl; 
      g.insert(x,y,w); 
     } 
    } 

    g.print(); 
} 

要從文件中讀取:

int main() 
{ 
    ifstream myfile("in.txt"); 
    if(myfile.is_open()) 
    { 
     int N,x,y; 
     double w; 
     myfile >> N; 
     for(int i = 0; i < N; i++) 
     { 
      myfile >> x; 
      while(myfile >> y, y != -1) 
      { 
       myfile >> w; 
       cout << "Edge from " << x << " to " << y << " with cost " << w << endl; 
      } 
     } 
     myfile.close(); 
    } 
    else 
     cout << "Unable to open file"; 
    return 0; 
} 
+0

我如何從文件中獲取數字? – zeta

+0

您已經在使用ifstream,請檢查編輯好的代碼。 –

+1

它工作完美,非常感謝你! – zeta

0

你應該忽略的線條和剛讀整數和雙打...

int n; f >> n; 
for (int i=0; i<n; i++) { 
    int v; f >> v; 
    assert(v == i); 
    int other; f >> other; 
    while (other != -1) { 
     double weight; f >> weight; 
     add_edge(v, other, weight); 
     f >> other; 
    } 
} 

這個假設沒有錯誤可言