| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 462 人关注过本帖
标题:高手帮忙指点下哪有问题
只看楼主 加入收藏
when159357
Rank: 2
等 级:论坛游民
帖 子:50
专家分:71
注 册:2009-11-11
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:2 
高手帮忙指点下哪有问题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node{
    int num;
    char name[20];
    int score;
    struct stud_node*next;
};
struct stud_node*Creat_Stu_Doc();
void Print_Stu_Doc(struct stud_node*head);
struct stud_node*Creat_Stu_Doc()
{
    struct stud_node*head,*p;
    int num,score;
    char name[20];
    int size = sizeof(struct stud_node);
    head = NULL;
    printf("input num,name and score:\n");
    scanf("%d%s%d",&num,name,&score);
    while(num!=0){
        p=(struct stud_node*)malloc(size);
        p->num=num;
        strcpy(p->name,name);
        p->score= score;
        
    }
    return head;
}
void Print_Stu_Doc(struct stud_node*head)
{
    struct stud_node*ptr;
    if(head == NULL){
        printf("\nNo Records\n");
        return ;
    }
    printf("\nthe students`records are:\n");
    printf("   num   name   score\n");
    for(ptr=head;ptr;ptr=ptr->next)
    printf("%8d%20s%6d\n",ptr->num,ptr->name,ptr->score);
   
}
int main(void)
{
    struct stud_node*head,*p;
    int choice,num,score;
    char name[20];
    int size = sizeof(struct stud_node);
   
    do{
        printf("1:create 2:print 0:exit\n");
        scanf("%d",&choice);
        switch(choice){
            case 1:
            head=Creat_Stu_Doc();
            break;
            case 2:
            Print_Stu_Doc(head);
            break;
            case 0:
            break;
        }
        
    }while(choice != 0);
        return 0;
}
运行貌似有问题 帮忙看下
搜索更多相关主题的帖子: include 
2010-01-23 13:57
fqtb16
Rank: 7Rank: 7Rank: 7
来 自:上海
等 级:黑侠
帖 子:96
专家分:504
注 册:2009-12-28
收藏
得分:10 
没人顶?

爱拼才会赢
2010-01-27 16:36
playmyself
Rank: 5Rank: 5
来 自:第3系4级宇宙空间
等 级:职业侠客
帖 子:76
专家分:399
注 册:2009-7-8
收藏
得分:10 
改完后代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node{
    int num;
    char name[20];
    int score;
    struct stud_node*next;
};
struct stud_node*Creat_Stu_Doc();
void Print_Stu_Doc(struct stud_node*head);
struct stud_node*Creat_Stu_Doc()
{
    struct stud_node*head,*p;
    int num,score;
    char name[20];
    int size = sizeof(struct stud_node);
    head = NULL;
    printf("input num,name and score:\n");
    scanf("%d%s%d",&num,name,&score);
    while(num!=0){
        p=(struct stud_node*)malloc(size);
        p->num=num;
        strcpy(p->name,name);
        p->score= score;
        p->next = NULL; //封口
        break; //这里停不下来了加个BREAK吧。
    }
    return p; //你处理的是P返回没处理的HEAD当然不行了。。
}
void Print_Stu_Doc(struct stud_node*head)
{
    struct stud_node*ptr;
    if(head == NULL){
        printf("\nNo Records\n");
        return ;
    }//下面这里又无限了,改一下,先处理再判断,没有时上面已经判断了
    printf("\nthe students`records are:\n");
    printf("   num   name   score\n");
    ptr=head;
    printf("%8d%20s%6d\n",ptr->num,ptr->name,ptr->score); //这里没对齐不知道什么意思,我就不改了
    while (ptr->next != NULL){
    ptr = ptr->next;
    printf("%8d%20s%6d\n",ptr->num,ptr->name,ptr->score); //
    }

   
}
int main(void)
{
    struct stud_node*head,*p;
    //int choice,num,score; //没用上,注释掉
    int choice;
    //char name[20]; //也是没用上
    int size = sizeof(struct stud_node);
   

    head = (struct stud_node*)malloc(size);//先申一个吧。
    head->next = NULL; //封口
    p = head; //临时指向
    do{
        printf("1:create 2:print 0:exit\n");
        scanf("%d",&choice);

        switch(choice){
            case 1:
            p->next=Creat_Stu_Doc();
            p=p->next; //想要链表就得考虑下一个。
            break;
            case 2:
            Print_Stu_Doc(head->next); //遍历从第2个开始
            break;
            case 0:
            break;
        }
        
    }while(choice != 0);
        return 0;
}

测试:
1:create 2:print 0:exit
2

No Records
1:create 2:print 0:exit
1
input num,name and score:
1 li 100
1:create 2:print 0:exit
2

the students`records are:
   num   name   score
       1                  li   100
1:create 2:print 0:exit
1
input num,name and score:
2 wang 200
1:create 2:print 0:exit
2

the students`records are:
   num   name   score
       1                  li   100
       2                wang   200
1:create 2:print 0:exit
0
请按任意键继续. . .
VC6,CFREE4.1通过。。

无聊创造奇迹。
2010-01-27 20:15
快速回复:高手帮忙指点下哪有问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016508 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved