| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 540 人关注过本帖
标题:帮忙看看下列源代码:分配不了自定义结构的
只看楼主 加入收藏
adidas2777
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-4-15
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
帮忙看看下列源代码:分配不了自定义结构的
#include
#include
#include
#define ERROR 0
#define OK 1

typedef struct ChongXiu{
    int term;
    int score;
}ChongXiu;

typedef struct Subject{

}Subject;

typedef struct student{
    long num;                        //学号
    char *name;                     //姓名
    int sex;                         //1表示男性,0表示女性
    int age;                        //年龄
    int room;                   //宿舍号码
    long tel;                  //tel1表示宿舍电话,tel2表示手机号码

    int subnum;              //课程编号
    char *subname;               //课程名字
    int score1,score2,score3;                    //1:考试成绩;2:平时成绩;3:综合成绩
    int xuefeng;                 //学分

    char cx;                 //y表示重修,n表示没有重修

    ChongXiu *elem;

    struct student *next;
}Student;


Student Create(Student &L,int n){                     //n表示学生个数
    Student *p,*q;
    int i=1;

    int score1,score2,score3;                    //1:考试成绩;2:平时成绩;3:综合成绩
    int xuefeng;

    Student *L=(Student *) malloc(sizeof(Student));
    if(!L) return (-2);
    L->next = NULL;                             // 先建立一个带头结点的单链表
    q=(Student*) malloc(sizeof(Student));

    q=L;

    while(i<=n){
    p=(Student*) malloc(sizeof(Student));

    printf("请输入学生的学号:");
    scanf("%ld",&(p->num));

    printf("请输入学生的姓名:");
    scanf("%s",p->name);

    printf("请输入学生的性别:(1为男性,0为女性)");
    scanf("%d",&(p->sex));

    printf("请输入学生的年龄:");
    scanf("%d",&(p->age));

    printf("请输入学生的宿舍号码:");
    scanf("%d",&(p->room));

    printf("请输入学生的手机号码:");
    scanf("%ld",&(p->tel));

    q=Insert(L,p);

    i++;
    }

    return OK;
}


Student Insert(Student L,Student p){
    Student *p1,*p2,*p3;
    p1=(Student*) malloc(sizeof(Student));
    p2=(Student*) malloc(sizeof(Student));
    p3=(Student*) malloc(sizeof(Student));
    p3=L;
    p1=p;
    if(L==NULL){
        L->next=p1;
        p1->next=NULL;}
        else{
            while((p1->num>p3->num)&&(p3->next!=NULL)){
                p2=p3;
                p3=p3->next;
            }
            if(p1->num<=p3->num){
                if(L==p3)L=p1;
                else p2->next=p1;
                p1->next=p3;
            }
            else{
                p3->next=p1;
                p1->next=NULL;
            }
        }


    }
    return L;
}




int main(){
    Student *L,*p;
    int n,choice;
    do{
        printf("1:新建多个学生档案\n2:增添或补充学生档案\n3:删除学生档案\n4:显示全部学生档案");
        scanf("%d",&choice);
        switch(choice){
            case 1:printf("请输入创建的档案数量:");
                   scanf("%d",&n);
                   if(Create(L,n)) printf("%d个学生档案成功创建",n);
                   else printf("创建失败");
                   break;
            case 2:  p=(Student*) malloc(sizeof(Student));

    printf("请输入学生的学号:");
    scanf("%ld",&(p->num));

    printf("请输入学生的姓名:");
    scanf("%s",p->name);

    printf("请输入学生的性别:(1为男性,0为女性)");
    scanf("%d",&(p->sex));

    printf("请输入学生的年龄:");
    scanf("%d",&(p->age));

    printf("请输入学生的宿舍号码:");
    scanf("%d",&(p->room));

    printf("请输入学生的手机号码:");
    scanf("%ld",&(p->tel));
    L=Insert(L,p);
    break;

    case 0;printf("退出系统");break;
        }
    }while(choice!=0);

    return 0;
}
搜索更多相关主题的帖子: long 宿舍 include 
2013-04-15 16:24
liu_鹏
Rank: 2
等 级:论坛游民
帖 子:33
专家分:75
注 册:2013-4-16
收藏
得分:10 
把你的代码运行了一下。 里面全是错误。  而且。 你为什么没有头文件呢?在仔细看看吧。。

不懂就发帖。懂了就恢复。
2013-04-17 08:29
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:10 
typedef struct ChongXiu{
    int term;
    int score;
}ChongXiu;
这样有问题吧,自引用结构怎么定义,建议看看数据结构的链表

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-04-19 06:55
快速回复:帮忙看看下列源代码:分配不了自定义结构的
数据加载中...
 
   



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

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