test_main.c
资源名称:C数据结构课程设计.rar [点击查看]
上传用户:janny_wxd
上传日期:2010-02-03
资源大小:261k
文件大小:5k
源码类别:
控制台编程
开发平台:
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
- #define OPSETSIZE 7
- #define STACK_INIF_SIZE 50
- #define STACKINCREMENT 10
- #define PR printf
- //FILE *fp_airline,*fp_customer,*fp;
- 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;
- status init_airline(airline *l){//初始化链表
- l=(airline*)malloc(sizeof(airline));
- if(l==NULL){
- exit(OVERFLOW);
- }
- l->next=NULL;
- return OK;
- }
- status init_customer(customer *l){//初始化链表
- l=(customer*)malloc(sizeof(customer));
- if(l==NULL){
- exit(OVERFLOW);
- }
- l->next=NULL;
- return OK;
- }
- 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;
- }
- status save_airline(airline *l)
- {
- FILE *fp_airline;
- airline *p=l->next ;
- char filename[50]="c:\airline.dat";
- if((fp_airline=fopen("filename","wb"))==NULL)
- {
- printf("can not open file to write:%sn",filename);
- return ERROR;
- }
- for (;p->next !=NULL;p=p->next ){
- if(fwrite(p,sizeof(airline),1,fp_airline)!=1)
- {
- PR("airline_dat write errorn");
- fclose(fp_airline);
- return ERROR;
- }
- }
- fclose(fp_airline);
- return OK;
- }
- status save_customer(customer *l)
- {
- FILE *fp_customer;
- customer *p=l->next ;
- char filename[50]="c:\customer.dat";
- PR("save_customern");
- if((fp_customer=fopen("filename","wb"))==NULL)
- {
- printf("can not open file to write:%sn",filename);
- return ERROR;
- }
- for (;p->next !=NULL;p=p->next ){
- if(fwrite(p,sizeof(customer),1,fp_customer)!=1)
- {
- PR("customer_dat write errorn");
- fclose(fp_customer);
- return ERROR;
- }
- }
- fclose(fp_customer);
- return OK;
- }
- status load_airline(airline *l)
- {
- FILE *fp_airline;
- airline *p=l->next ;
- char filename[50]="c:\airline.dat";
- if((fp_airline=fopen("filename","rb"))==NULL)
- {
- printf("can not open file to load:%sn",filename);
- return ERROR;
- }
- for (;p->next !=NULL;p=p->next ){
- if(fread(p,sizeof(airline),1,fp_airline)!=1)
- {
- PR("airline_dat load errorn");
- fclose(fp_airline);
- return ERROR;
- }
- }
- fclose(fp_airline);
- return OK;
- }
- status load_customer(customer *l)
- {
- FILE *fp_customer;
- customer *p=l->next ;
- char filename[50]="c:\customer.dat";
- if((fp_customer=fopen("filename","rb"))==NULL)
- {
- printf("can not open file to load:%sn",filename);
- return ERROR;
- }
- for (;p->next !=NULL;p=p->next ){
- if(fread(p,sizeof(customer),1,fp_customer)!=1)
- {
- PR("customer_dat load errorn");
- fclose(fp_customer);
- return ERROR;
- }
- }
- fclose(fp_customer);
- return OK;
- }
- airline * 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 l;
- }
- customer * 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 l;
- }
- void main()
- {
- airline *air;
- customer *cus;
- PR("mainn");
- init_airline(air);
- init_customer(cus);
- PR("init finishn");
- air=creat_airline(air);
- cus=creat_customer(cus);
- save_airline(air);
- save_customer(cus);
- }