9-17 数据结构-结构体作业

1.求 sizeof(name1)?

struct name1{

    char str;

    short x;

    int num;

};

答案:8。

2.

typedef struct _a

{

    char c1;

    long i;

    char c2;

    double f;

}a;

typedef struct _b

{

    char c1;

    char c2;

    long i;

    double f;

}b;

sizeof(a) = 32;

sizeof(b) = 24;

3.给了一个结构体,求 sizeof(struct A) = ________。

struct A{

    char a;

    char b;

    char c;

    short d;

    int e;

    short f;

}

答案:16。

4.有一个如下的结构体,请问在64位编译器下用 sizeof(struct A) 计算出的大小是多少?( ) 

struct A{

    long a1;

    short a2;

    int a3;

    int *a4;

}

A. 24 B. 28 C. 16 D. 18

答案:24。

5.有以下说明语句,则下列错误的引用 是( )。

struct stu

{

    int no;

    char name[21];

};

stu w, *p = &w;

A. w.no B*p.no C. p->no D. (*p).no

答案:B   .的优先级比*高。

6.写出下述程序结果:

typedef struct test

{

    char x1;

    short x2;

    float x3;

    char x4;

}TEST_T;

printf("%d", sizeof(TEST_T));

答案:12。

7.下面的代码输出是什么,为什么?(64位环境) 

struct {

    char *a1;

    long a2;

    short a3;

}A;

int main(void)

{

    printf("%d", sizeof(A));

}

答案:24。

8.设有如下结构定义: struct student { int num; char name[20]; char sex; int age; char addr[30];} stud; 若用printf("%s\n", .....)访问该结构中name值的正确方法是 ( ) 

A. stud -> name B. &stud.name

C. stud.&name D. stud.name

答案:D。

9.struct

{

short a; char b; float c;

}cs;

则sizeof(cs)的值是( ) 

A.4 B.5 C.7 D.8

答案:D。

10.如下函数的输出结果是:【 】

struct node

{

char a; short b; char c; int d;

};

struct node s = { 3, 5, 6, 99 };

struct node *pt = &s;

printf("%X\n", *(int*)pt);

答案:0x06000503

11.编程题:定义学生结构体,存储学生的学号、姓名、分数,定义长度为5的结构体数组,实现:

①输入学生信息

②输出学生信息

③计算学生的成绩总分、平均分

④按照学生的分数进行排序

⑤输出排序后的数组

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct{
	int id;
	char name[20];
	float score;
}Student;
int main(int argc, const char *argv[])
{
	Student stu[5];
	for(int i=0;i<5;i++){
		printf("请输入第%d学生学号:",i+1);
		scanf(" %d",&stu[i].id);
		printf("请输入第%d学生姓名:",i+1);
		scanf(" %s",stu[i].name);
		printf("请输入第%d学生分数:",i+1);
		scanf(" %f",&stu[i].score);
		printf("\n");
	}
	float sum = 0;
	for(int i=0;i<5;i++){
		printf("学号:%d 姓名:%s 分数:%.2f\n",stu[i].id,stu[i].name,stu[i].score);
		sum += stu[i].score;
	}
	printf("学生总分为:%.2f,\n平均分为:%.2f\n",sum,sum/5);
	
	Student t;
	for(int i=1;i<5;i++){
		for(int j=0;j<5-i;j++){
			if(stu[j].score<stu[j+1].score){
				t.id=stu[j].id;strcpy(t.name,stu[j].name);t.score=stu[j].score;
				stu[j].id=stu[j+1].id;strcpy(stu[j].name,stu[j+1].name);stu[j].score=stu[j+1].score;
				stu[j+1].id=t.id;strcpy(stu[j+1].name,t.name);stu[j+1].score=t.score;
			}
		}
	}
	printf("排序后的输出为:\n");
	for(int i=0;i<5;i++){
		printf("学号:%d 姓名:%s 分数:%.2f\n",stu[i].id,stu[i].name,stu[i].score);
	}
	return 0;
}

执行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值