#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DATA_FILE "data.txt"

struct STUDENT
{
    int id;
    char name[80];
    int take_num;
    int takes[6];
    struct STUDENT* left_next;/*link list*/
    struct STUDENT* right_next;       
};

struct STUDENT* head;
struct STUDENT* current;
struct STUDENT* previous;/*link list*/

/*void init(struct STUDENT students[]);*/
void load(FILE* fp,int select_num);
void make_tree(struct STUDENT* student_ptr,struct STUDENT** head,int select_num);
void traverse_tree(struct STUDENT** head);

struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num);

void show(struct STUDENT* student);
int add(struct STUDENT* student,int select_course);
void save(FILE* fp,struct STUDENT* student);

int main (void)
{
    FILE *fp;
    struct STUDENT students[256];
    int id=-1;//search by id
    char name[80]={0};//search by name
    int index;
    int course;
    int result;
    int select_num;//save the number selected
    struct STUDENT* consequence;
    
    //init(students);//對id，name做初始化 
    fp=fopen(DATA_FILE,"r");
    
    //load(fp,students);
    //fclose(fp);
    
    printf("please select enter name or enter id:\n");
    printf("enter name 按1，enter id 按2\n");
    scanf("%d",&select_num);
    //select_num=1;
    if(select_num==1)
    {
        load(fp,select_num);
        fclose(fp);
        printf("please enter name:");
        scanf("%s",name); 
        consequence=b_search(head,-1,name,1);
          
        if(consequence==NULL)
        {
            printf("name doesn`t exist,program ends\n");
            exit(1);            
        } 
        //id=name_to_id(students,name);  
        printf("%d ",consequence->id);
        printf("%s",consequence->name);          
    }
    else if(select_num==2)
    {
        load(fp,select_num);
        fclose(fp);
        printf("please enter id:");
        scanf("%d",&id);
        consequence=b_search(head,id,0,2);        
        
        
        if(consequence==NULL)
        {
            printf("id doesn`t exist,program ends\n");
            exit(1);            
        }
        printf("%d ",consequence->id);
        printf("%s",consequence->name);
    }
    
    while(1)
    {
        show(consequence);
        if(consequence->take_num==5)
        {
            printf("all courses are selected,program ends");
            break;                               
        }        
        printf("請輸入課程代號，-1結束選課");
        scanf("%d",&course);
        if(course==-1)
        {
            printf("程式結束\n"); 
            break;             
        }
        
        result=add(consequence,course);
        if(result==1)
        {
            printf("選課成功\n");             
        }
        else if(result==2)
        {
            printf("重複選課\n");            
        }
        else if(result==3)
        {
            printf("無此課程\n");     
        }
    }
    
    fp=fopen(DATA_FILE,"w");
    save(fp,head);
    fprintf(fp,"\b");
    fclose(fp);
    
    system("pause");
    return 0;
}

/*void init(struct STUDENT students[])
{
    int i,j;
    for(i=1;i<256;i++)
    {
        students[i].id=-1; 
        //students[i].name[80]={0};
        memset(students[i].name,0,sizeof(students[i].name));                
    }     
}*/

void load(FILE* fp,int select_num)
{
    int i,j,k;
    i=0;
    //if(select_num==2)/*enter id*/
    {
        while(!feof(fp))
        {
            i=i+1;
            struct STUDENT* student_ptr=(struct STUDENT*)malloc(sizeof(struct STUDENT));
           // printf("%d\n",i);
            fscanf(fp,"%d",&(student_ptr->id));
            fscanf(fp,"%s",student_ptr->name);
            fscanf(fp,"%d",&(student_ptr->take_num));
           // printf("%d ",student_ptr->id);
           // printf("%d\n",student_ptr->take_num);
            for(j=1;j<=(student_ptr->take_num);j++)
            {
                fscanf(fp,"%d",&(student_ptr->takes[j]));    
                                          
            }
            
            student_ptr->left_next=NULL;
            student_ptr->right_next=NULL;
            
            make_tree(student_ptr,&head,select_num);
            printf("%d\n",i);       
        } 
        traverse_tree(&head);
    }
    
}


void show(struct STUDENT* student)
{
    int i;
    printf("您已選以下課程:\n");
    for(i=1;i<=student->take_num;i++)
    {
        printf("%d",student->takes[i]);
        printf(" ");                                         
    }     
    printf("\n");
}

int add(struct STUDENT* student,int course)
{
    int i;
    if(course>=101&&course<=105)
    {
        for(i=1;i<=student->take_num;i++)
        {
            if(student->takes[i]==course)
                return 2;                                        
        }  
        student->takes[i]=course;
        student->take_num=(student->take_num)+1;
        return 1;                          
    }    
    return 3;
}

void save(FILE* fp,struct STUDENT* head)
{
    int i;
    if(head==NULL)
    {
        return;              
    }
    else
    {
        fprintf(fp,"%d ",head->id);
        fprintf(fp,"%s ",head->name);
        fprintf(fp,"%d ",head->take_num);
        for(i=1;i<=head->take_num;i++)
        {
            fprintf(fp,"%d",head->takes[i]);
            if(i<head->take_num)
                fprintf(fp," ");
                                      
        }
        fprintf(fp,"\n"); 
        //printf("%d",head->id);
        save(fp,head->left_next);
        save(fp,head->right_next); 
    }
}

struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num)
{
    //printf("%d ",select_num);
    struct STUDENT* result;
    if(select_num==1)
    {    
        if(head==NULL)
        {
            result=NULL;
            return result;               
        }
        else if(head!=NULL)
        {
            if(strcmp((head)->name,name)>0)
            {
                result=b_search(head->left_next,-1,name,1);
                return result;
            }         
            else if(strcmp((head)->name,name)<0)
            {
                result=b_search(head->right_next,-1,name,1);
                return result;
            }
            else if(strcmp((head)->name,name)==0)
            {
                
                return head;     
            }
        }
    }
    else if(select_num==2)
    {
        if(head==NULL)
        {
            return NULL;              
        }
        else
        {    
            if(id==head->id)
            {
                result=head;
                return result;                
            } 
            else if(id<head->id)
            {
                result=b_search(head->left_next,id,0,2); 
                return result;    
            }
            else if(id>head->id)
            {
                result=b_search(head->right_next,id,0,2); 
                return result;    
            }
        }        
    }
}


void make_tree(struct STUDENT* student_ptr,struct STUDENT** head,int select_num)
{
     if(select_num==1)
     {
         if(*head==NULL)                 
         {
             *head=student_ptr;                                
         }
         else if(*head!=NULL)
         {
             if(strcmp((*head)->name,student_ptr->name)>0)
             {
                 make_tree(student_ptr,&((*head)->left_next),select_num);                                           
             }     
             else if(strcmp((*head)->name,student_ptr->name)<=0)
             {
                 make_tree(student_ptr,&((*head)->right_next),select_num);     
             }
         }
     }
     else if(select_num==2)
     {   
        if(*head==NULL)
        {
            *head=student_ptr;
           // printf("%d\n",(*head)->id);
            /*current=student_ptr; 
            previous=student_ptr;*/                 
        }
        else if(*head!=NULL)
        {
            if((*head)->id>=student_ptr->id)
            {
                /*previous=current;
                current=head->left_next;*/
                if((*head)->left_next==NULL)
                {
                    (*head)->left_next=student_ptr;                 
                }
                else if((*head)->left_next!=NULL)
                {
                    make_tree(student_ptr,&((*head)->left_next),select_num);
                }                         
            }
            else if((*head)->id<student_ptr->id)
            {
                if((*head)->right_next==NULL)
                {
                    (*head)->right_next=student_ptr;                          
                }     
                else if((*head)->right_next!=NULL)
                {
                    make_tree(student_ptr,&((*head)->right_next),select_num);     
                }
            }     
        }  
     } 
    //printf("%s %d\n",head->name,head->id);
}

void traverse_tree(struct STUDENT** head)
{
    if(*head==NULL)
    {
        return;              
    }
    else
    {
        //printf("%s %d\n",(*head)->name,(*head)->id);
        traverse_tree(&((*head)->left_next));
        traverse_tree(&((*head)->right_next));  
    }   
}
