Home > Back-end >  Which bosses have a look
Which bosses have a look

Time:05-28

//with adjacency matrix as the storage structure, create undirected graph,
#include
#include
# define M 200
# define Max 32767

Typedef struct {
Char vexs [M];
Int arcs [M] [M].
Int vexnum arcnum;//the number of vertices and edges number
} Graph;
//return position function
Int Locatevex (Graph * G, char v) {
int i;

for (i=0; I & lt; G - & gt; Vexnum; I++) {

If (G - & gt; Vexs [I]==v)
return i;

}

return -1;

}

Void CreateGraph (Graph * G) {
Int I, j, k, v1, v2, w;
Printf (" please input your figure on the number of vertices and edges to be generated: ");
The scanf (" % d % d ", & amp; G - & gt; Vexnum, & amp; G - & gt; Arcnum);

for (i=0; I & lt; G - & gt; Vexnum; I++) {

Printf (" please enter the first % d a vertex information: ", I + 1);
The scanf (" % c ", the G & gt; Vexs [I]);
}

for (i=0; I & lt; G - & gt; Vexnum; I++) {

For (j=0; J & lt; G - & gt; Vexnum; J++) {

G - & gt; Arcs [I] [j]=32767;

If (I==j) {
G - & gt; Arcs [I] [j]=0;
}
}
}

Printf (" please enter the two vertices and the corresponding weight: \ n ");

For (k=0; K & lt; G - & gt; Arcnum; I++) {

The scanf (" % d % % c c ", & amp; V1, & amp; V2, & amp; W);
I=Locatevex (G, v1);
J=Locatevex (G, v2);
G - & gt; Arcs [I] [j]=w;
G - & gt; Arcs [j] [I]=G - & gt; Arcs [I] [j];
}
}

Void PrintGraph (Graph * G) {
Int I, j;
Printf (" adjacency matrix is: \ n ");

for (i=0; I & lt; G - & gt; Vexnum; I++) {

Printf (" % c ", the G & gt; Vexs [I]);
}

printf("\n");

for (i=0; I & lt; G - & gt; Vexnum; I++) {

Printf (" % c ", the G & gt; Vexs [I]);

For (j=0; J & lt; G - & gt; Vexnum; J++) {

If (G - & gt; Arcs [I] [j]==32767) {
Printf (" $");
} else {
Printf (" % d ", the G & gt; Arcs [I] [j]);
}

}
}

printf("\n");
}

Void menu () {
Printf (" * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
Printf (" * * 1. Create the figure 2. The print figure * * \ n ");
Printf (" * * 3. Depth first 4. Breadth-first * * \ n ");
Printf (" exit 5. * * * * \ n ");
Printf (" * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
}

Int main () {
Int I, num;
Graph G.

While (1) {
The menu ();//select menu
Printf (" please enter your choice: ");
The scanf (" % d ", num);

The switch (num) {
Case 1:
CreateGraph (& amp; G);//create the figure
break;

Case 2:
PrintGraph (& amp; G);//print figure
break;

Case 5:
The exit (0);
break;
}
}


return 0;
}

CodePudding user response:

The scanf (" % c ", the G & gt; Vexs [I]); Parameters for the array elements also want to use the address, the scanf (" % c ", & amp; G - & gt; Vexs [I]);
  • Related