gpt4 book ai didi

c - 声明在 c 中错误终止

转载 作者:行者123 更新时间:2023-12-04 09:41:07 25 4
gpt4 key购买 nike

我写了这段代码,但我在第 行收到“声明错误终止”错误

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;
}

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

最佳答案

如果您在 Windows 下编译,您应该知道 windef.h(包含在 windows.h 中)定义了 minmax 作为宏。您可以通过在包含这些文件之前定义 NOMINMAX 来防止这种情况,例如:

#define NOMINMAX

或者您可以简单地使用不同于 min 的函数名称。

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

关于c - 声明在 c 中错误终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com