作业1:
多文件编译
有若干个学校人员的信息,包括学生和教师。其中学生的数据包括:姓名、性别、职业s/S、分数。教师的数据包括:姓名、性别、职业t/T、职务。要求用同一个表格来处理以上数据。
1,定义函数在堆区申请空间n
struct B *p= (struct B * )malloc(sizeof(struct B)* n );
2,定义函数实现录入学校人员信息
3,定义函数输出学校人员信息
4,定义函数计算学生平均成绩
5,定义函数计算老师的个数
6,释放存储空间
head.h
#ifndef __HEAD_H
#define __HEAD_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct school
{
char name[30];
char Sex;
char job;
union
{
float score;
char position[30];
};
} sch;
sch *Create(int n);
void input(sch *p, int n);
void output(sch *p, int n);
void avg(sch *p, int n);
void teachernum(sch *p, int n);
sch *free_fun(sch *p);
#include "fun.c"
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
int n=100;
int num;
printf("请输入要输入的人员数:\n");
scanf("%d",&num);
sch *p=Create(n);
input(p,num);
output(p,num);
avg(p,num);
teachernum(p,num);
p = free_fun(p);
return 0;
}
fun.c
#include "head.h"
sch *Create(int n)
{
sch *p = (sch *)malloc(sizeof(sch) * n);
return p;
}
void input(sch *p, int n)
{
start:
for (int i = 0; i < n; i++)
{
printf("请输入第%d组姓名:\n", i + 1);
scanf("%s", (p + i)->name);
printf("请输入第%d组性别(男M女F):\n", i + 1);
scanf(" %c", &(p + i)->Sex);
printf("请输入第%d组职业(老师T学生S):\n", i + 1);
scanf(" %c", &(p + i)->job);
if ((p + i)->job == 'S' || (p + i)->job == 's')
{
printf("请输入第%d组分数:\n", i + 1);
scanf("%f", &(p + i)->score);
}
else if ((p + i)->job == 'T' || (p + i)->job == 't')
{
printf("请输入第%d组职务:\n", i + 1);
scanf("%s", (p + i)->position);
}
else
{
printf("输入错误!重新输入");
i = 0;
goto start;
}
}
}
void output(sch *p, int n)
{
printf("姓名\t性别\t职业\t分数/岗位\n");
for (int i = 0; i < n; i++)
{
printf("%s\t", (p + i)->name);
printf("%c\t", (p + i)->Sex);
printf("%c\t", (p + i)->job);
if ((p + i)->job == 'S' || (p + i)->job == 's')
{
printf("%f\n", (p + i)->score);
}
else if ((p + i)->job == 'T' || (p + i)->job == 't')
{
printf("%s\n", (p + i)->position);
}
}
}
void avg(sch *p, int n)
{
float sum = 0;
int count = 0;
float avg = 0;
for (int i = 0; i < n; i++)
{
if ((p + i)->job == 'S' || (p + i)->job == 's')
{
sum += (p + i)->score;
count++;
}
avg = sum / count;
}
printf("共有%d个学生,平均分为%f\n", count, avg);
}
void teachernum(sch *p, int n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
if ((p + i)->job == 'T' || (p + i)->job == 't')
{
count++;
}
}
printf("共有%d个老师\n", count);
}
sch *free_fun(sch *p)
{
if (p != NULL)
{
free(p);
p = NULL;
}
return p;
}
结果:
作业2:
多文件编译
在堆区申请5个连续的存储空间,实现车辆信息的输入(品牌,颜色,价格)
1>调用函数在堆区申请空间
2>调用函数实现输入
3>调用函数对价格排序
思路:和正常的冒泡是一样的
注意点:
1>if(条件) 条件是价格的比较 (p+j)->price (p+j+1)->price
2> 交换的是整个车的信息
例如: (p+j)表示整个车的地址
*(p+j)表示整个车的信息
交换的是*(p+j) 和*(p+j+1) 对应的整体信息
3> 注意中间变量t的类型,应该是结构体类型
4>调用函数输出
5>释放堆区空间
head.h
#ifndef __HEAD_H
#define __HEAD_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct car
{
char brand[30];
char color[30];
double price;
} Car;
Car *Create(int n);
void input(Car *p, int n);
void output(Car *p, int n);
void Bubble(Car *p, int n);
void swap(Car *p1, Car *p2);
Car *free_fun(Car *p);
#include "fun.c"
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
int n;
printf("请输入车的数量:\n");
scanf("%d", &n);
Car *p = Create(n);
input(p, n);
Bubble(p, n);
output(p, n);
p = free_fun;
return 0;
}
fun.c
#include "head.h"
Car *Create(int n)
{
Car *p = (Car *)malloc(sizeof(Car) * n);
return p;
}
void input(Car *p, int n)
{
for (int i = 0; i < n; i++)
{
printf("请输入品牌名:\n");
scanf("%s", (p + i)->brand);
printf("请输入颜色:\n");
scanf("%s", (p + i)->color);
printf("请输入价格:\n");
scanf("%lf", &(p + i)->price);
}
}
void Bubble(Car *p, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if ((p + j)->price > (p + j + 1)->price)
{
swap((p + j), (p + j + 1));
}
}
}
}
void swap(Car *p1, Car *p2)
{
Car t = *p1;
*p1 = *p2;
*p2 = t;
}
void output(Car *p, int n)
{
printf("品牌\t颜色\t价格\n");
for (int i = 0; i < n; i++)
{
printf("%s\t", (p + i)->brand);
printf("%s\t", (p + i)->color);
printf("%.2lf\n", (p + i)->price);
}
}
Car *free_fun(Car *p)
{
if (p != NULL)
{
free(p);
p = NULL;
}
return p;
}
结果: