c语言用链表平均值和,有关链表求平均值的求助——运行期错误

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

为了更加直观,我将很长的程序作了简化。已证实原程序将此简化程序中函数注释掉即无问题,且此简化程序报错与原程序相同。运行环境:c-free ,win-tc也可以。

#include "Stdio.h"

#include "Conio.h"

#include "string.h"

#include "math.h"

#include "ctype.h"

#include "stdlib.h"

#include "stdio.h"

struct TIME{

int year;

int month;

int day;

}time; typedef struct{

char address[20];

struct TIME time;

int data;

}PM; typedef struct node{

char address[20];

struct TIME time;

int data;

struct node *next;

}NODE; int average_draw(NODE*head,PM *draw,int n);

NODE *create(PM a[],int n); main()

{

FILE *fp;

int count=0,n;

PM a[MAX],draw[30];

NODE *headdraw,*headdraw2;

if((fp=fopen("d:\\PM.txt","a+"))==NULL){

puts("Can't open file!\n");

getch();

exit(1);

}

while(!feof(fp)){

fscanf(fp,"%s%d%d%d%d",a[count].address,&a[count].time.year,&a[count].time.month,&a[count].time.day,&a[count].data);

count++;

}

fclose(fp);

count--;

headdraw2=create(a,count);

n=average_draw(headdraw2,draw,30);

getch();

} NODE *create(PM a[],int n)

{

NODE*head=NULL,*p1=NULL,*p2=NULL;

int i,t=0;

for(i=0;i

t++;

p1=(NODE*)malloc(sizeof(NODE));

strcpy(p1->address,a[i].address);

(p1->time).year=a[i].time.year;

(p1->time).month=a[i].time.month;

(p1->time).day=a[i].time.day;

p1->data=a[i].data;

p1->next=NULL;

if(t==1)

head=p2=p1;

else{

p2->next=p1;

p2=p1;

}

}

return head;

} int average_draw(NODE*head,PM *draw,int n)

{

char city[20]={' '};

int i=0,j=2;

NODE*p1=NULL,*p2=NULL;

if(head==NULL){

printf("\nlist null!\n");

getch();

return 0;

}

while(head!=NULL&&i

p1=head;

p2=head;

while(p1!=NULL){

if(isspace(city[0])){

strcpy(city,p1->address);

draw[i].data=p1->data;

strcpy(draw[i].address,p1->address);

head=p1->next;

free(p1);

p1=head->next;

p2=head;

}

else if(strcmp(city,p1->address)==0){

draw[i].data+=((p1->data)-draw[i].data)/j;

j++;

p2->next=p1->next;

free(p1);

p1=p2->next;

}

else{

p2=p1;

p1=p1->next;

}

}

i++;

j=1;

city[0]=' ';

}

return i;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值