最近几天我一直在尝试调试该程序,但是我仍然找不到解决方案,因此希望您能为我提供帮助。
该程序应打印选举的获胜者。我只被分配用于实现print_winner
和表决功能。
如果输入的名称与选举中候选人的姓名之一(候选人是命令行参数)匹配,则表决功能应更新该候选人的投票总数以计入新的投票并返回true。如果不是,则应返回false。问题似乎只是在计票时,布尔值部分工作正常。
print_winner
函数应打印出在选举中获得最多选票的候选人的姓名,然后打印换行符。
这是我的代码:
#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, int argc, string argv[]);
void print_winner(string name, string argv[], int argc, int voter);
int main(int argc, string argv[]) {
string name;
// 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; i++) {
name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name, argc, argv)) {
printf("Invalid vote.\n");
}
}
// Display winner of election
print_winner(name, argv, argc, voter_count);
}
// Update vote totals given a new vote
bool vote(string name, int argc, string argv[]) {
// TODO
int voter_count = argc - 1;
int i, j;
candidate f[candidate_count];
for (i = 0; i < voter_count; i++) {
f[i].name = name;
f[i].votes = 0;
for (j = 1; j < argc; j++) {
if (strcmp(name, argv[j]) == 0) {
return true;
f[i].votes = 1;
}
}
}
for (i = 0; i < voter_count; i++) {
for (j = 0; j < voter_count; j++) {
if (i != j) {
if (strcmp(f[i].name, f[j].name) == 0) {
f[i].votes = f[i].votes + 1;
}
}
}
}
return false;
}
// Print the winner (or winners) of the election
void print_winner(string name, string argv[], int argc, int voter) {
// TODO
int voter_count = argc - 1;
int i, j;
candidate f[candidate_count];
for (i = 0; i < voter_count; i++) {
f[i].name = name;
f[i].votes = 0;
for (j = 1; j < argc; j++) {
if (strcmp(name, argv[j]) == 0) {
f[i].votes = 1;
}
}
}
for (i = 0; i < voter_count; i++) {
for (j = 0; j < voter_count; j++) {
if (i != j) {
if (strcmp(f[i].name, f[j].name) == 0) {
f[i].votes = f[i].votes + 1;
}
}
}
}
int max = f[0].votes;
j = 0;
for (i = 1; i < voter_count; i++) {
if (f[i].votes > max) {
max = f[i].votes;
j = i;
}
}
for (i = 0; i < voter_count; i++) {
if (max == f[i].votes) {
printf("%s\n", f[i].name);
}
}
return;
}
答案 0 :(得分:1)
这是您的代码中的一些问题:
vote()
和print_winner()
不应采用argc
和argv
,而应在全局数组candidates
上运行。vote()
和print_winner()
中的代码太复杂,无法处理任何有意义的数据。这是修改后的版本:
#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_winners(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; i++) {
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name)) {
printf("Invalid vote: %s\n", name);
}
}
// Display winner of election
print_winners();
return 0;
}
// Update vote totals given a new vote
bool vote(string name) {
for (int i = 0; i < candidate_count; i++) {
if (strcmp(candidates[i].name, name) == 0) {
candidates[i].votes += 1;
return true;
}
}
return false; // no such candidate
}
// Print the winner (or winners) of the election
void print_winners(void) {
int max_votes = 0;
for (int i = 0; i < candidate_count; i++) {
if (candidates[i].votes > max_votes) {
max_votes = candidates[i].votes;
}
}
for (int i = 0; i < voter_count; i++) {
if (candidates[i].votes == max_votes) {
printf("%s\n", candidates[i].name);
}
}
}