我写了这段代码,但是在这一行出现了“声明不正确”的错误。
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;
}
我无法调试这个问题。能否有人告诉我错误在哪里?
scanf("%.2lf", &threshold);
,scanf()
不允许在格式说明符中使用'.'
,并且指定 2 只会读取两个字符,而不是你想要的内容。你不能读取一个带有两位小数的数字,除非它是一个字符串,如果它是一个双精度浮点数,那么这没有太多意义。此外,你的main()
没有返回任何值,但是你的代码没有其他问题。 - Iharob Al Asimimain()
函数的返回类型从int
改为void
,但错误仍然存在。 - Ajay Ganvirmain()
必须返回int
,你只需要让main()
返回一个值,否则你的编译器可能有问题,你必须在问题中发布确切的错误信息,否则就没有帮助你的机会。 - Iharob Al Asimi