二级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语言结构体题目?
#include stdio.h
#include stdlib.h
#include string.h
#define N 3//可以改为10表示10条图书信息
typedef struct ss{
int bianhao;
char shu[80];
char ren[20];
double qian;
} SHU;
int main(){
SHU a[N];
SHU m,t;
int i,len,j;
for (i = 0; i N; i++){
printf(“输入第%d本书的编号\n”,i+1);
scanf(“%d”,a[i].bianhao);
getchar();
printf(“输入第%d本书的书名\n”,i+1);
gets(a[i].shu);
printf(“输入第%d本书的作者\n”,i+1);
gets(a[i].ren);
printf(“输入第%d本书的价格\n”,i+1);
scanf(“%lf”,a[i].qian);
}
for (i = 0;i N; i++){
m=a[i];
printf(“%d %s %s %.2lf\n”, m.bianhao,m.shu,m.ren,m.qian);
}
//排序
for(i=1;iN;i++)
{
for(j=0;jN-i;j++)
if(a[j].qiana[j+1].qian) /*大于号属于升序*/
{
t.bianhao=a[j].bianhao;
strcpy(t.shu,a[j].shu);
strcpy(t.ren,a[j].ren);
t.qian=a[j].qian;
a[j].bianhao=a[j+1].bianhao;
strcpy(a[j].shu,a[j+1].shu);
strcpy(a[j].ren,a[j+1].ren);
a[j].qian=a[j+1].qian;
a[j+1].bianhao=t.bianhao;
strcpy(a[j+1].shu,t.shu);
strcpy(a[j+1].ren,t.ren);
a[j+1].qian=t.qian;
}
}
m=a[N-1];
printf(“The top:%d %s %s %.2lf\n”, m.bianhao,m.shu,m.ren,m.qian);//排序后
for (i = 0;i N; i++){
m=a[i];
printf(“%d %s %s %.2lf\n”, m.bianhao,m.shu,m.ren,m.qian);
}
return EXIT_SUCCESS;
}
一道结构体的C语言试题
既然有人改好了那我就撤消了吧… 那个是值传递的吧,要地址传递…… 楼上很对,一般都用指针吧
#include stdio.h
#include string.h
typedef struct A{
int a;
char b[10];
double c;
}A;
void f(struct A *t){
t-a=1002;strcpy(t-b,”ChangRong”);t-c=1202.0;
}
main(){
struct A a={1001,”ZhangDa”,1098.0};
f(a);
printf(“%d %s %6.1f\n”,a.a,a.b,a.c);
}
感觉这样好看些…呵呵,用了C++的风格…… ,调用f的时候取a的地址(a),传递给指针t
一道结构体的c语言题,请帮帮忙
对于1,2程序!已测试!
#includestring.h
#includestdio.h
void main()
{ int max,min;
struct student
{char name[20];
int age;
};
struct student stu[3]={{“Li”,18},{“Wang”,22},{“Chen”,20}};
struct student *p;
p=stu;
max=p-age;
min=p-age;
for(p=stu;pstu+3;p++)
{
if(p-age max) max=p-age;
if(p-age min) min=p-age;
}
for(p=stu;pstu+3;p++)
{ if(p-age max p-age min)
{ printf(“the middle age is:”);
printf(“%s\ %d\n”,p-name,p-age);
}
}
}
对于 3
#includestring.h
#includestdio.h
void main()
{ int max,min;
struct student
{char name[20];
int age;
};
struct student stu[3];
struct student *p;
p=stu;
printf(“\nPlease input the students name and age:\n”);
for(p=stu;pstu+3;p++)
scanf(“%s%d”,p-name,p-age);
p=stu;
max=p-age;
min=p-age;
for(p=stu;pstu+3;p++)
{
if(p-age max) max=p-age;
if(p-age min) min=p-age;
}
for(p=stu;pstu+3;p++)
{ if(p-age max p-age min)
{ printf(“the middle age is:”);
printf(“%s\ %d\n”,p-name,p-age);
}
}
}
注意输入格式是:wang 18
li 22 等等中间空格隔开!
程序已经测试!