始终出现 segmentation fault, 不知道问题在哪? 求大神指教。
int main(){
FILE *fp;
double d;
double mean1;
double mean2;
double std1, std2;
if((fp = fopen("Table_1.txt", "rb")) == NULL)
{
return -1;
}
while (fscanf(fp, "%lf", &d)==1)
{
Add(d);
}
fclose(fp);
----------读取文件结束后,开始计算2个数值,mean和std----------------//
mean1 = count_mean(0);
mean2 = count_mean(1);//这里获取的数值不知道为什么不正确,测试时候发现,计算不了文件第二行的第一个数字'15.0'
//---------------这里是我读取的表---------------------------------
160 591 114 229 230 270 128 1657 624 1503
15.0 69.9 6.5 22.4 28.4 65.9 19.4 198.7 38.8 138.2
//--------------------------------------------------------------
std1 = count_std(mean1, 1);//segmentation就是报错这里,始终不知道问题是哪,并且计算出的结果也不对。
std2 = count_std(mean2, 2);
printf("Mean Std\n");
printf("%lf %lf\n", mean1, mean2);
printf("%lf %lf\n", mean2, std2);
return 0;
}
//以下是我的计算std的方法
double count_std(double mean, int column)
{
double std;
int count = 0;
double temp;
if(column = 1)
{
Reverse();
while(count < 10)
{
temp = temp + pow(mean - head -> data,2);
//printf("%lf\n", temp);
head = head -> next;
count++;
}
std = sqrt(temp/9);
}
else
{
while(count < 10)
{
temp = temp + pow(mean - head -> data,2);
head = head -> next;
count++;
}
std = sqrt(temp/9);
}
return std;
}
//以下是我计算mean的方法
double count_mean(int k)
{
double temp;
int count;
double mean;
int len = 10;
//
if(k == 0)
{
Reverse();
count = 0;
while(count < 10)
{
temp = temp + head -> data;
head = head -> next;
count++;
}
mean = temp/len;
}
else if(k == 1)
{
count = 0;
while(count < 10)
{
temp = temp + head -> data;
head = head -> next;
count++;
}
mean = temp/len;
}
return mean;
}
//---------这是一个链表结构的(double data, *next),添加方法---
void Add(double x)
{
struct Node *temp = malloc(sizeof(struct Node));
temp -> data = x;
temp -> next = head;
head = temp;
}