[英]How should I fix these garbage values?
我是C語言的學徒,想在這里學習一些東西。 我遇到了一個問題,我希望我能自己解決它。 但是,我是一個矮小的包裝工,很容易拖延,所以我需要你的大力支持。 正如標題明確指出的那樣,我不知道為什么我的 topMatches() function 會吐出一些隨機浮點值,在 scores[] 數組的開頭和結尾打印出來。 像這樣,
Output #1
(-0.00000, Lisa Rose),(0.99124, Gene Seymour),(0.92447, Michael Phillips),(0.89341, Claudia Puig),(0.66285, Mick LaSalle),(0.38125, Jack Matthews),(-1.00000, Toby)
Output #2
(107185664961793568883398204719104.00000, Lisa Rose),(0.99124, Gene Seymour),(0.92447, Michael Phillips),(0.89341, Claudia Puig),(0.66285, Mick LaSalle),(0.38125, Jack Matthews),(-1.00000, Toby)
Output #3
(0.99124, Lisa Rose),(0.92447, Gene Seymour),(0.89341, Michael Phillips),(0.66285, Claudia Puig),(0.38125, Mick LaSalle),(-118195603315995709432961818167345152.00000, Jack Matthews),(-1.00000, Toby)
...
該值應在 -1 和 1 的范圍內。非常感謝您的反饋。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
//data types
struct mInfo
{
char mName[20];
float rating;
};
struct cInfo
{
char name[20];
struct mInfo movi[7];
};
//prototype fxns
typedef double (*sim_fp)(struct cInfo *, const char *, const char *);
double sim_D(struct cInfo *prefs, const char *person1, const char *person2);
void topMatches(sim_fp fp, struct cInfo *prefs, const char *person1, int num);
int cmpFxn (const void * a, const void * b);
void reverseFxn(float arr[], int num);
int main()
{
int num = 7;
struct cInfo critics[num];
critics[0] = (struct cInfo) {"Lisa Rose", {"Lady in the Water", 2.5, "Snakes on a Plane", 3.5, "Just My Luck", 3, "Superman Returns", 3.5, "The Night Listener", 3, "You, Me and Dupree", 2.5}};
critics[1] = (struct cInfo) {"Gene Seymour",{"Lady in the Water", 3, "Snakes on a Plane", 3.5, "Just My Luck", 1.5, "Superman Returns", 5, "The Night Listener", 3, "You, Me and Dupree", 3.5}};
critics[2] = (struct cInfo) {"Michael Phillips",{"Lady in the Water", 2.5, "Snakes on a Plane", 3, "Superman Returns", 3.5, "The Night Listener", 4}};
critics[3] = (struct cInfo) {"Claudia Puig",{"Snakes on a Plane", 3.5, "Just My Luck", 3, "Superman Returns", 4, "The Night Listener", 4.5, "You, Me and Dupree", 2.5}};
critics[4] = (struct cInfo) {"Mick LaSalle",{"Lady in the Water", 3, "Snakes on a Plane", 4, "Just My Luck", 2, "Superman Returns", 3, "The Night Listener", 3, "You, Me and Dupree", 2}};
critics[5] = (struct cInfo) {"Jack Matthews",{"Lady in the Water", 3, "Snakes on a Plane", 4, "Superman Returns", 5, "You, Me and Dupree", 3.5}};
critics[6] = (struct cInfo) {"Toby",{"Snakes on a Plane", 4.5, "Superman Returns", 4, "You, Me and Dupree",1}};
topMatches(sim_D, critics, "Toby", 7);
return 0;
}
double sim_D(struct cInfo *prefs, const char *person1, const char *person2)
{
int i=0;
int x=0;
int next=0;
int p1;
int p2;
float X = 0;
float Y = 0;
float sumSq1 = 0;
float sumSq2 = 0;
float pSum = 0;
float num = 0;
float den = 0;
float Pscore = 0;
int nElements =0;
for (i=0;i<7;i++) {
if(strcmp(prefs[i].name, person1) ==0)
{
p1 = i;
}
else if(strcmp(prefs[i].name, person2) ==0)
{
p2 = i;
}
}
for (x=0;x<7;x++) {
for (next=0;next<7;next++)
{
if (!prefs[p1].movi[x].rating && !prefs[p2].movi[next].rating);
else if (strcmp(prefs[p1].movi[x].mName, prefs[p2].movi[next].mName) == 0)
{
X += prefs[p1].movi[x].rating;
Y += prefs[p2].movi[next].rating;
sumSq1 += pow(prefs[p1].movi[x].rating,2);
sumSq2 += pow(prefs[p2].movi[next].rating,2);
pSum += (prefs[p1].movi[x].rating*prefs[p2].movi[next].rating);
nElements++;
}
}
next = 0;
}
num = pSum-(X*Y/nElements);
den=sqrt((sumSq1-pow(X,2)/nElements)*(sumSq2-pow(Y,2)/nElements));
if(den ==0) return -1;
Pscore = num/den;
return Pscore;
}
void topMatches(sim_fp fp, struct cInfo *prefs, const char *person1, int num)
{
float scores[8];
char *buf;
int i=0;
for (i=0;i<num;i++)
{
if(strcmp(person1, prefs[i].name)==0)
{
continue;
}
scores[i] = fp(prefs, person1, prefs[i].name);
}
qsort(scores, num, sizeof(float), (*cmpFxn));
reverseFxn(scores, num);
printf("\n\n");
for(i=0;i<num;i++)
{
if (i == num-1)
{
printf("(%.5f, %s)", scores[num-1], prefs[num-1].name);
}
else
{
printf("(%.5f, %s),", scores[i], prefs[i].name);
}
}
}
void reverseFxn(float arr[], int num)
{
float scoresTmp[num];
int j;
for(j=0;j<num;j++)
{
scoresTmp[num-1-j] = arr[j];
}
for(j=0;j<num;j++)
{
arr[j]=scoresTmp[j];
}
}
int cmpFxn (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
scores[] 數組的每個元素的值需要在 -1 到 1 的范圍內。
struct cInfo
你有一個成員struct mInfo movi[7];
但在main()
中分配其中的 3 到 6 個。 通過從賦值切換到初始化,那些未顯式設置的記錄將被零初始化。 這似乎足以獲得一致的 output。int main() {
struct cInfo critics[] = {
{"Lisa Rose", {
{"Lady in the Water", 2.5},
{"Snakes on a Plane", 3.5},
{"Just My Luck", 3},
{"Superman Returns", 3.5},
{"The Night Listener", 3},
{"You, Me and Dupree", 2.5}
}},
{"Gene Seymour", {
{"Lady in the Water", 3},
{"Snakes on a Plane", 3.5},
{"Just My Luck", 1.5},
{"Superman Returns", 5},
{"The Night Listener", 3},
{"You, Me and Dupree", 3.5}
}},
{"Michael Phillips", {
{"Lady in the Water", 2.5},
{"Snakes on a Plane", 3},
{"Superman Returns", 3.5},
{"The Night Listener", 4}
}},
{"Claudia Puig", {
{"Snakes on a Plane", 3.5},
{"Just My Luck", 3},
{"Superman Returns", 4},
{"The Night Listener", 4.5},
{"You, Me and Dupree", 2.5}
}},
{"Mick LaSalle", {
{"Lady in the Water", 3},
{"Snakes on a Plane", 4},
{"Just My Luck", 2},
{"Superman Returns", 3},
{"The Night Listener", 3},
{"You, Me and Dupree", 2}
}},
{"Jack Matthews", {
{"Lady in the Water", 3},
{"Snakes on a Plane", 4},
{"Superman Returns", 5},
{"You, Me and Dupree", 3.5}
}},
{"Toby", {
{"Snakes on a Plane", 4.5},
{"Superman Returns", 4},
{"You, Me and Dupree",1}
}}
};
topMatches(sim_D, critics, "Toby", sizeof critics / sizeof *critics);
}
這是示例 output:
(0.99124, Lisa Rose),(0.92447, Gene Seymour),(0.89341, Michael Phillips),(0.66285, Claudia Puig),(0.38125, Mick LaSalle),(0.00000, Jack Matthews),(-1.00000, Toby)
if (!prefs[p1].movi[x].rating && !prefs[p2].movi[next].rating);
else if (strcmp(prefs[p1].movi[x].mName, prefs[p2].movi[next].mName) == 0) {
到:
if (
(prefs[p1].movi[x].rating || prefs[p2].movi[next].rating) &&
!strcmp(prefs[p1].movi[x].mName, prefs[p2].movi[next].mName)
) {
如果您引入幾個虛榮變量,它可以寫得更緊湊。
float
替換為double
並修復排序 function:int cmpFxn (const void * a, const void * b) {
double a2 = *(double *) a;
double b2 = *(double *) b;
if(a2 < b2) return -1;
if(a2 > b2) return 1;
return 0;
}
double scores[8];
. 您只迭代num
,即7
,並且您沒有為對應於 person1 的條目設置 scores[i] 所以我建議您這樣做: double scores[num];
memset(scores, 0, sizeof(scores));
sizeof a / sizeof *a
(小心 arrays 在作為參數傳遞時降級為指針) .至少這些問題:
錯誤比較 function
使用float
和cmpFxn
數組調用qsort()
,但cmpFxn()
比較int
。 *1
需要投射到float
。 *2
// return (*(int*)a - *(int*)b )
return (*(float*)a > *(float*)b ) - (*(float*)a < *(float*)b );
不要不使用return (*(float*)a - *(float*)b )
因為float
差異可能會溢出int
范圍或截斷為 0。
代碼風險sqrt(some_negative)
注意輕微的浮點偽影,這些偽影會導致數學上永遠不應小於 0 的負數。
// den=sqrt((sumSq1-pow(X,2)/nElements)*(sumSq2-pow(Y,2)/nElements));
t = (sumSq1-pow(X,2)/nElements)*(sumSq2-pow(Y,2)/nElements);
den = t >= 0.0 ? sqrt(t) : 0;
float
與double
代碼隨意混合float
和double
對象和函數。 建議只double
。
出於調試目的,很容易使用#define float double
來臨時執行此操作。
*1
cmpFxn()
也是int
的弱比較 function,因為它有int
溢出的風險。
// return ( *(int*)a - *(int*)b );
return (*(int*)a > *(int*)b ) - (*(int*)a < *(int*)b );
*2
如果涉及非數字,則比較更加復雜。 讓我們暫時假設情況並非如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.