CS50 plurality问题:check50检测print_winner函数异常求助
CS50 Plurality任务print_winner函数异常问题排查
完成CS50的plurality(多数决)编程任务时,自行编写的C语言代码手动测试正常,但在check50自动检测中,print_winner函数无法正确识别Bob、Charlie为单独获胜者,其余测试项均通过,调试器无法查看候选者的票数与名称,需要定位问题。
任务要求
- 程序需统计得票最多的候选人,无效选票需提示
- 若多位候选人得票相同,则全部输出
代码实现
#include <cs50.h> #include <stdio.h> #include <string.h> // Max number of candidates #define MAX 9 // Candidates have name and vote count typedef struct { string name; int votes; } candidate; // Array of candidates candidate candidates[MAX]; // Number of candidates int candidate_count; // Function prototypes bool vote(string name); void print_winner(void); int main(int argc, string argv[]) { // Check for invalid usage if (argc < 2) { printf("Usage: plurality [candidate ...]\n"); return 1; } // Populate array of candidates candidate_count = argc - 1; if (candidate_count > MAX) { printf("Maximum number of candidates is %i\n", MAX); return 2; } for (int i = 0; i < candidate_count; i++) { candidates[i].name = argv[i + 1]; candidates[i].votes = 0; } int voter_count = get_int("Number of voters: "); // Loop over all voters for (int i = 0; i < voter_count;) { string name = get_string("Vote: "); // Check for invalid vote if (!vote(name)) { printf("Invalid vote.\n"); } else { i++; } } // Display winner of election print_winner(); } // Update vote totals given a new vote bool vote(string name) { for (int i = 0; i < candidate_count; i++) { if (strcmp(name, candidates[i].name) == 0) { candidates[i].votes = candidates[i].votes + 1; return true; } } return false; } // Print the winner (or winners) of the election void print_winner(void) { char t[100]; for (int i = 0; i < candidate_count; i++) { for (int j = 0; j < candidate_count - i - 1; j++) { if (candidates[j].votes < candidates[j + 1].votes) { int temp = candidates[j].votes; candidates[j].votes = candidates[j + 1].votes; candidates[j + 1].votes = temp; strcpy(t, candidates[j].name); strcpy(candidates[j].name, candidates[j + 1].name); strcpy(candidates[j + 1].name, t); } } } printf("%s\n", candidates[0].name); for(int k = 1; k < candidate_count; k++) { if (candidates[0].votes == candidates[k].votes) { printf("%s\n", candidates[k].name); } } return; }
check50检测结果
:) plurality.c exists :) plurality compiles :) vote returns true when given name of first candidate :) vote returns true when given name of middle candidate :) vote returns true when given name of last candidate :) vote returns false when given name of invalid candidate :) vote produces correct counts when all votes are zero :) vote produces correct counts after some have already voted :) vote leaves vote counts unchanged when voting for invalid candidate :) print_winner identifies Alice as winner of election :( print_winner identifies Bob as winner of election print_winner function did not print winner of election :( print_winner identifies Charlie as winner of election print_winner function did not print winner of election :) print_winner prints multiple winners in case of tie :) print_winner prints all names when all candidates are tied
补充说明:数组t有100个元素,不会出现段错误,此为非问题点。
问题分析与解决
问题根源
print_winner函数中排序候选人时,使用strcpy操作candidates[j].name是错误的。因为candidates[j].name指向的是argv传入的只读字符串常量,对其执行strcpy写入操作会触发未定义行为,导致程序运行异常,这就是check50无法识别Bob、Charlie为单独获胜者的原因。
修复方案
排序时不需要复制字符串内容,直接交换name指针即可:
// Print the winner (or winners) of the election void print_winner(void) { for (int i = 0; i < candidate_count; i++) { for (int j = 0; j < candidate_count - i - 1; j++) { if (candidates[j].votes < candidates[j + 1].votes) { // 交换票数 int temp_votes = candidates[j].votes; candidates[j].votes = candidates[j + 1].votes; candidates[j + 1].votes = temp_votes; // 交换name指针,而非复制字符串 string temp_name = candidates[j].name; candidates[j].name = candidates[j + 1].name; candidates[j + 1].name = temp_name; } } } printf("%s\n", candidates[0].name); for(int k = 1; k < candidate_count; k++) { if (candidates[0].votes == candidates[k].votes) { printf("%s\n", candidates[k].name); } } return; }
额外说明
手动测试时可能未触发未定义行为的显性错误,但check50的测试环境会严格检测这类内存操作问题,导致函数输出异常。修改后即可通过所有测试项。
内容的提问来源于stack exchange,提问作者Bulat_Ishet_otvety
相关产品推荐
相关产品推荐

