鱼C论坛

 找回密码
 立即注册
查看: 1777|回复: 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
   while(0!=p1->num)
之前就应该输入。你不输入,谁知道p1->num是多少?
截图00.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-5-24 14:22:50 | 显示全部楼层
“学号是11492784的成绩是:0.000000”
这个怎么去掉
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-24 17:08:05 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *create();
void print(struct student *head);
struct student
{
    long num;
    float score;
    struct student *next;
};
int main()
{
    struct student *stu;
    stu=create();
    print(stu);
    return 0;
}
struct student *create()
{
    int n = 0;
    struct student *p1,*p2,*head;
    p2=p1=(struct student *)malloc(LEN);
    head=NULL;
    printf("请输入学号");
    scanf("%ld",&p1->num);
    printf("请输入成绩");
    scanf("%f",&p1->score);
    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("%ld",&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("学号是%ld的成绩是:%f\n",p->num,p->score);
            p=p->next;
        }while(p);
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-24 17:12:11 | 显示全部楼层    本楼为最佳答案   
   while(0!=p1->num)
之前就应该输入。你不输入,谁知道p1->num是多少?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

明白了,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

那请亲把我设为最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-28 06:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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