题1
main.c
#include "head11.h"
int main(int argc, const char *argv[])
{
int n;
printf("请输入学校人员数量n:");
scanf("%d",&n);
person *p = Create(n);
Input(p,n);
Output(p,n);
float ave = average_scores(p,n);
printf("学生平均成绩为:%.2f\n",ave);
int count = Count(p,n);
printf("老师人数为:%d\n",count);
p = free_fun(p);
return 0;
}
test.c
#include "head11.h"
person *Create(int n)
{
person *p = (person *)malloc(sizeof(person)*n);
if(p == NULL)
return NULL;
else
return p;
}
void Input(person *p,int n)
{
for(int i=0;i<n;i++)
{
printf("输入第%d个人员的姓名:",i+1);
scanf("%s",(p+i)->name);
printf("输入第%d个人员的性别:",i+1);
scanf(" %c",&(p+i)->sex);
printf("输入第%d个人员的职业:",i+1);
scanf(" %c",&(p+i)->occ);
if((p+i)->occ == 'S' || (p+i)->occ == 's')
{
printf("输入第%d个人员的分数:",i+1);
scanf("%f",&(p+i)->score);
}
else
{
printf("输入第%d个人员的职务:",i+1);
scanf("%s",(p+i)->post);
}
}
}
void Output(person *p,int n)
{
printf("姓名\t性别\t职业\t分数/职业\n");
for(int i=0;i<n;i++)
{
if((p+i)->occ == 'S' || (p+i)->occ == 's')
printf("%s\t%c\t%c\t%.2f\n",(p+i)->name,(p+i)->sex,(p+i)->occ,(p+i)->score);
else
printf("%s\t%c\t%c\t%s\n",(p+i)->name,(p+i)->sex,(p+i)->occ,(p+i)->post);
}
}
float average_scores(person *p,int n)
{
float sum=0;
float count=0;
for(int i=0;i<n;i++)
{
if((p+i)->occ == 'S' || (p+i)->occ == 's')
{
sum += (p+i)->score;
count++;
}
}
//printf("%f",sum);
return (sum/count);
}
int Count(person *p,int n)
{
int count=0;
for(int i=0;i<n;i++)
{
if((p+i)->occ == 'T' || (p+i)->occ == 't')
count++;
}
return count;
}
person *free_fun(person *p)
{
if(p!=NULL)
{
free(p);
p=NULL;
}
return p;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[32];
char sex;
char occ;
union{
float score;
char post[32];
};
}person;
person *Create(int n);
void Input(person *p,int n);
void Output(person *p,int n);
float average_scores(person *p,int n);
int Count(person *p,int n);
person *free_fun(person *p);
#endif
![](https://i-blog.csdnimg.cn/blog_migrate/4e131c0632b39cfd64836f99d1e0feb8.png)
题2
main.c
#include "head22.h"
int main(int argc, const char *argv[])
{
printf("输入车的数量:n\n");
int n;
scanf("%d",&n);
Car *p = Create(n);
Input(p,n);
Sort(p,n);
Output(p,n);
p = free_fun(p);
return 0;
}
test.c
#include "head22.h"
Car *Create(int n)
{
Car *p = (Car *)malloc(sizeof(Car)*n);
if(p == NULL)
return NULL;
else
return p;
}
void Input(Car *p,int n)
{
for(int i=0;i<n;i++)
{
printf("请输入第%d辆车的品牌:",i+1);
scanf("%s",(p+i)->name);
printf("请输入第%d辆车的颜色:",i+1);
scanf("%s",(p+i)->colour);
printf("请输入第%d辆车的价格:",i+1);
scanf("%f",&(p+i)->price);
}
}
void Output(Car *p,int n)
{
printf("品牌\t颜色\t价格\n");
for(int i=0;i<n;i++)
{
printf("%s\t%s\t%.2fW\n",(p+i)->name,(p+i)->colour,(p+i)->price);
}
}
void Sort(Car *p,int n)
{
for(int i=0;i<n-1;i++)
{
int count = 0;
for(int j=0;j<n-i-1;j++)
{
if((p+j)->price > (p+j+1)->price)
{
Car *t = Create(1);
*t = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = *t;
count++;
}
}
if(count == 0)
break;
}
}
Car *free_fun(Car *p)
{
if(p != NULL)
{
free(p);
p = NULL;
}
return p;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[32];
char colour[8];
float price;
}Car;
Car *Create(int n);
void Input(Car *p,int n);
void Output(Car *p,int n);
void Sort(Car *p,int n);
Car *free_fun(Car *p);
#endif
![](https://i-blog.csdnimg.cn/blog_migrate/660f45dd9feba5dd6b9c46136220dbcc.png)