![](/img/trans.png)
[英]Why does -1 >> 1 and 0xFFFFFFFF >> 1 produce different results?
[英]Integrated Terminal and Debugger produce different results
目标:打印得票最高的名字
所以我现在调试我的 PSET3 Plurality 大概有 8 个多小时了。 我认为一切都已修复,因为调试器中的结果是我想要的,但是,在我的集成终端中,它产生的结果不同。
调试器结果:
plurality/ $ debug50 plurality ./plurality me duck you
.
Number of voters: 7
Vote: me
Vote: me
Vote: me
Vote: d
Invalid vote.
Vote: duck
Vote: duck
Vote: you
me
综合终端结果:
plurality/ $ ./plurality me duck you
Number of voters: 7
Vote: me
Vote: me
Vote: me
Vote: d
Invalid vote.
Vote: duck
Vote: duck
Vote: you
duck
plurality/ $
“我”应该赢了,但为什么“鸭子”赢了?
我的代码(我想制作一个最小的可重现示例,但我不知道如何,这是我能想到的最小的,对不起。一直向下滚动。 ):
#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;
//Swapping Function
void swap (char ** xptr, char ** yptr);
// 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");
}
}
// Display winner of election
print_winner();
}
// Update vote totals given a new vote
bool vote(string name)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(candidates[i].name, name) == 0)
{
candidates[i].votes += 1;
return true;
}
}
return false;
}
本节可能会帮助您...
// Print the winner (or winners) of the election
void print_winner(void)
{
// TODO
for (int z = 0; z < candidate_count - 1; z++)
{
int n = 0;
while (n < candidate_count - 2)
{
int a = candidates[n+1].votes;
int b = candidates[n+2].votes;
char *x = candidates[n+1].name; //me
char *y = candidates[n+2].name; //duck
if (a > b)
{
a = a + b; //Swapping techniques credits to GeekforGreeks.org & Childre'sTechVideo
b = a - b;
a = a - b;
swap(&x, &y);
}
candidates[n+1].votes = a;
candidates[n+2].votes = b;
candidates[n+1].name = x;
candidates[n+2].name = y;
n++;
}
}
int c = 0;
while (c != candidate_count - 1)
{
c++;
if (candidates[candidate_count-1].votes == candidates[c].votes)
{
printf("%s\n", candidates[c].name);
}
}
}
//Swap function
void swap (char ** xptr, char ** yptr)
{
char * tmp = NULL;
tmp = *xptr;
*xptr = *yptr;
*yptr = tmp;
}
没有必要尝试排序...只需找到最高值并打印达到该数量的尽可能多的候选人。
int maxVotes = -1;
for (int z = 0; z < candidate_count; z++)
if( candidates[z].votes > maxVotes )
maxVotes = candidates[z].votes;
for (int z = 0; z < candidate_count; z++)
if( candidates[z].votes == maxVotes )
printf( "%s - %d votes\n", candidates[z].name, candidates[z].votes );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.