📜  投票系统的菜单驱动程序

📅  最后修改于: 2021-05-31 17:08:50             🧑  作者: Mango

在本文中,我们将编写一个菜单驱动程序来实现投票系统。该程序必须包含以下属性:

  • 投选票。
  • 显示每个候选人的票数。
  • 显示投票最多的候选人的姓名。

方法:请按照以下步骤解决问题:

  1. 向访问的人提供以下选项,如下所示:
    • 为您最喜欢的候选人投票。
    • 检查每个候选人的票数。
    • 检查领先的候选人,然后退出。
  2. 用户选择选项之一。
  3. 如果用户选择1,则显示候选者列表,并且用户现在可以从该候选者列表中进行选择。
  4. 如果用户选择2,则将显示候选者列表及其当前投票数。
  5. 如果用户选择3,则显示具有最大投票数的候选人的姓名。如果有多个候选人的最高票数,则显示一条错误消息,指出“无获胜者”。
  6. 该程序将继续直到用户选择0退出()为止。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Driver Code
int main()
{
    int choice, i, N;
  
    // Stores the names of candidates
    vector candidates
        = { "A", "B", "C", "D", "E" };
    N = candidates.size();
  
    // Stores the votes of candidates
    vector votes(N);
    do {
        cout << "\n1. Vote for your "
             << "favorite Candidate.\n";
        cout << "2. Check the number "
             << "of votes of each "
                "Candidate.\n";
        cout << "3. Check the candidate "
             << "who is leading.\n";
        cout << "0. Exit\n";
  
        // Take input of options
        cout << "Enter Your choice: ";
        cin >> choice;
        cout << "\n";
  
        // Switch Statement
        switch (choice) {
  
        case 1: {
            int candidatechoice;
  
            // Display the names of
            // all the candidates
            for (i = 0; i < N; i++)
                cout << i + 1 << "."
                     << candidates[i]
                     << "\n";
  
            cout << "Choose your candidate: ";
  
            // Taking user's vote
            cin >> candidatechoice;
            cout << "\n";
  
            // Update the vote of the
            // chosen candidate
            votes[candidatechoice - 1]++;
            break;
        }
        case 2: {
  
            // Display the name and votes
            // of each
            // candidate
            for (i = 0; i < N; i++)
                cout << i + 1 << "."
                     << candidates[i] << " "
                     << votes[i] << "\n";
            break;
        }
        case 3: {
            int mx = 0;
            string winner;
  
            // Find the candidate with
            // maximum votes
            for (int i = 0; i < N; i++)
                if (votes[i] > mx) {
                    mx = votes[i];
                    winner = candidates[i];
                }
            int flag = 0;
  
            // Check whether there are
            // more than one candidates
            // with maximum votes
            for (int i = 0; i < N; i
                   
                if (votes[i] == mx
                    && winner != candidates[i]) {
                flag = 1;
                break;
                }
            if (!flag)
                cout << "The current winner is "
                 << winner    << ".\n";
            else
                cout << "No clear winner\n";
        }
        default:
            "Select a correct option";
        }
    } while (choice != 0);
  
    return 0;
}


输出:

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”