2017-01-17 24 views
0

對於作業問題感到遺憾,我知道有些人不喜歡他們,但我很茫然。 我已經寫了一個代碼,根據我的任務,有用戶輸入他們想投票等,這一切都有效。第二部分的任務是修改它,所以如果兩個候選人有相同的投票,而不是在第三個是「...」第二個是「...」(第三個是第三個) ...「第二個輸出是」... + ...「第一個是」...「以相同的票數輸出兩位候選人

這是我的代碼,

class Program 
{ 
    static void Main(string[] args) 
    { 
     //create empty arrays 
     int[] votes = new int[5]; 
     string[] names = { "Ahmed", "Boo", "Celine", "Didi", "Elaine" }; 
     for (int i = 0; i < names.Length; i++) 
     { 
      Console.WriteLine("{0} {1}", i, names[i]); 
     } 

     //input votes 
     Input(votes); 

     //sort votes 
     Sort(votes, names); 

     if (votes[0] > 0)//If there are any votes for the top candidate then do not display the output 
     { 
      //output sorted data 
      Console.WriteLine("\nVote summary"); 
      Output(votes, names); 

      Console.WriteLine("\nIn third place: {0}", names[2]); 
      Console.WriteLine("In second place: {0}", names[1]); 
      Console.WriteLine("And the winner is: {0}", names[0]); 
     } 
     else 
     { 
      Console.WriteLine("No votes were made!"); 
     } 

     Console.ReadLine(); 
    }//end Main 

    static void Input(int[] arr) 
    { 
     int vote = EnterInt("Enter number of candidate you wish to vote for (0 to 4) or -1 to quit:"); 

     while (vote != -1) 
     { 
      if (vote < 0 || vote > 4) 
      { 
       Console.WriteLine("Invalid vote"); 
      } 
      else 
      { 
       arr[vote]++; 
      } 
      vote = EnterInt("Enter number of candidate you wish to vote for (0 to 4) or -1 to quit:"); 
     }//end while 
    }//end InputArray 


    public static void Sort(int[] votes, string[] names) 
    { 
     for (int pass = 1; pass < votes.Length; pass++) 
     { 
      int smallestPos = FindSmallest(votes, votes.Length - pass); 
      if (smallestPos != votes.Length - pass) 
      { 
       Swap(votes, smallestPos, votes.Length - pass); 
       Swap(names, smallestPos, votes.Length - pass); 
      } 
     }//end for 
    }//end Sort 

    public static int FindSmallest(int[] votes, int num) 
    { 
     int smallestPos = 0; 
     for (int i = 1; i <= num; i++) 
     { 
      if (votes[i] < votes[smallestPos]) 
      { 
       smallestPos = i; 
      } 
     }//end for 
     return smallestPos; 
    }//end FindSmallest 

    public static void Swap(int[] votes, int first, int second) 
    { 
     int temp = votes[first]; 
     votes[first] = votes[second]; 
     votes[second] = temp; 
    }//end Swap 

    public static void Swap(string[] names, int first, int second) 
    { 
     string temp = names[first]; 
     names[first] = names[second]; 
     names[second] = temp; 
    }//end Swap 

    public static void Output(int[] votes, string[] names) 
    { 
     for (int i = 0; i < votes.Length; i++) 
     { 
      Console.WriteLine("{0} {1}", names[i], votes[i]); 
     }//end for 
    }//end Output 


    static int EnterInt(string prompt) 
    { 
     Console.Write(prompt); 
     int num; 
     while (!int.TryParse(Console.ReadLine(), out num)) 
     { 
      Console.Write("Error! Please enter an integer number:"); 
     }//end while 
     return num; 
    } 
} 

謝謝你的幫助。

+2

如果你只在乎前三名則只是做比較,有4種可能性,前兩名分別是相等的,第二和第三都是平等的,所有3個都是平等的,或者一個都不相等。如果你想在包含一個或多個關係的情況下包含第四個和第五個候選者,它會變得更復雜一些。 – juharr

+0

如果您創建一個對象來容納所有內容(例如'類Candidate',其屬性爲'Name'和'Votes'),則不需要2次交換。至於任務有幾種方法,例如使用排序。或者您可以簡單地找到最大投票數的候選人,記住該數字,輸出所有具有相同數字的候選人(用+連接),找到下一個較低的最高票數,重複。 – Sinatr

+0

我可能會誤解這一點,但當然你可以添加一個if來比較[1]和[2],如果它們相等,那麼輸出你的第二個... + ...,如果他們不是那麼使用你現有的邏輯? –

回答

0

首先,我建議通過使您使用起來得心應手的LINQ功能,你使用單個集合來存儲所有候選人的名字和他們的投票數(由@Sinatr的意見建議),因爲它使代碼更簡單如.OrderBy()。

要做到這一點,你有很多選擇在您的處置,您可以用名稱創建自己的候選類投票屬性,或者您可以使用一個已經預定義的類型,如元組(字符串,int)字典,就像我在示例代碼中使用的那樣。

正如您已經注意到的那樣,這個問題的難點在於對​​三位候選人的選票排名進行排序,得票最多。我用過的方法是候選人得票數相同,然後將這些組從最高票數排序到最低票數。之後,我繼續檢索候選人的姓名,同時保持最大數量的姓名顯示在積分榜上,這是3。

我試着讓代碼儘可能簡單,同時保持您的結構大部分,請讓我知道你是否有任何疑問。

class Program 
{ 
    static Dictionary<string, int> candidates; //Make the variable accesible from any method for simplicity 
    static void Main(string[] args) 
    { 
     //initialize dictionary 
     candidates = new Dictionary<string, int>(); 
     candidates.Add("Ahmed", 0); 
     candidates.Add("Boo", 0); 
     candidates.Add("Celine", 0); 
     candidates.Add("Didi", 0); 
     candidates.Add("Elaine", 0); 

     //Print candidate names 
     for (int i = 0; i < candidates.Count; i++) 
     { 
      Console.WriteLine("{0} {1}", i, GetCandidate(i)); 
     } 

     //input votes 
     Input(); 

     //output votes if highest vote candidate has any 
     if (candidates.Max(c => c.Value) > 0) 
     { 
      Console.WriteLine("Vote summary"); 
      Output(); 

      //to arrange the standings we need to group the candidates to evaluate if they share the same amount of votes 
      //additionally I choose to skip the group of candidates who have 0 votes, as there is no point on displaying them 

      var grouped_candidates = candidates.GroupBy(c => c.Value).Where(g => g.Key > 0).OrderByDescending(g => g.Key); 

      //we can only display 3 names in the standings so we set a counter 
      int standing_spots = 3; 

      string firstplace = string.Empty, secondplace = string.Empty, thirdplace = string.Empty; 

      //retrieve winner(s) 
      var first_group = grouped_candidates.ElementAt(0); 
      var winner_names = first_group.Select(c => c.Key); 
      firstplace = GetStandings("In first place: ", standing_spots, winner_names); 

      //retrieve second position(s) 
      var second_group = grouped_candidates.ElementAtOrDefault(1); 
      if (second_group != null) 
      { 
       var second_names = second_group.Select(c => c.Key); 
       secondplace = GetStandings("In second place: ", standing_spots, second_names); 
      } 
      //retrieve third position(s) 
      var third_group = grouped_candidates.ElementAtOrDefault(2); 
      if (third_group != null) 
      { 
       var third_names = grouped_candidates.ElementAtOrDefault(2).Select(c => c.Key); 
       thirdplace = GetStandings("In third place: ", standing_spots, third_names); 
      } 

      //Print standings 
      if (!string.IsNullOrEmpty(thirdplace)) { Console.WriteLine(thirdplace); } 
      if (!string.IsNullOrEmpty(secondplace)) { Console.WriteLine(secondplace); } 
      Console.WriteLine(firstplace); 
     } 
     else 
     { 
      Console.WriteLine("No votes were made!"); 
     } 
     Console.ReadLine(); 
    } 
    static string GetStandings(string standing_txt, int standing_spots, IEnumerable<string> names) 
    { 
     if (standing_spots > 0) 
     { 
      if (names.Count() >= standing_spots) 
      { 
       standing_txt += string.Join("+", names.Take(standing_spots)); 
       standing_spots = 0; 
      } 
      else 
      { 
       standing_txt += string.Join("+", names); 
       standing_spots -= names.Count(); 
      } 
     } 
     return standing_txt; 
    } 
    static string GetCandidate(int index) 
    { 
     return candidates.ElementAt(index).Key; 
    } 
    static void Input() 
    { 
     int cand_num; 
     do 
     { 
      cand_num = EnterInt("Enter number of candidate you wish to vote for (0 to 4) or -1 to quit:"); 

      if (cand_num >= 0 && cand_num <= 4) 
      { 
       candidates[GetCandidate(cand_num)]++; //increment candidate vote value 
      } 
      else if (cand_num != -1) 
      { 
       Console.WriteLine("Invalid vote"); 
      } 

     } while (cand_num != -1); 
    } 
    public static void Output() 
    { 
     foreach (var candidate in candidates) 
     { 
      Console.WriteLine("{0} {1}", candidate.Key, candidate.Value); 
     } 
    } 
    static int EnterInt(string prompt) 
    { 
     Console.Write(prompt); 
     int num; 
     while (!int.TryParse(Console.ReadLine(), out num)) 
     { 
      Console.Write("Error! Please enter an integer number:"); 
     } 
     return num; 
    } 
} 
+0

謝謝。抱歉,我已經淹沒了最近的回覆 –

相關問題