在一个结构体数组中,存有三个人的姓名和年龄,输出三人中年龄居中者的姓名和年龄。(VS中)

 本程序中运用到了结构体知识,

struct的作用相关知识可以点击下面链接可看,我个人感觉这个博主写的很好

https://blog.csdn.net/Jiawei_Z/article/details/113824997?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167099068516800186527975%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167099068516800186527975&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-113824997-null-null.142^v68^control,201^v4^add_ask,213^v2^t3_control1&utm_term=struct%E5%9C%A8c%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E4%BD%9C%E7%94%A8&spm=1018.2226.3001.4187https://blog.csdn.net/Jiawei_Z/article/details/113824997?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167099068516800186527975%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167099068516800186527975&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-113824997-null-null.142%5Ev68%5Econtrol,201%5Ev4%5Eadd_ask,213%5Ev2%5Et3_control1&utm_term=struct%E5%9C%A8c%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E4%BD%9C%E7%94%A8&spm=1018.2226.3001.4187

struct初始化知识可以点击下面链接可看,我个人感觉这个博主写的也很好

https://download.csdn.net/download/weixin_38735119/13998703?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167099004016782429771294%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=167099004016782429771294&biz_id=1&utm_medium=distribute.pc_search_result.none-task-download-2~all~first_rank_ecpm_v1~rank_v31_ecpm-3-13998703-null-null.142^v68^control,201^v4^add_ask,213^v2^t3_control1&utm_term=struct%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96%E6%96%B9%E6%B3%95&spm=1018.2226.3001.4187.4https://download.csdn.net/download/weixin_38735119/13998703?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167099004016782429771294%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=167099004016782429771294&biz_id=1&utm_medium=distribute.pc_search_result.none-task-download-2~all~first_rank_ecpm_v1~rank_v31_ecpm-3-13998703-null-null.142%5Ev68%5Econtrol,201%5Ev4%5Eadd_ask,213%5Ev2%5Et3_control1&utm_term=struct%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96%E6%96%B9%E6%B3%95&spm=1018.2226.3001.4187.4

源程序:

#include
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中)_第1张图片
      

 本程序在VS中适用,其他编译器中自行修改。

此外,感兴趣的可以点进链接看看这位网友的拓展,相关拓展感觉很好https://zhidao.baidu.com/question/1603723310494814867.htmlhttps://zhidao.baidu.com/question/1603723310494814867.html

你可能感兴趣的:(实验,c++,visual,studio)