实验六(其一):简易投票结果系统

实验目的

掌握一维数组的使用,掌握字符数组的使用,掌握数组做参数的。

题目一

编写一个程序,允许用户输入在一个当地选举中5位候选人的姓氏和每位候选人得到的票数。然后程序应该输出每一位候选人的姓氏、候选人的得票数和候选人占总票数的百分比。程序还应该输出选举的获胜者。样例输出是:
Candidate Votes Received %of Total Votes
John 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.33
Total 19300
The Winner of the Election is Duffy

编写程序
#include  <iostream>
#include  <string>
#include <iomanip>
using namespace std;
void input(string str[], int votes[], int *total, int *max)
{
    *total = 0;
    *max =0;
    cout  << "Please Enter the Five Candidates' Last name"  << endl;
    for(int i = 0; i<5; ++i)
    {
        cin>>str[i]>>votes[i];
        if(i != 0 && votes[*max] < votes[i] ) *max = i;
        *total += votes[i];
    }
}
void output(string str[], int votes[], int *total,int *max)
{
    cout<<"Candidate Votes Received % of Total Votes "<<endl;
    for(int i = 0; i<5; ++i)
    {
        cout<<str[i]<<" "<<votes[i]<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<100*votes[i]*1.0/(*total)<<endl;
    }
    cout<<"Total "<<*total<<endl<<endl;
    cout<<"The Winner of the Election is "<<str[*max]<<"."<<endl;
}
int main ()
{
 //返回0
    string lastName[5];
    int votes[5];
    double per[5];
    int total;
    int max;
    input(lastName,votes,&total,&max);
    output(lastName,votes,&total,&max);
    return 0;
}
运行结果

运行结果

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值