|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问这三个错误怎么解决?没看懂哪出错了。
stu2”使用未定义的 struct“cesh”
“.xuehao”的左侧必须具有结构/联合类型
“.score”的左侧必须具有结构/联合类型
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #define LEN sizeof(struct cesh)
- struct cesh* creat();
- void print(struct cesh*head);
- struct cesh* shanchu(struct cesh*head,int xh);
- struct cesh* charu(struct cesh* head, struct cesh* stu2);
- int i;
- void main()
- {
- struct cesh* stu;
- struct cesh* p;
- struct cesh stu2;
- stu = creat();
- p = stu;
- print(p);
- int xh;
- //while (i)
- //{
- printf("输入你要删除的人的学号。\n");
- scanf_s("%d", &xh);
- print(shanchu(p, xh));
- //}
- printf("输入你要插入的学号。\n");
- scanf_s("%d", &stu2.xuehao);
- printf("输入成绩。\n");
- scanf_s("%lf", &stu2.score);
- p = charu(stu, &stu2);
- print(p);
- system("pause");
- }
- struct cesh
- {
- int xuehao;
- double score;
- struct cesh* next;
- };
- struct cesh* creat()
- {
- struct cesh *p1,*p2,*head;
- p1 = p2 = (struct cesh*)malloc(LEN);
- printf("输入学号。\n");
- scanf_s("%d", &p1->xuehao);
- printf("输入成绩。\n");
- scanf_s("%lf", &p1->score);
- head = NULL;
- i = 0;
- while (p1->xuehao)
- {
- i++;
- if (i == 1)
- {
- head = p1;
- }
- else
- {
- p2->next = p1;
- }
- p2 = p1;
- p1 = (struct cesh*)malloc(LEN);
- printf("输入学号\n");
- scanf_s("%d",&p1->xuehao);
- printf("输入成绩\n");
- scanf_s("%lf",&p1->score);
- }
- p2->next = NULL;
- return head;
- }
- void print(struct cesh* head)
- {
- struct cesh* p;
- printf("\n这里一共有 %d 个数据\n\n", i);
- p = head;
- if (head)
- {
- do
- {
- printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
- p = p->next;
- } while (p);
- }
- }
- struct cesh* shanchu(struct cesh* head, int xh)
- {
- struct cesh* p1;
- struct cesh* p2;
- p2 = p1 = head;
- if (head == NULL)
- {
- printf("已无数据。\n");
- goto end;
- }
- p1 = head;
- while ((xh != p1->xuehao) && p1->next != NULL)
- {
- p2 = p1;
- p1 = p1->next;
- }
- if (xh == p1->xuehao)
- {
- if (p1 == head)
- {
- head = p1->next;
- }
- else
- {
- p2->next = p1->next;
- }
- printf("\n成功删除学号为 %d 的成绩。\n", xh);
- i = i - 1;
- }
- else//正在尝试如果没有则加入此学号//bug多多
- {
- printf("\n没有找到学号: %d。\n", xh);
- //struct cesh* stu;
- //stu = creat();
- //print(stu);
- }
- end:
- return head;
- }
- struct cesh* charu(struct cesh* head, struct cesh* stu2)//第一个参数是被插入的链表
- //第二个参数是待插入的结构的地址
- {
- struct cesh* p0, * p1, * p2;
- p1 = head;
- p0 = stu2;
- if (head == NULL)
- {
- head = p0;
- p0->next == NULL;
- }
- else
- {
- while ((p0->xuehao > p1->xuehao) && (p1->next != NULL))
- {
- p2 = p1;
- p1 = p1->next;
- }
- if (p0->xuehao <= p1->xuehao)
- {
- if (p1 == head)
- {
- head = p0;
- p0->next = p1;
- }
- else
- {
- p2->next = p0;
- p0->next = p1;
- }
- }
- else
- {
- p1->next = p0;
- p0->next = NULL;
- }
- }
- i = i + 1;
- return head;
- }
复制代码
错误1:stu2使用未定义的struct "cesh"
错误原因:在主函数中声明了结构体变量stu2,但没有定义结构体"cesh"。
解决方法:在结构体定义之前添加结构体声明。
修改代码如下:
- struct cesh; // 添加结构体声明
- struct cesh* stu;
- struct cesh* p;
- struct cesh stu2;
复制代码
错误2:".xuehao"的左侧必须具有结构/联合类型
错误原因:在插入函数charu中,使用了".xuehao"访问结构体变量stu2的成员,但stu2是一个指向结构体的指针,需要使用"->"来访问成员。
解决方法:将".xuehao"修改为"->xuehao"。
修改代码如下:
- if (p0->xuehao <= p1->xuehao) // 修改为 p0->xuehao
复制代码
错误3:".score"的左侧必须具有结构/联合类型
错误原因:在插入函数charu中,使用了".score"访问结构体变量stu2的成员,但stu2是一个指向结构体的指针,需要使用"->"来访问成员。
解决方法:将".score"修改为"->score"。
修改代码如下:
- if (p0->xuehao <= p1->xuehao) // 修改为 p0->xuehao
复制代码
修改以上三处错误后,代码应该能够正常编译运行了。
|
|