二级C语言关于结构体的题目
struct
s
{
int
i;
struct
s
*i2;
};
static
struct
s
a[3]={1,
a[1],2,a[2],3,a[0]};
static
struct
s
*ptr;
ptr=a[1];
A)ptr-i++
B)ptr++-I
C)*ptr-i
D)++ptr-i
A选项:ptr指向的是a[1],ptr-i可表示为a[1].i,也就是2。i++运算是先用后加,所以A表达式的值为2
D选项:++ptr-i的意思是将指针ptr向后移一个元素,再取这个元素中i的值,也就是a[2].i的值3
#include
stdio.h
typedef
union
{
long
i;
//4字节
int
k[5];
//2*5=10字节
char
c;
//1字节
}
DATE;
//DATA类型的变量将占用10字节
struct
date
{
int
cat;
//2字节
DATE
cow;
//10字节
double
dog;//8字节
}
too;
//too占用2+10+8=20字节
DATE
max;
main()
{
printf(”%d\n”,sizeof()(struct
date)+sizeof(max));
sizeof()的功能是求某种数据类型或某个变量所占字节数
struct
data类型的数据占20个字节。max占10个字节。相加为30
}
A)25
B)30
C)18
D)8
C语言结构体题
#include stdio.h
#define N 5
void input_stu(struct Stu *stu);
void output_avg(struct Stu *stu);
struct Stu
{
int id;
char name[10];
int sex;
float math;
float eng;
float c;
};
int main()
{
struct Stu stu[N];
input_stu(stu);
output_avg(stu);
return 0;
}
void input_stu(struct Stu *stu)
{
int i = 1;
do
{
printf(“请输入第%d个学生的姓名 学号 性别 数学成绩 英语成绩 C语言成绩:”, i);
struct Stu *s = stu + i – 1;
scanf(“%s %d %d %f %f %f”, s-name, s-id, s-sex, s-math, s-eng, s-c);
i++;
} while (i = N);
}
void output_avg(struct Stu *stu)
{
int i = 1;
printf(“%-8s%-8s%-8s%-8s%-8s%-8s%-8s”,”学 号”, “姓 名”, “性 别”, “高 数”, “英 语”, “C”, “平均分”);
struct Stu *s = stu + i – 1;
float math_avg = stu-math, eng_avg = stu-eng, c_avg = stu-c;
putchar(10);
do
{
struct Stu *s = stu + i – 1;
math_avg = ((i – 1)*math_avg + s-math) / i;
eng_avg = ((i – 1) * eng_avg + s-eng) / i;
c_avg = ((i – 1)*c_avg + s-c) / i;
printf(“%-8d%-8s%-8s%-8.1f%-8.1f%-8.1f%-8.1f”,
s-id, s-name, s-sex == 1 ? “男” : “女”, s-math, s-eng, s-c, (s-math + s-eng + s-c) / 3);
putchar(10);
i++;
} while (i = N);
printf(“%-12s%-12s%-12s%”, “数学平均分”, “英语平均分”, “C平均分”);
putchar(10);
printf(“%-12.1f%-12.1f%-12.1f”, math_avg, eng_avg, c_avg);
}
c语言使用结构体的一道题?
#includestdio.h
struct DATE {
int year;
int month;
int day;
};
void push(int d[12],int*year,int*month,int*day) {
if(*dayd[*month-1])
(*day)++;
else if(*dayd[(*month)-1])printf(“error input!”);
else if(*month12)(*month)++,*day=1;
else (*year)++,*month=1,*day=1;
}
int main(void)
{
int d1[12]= {31,28,31,30,31,30,31,31,30,31,30,31},d2[12]= {31,29,31,30,31,30,31,31,30,31,30,31};
struct DATE before;
scanf(“%d%d%d”,before.year,before.month,before.day);
if((before.year%4==0before.year%100!=0)||before.year%400==0)push(d2,before.year,before.month,before.day);
else push(d1,before.year,before.month,before.day);
printf(“%d年%d月%d日”,before.year,before.month,before.day);
return 0;
}