jj314145738 于 2017.09.05 02:17 提问

int main()
{

``````//srand(time_t(NULL));
time_t t;
srand((unsigned) time(&t));
char filename[100] = "hep.txt";
bool directed = false;
int R = 100;

Graph::BuildFromFile2WC(filename);
.....
}

//下面是调用的原函数
void Graph::BuildFromFile2WC(char *file){

if (built)
return;
built = true;

FILE* in = fopen(file, "r");
fscanf(in,"%ld %ld", &n, &m);
``````

....}

5个回答

caozhy      2017.09.05 08:48

n和m在哪里定义的，是不是常量？

Graph::BuildFromFile2WC(filename);

jj314145738   2017.09.05 09:37

class Graph
{
private:
static int n;
static int m;
static vector degree;
static vector indegree;
static vector outdegree;
static void qsort_edges(int h, int t);

public:
....
static void BuildFromFile2WC(char *file);// undirected graph + wic
...
}Graph是一个类，他的定义如上，n,m的定义在里面。BuildFromFile2WC是成员函数，那应该怎么改啊

CH1314QM   2017.09.05 09:48

jj314145738   2017.09.05 10:02

{
private:
static int n;
static int m;
static vector degree;
static vector indegree;
static vector outdegree;
static void qsort_edges(int h, int t);

public:
static void QuickSort(int p,int r);
static int partion(int p,int r);
static void insertsort_edges(int right);
static void merge_edges(int start, int middle, int left);
static void mergesort_edges(int start, int end);
static void merge(int start, int middle, int end);
static bool built;
static vector index;
static vector correspond;
static void Build();
static int GetN();
static int GetM();
static vector edges;
static int GetDegree(int node);
static int GetOutDegree(int node);
static int GetInDegree(int node);
static int GetNeighbor(int node);
static Edge GetEdge(int node, int idx);
static void BuildIC();
static void BuildWC();
static void Build2GC();
static void Build2WC();
static void Build2DWC();
static void BuildFromFile2DWC(char *file); //directed graph + wic
static void BuildFromFile2OIC(char *file);
static void BuildFromFile2DUC(char *file, double p);//directed graph + uic
static void BuildFromFile2WC(char *file);// undirected graph + wic
static void BuildFromFile2UC(char *file, double p);//undirected graph + uic
static void Build2IC();
static void sortEdges();
static void Stats();
};//这个是类定义，完整的
bool Graph::built = false;
int Graph::n = 0;
int Graph::m = 0;
vector Graph::index;
vector Graph::degree(MAX_NODE,0);
vector Graph::indegree(MAX_NODE,0);
vector Graph::outdegree(MAX_NODE,0);
vector Graph::edges;
vector Graph::correspond(MAX_EDGE,0);//变量的初始化。函数就不一一列了。
void Graph::BuildFromFile2WC(char *file){

``````if (built)
return;
built = true;

FILE* in = fopen(file, "r");
fscanf(in,"%ld %ld", &n, &m);

degree.resize(n);
indegree.resize(n);
outdegree.resize(n);
edges.resize(2*m);
correspond.resize(2*m);

for (int i=0; i<m; i++)
{
//scanf("%ld %ld", &edges[i].u, &edges[i].v);
fscanf(in,"%ld %ld", &edges[i].u, &edges[i].v);
//inverse
//int temp = 0;
//temp = edges[i].u;
//edges[i].u = edges[i].v;
//edges[i].v = temp;

//edges[i].u;
//edges[i].v;
edges[i].w1=0;
edges[i].w2=0;
edges[i+m].u = edges[i].v;
edges[i+m].v = edges[i].u;
edges[i+m].w1=0;
edges[i+m].w2=0;
edges[i].c   = 1;
edges[i+m].c = 1;
degree[edges[i].u]++;
degree[edges[i].v]++;
indegree[edges[i].v]++;
indegree[edges[i].u]++;
outdegree[edges[i].u]++;
outdegree[edges[i].v]++;
correspond[i] = i+m;
correspond[i+m] = i;
}

qsort_edges(0, 2*m-1);
int newlocation[MAX_EDGE] = {0};
int m1 = 0;
for (int i=1; i<2*m; i++)
{
if ((edges[i].u != edges[m1].u) || (edges[i].v != edges[m1].v))
{
m1++;
edges[m1] = edges[i];
}
else
{
edges[m1].c++;
if (edges[i].w1<edges[m1].w1) edges[m1].w1=edges[i].w1;
edges[m1].w2=min(edges[m1].w2,edges[i].w2);
//if (edges[i].u==141 && edges[i].v==0) printf("%d %lg %lg %lg\n",i,edges[m1].w2,edges[i].w2,min(edges[m1].w2,edges[i].w2));
}
newlocation[i] = m1;
}

for(int i=0; i<2*m; i++){
correspond[newlocation[i]] = newlocation[correspond[i]];
}

if (m!=0)
m = m1+1;

index.resize(n);
for (int i=0; i<n; i++)
index[i] = 0;
for (int i=0; i<m; i++)
index[edges[i].u] = i;
for (int i=1; i<n; i++)
if (index[i] < index[i-1])
index[i] = index[i-1];

for (int i=0;i<m;i++)
{
//if (edges[i].w1==0)
edges[i].w1=-log(1-pow(1-1.0/indegree[edges[i].v], edges[i].c));
//if (edges[i].w2==0)
edges[i].w2=-log(1-pow(1-1.0/indegree[edges[i].u], edges[i].c));
}

FILE *out = fopen("directed.txt", "w");
for (int i=0; i<m; i++){
//if(indegree[edges[i].v]==0)
//continue;
fprintf(out, "%d\t%d\t%f\n", edges[i].u, edges[i].v, 1.0/indegree[edges[i].v]);
}

fclose(out);
``````

}//这个是我需要调用的那个函数，的定义。
main函数里
int main()
{

``````//srand(time_t(NULL));
time_t t;
srand((unsigned) time(&t));
char filename[100] = "hep.txt";
bool directed = false;
int R = 100;
Graph graph1;
graph1.BuildFromFile2WC(filename);//后面就省略， 到这就出错了，segmentation fault
``````
jj314145738   2017.09.05 10:05