-1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
typedef struct freqm {
int dfreq[100];
char word[10];
} freqm;
freqm fm[10];
int mat[10][10];
float sim[10][10];
int dsize[20];
int search(freqm fm[10], int t_cnt, char term[10]);
void print_cnt(freqm fm[10], int d_cnt, int t_cnt);
int read_files(int d_cnt);
void build_sim(int d_cnt, int t_cnt);
void print(int d_cnt);
void main() {
int d_cnt, c_cnt, t_cnt;
clrscr();
printf("\n\tenter no of documents");
scanf("%d", &d_cnt);
t_cnt = read_files(d_cnt);
print_cnt(fm, d_cnt, t_cnt);
build_sim(d_cnt, t_cnt);
print(d_cnt);
getch();
}
int read_files(int d_cnt) {
int flag = 0, i, j, t_cnt = 0, count;
char str[10], str1[10], input[10], term[10];
FILE *fp;
printf("\nenter ip prefix");
scanf("%s", input);
for (i = 1; i <= d_cnt; i++) {
strcpy(str1, input);
sprintf(str, "%d", i);
strcat(str1, str);
strcat(str1, ".txt");
fp = fopen(str1, "r");
while (fscanf(fp, "%s %d", term, &count) != EOF) {
flag = 0;
//fflush(stdin);
dsize[i] = dsize[i] + count;
if (i > 1)
flag = search(fm, t_cnt, term);
if (i == 1 || flag == -1) {
fm[t_cnt].dfreq[i] = count;
strcpy(fm[t_cnt].word, term);
t_cnt++;
} else
fm[flag].dfreq[i] = count;
}
}
return t_cnt;
}
void print_cnt(freqm fm[10], int d_cnt, int t_cnt) {
int i, j;
for (i = 0; i < t_cnt; i++) {
printf("\n%s", fm[i].word);
for (j = 1; j <= d_cnt; j++)
printf("\t%d", fm[i].dfreq[j]);
}
}
int search(freqm fm[10], int t_cnt, char term[10]) {
int i;
for (i = 0; i < t_cnt; i++)
if (strcmp(fm[i].word, term) == 0)
return i;
return -1;
}
void build_sim(int d_cnt, int t_cnt) {
int i, j, sum = 0, k;
float res = 0, res1, temp, temp1;
for (i = 1; i <= d_cnt; i++) {
temp = dsize[i];
for (j = i + 1; j <= d_cnt; j++) {
sum = 0;
temp1 = dsize[j];
for (k = 0; k < t_cnt; k++)
sum = sum + min(fm[k].dfreq[i], fm[k].dfreq[j]);
printf("sum=== %d", sum);
printf("t1 %d %d", temp, temp1);
//res1=(float)(sqrt(temp)*sqrt(temp1));
printf("\n%f", res1);
res = sum/sqrt(temp1) * sqrt(temp);
sim[i][j] = res;
}
}
}
void print(int d_cnt) {
int i, j;
for (i = 1; i <= d_cnt; i++) {
printf("\n");
for (j = 1; j <= d_cnt; j++)
printf("\t%f", sim[i][j]);
}
}
//input1.txt
T1 0
T2 3
T3 5
T4 1
//input2.txt
T1 5
T2 0
T3 0
T4 1
我正在爲單通道聚類算法編寫代碼。但在功能build_sim
即使temp
和temp1
的值是正的,即9和6,我也會得到domain_error。如何解決此問題?sqrt()函數中的c中的domain_error
我正在使用turboc。這是與turbo c有關的問題嗎?
最好把你的代碼縮短到一個可編譯/可運行的小片段,它描述了你得到的錯誤!沒有人會閱讀和糾正這個114行壓縮代碼 – chouaib
好的,謝謝你的建議chouaib – user3526905
'temp'和'temp1'的值不是9和6.你打印錯誤。 '%d'是整數。對於浮點數,你需要'%f'。 –