栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

使用结构体输入参加某会议成员的信息,并计算男女比例C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用结构体输入参加某会议成员的信息,并计算男女比例C++

在AMTA会议上,主要参会人员一共有八个,其中对应他们的名字调出他们的职位以及爱好,年龄,性别等。要求输入a时为名字,b为职位,c为个人简称,d为爱好,e为年龄,p为性别,q为显示男女比例及退出。(case字母不区分大小写。成员信息后续可以直接读取txt文件,这里由于人员较少,我选择直接手动给定一个数组。)

代码如下:

//AMTA会议

#include 

using namespace std;

const int strsize = 30;
const int NUM = 8;
double malenum = 0.0;
double femalenum = 0.0;
double ratio;

//构造显示函数:
void display_name();
void display_title();
void display_amtaname();
void display_preference();
void display_age();
void display_sex();

//构建结构体,其中s是性别的另一种表达,s=0,1;0为女性,1为男性
struct amta
{
    char fullname[strsize];
    char title[strsize];
    char amtaname[strsize];
    char sports[strsize];
    int age;
    string sex;
    int s;
};

//输入结构体数组的内容
amta people[8] =
    {
        {"Derri Bai", "Chief Executive Officer", "BCEO", "Basketball", 35, "male", 1},
        {"Smit Eri", "Junior Programmer", "JPS", "Ping Pong", 35, "male", 1},
        {"Tank Du", "Chairman of Board", "CBD", "Swim", 45, "female", 0},
        {"Tiger Guo", "Manager", "MG", "Liao Mei", 34, "male", 1},
        {"Pen Yu", "Analyst Trainee", "ATY", "Study", 43, "female", 0},
        {"Ha Huan", "Chief Maintenance Officer", "CMOH", "Game", 35, "male", 1},
        {"Bo Wan", "Chief Technology Officer", "CTOW", "Basketball", 34, "male", 1},
        {"SY San", "Staf", "WSYS", "Song", 32, "female", 0}};

//定义显示函数
void display_name()
{
    for (int i = 0; i < NUM; i++)
        cout << "Fullname: " << people[i].fullname << endl;
}

void display_title()
{
    for (int i = 0; i < NUM; i++)
        cout << "Title: " << people[i].title << endl;
}

void display_amtaname()
{
    for (int i = 0; i < NUM; i++)
        cout << "Amtaname: " << people[i].amtaname << endl;
}

void display_preference()
{
    for (int i = 0; i < NUM; i++)
        cout << people[i].fullname << " 's preference is: " << people[i].sports << endl;
}

void display_age()
{
    for (int i = 0; i < NUM; i++)
        cout << people[i].fullname << " 's age is: " << people[i].age << endl;
}

void display_sex()
{
    for (int i = 0; i < NUM; i++)
        cout << people[i].fullname << " 's sex is: " << people[i].sex << endl;
}

//主函数
int main()
{
    char ch;

    cout << "Benevolent Order of Programmers Report" << endl;
    cout << "a. display by name          b. display by titlen"
         << "c. display by amtaname       d. display by preferencen"
         << "e. display by age           p. display by sexn"
         << "q. putout ratio and quit" << endl;

    cout << "Enter a case: ";

    while (cin >> ch && ch != 'q')
    {
        switch (ch)
        {
        case 'a':
        case 'A':
            display_name();
            break;
        case 'b':
        case 'B':
            display_title();
            break;
        case 'c':
        case 'C':
            display_amtaname();
            break;
        case 'd':
        case 'D':
            display_preference();
            break;
        case 'e':
        case 'E':
            display_age();
            break;
        case 'p':
        case 'P':
            display_sex();
            break;
        }
        cout << "Next case: ";
    }

//统计男女人数,输出比例并计算比率
    for (int i = 0; i < NUM; i++)
    {
        if (people[i].s == 1)
        {
            malenum++;
        }
        else if (people[i].s == 0)
        {
            femalenum++;
        }
    }
    ratio = malenum / femalenum;
    cout << "Number of male and female are: " << malenum << ":" << femalenum << endl;
    cout << "The ratio of people is:" << ratio << endl;
    cout << "Bye!" << endl;

    return 0;
}

 结果如下:

 这道简单的题目属于自编题。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629107.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号