首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

机构体里的共同体声明,怎么给里面的共同体赋值

2012-02-22 
机构体里的共同体声明,如何给里面的共同体赋值#include stdio.h#include stdlib.h#definesizesizeof(s

机构体里的共同体声明,如何给里面的共同体赋值
#include <stdio.h>
#include <stdlib.h>
#define   size   sizeof(struct   person)

struct   person
{
char   name[20];
char   sex[10];
struct   date   birth;
char   pos;//职称
union
{
char   add[50];//地址
char   grade[50];
}ag;
union
{
int   score;
int   salary;//工资
}cs;
};

struct   date  
{
int   year;
int   month;
int   day;
}birth;
//---------------------------
struct   person   *temp,*head,*last;

void   main()
{
int   n=1;
last=temp=(struct   person   *)malloc(size);
printf( "请输入姓名: ");
scanf( "%s ",temp-> name);
printf( "请输入性别: ");
scanf( "%s ",temp-> sex);
printf( "请输入出生日期; ");
scanf( "%d,%d,%d ",&temp-> birth.year,&temp-> birth.month,&temp-> birth.day);
printf( "请输入职称(T/S): ");
scanf( "%s ",temp-> pos);
if(temp-> pos== 't ')
{
printf( "请输入家庭地址; ");
scanf( "%s ",temp-> ag.add);
printf( "请输入工资 ");
scanf( "%d ",temp-> cs.salary);
}
else   if(temp-> pos== 's ')
{
printf( "请输入年级; ");
scanf( "%s ",temp-> ag.grade);
printf( "请输入成绩 ");
scanf( "%d ",temp-> cs.score);
}

do{
if(n==1)
{
head=temp;
n++;
}
else   if(n> 1)
{
temp=(struct   person   *)malloc(size);
printf( "请输入姓名: ");
scanf( "%s ",temp-> name);
printf( "请输入性别: ");
scanf( "%s ",temp-> sex);
printf( "请输入出生日期; ");
scanf( "%d,%d,%d ",&temp-> birth.year,&temp-> birth.month,&temp-> birth.day);
printf( "请输入职称(T/S): ");
scanf( "%s ",temp-> pos);
if(temp-> pos== 't '||temp-> pos== 'T ')
{
printf( "请输入家庭地址; ");
scanf( "%s ",temp-> ag.add);
printf( "请输入工资 ");
scanf( "%d ",&temp-> cs.salary);
}
else   if(temp-> pos== 's '||temp-> pos== 'S ')
{
printf( "请输入年级; ");
scanf( "%s ",temp-> ag.grade);
printf( "请输入成绩 ");
scanf( "%d ",&temp-> cs.score);
}
n++;
last=temp;
}
}while(n <=10);

}

请问我错在哪里??

[解决办法]

#include <stdio.h>
#include <stdlib.h>
#define size sizeof(struct person)

/* 逻辑错误,要先定义 */
struct date
{
int year;
int month;
int day;
}birth;

struct person
{
char name[20];
char sex[10];
struct date birth;
char pos;//职称
union
{
char add[50];//地址
char grade[50];
}ag;
union
{
int score;
int salary;//工资
}cs;
};


//---------------------------
struct person *temp,*head,*last;

void main()
{
int n=1;
last=temp=(struct person *)malloc(size);
printf( "请输入姓名: ");
scanf( "%s ",temp-> name);
printf( "请输入性别: ");
scanf( "%s ",temp-> sex);


printf( "请输入出生日期; ");
scanf( "%d,%d,%d ",&temp-> birth.year,&temp-> birth.month,&temp-> birth.day);
printf( "请输入职称(T/S): ");
scanf( "%s ",temp-> pos);
if(temp-> pos== 't ')
{
printf( "请输入家庭地址; ");
scanf( "%s ",temp-> ag.add);
printf( "请输入工资 ");
scanf( "%d ",temp-> cs.salary);
}
else if(temp-> pos== 's ')
{
printf( "请输入年级; ");
scanf( "%s ",temp-> ag.grade);
printf( "请输入成绩 ");
scanf( "%d ",temp-> cs.score);
}

do{
if(n==1)
{
head=temp;
n++;
}
else if(n> 1)
{
temp=(struct person *)malloc(size);
printf( "请输入姓名: ");
scanf( "%s ",temp-> name);
printf( "请输入性别: ");
scanf( "%s ",temp-> sex);
printf( "请输入出生日期; ");
scanf( "%d,%d,%d ",&temp-> birth.year,&temp-> birth.month,&temp-> birth.day);
printf( "请输入职称(T/S): ");
scanf( "%s ",temp-> pos);
if(temp-> pos== 't '||temp-> pos== 'T ')
{
printf( "请输入家庭地址; ");
scanf( "%s ",temp-> ag.add);
printf( "请输入工资 ");
scanf( "%d ",&temp-> cs.salary);
}
else if(temp-> pos== 's '||temp-> pos== 'S ')
{
printf( "请输入年级; ");
scanf( "%s ",temp-> ag.grade);
printf( "请输入成绩 ");
scanf( "%d ",&temp-> cs.score);
}
n++;
last=temp;
}
}while(n <=10);

}

热点排行