test_main_new.c
资源名称:C数据结构课程设计.rar [点击查看]
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:7k
源码类别:
控制台编程
开发平台:
C/C++
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- //#define SIZE 3
- //#define MAX 13
- #define OK 1
- #define TRUE 1
- //#define FALSE 0
- #define ERROR 0
- #define OVERFLOW -2
- typedef int status;
- typedef struct airline{
- char line_num[8];//航班号
- char plane_num[8];//飞机号
- char end_place[20];//目的的
- int total;//座位总数
- int left;//剩余座位
- struct airline *next;//下一个结点
- }airline;
- typedef struct customer{
- char name[9];//顾客名
- char line_num[8];//航班号
- int seat_num;//座位号
- struct customer *next;//下一个结点
- }customer;
- airline *init_airline(){//初始化链表
- airline *l;
- l=(airline*)malloc(sizeof(airline));
- if(l==NULL){
- exit(OVERFLOW);
- }
- l->next=NULL;
- return l;
- }
- customer * init_customer(){//初始化链表
- customer *l;
- l=(customer*)malloc(sizeof(customer));
- if(l==NULL){
- exit(OVERFLOW);
- }
- l->next=NULL;
- return l;
- }
- status insert_airline(airline **p,char *line_num,char *plane_num,char *end_place,int total,int left){//airline链表插入操作
- airline *q;
- q=(airline*)malloc(sizeof(airline));
- strcpy(q->line_num , line_num);
- strcpy(q->plane_num , plane_num);
- strcpy(q->end_place , end_place);
- q->total =total;
- q->left =left;
- q->next=NULL;
- (*p)->next=q;
- (*p)=(*p)->next;
- // printf("insert %d ,%dis succssed!n",e,bl);
- return OK;
- }
- status insert_customer(customer **p,char *name,char *line_num,int seat){//customer链表插入操作
- customer *q;
- q=(customer*)malloc(sizeof(customer));
- strcpy(q->name , name);
- strcpy(q->line_num , line_num);
- q->seat_num =seat;
- q->next=NULL;
- (*p)->next=q;
- (*p)=(*p)->next;
- // printf("insert %d ,%dis succssed!n",e,bl);
- return OK;
- }
- customer *delete_cus(customer *h,char *name)
- {
- customer *p,*pr,*q,*qr;
- qr=pr=h;
- p=pr->next ;
- q=qr->next ;
- while(p!=NULL)
- {
- if(strcmp(name,p->name )==0)
- {
- qr->next =q->next ;
- PR("delet %sn",q->name );
- return h;
- }
- qr=qr->next ;
- q=qr->next ;
- }
- PR("没有这用户,无法完成删除任务!n");
- return 0;
- }
- airline *modefy_airline(airline *l,char *line_num)
- {
- airline *q;
- q=l->next ;
- for(;q->next !=NULL;q=q->next )
- {
- if(strcmp(line_num,q->line_num )==0)
- {
- q->left ++;
- PR("modefy %sn",q->line_num );
- return l;
- }
- }
- PR("没有这个航线,无法完成修改任务!n");
- return 0;
- }*/
- void save()
- {
- FILE *fp;
- char filename[]="c:\air.dat";
- printf("%sn",filename);
- if((fp=fopen(filename,"wb"))==NULL)
- {
- printf("can not open file to writen");
- exit(0);
- }
- fprintf(fp,"%s",filename);
- }
- void save1()
- {
- FILE *fp;
- // char ch,name[30],*line;
- // int i=0;
- char filename[]="c:\data.dat";
- printf("%sn",filename);
- if((fp=fopen(filename,"wb"))==NULL)
- {
- printf("can not open filen");
- exit(0);
- }
- fprintf(fp,"%s",filename);
- // fprintf(fp,"%sn",filename);
- }
- /*status save_airline(airline *l)
- {
- FILE *fp_air;
- char filename[]="c:\air.txt";
- airline *p=l->next ;
- if((fp_air=fopen("filename","w"))==NULL)
- {
- printf("can not open file to write:%sn",filename);
- exit(0);
- }
- for(;p!=NULL;p=p->next )
- {
- // fprintf(fp_air,"%sn",filename);
- printf("%s,%s,%s,%d,%dn",p->line_num ,p->plane_num ,p->end_place ,p->total ,p->left );
- fputs(filename,fp_air);
- // fprintf(fp_air,"%s,%s,%s,%d,%dn",p->line_num ,p->plane_num ,p->end_place ,p->total ,p->left );
- }
- PR("air_dat write n");
- fclose(fp_air);
- return OK;
- }
- status save_customer(customer *l)
- {
- FILE *fp_customer;
- customer *p=l->next ;
- char filename[]="c:\customer.txt";
- if((fp_customer=fopen("filename","w"))==NULL)
- {
- printf("can not open file to write:%sn",filename);
- return ERROR;
- }
- for(;p!=NULL;p=p->next )
- {
- PR("opened %sn",filename);
- PR("%s,%s,%dn",p->name ,p->line_num ,p->seat_num );
- fprintf(fp_customer,"%s,%s,%d",p->name ,p->line_num ,p->seat_num );
- }
- PR("customer_dat write n");
- fclose(fp_customer);
- return OK;
- }
- int changStrInt(char *ch)
- {
- int a=1,b=0,c=0,i;
- for (i=strlen(ch)-1;i>=0;i--)
- {
- if (ch[i]<58&&ch[i]>47)
- {
- b=a*(ch[i]-48);
- a=a*10;
- c=c+b;
- }
- else
- {
- PR("%c 不合法,无法将此字符串转化为整形!n",ch[i]);
- return 0;
- }
- // printf("the c is %dn",c);
- }
- return c;
- }
- status load_airline(airline *l)
- {
- FILE *fp_airline;
- int flag=0,i=0;
- char ch;
- char line_num[8];//航班号
- char plane_num[8];//飞机号
- char end_place[20];//目的的
- char total_str[5];
- char left_str[5];
- int total;//座位总数
- int left;//剩余座位
- airline *p=l;
- char filename[50]="c:\airline.dat";
- if((fp_airline=fopen("filename","rb"))==NULL)
- {
- printf("can not open file to load:%sn",filename);
- return ERROR;
- }
- while(!feof(fp_airline))
- {
- ch=fgetc(fp_airline);
- if(ch!='#')
- {
- if(flag==0&&ch!=',')
- {
- line_num[i]=ch;
- i++;
- }
- else if(flag==1&&ch!=',')
- {
- plane_num[i]=ch;
- i++;
- }
- else if(flag==2&&ch!=',')
- {
- end_place[i]=ch;
- i++;
- }
- else if(flag==3&&ch!=',')
- {
- total_str[i]=ch;
- i++;
- }
- else if(flag==4&&ch!=',')
- {
- left_str[i]=ch;
- i++;
- }
- else if (ch==',')
- {
- flag++;
- i=0;
- }
- else
- {
- PR("错误n");
- return ERROR;
- }
- }
- else
- {
- flag=0;
- total=changStrInt(total_str);
- left=changStrInt(left_str);
- insert_airline(&p,line_num,plane_num,end_place,total,left);
- p=p->next ;
- }
- }
- fclose(fp_airline);
- return OK;
- }
- status load_customer(customer *l)
- {
- FILE *fp_customer;
- int flag=0,i=0;
- char ch;
- char name[9];
- char line_num[8];//航班号
- char seat_num_str[5];
- int seat_num;//座位
- customer *p=l;
- char filename[50]="c:\customer.dat";
- if((fp_customer=fopen("filename","rb"))==NULL)
- {
- printf("can not open file to load:%sn",filename);
- return ERROR;
- }
- while(!feof(fp_customer))
- {
- ch=fgetc(fp_customer);
- if(ch!='#')
- {
- if(flag==0&&ch!=',')
- {
- name[i]=ch;
- i++;
- }
- else if(flag==1&&ch!=',')
- {
- line_num[i]=ch;
- i++;
- }
- else if(flag==2&&ch!=',')
- {
- seat_num_str[i]=ch;
- i++;
- }
- else if (ch==',')
- {
- flag++;
- i=0;
- }
- else
- {
- PR("错误n");
- return ERROR;
- }
- }
- else
- {
- flag=0;
- seat_num=changStrInt(seat_num_str);
- insert_customer(&p,name,line_num,seat_num);
- p=p->next ;
- }
- }
- fclose(fp_customer);
- return OK;
- }
- status creat_airline(airline **l)
- {
- airline *p=*l;
- int i=0;
- char *line_num[3]={"bjnc01","bjsh02","shgz03"};
- char *plane_num[3]={"plane1","plane2","plane3"};
- char *end_place[3]={"南昌","上海","广州"};
- int total[3]={100,100,100};
- int left[3]={51,50,78};
- for (i=0;i<3;i++){
- insert_airline(&p,line_num[i],plane_num[i],end_place[i],total[i],left[i]);
- }
- return OK;
- }
- status creat_customer(customer **l)
- {
- customer *p=*l;
- int i=0;
- char *name[3]={"欧阳锦林","尹焕亮","付胜"};
- char *line_num[3]={"bjnc01","bjsh02","shgz03"};
- int seat_num[3]={1,5,10};
- for (i=0;i<3;i++){
- insert_customer(&p,name[i],line_num[i],seat_num[i]);
- }
- return OK;
- }*/
- void main()
- {
- /* airline *air=init_airline();
- customer *cus=init_customer();
- creat_airline(&air);
- creat_customer(&cus);
- save_airline(air);
- save_customer(cus);*/
- save();
- save1();
- }