我是编程的初学者。目前正在学习edx上的CS50课程。我在第三周,必须编写一个简单的投票计数器,但是在第65行我得到了以下错误
plurality.c:65:25:错误:成员参考基类型'candidate9‘不是一个结构或联合合并(candidates.votes,0,MAX-1,candidates.name);
但是我的candidates.votes和candidates.name是数组不是吗?请帮我解决这件事我只是不知道怎么解决。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
void merge(int a[], int low, int mid, int high); //This function will merge the two sorted arrays
void mergesort(int a[], int low, int high, string name[]); //This function will merge the arrays, is recursive
// 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; i++)
{
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name))
{
printf("Invalid vote.\n");
}
}
mergesort(candidates.votes, 0, MAX-1, candidates.name);
// Display winner of election
print_winner();
}
// Update vote totals given a new vote
bool vote(string name)
{
// TODO
for(int i =0; i< MAX; i++)
{
if(candidates[i].name == name)
{
candidates[i].votes++;
return true;
}
}
return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
// TODO
int win = candidates.votes[MAX-1]
for(int i = MAX-2; i >=0; i--)
{
if(win != candidates[i].votes)
{
printf("%s",candidates[i].name);
return;
} else
{
printf("%s", candidates[i].name);
win = candidates[i].votes;
}
}
return;
}
void mergesort(int a[], int low, int high, string name[])
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
mergesort(a, low, mid);
mergesort(a, mid+1, high);
merge(a, low, mid, high, string name);
}
else
{
return;
}
}
void merge(int a[], int low, int mid, int high, string name)
{
int i, j, k, b[high], c[high];
i = low;
j = mid + 1;
k = low;
while(i <= mid && j <= high )
{
if(a[i] < a[j])
{
b[k] = a[i];
c[k] = name[i];
i++;
k++;
}
else
{
b[k] = a[j];
c[k] = name[j];
// printf("%i ", b[k]);
j++;
k++;
}
}
while(i <= mid)
{
b[k] = a[i];
c[k] = name[j];
//printf("%i ", b[k]);
k++;
i++;
}
while(j <= high)
{
//printf("%i ", b[k]);
b[k] = a[j];
c[k] = name[j];
k++;
j++;
}
for(i = low; i <= high ; i++)
{
a[i] = b[i];
name[i] = c[i];
// printf("%i", a[i]);
}
}发布于 2022-08-20 06:59:38
您至少有两个问题(可能更多,但我没有要编译的cs50头):
不要将字符串与==进行比较
if(candidates[i].name == name)相反,使用strcmp
if(strcmp(candidates[i].name, name) == 0)主要问题是:如果您没有一个ints和字符串数组,则无法将它们传递给一个函数。
typedef struct
{
string name;
int votes;
}
candidate;
// Array of candidates
candidate candidates[MAX];
...
mergesort(candidates.votes, 0, MAX-1, candidates.name);candidates.votes和candidates.name不是数组,您需要将candidates传递给mergesort函数,而不是
if(a[i] < a[j])使用类似的东西
if(candidates[i].votes < candidates[j].votes)如果作业不需要实现mergesort,请考虑使用qsort,因为它将大大简化工作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 9
// Candidates have name and vote count
typedef struct
{
char *name;
int votes;
} candidate;
// Sort by: votes (DESC order), name (ASC order)
int comp(const void *pa, const void *pb)
{
const candidate *a = pa;
const candidate *b = pb;
return
a->votes > b->votes ? -1 :
a->votes < b->votes ? +1 :
strcmp(a->name, b->name);
}
int main(void)
{
candidate candidates[MAX] =
{
{"Ingram", 1}, {"Macias", 5}, {"Sosa", 3}, {"Mayer", 3}, {"Walton", 2},
{"Christensen", 4}, {"Mckay", 1}, {"Carter", 6}, {"Fitzgerald", 3}
};
qsort(candidates, MAX, sizeof(candidate), comp);
for (size_t i = 0; i < MAX; i++)
{
printf("%d %s\n", candidates[i].votes, candidates[i].name);
}
return 0;
}输出:
6 Carter
5 Macias
4 Christensen
3 Fitzgerald
3 Mayer
3 Sosa
2 Walton
1 Ingram
1 Mckay发布于 2022-08-20 06:59:38
您已经将candidates数组声明为:
candidate candidates[MAX];然而,您正在尝试访问数组本身的votes和name成员,而不是单个元素。
mergesort(candidates.votes, 0, MAX-1, candidates.name);从mergesort的签名
void mergesort(int a[], int low, int high, string name[])您似乎需要将数组的每个成员的选票和名称值收集到新的数组中,然后将它们传递给函数。
int votes[MAX];
for (size_t i = 0; i < MAX; i++) {
votes[i] = candidates[i].votes;
}发布于 2022-08-20 09:51:49
您可以完全删除merge()和mergesort()代码。(不再有编译器报告或崩溃。)
只需声明一个全局计数器(真不敢相信我会这么建议!)
int maxCnt; // global data value will be 0在vote()内部,当候选人的人数增加时,会添加类似的内容。(还更正了名称匹配问题。)
if( strcmp( candidates[i].name, name ) == 0 )
{
candidates[i].votes++;
if( maxCnt < candidates[i].votes )
maxCnt = candidates[i].votes;
return true;
}然后,您可以直接进入print(),因为程序已经知道最高的记录(可能由多个候选人共享)。
printf( "Winner(s): " );
for( int i = 0; i < candidate_cnt; i++ )
if( candidates[i].votes == maxCnt )
printf( "%s ", candidates[i].name );
printf( "\n" );如果需要所有候选人的名单(按选票的降序排列)。这会在列表中首先打印出得票率最高的候选人,但也会发现最高的票数低于当前的票数。这重复,直到阈值最终下降到低于最后一位候选人。
int nextCnt = 0;
int thisCnt = maxCnt;
do {
nextCnt = -1;
for( i = 0; i < candidate_cnt; i++ ) {
int nVotes = candidates[i].votes;
if( nVotes == thisCnt )
printf( "%s -- %d\n", candidates[i].name, nVotes );
if( nVotes < thisCnt && nVotes > nextCnt )
nextCnt = nVotes;
}
thisCnt = nextCnt;
} while( nextCnt >= 0 ); https://stackoverflow.com/questions/73424606
复制相似问题