1.练习:定义5学生结构体 (姓名name,年龄age,分数score)
多文件编译
1>定义函数实现输入
2>定义函数计算平均分数
3>定义函数计算最大年龄的信息4>定义函数实现按照分数升序排排序
5>定义函数输出
//head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct student{
char name[15];
int age;
int score;
};
void Input(struct student *s,int n);
float Averagescore(struct student *s,int n);
int Maxage(struct student *s,int n);
void Sort(struct student *s,int n);
void Output(struct student *s,int n);
#endif
//test.c
#include "head.h"
void Input(struct student *s,int n){
for(int i=0;i<n;i++){
printf("请输入学生%d的名字:",i+1);
scanf("%s",(s+i)->name);
printf("请输入学生%d的年龄:",i+1);
scanf("%d",&(s+i)->age);
printf("请输入学生%d的分数:",i+1);
scanf("%d",&(s+i)->score);
}
}
float Averagescore(struct student *s,int n){
int sum =0;
for(int i=0;i<n;i++){
sum+=(s+i)->score;
}
float res = 1.0*sum/n;
return res;
}
int Maxage(struct student *s,int n){
int max;
for(int i=0;i<n;i++){
if(i==0)
max = (s+i)->age;
if(max<(s+i)->age)
max = (s+i)->age;
}
return max;
}
void Sort(struct student *s,int n){
int cnt;
struct student temp;
for(int i=0;i<n;i++){
cnt = 0;
for(int j=0;j<n-1-i;j++){
if((s+j)->score>(s+j+1)->score){
temp = *(s+j);
*(s+j) =* (s+j+1);
*(s+j+1) = temp;
cnt++;
}
}
if(cnt==0)
break;
}
}
void Output(struct student s[],int n){
for(int i=0;i<n;i++){
printf("姓名为:%s,年龄为:%d,分数为:%d\n",(s+i)->name,(s+i)->age,(s+i)->score);
}
}
//main.c
#include "head.h"
int main(int argc, const char *argv[])
{
int num;
printf("请输入学生个数:");
scanf("%d",&num);
struct student *s = (struct student *)malloc(num*sizeof(struct student));
Input(s,num);
float averagescore = Averagescore(s,num);
printf("学生的平均分为:%.2f\n",averagescore);
int maxage = Maxage(s,num);
printf("学生的最大年龄为:%d\n",maxage);
printf("原数组如下:\n");
Output(s,num);
Sort(s,num);
printf("按分数升序排序后数组如下:\n");
Output(s,num);
free(s);
s=NULL;
#if 0
struct student s[4];
Input(s,4);
float averagescore = Averagescore(s,4);
printf("学生的平均分为:%2f\n",averagescore);
int maxage = Maxage(s,4);
printf("学生的最大年龄为:%d\n",maxage);
printf("原数组如下:\n");
Output(s,4);
Sort(s,4);
printf("排序后数组如下:\n");
Output(s,4);
#endif
return 0;
}
2.在堆区申请5个连续的存储空间,实现车辆信息的输入(品牌,颜色,
价格)
1>调用函数在堆区申请空间
2>调用函数实现输入
3>调用函数对价格排序
4>调用函数输出
5>释放堆区空间
//head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Car{
char brand[20];
char color[10];
float cost;
}datatype;
datatype *SpaceApplication(int n);
void Input(datatype *p,int n);
void Sort(datatype *p,int n);
void Output(datatype *p,int n);
#endif
//test.c
#include "head.h"
datatype *SpaceApplication(int n){
datatype * res = (datatype *)malloc(sizeof(datatype)*n);
if(res==NULL)
return NULL;
return res;
}
void Input(datatype *p,int n){
for(int i=0;i<n;i++){
printf("请输入第%d辆汽车的品牌:",i+1);
scanf("%s",(p+i)->brand);
printf("请输入第%d辆汽车的颜色:",i+1);
scanf("%s",(p+i)->color);
printf("请输入第%d辆汽车的价格:",i+1);
scanf("%f",&(p+i)->cost);
}
}
void Sort(datatype *p,int n){
int cnt;
datatype temp;
for(int i=0;i<n;i++){
cnt = 0;
for(int j=0;j<n-1-i;j++){
if((p+j)->cost > (p+j+1)->cost){
temp = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = temp;
cnt++;
}
}
if(cnt==0)
break;
}
}
void Output(datatype *p,int n){
for(int i=0;i<n;i++){
printf("品牌为:%s,颜色为:%s,价格为:%.2f万元\n",(p+i)->brand,(p+i)->color,(p+i)->cost);
}
}
//main.c
#include "head.h"
int main(int argc, const char *argv[])
{
int n;
printf("请输入n的值:");
scanf("%d",&n);
datatype *res = SpaceApplication(n);
Input(res,n);
printf("排序前信息如下:\n");
Sort(res,n);
printf("冒泡排序信息如下:\n");
Output(res,n);
free(res);
res = NULL;
return 0;
}