You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CS50 Pset3 Tideman代码无法编译,请求技术协助

Tideman代码编译失败问题排查与修复建议

我不清楚自己的Tideman代码为何无法编译,需要帮助与建议!以下是我的代码:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];

// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];

// Each pair has a winner, loser
typedef struct
{
    int winner;
    int loser;
}
pair;

// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];  //no. of pairs= n(n-1)/2
int pair_count;
int candidate_count;

// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);
bool hascycle(int winner, int loser); // 补充缺失的函数原型

int main(int argc, string argv[])
{
// Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: tideman [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] = argv[i + 1];
    }

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

    pair_count = 0;
    int voter_count = get_int("Number of voters: ");

    // Query for votes
    for (int i = 0; i < voter_count; i++)
    {
        // ranks[i] is voter's ith preference
        int ranks[candidate_count];

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            if (!vote(j, name, ranks))
            {
                printf("Invalid vote.\n");
                return 3;
            }
        }

        record_preferences(ranks);

        printf("\n");
    }

    add_pairs();
    sort_pairs();
    lock_pairs();
    print_winner();
    return 0;
}

// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
    // TODO

    for(int i=0; i < candidate_count; i++ )
    {
        if(strcmp(candidates[i],name)==0)
        {
            ranks[rank]=i;
            return true;
        }
    }
    return false;
}

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    // TODO

    for(int i=0; i < candidate_count; i++)
    {
        for(int j=i+1; j < candidate_count; j++)
        {
            preferences[ranks[i]][ranks[j]]++;
        }
    }
    return;
}

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    // TODO

    for(int i=0; i < candidate_count; i++)
    {
        for(int j=i+1; j < candidate_count; j++)
        {
            if (preferences[i][j] < preferences[j][i])
            {
                pairs[pair_count].winner= j;
                pairs[pair_count].loser= i;
                pair_count++;
            }

            else if (preferences[i][j]> preferences[j][i])
            {
                pairs[pair_count].winner= i;
                pairs[pair_count].loser= j;
                pair_count++;
            }

        }
    }
    return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    // TODO

    for(int i = pair_count-1; i >=0; i--)  // This nested loop is for bubble sort. Need i loop so that ith pair can start comparing. i.e after the 1st pair is done comparing with the rest of the pairs, the 2nd pair starts comparing with all other pairs so on&so forth)
    {                                      // Need start i from decreasing count so that each new pair will compare with lesser number of pairs as the pairs at the back has already been compared previously.
        for(int j=0; j <= i-1; j++)        // j loop is to compare ith pair with all other j pair.
        {
            if((preferences[pairs[j].winner][pairs[j].loser]-preferences[pairs[j].loser][pairs[j].winner]) < (preferences[pairs[j+1].winner][pairs[j+1].loser]-preferences[pairs[j+1].loser][pairs[j+1].winner]))
            {
                pair temp= pairs[j];
                pairs[j]= pairs[j+1];
                pairs[j+1]= temp;
            }
        }
    }
    return;
}

bool hascycle (int winner, int loser)
{
    for(int i=0; i < candidate_count; i++)
    {
        bool found= false;
        while(winner!=loser && winner!= -1)
        {
        if(locked[i][winner])
        {
            winner= i;
            found= true;
        }
        if(!found)
        winner= -1;
        }

        if(winner==loser)
        {
            return true;
        }

        return false;
    }
} // 补充缺失的闭合大括号


// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    // TODO

    for(int i=0; i < pair_count; i++)
    {
        if(!hascycle(pairs[i].winner, pairs[i].loser))
        locked[pairs[i].winner][pairs[i].loser] = true; // 修改数组访问语法
    }
    return;
}

// Print the winner of the election
void print_winner(void)
{
    // TODO

    for(int i=0; i< candidate_count; i++)

    {
        int false_count=0;

        for(int j=0; j<candidate_count; j++) // 声明变量j
        {
            if(locked[j][i]==false)
            {
                false_count++;

                if(false_count==candidate_count)
                {
                    printf("%s\n", candidates[i]);
                }
            }

        }
    }
    return;
}

编译检查结果:

✅ tideman.c 文件存在
❌ tideman 编译失败
⚠️ 当输入候选者姓名时vote函数返回true:需编译通过后才能检查
⚠️ 当输入无效姓名时vote函数返回false:需编译通过后才能检查
⚠️ vote函数正确设置第一顺位偏好:需编译通过后才能检查
⚠️ vote函数正确设置所有顺位偏好:需编译通过后才能检查
⚠️ record_preferences函数正确记录第一位选民的偏好:需编译通过后才能检查
⚠️ record_preferences函数正确记录所有选民的偏好:需编译通过后才能检查
⚠️ add_pairs函数在无平局时生成正确的配对数量:需编译通过后才能检查
⚠️ add_pairs函数在有平局时生成正确的配对数量:需编译通过后才能检查
⚠️ add_pairs函数用获胜配对填充数组:需编译通过后才能检查
⚠️ add_pairs函数不会用失败配对填充数组:需编译通过后才能检查
⚠️ sort_pairs函数按获胜优势降序排序配对:需编译通过后才能检查
⚠️ lock_pairs函数在无环时锁定所有配对:需编译通过后才能检查
⚠️ lock_pairs函数在最终配对会产生环时跳过该配对:需编译通过后才能检查
⚠️ lock_pairs函数在中间配对会产生环时跳过该配对:需编译通过后才能检查
⚠️ print_winner函数在有候选者全胜时输出胜者:需编译通过后才能检查
⚠️ print_winner函数在存在平局配对时输出胜者:需编译通过后才能检查

编译错误修复点:

  • hascycle函数缺失闭合大括号:在函数末尾添加},否则会导致语法结构混乱。
  • lock_pairs中数组访问错误:将locked(pairs[i].winner, pairs[i].loser)= true;改为locked[pairs[i].winner][pairs[i].loser] = true;,圆括号是函数调用语法,二维数组需要用方括号访问。
  • print_winner内层循环变量未声明:将for(j=0; j<candidate_count; j++)改为for(int j=0; j<candidate_count; j++),C语言要求变量必须先声明再使用。
  • 补充hascycle函数原型:在函数声明区域添加bool hascycle(int winner, int loser);,否则编译器无法识别该函数。

内容的提问来源于stack exchange,提问作者TSH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:01:06