也是差不多
设计一个候选人得票的统计程序。设有3个候选人,每次模拟一个选民投票,输入一个候选人的名字,选民人数任意,全部选民输入完后要求输出每个候选人的得票结果。(用struct类型。此程序的功能可以自行拓展)
#include<iostream>
#include<string>
struct houxuanren
{
char name[20];
int age;
char gender[6];
char minzu[20];
char mianmao[10];
char job[10];
int vote;
};
void shuchu(houxuanren r)
{
std::cout << r.name <<" "<<r.age<<" "<<r.gender<<" "
<<r.minzu<<" "<<r.mianmao<<" "<<r.job<< std::endl;
}
void main()
{
char toupiao [5];
int i, x=0, y=0, z=0;
houxuanren first = { "张三",31,"男","汉族","中共党员","工人",0 };
houxuanren second = { "李四",37,"男","回族","民盟","商人",0 };
houxuanren third = { "王五",40,"男","维吾尔族","群众","教师",0 };
std::cout << "请对以下候选人投票:"<< std::endl;
std::cout << "姓名 年龄 性别 民族 政治面貌 职业" << std::endl;
shuchu(first);
shuchu(second);
shuchu(third);
for (i = 0; i < 5; i++)
{
std::cout << "请第" << i + 1 << "位选民投票" << std::endl;
std::cin >> toupiao;
if (strcmp(toupiao, first.name) == 0)
first.vote++;
if (strcmp(toupiao, second.name) == 0)
second.vote++;
if (strcmp(toupiao, third.name) == 0)
third.vote++;
}
std::cout << "张三的得票情况为" << first.vote << std::endl;
std::cout << "李四的得票情况为" << second.vote << std::endl;
std::cout << "王五的得票情况为" << third.vote << std::endl;
if(first.vote==second.vote|| first.vote ==third.vote||second.vote == third.vote)
std::cout << "候选人有相同数目选票" << std::endl;
else if (first.vote >second.vote)
if (first.vote >third.vote)
std::cout << "张三选票最多,担任席位" << std::endl;
else if (second.vote >first.vote)
if (second.vote > third.vote)
std::cout << "李四选票最多,担任席位" << std::endl;
else if (third.vote > first.vote)
if (third.vote > second.vote)
std::cout << "王五选票最多,担任席位" << std::endl;
system("pause");
}