#include<stdio.h> #include<string.h> #define maxv 10 #define inf 32767 typedef char infotype; typedef struct { int no; infotype info; } vertextype; typedef struct { int edges[maxv][maxv]; vertextype vexs[maxv]; int n; } matgraph;

void creatematgraph(matgraph *T, int n) { char ch[10]; int k = 0, i = 0; int j = 0, r; while (k < n) { gets(ch); T->vexs[k].no = i; T->vexs[k].info = ch[0]; k++; if (k >= n) break; } i = 0; while (i < n) { j = 0; while (j < n) { printf("Is the edge [%d][%d] exist? ", i, j); scanf("%d", &r); T->edges[i][j] = r; j++; } i++; } }

void printgraph(matgraph *T, int n) { int k = 0; while (k < n) { printf("No%d: %c\n", T->vexs[k].no, T->vexs[k].info); k++; } int i = 0, j = 0; while (i < n) { j = 0; while (j < n) { printf("%d\t", T->edges[i][j]); j++; } printf("\n"); i++; } }

int main() { matgraph L = {0}; int n = 5; creatematgraph(&L, n); printgraph(&L, n); return 0; }

修复的问题如下:

  1. creatematgraph 函数的参数 matgraph T 应该改为 matgraph *T。
  2. while(ch) 应该改为 while(k < n)。
  3. ch=gets(); 应该改为 gets(ch);。
  4. T->vexs[k].info=ch; 应该改为 T->vexs[k].info=ch[0];。
  5. scanf("%d",r); 应该改为 scanf("%d", &r);。
  6. while(i<n) 和 while(j<n) 循环块内的代码应该分别放在两个 while 循环内。
  7. printf("No%d:%s/t",T->vexs[k].no,T->vexs[k].info); 应该改为 printf("No%d: %c\n", T->vexs[k].no, T->vexs[k].info);。
  8. printf("%d/t",T->edges[i][j]); 应该改为 printf("%d\t", T->edges[i][j]);。
  9. i++; 应该放在 while(j<n) 循环外面。
  10. matgraph L; 应该改为 matgraph L = {0};.
C语言邻接矩阵图代码修复及优化

原文地址: https://www.cveoy.top/t/topic/qfYP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录