学了c语言半年多,好不容易编个程序,结果不知为何程序老是出错,谢谢了
程序代码:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <conio.h> int N=0; #define NA 20 struct train_information { int id; //%5d int seat[100][NA]; //%1d int node; //%5d char name[20][NA]; int month; int day; struct Time { int hour; int min; }Time[NA]; struct train_information *next; }; void save_traininformation(struct train_information *head) { int i,j; struct train_information *p; FILE *fp; p=head; fp=fopen("D:\\车次信息.txt","w"); if(fp==NULL) { printf("error to open the file !\n"); return ; } fprintf(fp,"%2d",N); while(p!=NULL) { fprintf(fp,"%5d",p->id); fprintf(fp,"%5d",p->node); for(j=0;j<100;j++) { for(i=0;i<p->node+1;i++) { fprintf(fp,"%1d",p->seat[j][i]); } } for(i=0;i<p->node+2;i++) { fprintf(fp,"%20s",p->name[i]); } fprintf(fp,"%5d",p->month); fprintf(fp,"%5d",p->day); for(i=0;i<p->node+2;i++) { fprintf(fp,"%5d",p->Time[i].hour); fprintf(fp,"%5d",p->Time[i].min); } p=p->next; } fclose(fp); } void print(struct train_information *head) { int k; struct train_information *p; p=head; printf("列车的基本信息如下:\n"); while(p!=NULL) { printf("%d次列车 从%s出发 终点站%s\n",p->id,p->name[0],p->name[p->node+1]); printf("出发时间:%d月%d日\n",p->month,p->day); for(k=0;k<=p->node+1;k++) { printf("%s-- %d %d\n",p->name[k],p->Time[k].hour,p->Time[k].min); } p=p->next; } } struct train_information* duqu() { int j,i,count=0; FILE *fp; struct train_information *p,*pr,*head=NULL; p=pr=(struct train_information *)malloc(sizeof(struct train_information)); fp=fopen ("D:\\车次信息.txt","r"); fscanf(fp,"%2d",&N); printf("%d\n",N); while(count<N) { fscanf(fp,"%5d",&p->id); fscanf(fp,"%5d",&p->node); printf("%d",p->node); for(j=0;j<100;j++) { for(i=0;i<p->node+1;i++) { fscanf(fp,"%1d",&p->seat[j][i]); } } for(i=0;i<p->node+2;i++) { fscanf(fp,"%20s",&p->name[i]); } fscanf(fp,"%5d",&p->month); fscanf(fp,"%5d",&p->day); for(i=0;i<p->node+2;i++) { fscanf(fp,"%5d",&p->Time[i].hour); fscanf(fp,"%5d",&p->Time[i].min); } count++; if(count==1) { p->next=NULL; pr=p; head=p; } else { p->next=NULL; pr->next=p; pr=p; } p=(struct train_information *)malloc(sizeof(struct train_information)); } free(p); return head; } void input_train() { int j,k,n,w; FILE *fp; struct train_information *p,*pr,*head; p=pr=(struct train_information*)malloc(sizeof(struct train_information)); fp=fopen ("D:\\车次信息.txt","w"); if(fp==NULL) { printf("error to open the file!\n"); return ; } for(w=0;;w++) { system ("cls"); printf("写入第%d辆火车的信息:",w+1); printf("列车编号:"); scanf("%d",&p->id); printf("输入列车中途停靠站点数量:"); scanf("%d",&p->node); for(k=0;k<100;k++) { for(j=0;j<p->node+1;j++) { p->seat[k][j]=1; //train_information.seat表示座位状态 } } printf("输入列车始发站:"); scanf("%s",&p->name[0]); for(k=1;k<p->node+1;k++) { printf("输入列车的下一站:"); scanf("%s",&p->name[k]); } printf("输入列车终点站:"); scanf("%s",&p->name[p->node+1]); printf("输入列车出发时间:\n"); printf("month:"); scanf("%d",&p->month); printf("day:"); scanf("%d",&p->day); for(k=0;k<=p->node+1;k++) { printf("第%d站的出发时间:\n",k+1); printf("hour:"); scanf("%d",&p->Time[k].hour); printf("minute:"); scanf("%d",&p->Time[k].min); } if(w==0) { p->next=NULL; pr=p; head=p; } else { pr->next=p; pr=p; } printf("是否开始下一辆列车(0继续,1退出):"); scanf("%d",&n); if(n==1) { break; } p=(struct train_information *)malloc(sizeof(struct train_information)); } N=w+1; pr->next=NULL; save_traininformation(head); free(p); fclose(fp); } int main() { struct train_information *head; input_train(); head=duqu(); print(head); return 0; }