![](/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.