声明在c中未正确终止

时间:2015-04-15 20:02:15

标签: c

我已经编写了这段代码,但是我得到了#34;声明被错误地终止了#34;行错误

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 个答案:

答案 0 :(得分:4)

如果您要在Windows下进行编译,则应注意windef.h(由windows.h包含)将minmax定义为宏。您可以在包含这些文件之前定义NOMINMAX来阻止这种情况,例如:

#define NOMINMAX

或者您可以简单地使用min以外的其他函数名称。

如果您在Windows上,可能仍然在某处定义min,在这种情况下您应该获取预处理器输出以查看位置,例如gcc -E ...

相关问题