数据结构作业2

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
答案:A

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

答案:8
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);

0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28
  03   00   05  00  06    00   00   00

0x00050003
0x50003

11.编程题:定义学生结构体,存储学生的学号、姓名、分数,定义长度为5的结构体数组,实现:
①输入学生信息
②输出学生信息
③计算学生的成绩总分、平均分
④按照学生的分数进行排序
⑤输出排序后的数组

头文件.h

#ifndef _XS_
#define _XS_
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define LEN 5

typedef struct student
{
        int id;//学号
        char name[20];//姓名
        float score;//分数
} student;


//输入学生信息
void stu_input(student *s);


//输出学生信息
void stu_output(student *s);


//计算学生的成绩总分、平均分
void getScore(student *s,float *groSco,float *aveSco );


//按照学生的分数进行排序
void stu_sort(student *s);




#endif

源文件.c

#include "xs.h"


//输入学生信息
void stu_input(student *s) 
{
        for(int i=0;i<LEN;i++){
                printf("请输入第%d个学生信息:",i+1);
                scanf("%d",&(s+i)->id);
                scanf(" %s",(s+i)->name);
                scanf("%f",&(s+i)->score);
    
        }
}

//输出学生信息
void stu_output(student *s){

        for(int i=0;i<LEN;i++){
                printf("%d %s %.2f\n",(s+i)->id,(s+i)->name,(s+i)->score);
    
        }


}


//计算学生的成绩总分、平均分
void getScore(student *s,float *groSco,float *aveSco )
{
        int gs=0;
        int as=0;
        for(int i=0;i<LEN;i++)
        {
                gs+=(s+i)->score;
        }

        as=gs/LEN;
        *groSco=gs;
        *aveSco=as;
}

//按照学生的分数进行排序
void stu_sort(student *s){
        for(int i=1;i<LEN;i++){
                for(int j=0;j<LEN-i;j++){
                        if((s+j)->score>(s+j+1)->score){
                                student t=*(s+j);
                                *(s+j)=*(s+j+1);
                                *(s+j+1)=t;
                        }
                }
        }
}

入口文件main方法

#include "xs.h"



int main(int argc,const char *argv[])
{
        //从堆去分配内存
        student *s=(student*)malloc(sizeof(student)*LEN);
        
        //检测内存分配是否成功
        if(NULL==s){
                printf("分配内存失败\n");
                return 0;
        }

        //调用输入方法
        stu_input(s);

        //调用输出方法
        stu_output(s); 

        //学生总成绩 
        float gs=0;

        //学生平均成绩
        float as=0;

        //获取学生总成绩和评价成绩
        getScore(s,&gs,&as);

        //打印输出总成绩和平均成绩
        printf("总分为:%.2f,平均分为:%.2f\n",gs,as);

        //对学生按照成绩进行排序
        stu_sort(s);

        //输出排序后的结果
        stu_output(s);

        //释放堆内存
        free(s);

        //给指针置空
        s=NULL;
        return 0;
}

测试结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值