链表c语言常见错误,C语言链表问题 出现段错误

#include

#include

#include

#include

#define MAXLEN 100

struct zahlen{

double wert;

struct zahlen *next;

};

int main(int argc, char *argv[])

{

FILE *datei;

char zeile[MAXLEN];

int nz;

double zahl;

int rc;

char *start;

struct zahlen *root, *curr;

double summe;

double mittelwert;

double max,min;

double standardabweichung;

if(argc < 2){

fprintf(stderr, "Fehlender Dateiname\n");

exit(1);

}

if(datei = fopen(argv[1], "r"), datei == NULL){

perror(argv[1]);

exit(1);

}

nz = 0;

while(fgets(zeile, MAXLEN, datei) != NULL){

start = zeile;

while(*start && strchr("0123456789", *start) == NULL)

start++;

rc = sscanf(start, "%lf", &zahl);

if(rc != 1)

continue;

printf("%d. Messwert: %7.3lf\n", nz+1, zahl);

nz++;

/*Dynamische Speichernutzung*/

if(root == NULL){

root = calloc(1, sizeof(struct zahlen));

if (root == NULL){

perror("calloc");

exit(1);

}

root->wert = zahl;

root->next = NULL;

curr = root;

}

else{

curr->next = calloc(1, sizeof(struct zahlen));

if(curr->next == NULL){

perror("calloc");

exit(1);

}

curr = curr->next;

curr->wert = zahl;

curr->next = NULL;

}

}

if(!feof(datei)){

perror(argv[1]);

exit(1);

}

printf("Das waren %d Messwerten\n", nz);

fclose(datei);

curr = root;

summe = 0;

while(curr != NULL){

summe += curr->wert;

curr = curr->next;

}

mittelwert = summe / (double)nz;

printf("Summe: %7.3lf\n", summe);

printf("Mittelwert: %7.3lf\n", mittelwert);

curr = root;

min = 0;

max = 0;

while(curr != NULL){

if(curr->wert > max)

max = curr->wert;

if(curr->wert < min)

min = curr->wert;

curr = curr->next;

}

printf("Max. Messwert: %7.3lf\n", max);

printf("Min. Messwert: %7.3lf\n", min);

curr = root;

standardabweichung = 0;

while(curr != NULL){

standardabweichung += pow((curr->wert - mittelwert), 2);

curr = curr->next;

}

standardabweichung = sqrt(standardabweichung / ((double)(nz - 1)));

printf("Standardabweichung: %7.3lf\n", standardabweichung);

return 0;

}

//如题,功能是读取文件中的数,每行都有一个数字,也会有单位

//因为题主在德国,所以有些变量设置用了德文单词,请见谅!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值