C.CS50 纯度分配,打印选举的获胜者
C.CS50 Purality assignment , printing the winner of an election
最近几天我一直在尝试调试这个程序,但我仍然找不到解决方案,所以我希望你能帮助我。
这个程序应该打印选举的获胜者。我只负责实现 print_winner
和投票功能。
如果输入的名字与选举中候选人的名字之一相匹配(候选人是命令行参数),那么投票函数应该更新该候选人的投票总数以说明新的投票和return 是的。如果不是,它应该 return 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;
}
您的代码存在一些问题:
- 函数
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);
}
}
}
最近几天我一直在尝试调试这个程序,但我仍然找不到解决方案,所以我希望你能帮助我。
这个程序应该打印选举的获胜者。我只负责实现 print_winner
和投票功能。
如果输入的名字与选举中候选人的名字之一相匹配(候选人是命令行参数),那么投票函数应该更新该候选人的投票总数以说明新的投票和return 是的。如果不是,它应该 return 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;
}
您的代码存在一些问题:
- 函数
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);
}
}
}