使用结构体输入参加某会议成员的信息,并计算男女比例C++
生活随笔
收集整理的這篇文章主要介紹了
使用结构体输入参加某会议成员的信息,并计算男女比例C++
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在AMTA會議上,主要參會人員一共有八個,其中對應(yīng)他們的名字調(diào)出他們的職位以及愛好,年齡,性別等。要求輸入a時為名字,b為職位,c為個人簡稱,d為愛好,e為年齡,p為性別,q為顯示男女比例及退出。(case字母不區(qū)分大小寫。成員信息后續(xù)可以直接讀取txt文件,這里由于人員較少,我選擇直接手動給定一個數(shù)組。)
代碼如下:
//AMTA會議#include <iostream>using namespace std;const int strsize = 30; const int NUM = 8; double malenum = 0.0; double femalenum = 0.0; double ratio;//構(gòu)造顯示函數(shù): void display_name(); void display_title(); void display_amtaname(); void display_preference(); void display_age(); void display_sex();//構(gòu)建結(jié)構(gòu)體,其中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; };//輸入結(jié)構(gòu)體數(shù)組的內(nèi)容 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}};//定義顯示函數(shù) 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; }//主函數(shù) int main() {char ch;cout << "Benevolent Order of Programmers Report" << endl;cout << "a. display by name b. display by title\n"<< "c. display by amtaname d. display by preference\n"<< "e. display by age p. display by sex\n"<< "q. putout ratio and quit" << endl;cout << "Enter a case: ";while (cin >> ch && ch != 'q' && 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: ";}//統(tǒng)計男女人數(shù),輸出比例并計算比率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; }?結(jié)果如下:
?這道簡單的題目屬于自編題。
總結(jié)
以上是生活随笔為你收集整理的使用结构体输入参加某会议成员的信息,并计算男女比例C++的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zsh与oh-my-zsh ← 阳志平的
- 下一篇: 2021年四川高考成绩等位分查询,202