该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
为了更加直观,我将很长的程序作了简化。已证实原程序将此简化程序中函数注释掉即无问题,且此简化程序报错与原程序相同。运行环境: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;
}