????????今天真是美好而又充实的一天,随着对C语言的深入学习了解,计算机可以代替我计算的东西也越来越多啦。
? ? ? ? 今天主要展示一下运用冒泡排序实现对年龄,姓名排序,利用子函数减轻主函数负担便于编程书写,然后调用。
????????就这么简单。 ? ? ??
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 20
#define M 3
#define SIZE 5
//定义一个结构体数据类型
typedef struct student
{
char name [N];
int age;
float scores[M];
}Stu;
//输入子函数
//参数1:空间首地址 Stu * ps;
//参数2:元素个数 int count;
void input(Stu *ps,int count);
void input(Stu *ps,int count)
{
int i=0,j=0;
printf("请输入姓名,年龄,三门成绩\n");
for(i=0;i<count;i++)
{
scanf("%s%d",ps->name,&ps->age);
for(j=0;j<M;j++)
{
scanf("%f",&ps->scores[j]);
}
ps++;
}
}
//输出子函数
//参数1:空间首地址 Stu * ps;
//参数2:元素个数 int count;
void output(Stu *ps,int count);
void output(Stu *ps,int count)
{
int i=0,j=0;
for(i=0;i<count;i++)
{
printf("%20s ",ps->name);
printf("%5d ",ps->age);
for(j=0;j<M;j++)
{
printf("%5.2f ",ps->scores[j]);
}
printf("\n");
ps++;
}
}
//获得年龄最大的人的首地址的子函数
//参数1:空间首地址 Stu * ps;
//参数2:元素个数 int count;
//返回值:Stu *
Stu * agePmax(Stu *ps,int count);
Stu * agePmax(Stu *ps,int count)
{
Stu *pMax=NULL;
pMax=ps;
int i=0;
for(i=0;i<count-1;i++)
{
ps++;
if(pMax->age < ps->age)
{
pMax=ps;
}
}
return pMax;
}
//获得年龄最小的人的首地址的子函数
//参数1:空间首地址 Stu * ps;
//参数2:元素个数 int count;
//返回值:Stu *
Stu * agePmin(Stu *ps,int count);
Stu * agePmin(Stu *ps,int count)
{
int i=0;
Stu *pMin=NULL;
pMin=ps;
for(i=0;i<count;i++)
{
if(pMin->age > ps->age)
{
pMin=ps;
}
ps++;
}
return pMin;
}
/* //写一子函数实现整数交换
void swap1(int *pa,int *pb)
{
int temp=0;
temp=*pa;
*pa=*pb;
*pb=temp;
}*/
//写一子函数实现 交换stu
//参数1和参数2:结构体的首地址
void swap2(Stu *pa,Stu *pb)
{
Stu temp;
temp=*pa;
*pa=*pb;
*pb=temp;
}
//年龄从小到大的排序的子函数
//参数1:空间首地址 Stu * ps;
//参数2:元素个数 int count;
//返回值:void
void sortByage(Stu * ps,int count);
void sortByage(Stu * ps,int count)
{
int i=0,j=0;
for(i=0;i<count-1;i++)
{
for(j=0;j<count-1-i;j++)
{
if((ps+j)->age>(ps+j+1)->age)
{
//交换(ps+j),(ps+j+1)地址上的值
swap2((ps+j),(ps+j+1));
/* Stu temp;
temp=*(ps+j);
*(ps+j)=*(ps+j+1);
*(ps+j+1)=temp;*/
}
}
}
}
//写一子函数实现名字排序
//参数1:空间首地址 Stu * ps;
//参数2:元素个数 int count;
//返回值:void
void sortByName(Stu *ps,int count);
void sortByName(Stu *ps,int count)
{
int i=0,j=0;
for(i=0;i<count-1;i++)
{
for(j=0;j<count-1-i;j++)
{
if(strcmp((ps+j)->name,(ps+j+1)->name)>0)
{
swap2((ps+j),(ps+j+1));
}
}
}
}
int main(int argc, const char *argv[])
{
int op=0;
Stu * ps=NULL;
Stu * pMax=NULL;
Stu * pMin=NULL;
//使用malloc分配SIZE个存Stu的空间
//参数1:你要分配的空间的字节数 sizeof(Stu)*SIZE
//返回值:成功返回:void* 失败返回:NULL void 空void*任何数据类型的地址
ps=(Stu *)malloc(sizeof(Stu)*SIZE);
if(NULL==ps)
{
perror("malloc error");
return -1;
}
while(1)
{
printf("1******input\n");
printf("2******output\n");
printf("3******agePmax\n");
printf("4******agePmin\n");
printf("5******sortByage\n");
printf("6******sortByName\n");
printf("7******sortByScores\n");
printf("-1*****exit\n");
printf("请输入选项\n");
scanf("%d",&op);
if(-1==op)break;
switch(op)
{
case 1:
input(ps,SIZE);
break;
case 2:
output(ps,SIZE);
break;
case 3:
pMax=agePmax(ps,SIZE);
printf("年龄最大的是%s\n",pMax->name);
break;
case 4:
pMin=agePmin(ps,SIZE);
printf("年龄最小的是%s\n",pMin->name);
break;
case 5:
sortByage(ps,SIZE);
break;
case 6:
sortByName(ps,SIZE);
}
}
free(ps);
ps=NULL;
return 0;
}
? ? ? ? ?
|