获取输入法候选列表_C ++程序输入候选人列表,并根据收到的选票找到选举的获胜者...

获取输入法候选列表

Description: In the following article we are going to learn how to solve problem of such type using class definitions.

说明:在下面的文章中,我们将学习如何解决使用类定义这种类型的问题。

Problem statement:

问题陈述:

Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.

编写一个程序,允许用户输入本地选举中五个候选人的姓氏以及每个候选人获得的票数 。 然后,程序应输出每个候选人的姓名,获得的票数以及该候选人获得的总票数的百分比。 您的程序还应该输出选举的获胜者

Solution:

解:

Construction of object for the problem...

问题构造对象...

For this very problem, we need to create a new object, candidate, which has members:

对于这个问题,我们需要创建一个新的对象,候选对象,其成员如下:

  1. Last name

  2. No of votes received

    没有收到选票

Thus define a class like following:

因此定义一个如下的类:

class candidate{
	//public member since we are not 
	//bothered about security of members
	public: 
		string name;
		int vote;
};

Thus now a new type of object variable we will create that is candidate & the algorithm is based on the class definition.

因此,现在我们将创建一种新的对象变量类型,即候选变量,并且该算法基于类定义。

Algorithm:

算法:

  1. Create an array of 5 element of type candidate.

    创建一个由5个类型为候选人类型的元素组成的数组。

  2. Input all the entry & assign them to the candidate array.

    输入所有条目并将其分配给候选数组。

  3. Like for s be the input name & v be the no of 
    received vote for ith candidate, we assign it by-
    Array[i].name=s; //members are assigned their values
    Array[i].vote=v;
    
    
  4. To count the total number of votes, sum all the votes received.

    要计算总票数,请将所有收到的票数相加。

  5. For i=0:4
    total_vote+= array[i].vote
    End for loop
    
    
  6. To calculate percentage vote received for each of the candidates, divide no of votes received by total_vote& make percentage. (Take care of integer division, result may reflect 0, if you change the order, first multiply with 100 or use floating calculation).

    要计算每个候选人获得的投票百分比,请将获得的投票数除以total_vote&make百分比。 (请注意整数除法,结果可能反映为0,如果更改顺序,请先乘以100或使用浮点计算)。

  7. For figure out the winner

    为了找出赢家

  8. Set max= INT_MIN, count=0;
    for i=0:4
    Find the max vote received by any candidate in the election
    End for loop
    
    For i=0:4
    if no of received vote for i th candidate==max
    Store i
    Increase count // no of winner candidate
    
    Print the name(s) of winner(s) using the stored indexes.
    
    

C ++实现 (C++ implementation)

#include <bits/stdc++.h>
using namespace std;

class candidate{
	public:
		string name;
		int vote;
};

void outputElection(candidate* arr){
	int total_vote=0;
	for(int i=0;i<5;i++){
		//finding total no of votes
		total_vote=total_vote+arr[i].vote; 
	}

	cout<<"result of the election............."<<endl;
	cout<<"name of candidate"<<"\t"<<"vote received"<<"\t"<<"percentage"<<endl;
	for(int i=0;i<5;i++){
		cout<<arr[i].name<<"\t\t\t";
		cout<<arr[i].vote<<"\t\t";
		cout<<(arr[i].vote*100)/total_vote<<"%"<<endl;
	}

	int max=INT_MIN,count=0;
	int index[5]={0};

	for(int i=0;i<5;i++){
		if(arr[i].vote>max){
			max=arr[i].vote;
		}
	}
	
	for(int i=0;i<5;i++){
		if(arr[i].vote==max){
			index[count]=i;
			count++;
		}
	}
	
	if(count==1)
		cout<<"The winner is "<<arr[index[count-1]].name<<endl;
	else{
		cout<<"There is tie between:"<<endl;
		for(int i=0;i<count-1;i++)
			cout<<arr[index[i]].name<<", ";
		cout<<arr[index[count-1]].name<<endl;
		cout<<"all are winner\n";
	}
	return ;
}

int main(){
	string s;
	int v;
	candidate arr[5];
	cout<<"enter candidates last name, there are five candidates\n";
	for(int i=0;i<5;i++){
		cout<<"enter candidate "<<i<<" last name\n";
		cin>>s;
		arr[i].name=s;
		cout<<"enter no of votes received by candidate "<<i<<endl;
		cin>>v;
		arr[i].vote=v;
	}
	outputElection(arr);
	return 0;
}


Output (first run):

输出(首次运行):

enter candidates last name, there are five candidates
enter candidate 0 last name
Peter
enter no of votes received by candidate 0
30
enter candidate 1 last name
Roy
enter no of votes received by candidate 1
20
enter candidate 2 last name
Ali
enter no of votes received by candidate 2
40
enter candidate 3 last name
Hales
enter no of votes received by candidate 3
60
enter candidate 4 last name
john
enter no of votes received by candidate 4
10
result of the election.............
name of candidate       vote received   percentage
Peter                   30              18%
Roy                     20              12%
Ali                     40              25%
Hales                   60              37%
john                    10              6%
The winner is Hales


Output (second run):

输出(第二次运行):

enter candidates last name, there are five candidates
enter candidate 0 last name
Morgan
enter no of votes received by candidate 0
25
enter candidate 1 last name
Wasim
enter no of votes received by candidate 1
15
enter candidate 2 last name
Stones
enter no of votes received by candidate 2
25
enter candidate 3 last name
Harris
enter no of votes received by candidate 3
15
enter candidate 4 last name
Enderson
enter no of votes received by candidate 4
20
result of the election.............
name of candidate       vote received   percentage
Morgan                  25              25%
Wasim                   15              15%
Stones                  25              25%
Harris                  15              15%
Enderson                20              20%
There is tie between:
Morgan, Stones
all are winner


翻译自: https://www.includehelp.com/cpp-programs/input-list-of-candidates-and-find-winner-of-the-election-based-on-received-votes.aspx

获取输入法候选列表

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值