# 想用c编写一个用来存储多边形顶点的程序，但是结果和预期的不对，可以麻烦帮我看看吗

0

``````#include <stdio.h>
#include <stdlib.h>

//定义六边形结构体
typedef struct hexagon{
int num;
int x;
int y;
struct hexagon* next;
}Hexagon;

//创建新的六边形节点
Hexagon* creat_hexagon(int num,int x,int y){
Hexagon* new_hexagon = (Hexagon*)malloc(sizeof(Hexagon));
new_hexagon->num = num;
new_hexagon->x = x;
new_hexagon->y = y;
new_hexagon->next = NULL;
return new_hexagon;
}

//在链表的结尾添加新的六边形节点
}else{
while(current_hexagon->next != NULL){
current_hexagon = current_hexagon->next;
}
current_hexagon->next = new_hexagon->next;
}
}

// 输出链表中存储的六边形信息
for(int i = 0; i <= 6; i++)
{
}

}
}

int main() {
int max = 6;
int x,y,num;

// 创建并添加新的六边形节点
printf("请输入六边形各点的的序号和坐标：");
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex1 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex2 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex3 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex4 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex5 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex6 = creat_hexagon(num ,x ,y);
return 0;
}
``````

echo_lovely 5个月前

@echo_lovely: 包裹起来是什么意思呀，截张图吗，我是新人不太了解

@笨小孩罢了: https://www.cnblogs.com/echo-lovely/p/16215739.html markdown语法，github，博客园，csdn写博客都会用到代码块，那个就是代码块

echo_lovely 5个月前

@笨小孩罢了: @笨小孩罢了: 我只是让你把你的代码用代码块包裹下，这样大家可以复制，不用调整缩进

echo_lovely 5个月前

@echo_lovely: 好的，谢谢指导，我去看看

@echo_lovely你好，下了个软件才发现是调试用的，请问是这样吗？

0

• 六边形有六个点，你应该用数组好一些，这样可以看出来
``````//定义六边形结构体
typedef struct hexagon{
int num;
int x;//这里用数组int[6]
int y;//这里用数组int[6]
struct hexagon* next;
}Hexagon;
``````
• 按你这个思路，需要用循环输入
每次创建一个六边形，他的num不变，第一个点初始化了，后面还得用同一个num，初始化后面5个点
所以输出六边形的时候，每个节点遍历了六次
``````for(int i = 0; i <= 6; i++)
{
}

``````
echo_lovely | 园豆：1109 (小虾三级) | 2023-04-28 15:11
``````int main() {
int max = 6;
int x,y,num;
for(num = 0; num<max; i++){
int number = 0;
for(number =0; number< 6； number++){
printf("请输入第 %d 个六边形的第 %d 个坐标：", num, number);
scanf("%d %d", &x, &y);
Hexagon* hex = creat_hexagon(num ,x ,y);//创建新的
}
}

/* 创建并添加新的六边形节点
printf("请输入六边形各点的的序号和坐标：");
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex1 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex2 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex3 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex4 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex5 = creat_hexagon(num ,x ,y);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex6 = creat_hexagon(num ,x ,y);
*/
return 0;
}

``````

0