首页 新闻 会员 周边 捐助

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

0
悬赏园豆:10 [待解决问题]
#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;
} 

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

// 输出链表中存储的六边形信息
void print_hexagon(Hexagon* head) {
    while (head != NULL) {
     for(int i = 0; i <= 6; i++)
     {
      printf("六边形的第%d个节点坐标为: (%d,%d)", head->num , head->x, head->y);
   head = head + 1;  
  }
    
    }
}

int main() {
 int max = 6; 
    Hexagon* head = NULL;
    int x,y,num;

    // 创建并添加新的六边形节点
 printf("请输入六边形各点的的序号和坐标:");
 scanf("%d%d%d", &num, &x, &y);
 Hexagon* hex1 = creat_hexagon(num ,x ,y);
 add_hexagon(&head, hex1);
 scanf("%d%d%d", &num, &x, &y);
 Hexagon* hex2 = creat_hexagon(num ,x ,y);
 add_hexagon(&head, hex2);
 scanf("%d%d%d", &num, &x, &y);
 Hexagon* hex3 = creat_hexagon(num ,x ,y);
 add_hexagon(&head, hex3);
 scanf("%d%d%d", &num, &x, &y);
 Hexagon* hex4 = creat_hexagon(num ,x ,y);
 add_hexagon(&head, hex4);
 scanf("%d%d%d", &num, &x, &y);
 Hexagon* hex5 = creat_hexagon(num ,x ,y);
 add_hexagon(&head, hex5);
 scanf("%d%d%d", &num, &x, &y);
 Hexagon* hex6 = creat_hexagon(num ,x ,y);
 add_hexagon(&head, hex6);
 print_hexagon(head); 
    return 0;
}
笨小孩罢了的主页 笨小孩罢了 | 初学一级 | 园豆:114
提问于:2023-04-24 21:23

你用

你的代码

包裹起来,大伙给你说

echo_lovely 1年前

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

笨小孩罢了 1年前

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

echo_lovely 1年前

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

echo_lovely 1年前

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

笨小孩罢了 1年前

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

笨小孩罢了 1年前
< >
分享
所有回答(2)
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++)
     {
      printf("六边形的第%d个节点坐标为: (%d,%d)", head->num , head->x, head->y);
   head = head + 1;  
  }

echo_lovely | 园豆:1546 (小虾三级) | 2023-04-28 15:11
int main() {
 int max = 6; 
    Hexagon* head = NULL;
	Hexagon* temp = head;//保存链表头节点地址,用于最后从此开始遍历
    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);//创建新的
			add_hexagon(&head, hex1);//添加到head后
			head = head.next//head走到最新的节点
		}
	}
	head = temp; // 将链表头结点还给head
	print_hexagon(head); // 打印,如果上一行不写,这里可以用temp试试,看能不能遍历

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

支持(0) 反对(0) echo_lovely | 园豆:1546 (小虾三级) | 2023-04-28 15:34

我没有调试你的代码,我这边没有vc6.0,C编译器

支持(0) 反对(0) echo_lovely | 园豆:1546 (小虾三级) | 2023-04-28 15:39
0

1、print_hexagon函数里面应该是head = head->next,而不是+1

2、add_hexagon里面最后一条赋值语句应该是current_hexagon->next = new_hexagon;因为要把new_hexagon塞到链表里面

3、print_hexagon函数for(int i = 0; i <= 6; i++)这里只有6条边,应该改成:for(int i = 0; i < 6; i++)或者for(int i = 1; i <= 6; i++)

Arthurian | 园豆:1123 (小虾三级) | 2023-05-15 22:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册