本程序中运用到了结构体知识,
struct的作用相关知识可以点击下面链接可看,我个人感觉这个博主写的很好
struct初始化知识可以点击下面链接可看,我个人感觉这个博主写的也很好
源程序:
#include<stdio.h>
struct person
{
char name[10];
int age;
};
int main()
{
struct person p[3] = { {"张三",18},{"李四",19},{"王五",20} };
int i,j,h,man[3];
for (i = 0; i < 3; i++)
{
man[i] = p[i].age;
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (man[i]< man[j])
{
h = man[i];
man[i] = man[j];
man[j] = h;
}
}
}
h = 0;
for (i = 0; i < 3; i++)
{
if (man[1] == p[i].age)
{
h = i;
break;//break可用于退出循环
}
}
printf("\n他们中年龄居中的是: ");
printf("%s", p[h].name);
printf("%4d\n", p[h].age);//%4d 表示输出4位整型数, 不够4位右对齐
return 0;
}
结果展示:
本程序在VS中适用,其他编译器中自行修改。
此外,感兴趣的可以点进链接看看这位网友的拓展,相关拓展感觉很好https://zhidao.baidu.com/question/1603723310494814867.htmlhttps://zhidao.baidu.com/question/1603723310494814867.html