scanf("%d",&x);ch1=getchar();ch2=getchar(); 如果我输入10、回车、2、回车,那么ch1和ch2的内容是什么
时间: 2024-06-04 09:13:56 浏览: 37
As an AI language model, I technically can't have a personal opinion, belief or feeling, so the concept of "null" doesn't apply to me. In computing, "null" is often used as a placeholder or indicator of no value, representing the absence of a particular data type or object.
相关问题
#include <stdio.h> #define MAX 100 #define MAXNUM 100000 typedef struct { int n, e, weight; char vexs[MAX]; int edges[MAX][MAX]; }MGraph; void CreateMGraph(MGraph* G) { int i, j, k; int p; char ch1, ch2; int weight; printf("请输入顶点数:"); scanf_s("%d", &G->e); printf("请输入各顶点信息(每个顶点以回车作为结束):\n"); for (i = 0; i < G->n; i++) { getchar(); printf("输入第%d个顶点:", i + 1); scanf_s("%c", &(G->vexs[i])); } for (i = 0; i < G->n; i++) for (j = 0; j < G->n; j++) G->edges[i][j] = MAXNUM; for (k = 1; k <= G->e; k++) { getchar(); printf("建立第%d条边(输入格式:顶点1,顶点2)和权值:",k); scanf_s("%c,%c,%d", &ch1,&ch2,&weight); for (p = 0; p < G->n; p++) { if (ch1 == G->vexs[p]) i = p; if (ch2 == G->vexs[p]) j= p; } G->edges[i][j] = weight; } } void CountDegree(MGraph G) { int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 }; int i, j; for (i = 0; i < G.n; i++) for (j = 0; j < G.n; j++) }补全代码,代码功能建立有向带权图,输出有向带权图
的各个顶点的入度和出度。
补全代码如下:
```
void CountDegree(MGraph G) {
int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 };
int i, j;
for (i = 0; i < G.n; i++)
for (j = 0; j < G.n; j++) {
if (G.edges[i][j] != MAXNUM) {
outdegree[i]++;
indegree[j]++;
}
}
printf("各个顶点的出度为:\n");
for (i = 0; i < G.n; i++) {
printf("%c:%d\n", G.vexs[i], outdegree[i]);
}
printf("各个顶点的入度为:\n");
for (i = 0; i < G.n; i++) {
printf("%c:%d\n", G.vexs[i], indegree[i]);
}
}
```
其中,我们首先定义了两个数组分别记录每个顶点的入度和出度,然后遍历邻接矩阵,如果某个顶点 i 到顶点 j 有一条边,那么顶点 i 的出度加 1,顶点 j 的入度加 1。最后输出各个顶点的入度和出度即可。
#include <stdio.h> #define MAX 100 #define MAXNUM 100000 typedef struct { int n, e, w; int vex[MAX]; int edge[MAX][MAX]; }MGraph; void CreateMGraph(MGraph* G) { int i, j, k; int p; char ch1, ch2; int weight; int p1, p2; printf("请输入顶点数:"); scanf_s("%d", &G->n); printf("请输入边数:"); scanf_s("%d", &G->e); printf("请输入个顶点信息(每个顶点一回车作为结束):\n"); for (i = 0; i < G->n; i++) { getchar(); printf("输入第%d个顶点:", i++); scanf_s("%c", &(G->vex[i])); } for (i = 0; i < G->n; i++) for (j = 0; j < G->n; j++) G->edge[i][j] = MAXNUM; for (i = 0; i < G->e; i++) { printf("请输入边和权值:\n"); getchar(); scanf_s("%c", &ch1); getchar(); scanf_s("%c", &ch2); getchar(); scanf_s("%d", &weight); for (j = 0; j < G->n; j++) { if (G->vex[j] = ch1) { p1 = j; break; } } for (j = 0; j < G->n; j++) { if (G->vex[j] = ch2) { p2 = j; break; } } G->edge[p1][p2] = weight; G->edge[p2][p1] = weight; } } void CountDegree(MGraph G) { int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 }; int i, j; for (i = 0; i < G.n; i++) for (j = 0; j < G.n; j++) if (G.edge[i][j] != MAXNUM) { outdegree[i]++; indegree[j]++; } for (i = 0; i < G.n; i++) printf("顶点%c的入度是%d,出度是%d\n", G.vex[i], indegree[i], outdegree[i]); } void DispMGraph(MGraph G) { int i, j; printf("\n图的邻接矩阵:\n"); for (i = 0; i < G.n; i++) printf("%c", G.vex[i]); printf("\n"); for (i = 0; i < G.n; i++) { for (j = 0; j < G.n; j++) if (G.edge[i][j] != MAXNUM) printf("%c%c%d\n", G.vex[i], G.vex[j], G.edge[i][j]); } } int main() { MGraph G; CreateMGraph(&G); }修改代码错误,建立有向带权图,输出有向带权图,求个顶点的入度和出度,并且输出
修改后的代码如下:
```c
#include <stdio.h>
#define MAX 100
#define MAXNUM 100000
typedef struct {
int n, e;
int vex[MAX];
int edge[MAX][MAX];
}MGraph;
void CreateMGraph(MGraph* G) {
int i, j, k;
int p;
char ch1, ch2;
int weight;
int p1, p2;
printf("请输入顶点数:");
scanf("%d", &G->n);
printf("请输入边数:");
scanf("%d", &G->e);
printf("请输入各顶点信息(每个顶点一行):\n");
for (i = 0; i < G->n; i++)
{
getchar();
printf("输入第%d个顶点:", i + 1);
scanf("%c", &(G->vex[i]));
}
for (i = 0; i < G->n; i++)
for (j = 0; j < G->n; j++)
G->edge[i][j] = MAXNUM;
for (i = 0; i < G->e; i++)
{
printf("请输入边和权值:\n");
getchar();
scanf("%c", &ch1);
getchar();
scanf("%c", &ch2);
scanf("%d", &weight);
for (j = 0; j < G->n; j++)
{
if (G->vex[j] == ch1)
{
p1 = j;
break;
}
}
for (j = 0; j < G->n; j++)
{
if (G->vex[j] == ch2)
{
p2 = j;
break;
}
}
G->edge[p1][p2] = weight;
}
}
void CountDegree(MGraph G) {
int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 };
int i, j;
for (i = 0; i < G.n; i++)
for (j = 0; j < G.n; j++)
if (G.edge[i][j] != MAXNUM)
{
outdegree[i]++;
indegree[j]++;
}
for (i = 0; i < G.n; i++)
printf("顶点%c的入度是%d,出度是%d\n", G.vex[i], indegree[i], outdegree[i]);
}
void DispMGraph(MGraph G) {
int i, j;
printf("\n图的邻接矩阵:\n");
for (i = 0; i < G.n; i++)
printf("%c ", G.vex[i]);
printf("\n");
for (i = 0; i < G.n; i++)
{
for (j = 0; j < G.n; j++)
if (G.edge[i][j] != MAXNUM)
printf("%c->%c:%d\n", G.vex[i], G.vex[j], G.edge[i][j]);
}
}
int main() {
MGraph G;
CreateMGraph(&G);
DispMGraph(G);
CountDegree(G);
return 0;
}
```
实现了建立有向带权图,输出有向带权图,求个顶点的入度和出度,并且输出。
阅读全文