鱼C论坛

 找回密码
 立即注册
查看: 1959|回复: 5

[已解决]这个代码哪里错了?

[复制链接]
发表于 2017-5-24 14:21:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是原代码:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();
void print(struct student *head);
struct student
{
        long num;
        float score;
        struct student *next;
};
main()
{
        struct student *stu;
        stu=creat();
        print(stu);
        printf("\n\n");
        system("pause");
}
struct student *creat()
{
        int n;
        struct student *p1,*p2,*head;
        p2=p1=(struct student *)malloc(LEN);
        head=NULL;
        while(0!=p1->num)
        {
        n=n+1;
                if(n==1)
                        head=p1;
          else p2->next=p1;
          p2=p1;
          p1=(struct student *)malloc(LEN);
          printf("请输入学号");
          scanf("%d",&p1->num);
          printf("请输入成绩");
          scanf("%f",&p1->score);
        }
        p2->next=NULL;
        return head;
}
void print(struct student *head)
{
        struct student *p;
        p=head;
        if(NULL!=head)
        {
                do{
                        printf("学号是%d的成绩是:%f\n",p->num,p->score);
                        p=p->next;
                }while(p);
        }
}
我输出的结果是这样的:
请问怎样改才能将这个去掉
最佳答案
2017-5-24 17:12:11
  1.    while(0!=p1->num)
复制代码

之前就应该输入。你不输入,谁知道p1->num是多少?
截图00.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-5-24 14:22:50 | 显示全部楼层
“学号是11492784的成绩是:0.000000”
这个怎么去掉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-24 17:08:05 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define LEN sizeof(struct student)
  4. struct student *create();
  5. void print(struct student *head);
  6. struct student
  7. {
  8.     long num;
  9.     float score;
  10.     struct student *next;
  11. };
  12. int main()
  13. {
  14.     struct student *stu;
  15.     stu=create();
  16.     print(stu);
  17.     return 0;
  18. }
  19. struct student *create()
  20. {
  21.     int n = 0;
  22.     struct student *p1,*p2,*head;
  23.     p2=p1=(struct student *)malloc(LEN);
  24.     head=NULL;
  25.     printf("请输入学号");
  26.     scanf("%ld",&p1->num);
  27.     printf("请输入成绩");
  28.     scanf("%f",&p1->score);
  29.     while(0!=p1->num)
  30.     {
  31.         n=n+1;
  32.         if(n==1)
  33.             head=p1;
  34.         else p2->next=p1;
  35.         p2=p1;
  36.         p1=(struct student *)malloc(LEN);
  37.         printf("请输入学号");
  38.         scanf("%ld",&p1->num);
  39.         printf("请输入成绩");
  40.         scanf("%f",&p1->score);
  41.     }
  42.     p2->next=NULL;
  43.     return head;
  44. }
  45. void print(struct student *head)
  46. {
  47.     struct student *p;
  48.     p=head;
  49.     if(NULL!=head)
  50.     {
  51.         do{
  52.             printf("学号是%ld的成绩是:%f\n",p->num,p->score);
  53.             p=p->next;
  54.         }while(p);
  55.     }
  56. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-24 17:12:11 | 显示全部楼层    本楼为最佳答案   
  1.    while(0!=p1->num)
复制代码

之前就应该输入。你不输入,谁知道p1->num是多少?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-24 20:16:01 | 显示全部楼层
超凡天赐 发表于 2017-5-24 17:12
之前就应该输入。你不输入,谁知道p1->num是多少?

明白了,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-24 20:46:00 | 显示全部楼层

那请亲把我设为最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-19 01:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表