【数据结构(二)】作业

1. 有若干个学校人员的信息,包括学生和教师。

请输入第1人的姓名、性别和类型(s或者t)
a m s
请输入第1人的分数
100
请输入第2人的姓名、性别和类型(s或者t)
b m t
请输入第2人的职务
gj
请输入第3人的姓名、性别和类型(s或者t)
c m s 
请输入第3人的分数
20
第1个学生的姓名:a,性别:m,分数:100
第2个教师的姓名:b,性别:m,职务:gj
第3个学生的姓名:c,性别:m,分数:20
student:60  teacher:1
#include"head.h"

int n = 3;
struct INFORMATION *createSpace(struct INFORMATION *p)
{
    p=(struct INFORMATION *)malloc(sizeof(p)*n);
    if(NULL==p)
    {
        puts("ERROR");
        return p;
    }
    return p;
}

void input(struct INFORMATION *p)
{
    for(int i=0;i<n;i++)
    {
        printf("请输入第%d人的姓名、性别和类型(s或者t)\n",i+1);
        scanf("%49s %c %c", p[i].name, &p[i].sex, &p[i].job);
        if(p[i].job=='s' || p[i].job=='S')
        {
            printf("请输入第%d人的分数\n",i+1);
            scanf("%d",&p[i].d.score);
        }else{
            printf("请输入第%d人的职务\n",i+1);
            scanf("%s",p[i].d.position);
        }
    }
}

int getTeacherCount(struct INFORMATION *p)
{
    int count = 0;
    for(int i=0;i<n;i++)
    {
        if(p[i].job=='t' || p[i].job=='T')
        {
            count++;
        }
    }
    return count;
}

int getStudentScoreAgv(struct INFORMATION *p)
{
    int count = 0;
    int sum = 0;
    for(int i=0;i<n;i++)
    {
        if(p[i].job=='s' || p[i].job=='S')
        {
            count++;
            sum+=p[i].d.score;
        }
    }
    return sum/count;
}

void output(struct INFORMATION *p)
{
    for(int i=0;i<n;i++)
    {
        if(p[i].job=='s' || p[i].job=='S')
        {
            printf("第%d个学生的姓名:%s,性别:%c,分数:%d\n",i+1,p[i].name,p[i].sex,p[i].d.score);
        }else{
            printf("第%d个教师的姓名:%s,性别:%c,职务:%s\n",i+1,p[i].name,p[i].sex,p[i].d.position);
        }
    }
}

struct INFORMATION * freeSpace(struct INFORMATION *p)
{
    if(p==NULL)
    {
        return p;
    }
    free(p);
    p=NULL;
    return p;
}

func.c

#include"head.h"
int main()
{
    struct INFORMATION *p=createSpace(p);
    input(p);
    int studentScoreAgv=getStudentScoreAgv(p);
    int teacherCount=getTeacherCount(p);
    output(p);
    printf("student:%d  teacher:%d\n",studentScoreAgv,teacherCount);
    p=freeSpace(p);
    return 0;
}

main.c

#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
union DIFFERENT
{
    int score;
    char position[20];
};
struct INFORMATION
{
    char name[20];
    char sex;
    char job;
    union DIFFERENT d;

};
struct INFORMATION *createSpace(struct INFORMATION *p);
void input(struct INFORMATION *p);
int getTeacherCount(struct INFORMATION *p);
int getStudentScoreAgv(struct INFORMATION *p);
void output(struct INFORMATION *p);
struct INFORMATION* freeSpace(struct INFORMATION *p);
#endif

head.h

2.求结构体的sizeof大小

(1) 11
hhh

(2) 10 在这里插入图片描述
(3) 12
在这里插入图片描述

3.写一个简单的宏,交换 A,B 两数的值(假设两个变量都是 char 或 int 类型)。

交换前:x = 5, y = 10
交换后:x = 10, y = 5
#include <stdio.h>
#define SWAP(a, b) do { \
                      typeof(a) temp = (a); \
                      (a) = (b); \
                      (b) = temp; \
                    } while(0)

int main() {
    int x = 5, y = 10;
    printf("交换前:x = %d, y = %d\n", x, y);
    SWAP(x, y);
    printf("交换后:x = %d, y = %d\n", x, y);
    return 0;
}

4.关键字static的含义

局部:延长生命周期,当前文件作用域不变
函数:函数的生命周期延长至整个文件,不可跨文件调用
指针:不可以指向auto类型的变量地址

5.思维导图

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值