C语言中声明错误的终止。

3

我写了这段代码,但是在这一行出现了“声明不正确”的错误。

int min(int a, int b){..}

以下是我编写的代码。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>


#define SIZE 10
#define WORD_SIZE 50
#define LIST_SIZE 500

typedef struct node
{
    char word[WORD_SIZE];
    int frequency[SIZE];
}node;
int no_of_words;

int filewords[SIZE];
int cluster[SIZE][SIZE];

void process(FILE *, node[], int);
void display(node[], int);
void generate_sim_mat(node [], double [][SIZE], int);
int build_cluster(double [][SIZE], int );
void disp(int);

int min(int a, int b)
{
    return a < b ? a : b;
}

int main()
{
    node word_list[LIST_SIZE];
    int num_files;
    int i;
    int cluster_count;
    FILE *fp;
    char filename[WORD_SIZE];
    double sim_mat[SIZE][SIZE];

    printf("Enter number of files:");
    scanf("%d", &num_files);

    for(i = 0; i < num_files; i++)
    {
        scanf("%s", filename);

        fp = fopen(filename, "r");
        if(!fp)
        {
            printf("Could not open file:%s\n", filename);
            continue;
        }
        printf("File %s opened, Now processing it\n", filename);

        process(fp, word_list, i);
        fclose(fp);
    }
display(word_list, num_files);
generate_sim_mat(word_list, sim_mat, num_files);
cluster_count = build_cluster(sim_mat, num_files);
disp(cluster_count);
}

void disp(int cc)
{
    int i, j;
    for(i = 0; i < cc; i++)
    {
        printf("%d---", i);
        for(j = 1; j <= cluster[i][0]; j++)
            printf("%d ", cluster[i][j]);
        printf("\n");
    }
}

void display(node word_list[], int fnos)
{
    int i, j;
    for(i = 0; i < no_of_words; i++)
    {
        printf("%s\t", word_list[i].word);
        for(j = 0; j < fnos; j++)
            printf("%d ", word_list[i].frequency[j]);
        printf("\n");
    }
}

void process(FILE *fp, node word_list[], int fno)
{
    char word[WORD_SIZE], ch;
    int i, j, k, word_count = 0;
    while((ch = getc(fp)) != EOF)
    {
        k = 0;
        while(isalpha(ch))
        {
            word[k++] = tolower(ch);
            ch = getc(fp);
        }
        if(k > 0)
        {
            word_count++;
            word[k] = '\0';
            for(i = 0; i < no_of_words; i++)
                if(strcmp(word_list[i].word, word) == 0)
                {
                    word_list[i].frequency[fno]++;
                    break;
                }
            if(i == no_of_words)
            {
                strcpy(word_list[i].word, word);
                for(j = 0; j < SIZE; j++)
                    word_list[i].frequency[j] = 0;
                word_list[i].frequency[fno] = 1;
                no_of_words++;
            }
        }
    }
    filewords[fno] = word_count;
}

void generate_sim_mat(node word_list[], double sim_mat[][SIZE], int fno)
{
    int i, j, k;
    int sum;
    for(i = 0; i < fno; i++)
    {
        for(j = 0; j < fno; j++)
        {
            sum = 0;
            for(k = 0; k < no_of_words; k++)
                sum += min(word_list[k].frequency[i], word_list[k].frequency[j]);

            sim_mat[i][j] = sum/(sqrt(filewords[i]) * sqrt(filewords[j]));            
            printf("%.2lf ", sim_mat[i][j]);
        }
        printf("\n");
    }
}

int build_cluster(double sim_mat[][SIZE], int fno)
{
    int cluster_count = 1;
    double threshold, sum, res;
    int i, j, k, l, flag;
    printf("Enter threshold value:");
    scanf("%.2lf", &threshold);

    cluster[0][0] = 1;
    cluster[0][1] = 0;

    for(i = 1; i < fno; i++)
    {
        flag = 0;
        for(j = 0; j < cluster_count; j++)
        {
            sum = 0;
            for(k = 1; k <= cluster[j][0]; k++)
                sum += sim_mat[cluster[j][k]][i];
            res = sum / cluster[j][0];
            if(res >= threshold)
            {
                flag = 1;
                l = ++cluster[j][0];
                cluster[j][l] = i;
            }
        }
        if(!flag)
        {
            cluster[j][0] = 1;
            cluster[j][1] = i;
            cluster_count++;
        }
    }
    return cluster_count;
}

我无法调试这个问题。能否有人告诉我错误在哪里?


1
你的代码编译正确,除了这个 scanf("%.2lf", &threshold);scanf() 不允许在格式说明符中使用 '.',并且指定 2 只会读取两个字符,而不是你想要的内容。你不能读取一个带有两位小数的数字,除非它是一个字符串,如果它是一个双精度浮点数,那么这没有太多意义。此外,你的 main() 没有返回任何值,但是你的代码没有其他问题。 - Iharob Al Asimi
我将 main() 函数的返回类型从 int 改为 void,但错误仍然存在。 - Ajay Ganvir
2
为什么你要这样做?这根本没有任何意义... main() 必须返回 int,你只需要让 main() 返回一个值,否则你的编译器可能有问题,你必须在问题中发布确切的错误信息,否则就没有帮助你的机会。 - Iharob Al Asimi
为什么不先声明min函数原型,就像你为其他函数所做的那样,然后再实现它并重新编译呢?虽然你的代码在没有这样做的情况下看起来还不错,但是你现在使用的编译器可能会对此有语法问题。试一试吧。 - Unavailable
是的,看起来min函数原型已经在库中定义过了,你之前因为这个而收到了那个错误。 - Unavailable
显示剩余2条评论
1个回答

4

如果您正在Windows下编译,需要注意的是windows.h引用的windef.h定义了minmax宏。您可以通过在包含这些文件之前定义NOMINMAX来避免此问题,例如:

#define NOMINMAX

或者你可以使用不同于min的函数名。

如果你不是在Windows系统上,可能仍然存在min被定义在其他地方的情况,这时你需要获取预处理器的输出来查看具体位置,例如使用gcc -E ...命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接