//使用公共函数
//(1)图书信息录入功能(图书信息用文件保存)--输入?
//(2)图书信息浏览功能--输出
//(3)查询功能(至少一种查询方式)、排序功能(至少一种排序方式): 
// ①按书名查询 ②按作者名查询  ③ 按照价钱排序 ④按出版时间排序等等
//(4)图书信息的删除与修改

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define CHAR 20                //CHAR 输入名字的最大长度(包括图书名)

//包含的图书信息
typedef struct book_ifo{        //
    int num;
    char writer[CHAR];
    char name[CHAR];
    double price;
    struct date *time;         //时间结构(收录时间)
    struct book_ifo *next;
}BOOK;

//时间结构
struct date{
    int year,mounth,day;
};
struct date *time;

BOOK *book;
int size = sizeof(BOOK);

BOOK *creat();           //录入
void browse();          //浏览(将链表顺序输出)共用排序函数
void search();          //查询
void edit();            //删除与修改
void store(BOOK *head);           //存储
void output(int choice);
//void check(BOOK *);              //检测
BOOK *Head(BOOK *);                           //存储头指针以便其它函数调用
void modify(BOOK *head,int num);             //修改
void del();                     //删除

//主函数
int main()
{
    int choice;                      //声明变量

    printf("\n \t \t \t  欢迎您进入图书管理系统!\n\n");

    do{
        printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");   
        printf("\t\t\t[1]:信息录入\t");printf("[3]:查询图书\n");  
        printf("\t\t\t[2]:浏览图书\t");printf("[4]:修改与删除\n\n");  
        printf("\t\t\t〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n\n");  
        printf("\t\t\t0:退出\n\n");  
        printf("Enter your choice:");            //此处判断输入是否合法(未实现)
        scanf("%d",&choice);
        switch(choice)
        {                                    //复合语句开始
            case 1:
                printf("录入\n");             //录入
                creat();
                break;
            case 2:
                printf("浏览\n");             //浏览
                browse();
                break;
            case 3:
                printf("查询\n");             //查询
                search();
                break;
            case 4:
                printf("删除与修改\n");             //删除与修改
                edit();
                break;
            case 0:
                printf("exit!\n");
                break;
            default:
                printf("Please enter right choice 0--4:\n\t");     //输入错误提示
        }
    }while(choice != 0);
    

    return 0;
}

//录入函数
BOOK *creat()           //录入
{
    BOOK *head, *p, *tail;
    head = tail = NULL;
    int num;
    char writer[CHAR],name[CHAR];
    double price;

    printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");   
    printf("enter 0 end input!\n");
    printf("please enter book's number:\n");
    printf("\n\t\t\t********************************\n\n");   
    scanf("%d",&num);
    do{
        p=(BOOK *)malloc(size);
    

        if(head == NULL)
            head = p;
        else
            tail->next = p;
        tail = p;
        p->next = NULL;
        p->num = num;
        printf("enter book's name:\n");
        scanf("%s",name);
        strcpy(p->name,name);
        printf("enter writer's name:\n");
        scanf("%s",&writer);
        strcpy(p->writer,writer);
        printf("enter price:\n");
        scanf("%lf",&price);
        printf("\n\t**************************\n\n");   
        p->price = price;
        printf("enter next book's number:\n");
        scanf("%d",&num);
    }while(num != 0);
    store(head);                   //存储
                          //存储头指针以便其它函数调用
    free(p);
   

    return head;
}

//浏览函数
void browse()          //浏览
{
    int num;
    float price;
    char writer[CHAR], name[CHAR];
    FILE *fp;


    printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

    if((fp = fopen("bookinfo.txt","r")) == NULL){
        printf("open fail!\n");
        exit(0);
    }
    while(! feof(fp))
    {
        fscanf(fp,"%d %s %s %f",&num,writer,name,&price);
        printf("number: %d\t writer: %s\t bookname: %s\t price: %.2f\n",num,writer,name,price);
    };
    if(fclose(fp)){
        printf("close error!\n");
        exit(0);
    }
}

//查询函数
void search()          //查询
{
    printf("查询调用成功\n");

}

//修改与删除                                        !!!!!!!!!!!!!!!!!!!!!!!! 待完成!!!!!!!!!!!!!!!!!!!
void edit()            
{
    int choice,num;

    printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");   
    printf("\t\t\t[1]修改 \t [2]删除\n\t\t\t[0]exit!\n\n");
    printf("please enter your choice!\n");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1:
        printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");
        printf("\t\t\t输入要修改的图书编号\n\n");
        scanf("%d",&num);
        BOOK *head;                   //如何在这里获得 头指针
        modify(head, num);
        break;
    case 2:
        printf("删除\n");
        break;
    default:
        printf("Please enter right choice 0--2:\n\t");  
    }
}

void store(BOOK *head)                          //存储
{
    FILE *fp;
    BOOK *ptr,*p;
    p = head;

    if((fp = fopen("bookinfo.txt","w")) == NULL){
        printf("File open error!\n");
        exit(0);
    }
     //check(p);                                            //检测输入的信息是否已存在(书目num)

    for(ptr = head;ptr;ptr = ptr->next)
    fprintf(fp,"%d   %s   %s %6.2f\n",ptr->num,ptr->writer,ptr->name,ptr->price);
    if(fclose(fp)){
        printf("Can not close the file!\n");
        exit(0);
    }
}

void modify(BOOK *head,int num)
{

}