#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);
void traverse_tree(struct STUDENT* head);
int search(struct STUDENT students[],int id);
int b_search(struct STUDENT students[],int id,int head,int tail);
int b_search_name(struct STUDENT students[],char* name,int head,int tail);
int search_by_name(struct STUDENT students[],char* name);
void show(struct STUDENT students[],int id,int index);
int add(struct STUDENT students[],int id,int index,int course);
void save(FILE* fp,struct STUDENT students[]);
int main (void)
{
FILE *fp;
struct STUDENT students[256];
int id;//search by id
char name[80];//search by name
int index;
int course;
int result;
int select_num;//save the number selected
//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=2;
if(select_num==1)
{
printf("please enter name:");
scanf("%s",name);
index=search_by_name(students,name);
if(index==-1)
{
printf("name doesn`t exist,program ends\n");
exit(1);
}
//id=name_to_id(students,name);
id=students[index].id;
}
else if(select_num==2)
{
load(fp,select_num);
printf("please enter id:");
scanf("%d",&id);
index=search(students,id);
if(index==-1)
{
printf("id doesn`t exist,program ends\n");
exit(1);
}
}
while(1)
{
show(students,id,index);
if(students[index].take_num==5)
{
printf("all courses are selected,program ends");
break;
}
printf("請輸入課程代號,-1結束選課");
scanf("%d",&course);
if(course==-1)
{
printf("程式結束\n");
break;
}
result=add(students,id,index,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,students);
fclose(fp);
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);
//printf("%d\n",i);
}
traverse_tree(head);
}
}
int search(struct STUDENT students[],int id)
{
int i,index;
int j;
struct STUDENT temp;//資料暫存處
index=-1;
/*for(i=1;i<256;i++)
{
if(students[i].id==id)
{
index=i;
break;
}
}*/
//insertion sort
for(i=2;i<256;i++)
{
/*temp.id=students[i].id;//暫存資料
temp.name=students[i].name;
temp.take_num=students[i].take_num;
temp.takes=students[i].takes;*/
//printf("%d %d\n",i,students[i].id);
temp=students[i];
for(j=i-1;j>=1;j--)
{
if(students[j].id<temp.id)
break;
else if(students[j].id>=temp.id)
{
students[j+1]=students[j];
}
}
if(j==i-1)
continue;
else
students[j+1]=temp;
}/*insertion sort*/
for(i=1;i<256;i++)
{
//printf("%d %d\n",i,students[i].id);
}
//system("pause");
//binary search student
index=b_search(students,id,1,255);
printf("%d",index);
return index;
}
int search_by_name(struct STUDENT students[],char* name)
{
int i,j,index=-1;
int comp_result;
struct STUDENT temp;
/*for(i=1;i<256;i++)
{
if(strcmp(students[i].name,name)==0)
{
index=i;
break;
}
} */
/*sorting string by bubble sort*/
for(j=255;j>=2;j--)
{
for(i=1;i<j;i++)
{
comp_result=strcmp(students[i].name,students[i+1].name);
if(comp_result==1)
{
temp=students[i];
students[i]=students[i+1];
students[i+1]=temp;
}
else if(comp_result==0||comp_result==-1)
continue;
}
}
for(i=1;i<256;i++)
{
//printf("%d %s\n",i,students[i].name);
}
index=b_search_name(students,name,1,255);
//printf("%d",index);
//system("pause");
return index;
}
void show(struct STUDENT students[],int id,int index)
{
int i;
printf("您已選以下課程:\n");
for(i=1;i<=students[index].take_num;i++)
{
printf("%d",students[index].takes[i]);
printf(" ");
}
printf("\n");
}
int add(struct STUDENT students[],int id,int index,int course)
{
int i;
if(course>=101&&course<=105)
{
for(i=1;i<=students[index].take_num;i++)
{
if(students[index].takes[i]==course)
return 2;
}
students[index].takes[i]=course;
students[index].take_num=students[index].take_num+1;
return 1;
}
return 3;
}
void save(FILE* fp,struct STUDENT students[])
{
int i,j;
for(i=1;i<256;i++)
{
if(students[i].id!=-1)
{
fprintf(fp,"%d",students[i].id);
fprintf(fp,"\t");
fprintf(fp,"%s",students[i].name);
fprintf(fp,"\t");
fprintf(fp,"%d",students[i].take_num);
fprintf(fp,"\t");
for(j=1;j<=students[i].take_num;j++)
{
fprintf(fp,"%d",students[i].takes[j]);
fprintf(fp,"\t");
}
fprintf(fp,"\n");
}
}
}
int b_search(struct STUDENT students[],int id,int head,int tail)
{
int index;
if(head==tail)
{
if(students[head].id!=id)
return -1;
else
return head;
}
else
{
if(students[(head+tail)/2].id==id)
{
index=(head+tail)/2;
return index;
}
else if(students[(head+tail)/2].id>id)
{
index=b_search(students,id,head,(head+tail)/2);
return index;
}
else if(students[(head+tail)/2].id<id)
{
index=b_search(students,id,((head+tail)/2)+1,tail);
return index;
}
}
}
int b_search_name(struct STUDENT students[],char* name,int head,int tail)
{
int comp_result;
int index=-1;
if(head==tail)
{
if(strcmp(name,students[head].name)==0)
return head;
else
return index;
}
comp_result=strcmp(name,students[(head+tail)/2].name);
if(comp_result<0)
{
index=b_search_name(students,name,head,(head+tail)/2);
return index;
}
else if(comp_result==0)
{
return (head+tail)/2;
}
else if(comp_result>0)
{
index=b_search_name(students,name,((head+tail)/2)+1,tail);
return index;
}
}
void make_tree(struct STUDENT* student_ptr,struct STUDENT* head)
{
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);
}
}
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);
}
}
}
//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);
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBEQVRBX0ZJTEUgImRhdGEudHh0IgoKc3RydWN0IFNUVURFTlQKewogICAgaW50IGlkOwogICAgY2hhciBuYW1lWzgwXTsKICAgIGludCB0YWtlX251bTsKICAgIGludCB0YWtlc1s2XTsKICAgIHN0cnVjdCBTVFVERU5UKiBsZWZ0X25leHQ7LypsaW5rIGxpc3QqLwogICAgc3RydWN0IFNUVURFTlQqIHJpZ2h0X25leHQ7ICAgICAgIAp9OwoKc3RydWN0IFNUVURFTlQqIGhlYWQ7CnN0cnVjdCBTVFVERU5UKiBjdXJyZW50OwpzdHJ1Y3QgU1RVREVOVCogcHJldmlvdXM7LypsaW5rIGxpc3QqLwoKLyp2b2lkIGluaXQoc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSk7Ki8Kdm9pZCBsb2FkKEZJTEUqIGZwLGludCBzZWxlY3RfbnVtKTsKdm9pZCBtYWtlX3RyZWUoc3RydWN0IFNUVURFTlQqIHN0dWRlbnRfcHRyLHN0cnVjdCBTVFVERU5UKiBoZWFkKTsKdm9pZCB0cmF2ZXJzZV90cmVlKHN0cnVjdCBTVFVERU5UKiBoZWFkKTsKCmludCBzZWFyY2goc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSxpbnQgaWQpOwppbnQgYl9zZWFyY2goc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSxpbnQgaWQsaW50IGhlYWQsaW50IHRhaWwpOwppbnQgYl9zZWFyY2hfbmFtZShzdHJ1Y3QgU1RVREVOVCBzdHVkZW50c1tdLGNoYXIqIG5hbWUsaW50IGhlYWQsaW50IHRhaWwpOwppbnQgc2VhcmNoX2J5X25hbWUoc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSxjaGFyKiBuYW1lKTsKCnZvaWQgc2hvdyhzdHJ1Y3QgU1RVREVOVCBzdHVkZW50c1tdLGludCBpZCxpbnQgaW5kZXgpOwppbnQgYWRkKHN0cnVjdCBTVFVERU5UIHN0dWRlbnRzW10saW50IGlkLGludCBpbmRleCxpbnQgY291cnNlKTsKdm9pZCBzYXZlKEZJTEUqIGZwLHN0cnVjdCBTVFVERU5UIHN0dWRlbnRzW10pOwoKaW50IG1haW4gKHZvaWQpCnsKICAgIEZJTEUgKmZwOwogICAgc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbMjU2XTsKICAgIGludCBpZDsvL3NlYXJjaCBieSBpZAogICAgY2hhciBuYW1lWzgwXTsvL3NlYXJjaCBieSBuYW1lCiAgICBpbnQgaW5kZXg7CiAgICBpbnQgY291cnNlOwogICAgaW50IHJlc3VsdDsKICAgIGludCBzZWxlY3RfbnVtOy8vc2F2ZSB0aGUgbnVtYmVyIHNlbGVjdGVkCiAgICAKICAgIC8vaW5pdChzdHVkZW50cyk7Ly/lsI1pZO+8jG5hbWXlgZrliJ3lp4vljJYgCiAgICBmcD1mb3BlbihEQVRBX0ZJTEUsInIiKTsKICAgIAogICAgLy9sb2FkKGZwLHN0dWRlbnRzKTsKICAgIC8vZmNsb3NlKGZwKTsKICAgIAogICAgcHJpbnRmKCJwbGVhc2Ugc2VsZWN0IGVudGVyIG5hbWUgb3IgZW50ZXIgaWQ6XG4iKTsKICAgIHByaW50ZigiZW50ZXIgbmFtZSDmjIkx77yMZW50ZXIgaWQg5oyJMlxuIik7CiAgICAvL3NjYW5mKCIlZCIsJnNlbGVjdF9udW0pOwogICAgc2VsZWN0X251bT0yOwogICAgaWYoc2VsZWN0X251bT09MSkKICAgIHsKICAgICAgICBwcmludGYoInBsZWFzZSBlbnRlciBuYW1lOiIpOwogICAgICAgIHNjYW5mKCIlcyIsbmFtZSk7IAogICAgICAgIGluZGV4PXNlYXJjaF9ieV9uYW1lKHN0dWRlbnRzLG5hbWUpOyAgCiAgICAgICAgaWYoaW5kZXg9PS0xKQogICAgICAgIHsKICAgICAgICAgICAgcHJpbnRmKCJuYW1lIGRvZXNuYHQgZXhpc3QscHJvZ3JhbSBlbmRzXG4iKTsKICAgICAgICAgICAgZXhpdCgxKTsgICAgICAgICAgICAKICAgICAgICB9IAogICAgICAgIC8vaWQ9bmFtZV90b19pZChzdHVkZW50cyxuYW1lKTsgIAogICAgICAgIGlkPXN0dWRlbnRzW2luZGV4XS5pZDsgICAgICAgICAgCiAgICB9CiAgICBlbHNlIGlmKHNlbGVjdF9udW09PTIpCiAgICB7CiAgICAgICAgbG9hZChmcCxzZWxlY3RfbnVtKTsKICAgICAgICBwcmludGYoInBsZWFzZSBlbnRlciBpZDoiKTsKICAgICAgICBzY2FuZigiJWQiLCZpZCk7CiAgICAgICAgICAgICAgICAKICAgICAgICBpbmRleD1zZWFyY2goc3R1ZGVudHMsaWQpOwogICAgICAgIAogICAgICAgIGlmKGluZGV4PT0tMSkKICAgICAgICB7CiAgICAgICAgICAgIHByaW50ZigiaWQgZG9lc25gdCBleGlzdCxwcm9ncmFtIGVuZHNcbiIpOwogICAgICAgICAgICBleGl0KDEpOyAgICAgICAgICAgIAogICAgICAgIH0KICAgIH0KICAgIAogICAgd2hpbGUoMSkKICAgIHsKICAgICAgICBzaG93KHN0dWRlbnRzLGlkLGluZGV4KTsKICAgICAgICBpZihzdHVkZW50c1tpbmRleF0udGFrZV9udW09PTUpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoImFsbCBjb3Vyc2VzIGFyZSBzZWxlY3RlZCxwcm9ncmFtIGVuZHMiKTsKICAgICAgICAgICAgYnJlYWs7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgIH0gICAgICAgIAogICAgICAgIHByaW50Zigi6KuL6Ly45YWl6Kqy56iL5Luj6Jmf77yMLTHntZDmnZ/pgbjoqrIiKTsKICAgICAgICBzY2FuZigiJWQiLCZjb3Vyc2UpOwogICAgICAgIGlmKGNvdXJzZT09LTEpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIueoi+W8j+e1kOadn1xuIik7IAogICAgICAgICAgICBicmVhazsgICAgICAgICAgICAgCiAgICAgICAgfQogICAgICAgIAogICAgICAgIHJlc3VsdD1hZGQoc3R1ZGVudHMsaWQsaW5kZXgsY291cnNlKTsKICAgICAgICBpZihyZXN1bHQ9PTEpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIumBuOiqsuaIkOWKn1xuIik7ICAgICAgICAgICAgIAogICAgICAgIH0KICAgICAgICBlbHNlIGlmKHJlc3VsdD09MikKICAgICAgICB7CiAgICAgICAgICAgIHByaW50Zigi6YeN6KSH6YG46KqyXG4iKTsgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihyZXN1bHQ9PTMpCiAgICAgICAgewogICAgICAgICAgICBwcmludGYoIueEoeatpOiqsueoi1xuIik7ICAgICAKICAgICAgICB9CiAgICB9CiAgICAKICAgIGZwPWZvcGVuKERBVEFfRklMRSwidyIpOwogICAgc2F2ZShmcCxzdHVkZW50cyk7CiAgICBmY2xvc2UoZnApOwogICAgCiAgICAKICAgIHJldHVybiAwOwp9CgovKnZvaWQgaW5pdChzdHJ1Y3QgU1RVREVOVCBzdHVkZW50c1tdKQp7CiAgICBpbnQgaSxqOwogICAgZm9yKGk9MTtpPDI1NjtpKyspCiAgICB7CiAgICAgICAgc3R1ZGVudHNbaV0uaWQ9LTE7IAogICAgICAgIC8vc3R1ZGVudHNbaV0ubmFtZVs4MF09ezB9OwogICAgICAgIG1lbXNldChzdHVkZW50c1tpXS5uYW1lLDAsc2l6ZW9mKHN0dWRlbnRzW2ldLm5hbWUpKTsgICAgICAgICAgICAgICAgCiAgICB9ICAgICAKfSovCgp2b2lkIGxvYWQoRklMRSogZnAsaW50IHNlbGVjdF9udW0pCnsKICAgIGludCBpLGosazsKICAgIGk9MDsKICAgIGlmKHNlbGVjdF9udW09PTIpLyplbnRlciBpZCovCiAgICB7CiAgICAgICAgd2hpbGUoIWZlb2YoZnApKQogICAgICAgIHsKICAgICAgICAgICAgaT1pKzE7CiAgICAgICAgICAgIHN0cnVjdCBTVFVERU5UKiBzdHVkZW50X3B0cj0oc3RydWN0IFNUVURFTlQqKW1hbGxvYyhzaXplb2Yoc3RydWN0IFNUVURFTlQpKTsKICAgICAgICAgICAvLyBwcmludGYoIiVkXG4iLGkpOwogICAgICAgICAgICBmc2NhbmYoZnAsIiVkIiwmKHN0dWRlbnRfcHRyLT5pZCkpOwogICAgICAgICAgICBmc2NhbmYoZnAsIiVzIixzdHVkZW50X3B0ci0+bmFtZSk7CiAgICAgICAgICAgIGZzY2FuZihmcCwiJWQiLCYoc3R1ZGVudF9wdHItPnRha2VfbnVtKSk7CiAgICAgICAgICAgLy8gcHJpbnRmKCIlZCAiLHN0dWRlbnRfcHRyLT5pZCk7CiAgICAgICAgICAgLy8gcHJpbnRmKCIlZFxuIixzdHVkZW50X3B0ci0+dGFrZV9udW0pOwogICAgICAgICAgICBmb3Ioaj0xO2o8PShzdHVkZW50X3B0ci0+dGFrZV9udW0pO2orKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZnNjYW5mKGZwLCIlZCIsJihzdHVkZW50X3B0ci0+dGFrZXNbal0pKTsgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBzdHVkZW50X3B0ci0+bGVmdF9uZXh0PU5VTEw7CiAgICAgICAgICAgIHN0dWRlbnRfcHRyLT5yaWdodF9uZXh0PU5VTEw7CiAgICAgICAgICAgIAogICAgICAgICAgICBtYWtlX3RyZWUoc3R1ZGVudF9wdHIsaGVhZCk7CiAgICAgICAgICAgIC8vcHJpbnRmKCIlZFxuIixpKTsgICAgICAgCiAgICAgICAgfSAKICAgICAgICB0cmF2ZXJzZV90cmVlKGhlYWQpOwogICAgfQogICAgCn0KCmludCBzZWFyY2goc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSxpbnQgaWQpCnsKICAgIGludCBpLGluZGV4OwogICAgaW50IGo7CiAgICBzdHJ1Y3QgU1RVREVOVCB0ZW1wOy8v6LOH5paZ5pqr5a2Y6JmVIAogICAgaW5kZXg9LTE7CiAgICAvKmZvcihpPTE7aTwyNTY7aSsrKQogICAgewogICAgICAgIGlmKHN0dWRlbnRzW2ldLmlkPT1pZCkKICAgICAgICB7CiAgICAgICAgICAgIGluZGV4PWk7CiAgICAgICAgICAgIGJyZWFrOyAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICB9ICAgICAgICAgICAgICAgICAgCiAgICB9Ki8KICAgIAogICAgLy9pbnNlcnRpb24gc29ydAogICAgZm9yKGk9MjtpPDI1NjtpKyspCiAgICB7CiAgICAgICAgLyp0ZW1wLmlkPXN0dWRlbnRzW2ldLmlkOy8v5pqr5a2Y6LOH5paZIAogICAgICAgIHRlbXAubmFtZT1zdHVkZW50c1tpXS5uYW1lOwogICAgICAgIHRlbXAudGFrZV9udW09c3R1ZGVudHNbaV0udGFrZV9udW07CiAgICAgICAgdGVtcC50YWtlcz1zdHVkZW50c1tpXS50YWtlczsqLwogICAgICAgIC8vcHJpbnRmKCIlZCAlZFxuIixpLHN0dWRlbnRzW2ldLmlkKTsKICAgICAgICB0ZW1wPXN0dWRlbnRzW2ldOwogICAgICAgIGZvcihqPWktMTtqPj0xO2otLSkKICAgICAgICB7CiAgICAgICAgICAgIGlmKHN0dWRlbnRzW2pdLmlkPHRlbXAuaWQpCiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgZWxzZSBpZihzdHVkZW50c1tqXS5pZD49dGVtcC5pZCkgIAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzdHVkZW50c1tqKzFdPXN0dWRlbnRzW2pdOyAgICAKICAgICAgICAgICAgfSAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgfSAKICAgICAgICAKICAgICAgICBpZihqPT1pLTEpCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIGVsc2UgICAgCiAgICAgICAgICAgIHN0dWRlbnRzW2orMV09dGVtcDsgICAgICAgICAgICAgICAgIAogICAgfS8qaW5zZXJ0aW9uIHNvcnQqLwogICAgCiAgICBmb3IoaT0xO2k8MjU2O2krKykKICAgIHsKICAgICAgLy9wcmludGYoIiVkICVkXG4iLGksc3R1ZGVudHNbaV0uaWQpOyAgICAgICAgICAgICAgIAogICAgfQogICAgLy9zeXN0ZW0oInBhdXNlIik7CiAgICAvL2JpbmFyeSBzZWFyY2ggc3R1ZGVudAogICAgaW5kZXg9Yl9zZWFyY2goc3R1ZGVudHMsaWQsMSwyNTUpOwogICAgcHJpbnRmKCIlZCIsaW5kZXgpOwogICAgcmV0dXJuIGluZGV4Owp9CgppbnQgc2VhcmNoX2J5X25hbWUoc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSxjaGFyKiBuYW1lKQp7CiAgICBpbnQgaSxqLGluZGV4PS0xOwogICAgaW50IGNvbXBfcmVzdWx0OwogICAgc3RydWN0IFNUVURFTlQgdGVtcDsKICAgIC8qZm9yKGk9MTtpPDI1NjtpKyspCiAgICB7CiAgICAgICAgaWYoc3RyY21wKHN0dWRlbnRzW2ldLm5hbWUsbmFtZSk9PTApCiAgICAgICAgewogICAgICAgICAgICBpbmRleD1pOwogICAgICAgICAgICBicmVhazsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICB9ICAgICAgICAgICAgICAgICAgCiAgICB9ICAgKi8KICAgIAogICAgLypzb3J0aW5nIHN0cmluZyBieSBidWJibGUgc29ydCovCiAgICBmb3Ioaj0yNTU7aj49MjtqLS0pCiAgICB7CiAgICAgICAgZm9yKGk9MTtpPGo7aSsrKQogICAgICAgIHsKICAgICAgICAgICAgY29tcF9yZXN1bHQ9c3RyY21wKHN0dWRlbnRzW2ldLm5hbWUsc3R1ZGVudHNbaSsxXS5uYW1lKTsgCiAgICAgICAgICAgIGlmKGNvbXBfcmVzdWx0PT0xKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0ZW1wPXN0dWRlbnRzW2ldOwogICAgICAgICAgICAgICAgc3R1ZGVudHNbaV09c3R1ZGVudHNbaSsxXTsKICAgICAgICAgICAgICAgIHN0dWRlbnRzW2krMV09dGVtcDsgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKGNvbXBfcmVzdWx0PT0wfHxjb21wX3Jlc3VsdD09LTEpCiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICAKICAgICAgICB9IAogICAgfQogICAgZm9yKGk9MTtpPDI1NjtpKyspCiAgICB7CiAgICAgIC8vcHJpbnRmKCIlZCAlc1xuIixpLHN0dWRlbnRzW2ldLm5hbWUpOyAgICAgICAgICAgICAgIAogICAgfQogICAgaW5kZXg9Yl9zZWFyY2hfbmFtZShzdHVkZW50cyxuYW1lLDEsMjU1KTsKICAgIC8vcHJpbnRmKCIlZCIsaW5kZXgpOwogICAgLy9zeXN0ZW0oInBhdXNlIik7CiAgICByZXR1cm4gaW5kZXg7Cn0KCgp2b2lkIHNob3coc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSxpbnQgaWQsaW50IGluZGV4KQp7CiAgICBpbnQgaTsKICAgIHByaW50Zigi5oKo5bey6YG45Lul5LiL6Kqy56iLOlxuIik7CiAgICBmb3IoaT0xO2k8PXN0dWRlbnRzW2luZGV4XS50YWtlX251bTtpKyspCiAgICB7CiAgICAgICAgcHJpbnRmKCIlZCIsc3R1ZGVudHNbaW5kZXhdLnRha2VzW2ldKTsKICAgICAgICBwcmludGYoIiAiKTsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgfSAgICAgCiAgICBwcmludGYoIlxuIik7Cn0KCmludCBhZGQoc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSxpbnQgaWQsaW50IGluZGV4LGludCBjb3Vyc2UpCnsKICAgIGludCBpOwogICAgaWYoY291cnNlPj0xMDEmJmNvdXJzZTw9MTA1KQogICAgewogICAgICAgIGZvcihpPTE7aTw9c3R1ZGVudHNbaW5kZXhdLnRha2VfbnVtO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKHN0dWRlbnRzW2luZGV4XS50YWtlc1tpXT09Y291cnNlKQogICAgICAgICAgICAgICAgcmV0dXJuIDI7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgIH0gIAogICAgICAgIHN0dWRlbnRzW2luZGV4XS50YWtlc1tpXT1jb3Vyc2U7CiAgICAgICAgc3R1ZGVudHNbaW5kZXhdLnRha2VfbnVtPXN0dWRlbnRzW2luZGV4XS50YWtlX251bSsxOwogICAgICAgIHJldHVybiAxOyAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICB9ICAgIAogICAgcmV0dXJuIDM7Cn0KCnZvaWQgc2F2ZShGSUxFKiBmcCxzdHJ1Y3QgU1RVREVOVCBzdHVkZW50c1tdKQp7CiAgICBpbnQgaSxqOwogICAgZm9yKGk9MTtpPDI1NjtpKyspCiAgICB7CiAgICAgICAgaWYoc3R1ZGVudHNbaV0uaWQhPS0xKQogICAgICAgIHsKICAgICAgICAgICAgZnByaW50ZihmcCwiJWQiLHN0dWRlbnRzW2ldLmlkKTsKICAgICAgICAgICAgZnByaW50ZihmcCwiXHQiKTsKICAgICAgICAgICAgZnByaW50ZihmcCwiJXMiLHN0dWRlbnRzW2ldLm5hbWUpOwogICAgICAgICAgICBmcHJpbnRmKGZwLCJcdCIpOwogICAgICAgICAgICBmcHJpbnRmKGZwLCIlZCIsc3R1ZGVudHNbaV0udGFrZV9udW0pOwogICAgICAgICAgICBmcHJpbnRmKGZwLCJcdCIpOwogICAgICAgICAgICBmb3Ioaj0xO2o8PXN0dWRlbnRzW2ldLnRha2VfbnVtO2orKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZnByaW50ZihmcCwiJWQiLHN0dWRlbnRzW2ldLnRha2VzW2pdKTsKICAgICAgICAgICAgICAgIGZwcmludGYoZnAsIlx0Iik7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgIH0gICAgICAgICAgICAgICAgIAogICAgICAgICAgICBmcHJpbnRmKGZwLCJcbiIpOyAgICAgCiAgICAgICAgfSAgICAgICAgICAgICAgICAgIAogICAgfSAgICAgCn0KCmludCBiX3NlYXJjaChzdHJ1Y3QgU1RVREVOVCBzdHVkZW50c1tdLGludCBpZCxpbnQgaGVhZCxpbnQgdGFpbCkKewoKICAgIGludCBpbmRleDsKICAgIGlmKGhlYWQ9PXRhaWwpCiAgICB7CiAgICAgICAgaWYoc3R1ZGVudHNbaGVhZF0uaWQhPWlkKQogICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gaGVhZDsgICAgICAgICAgICAgICAgICAKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBpZihzdHVkZW50c1soaGVhZCt0YWlsKS8yXS5pZD09aWQpICAKICAgICAgICB7CiAgICAgICAgICAgIGluZGV4PShoZWFkK3RhaWwpLzI7CiAgICAgICAgICAgIHJldHVybiBpbmRleDsgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICB9IAogICAgICAgIGVsc2UgaWYoc3R1ZGVudHNbKGhlYWQrdGFpbCkvMl0uaWQ+aWQpCiAgICAgICAgewogICAgICAgICAgICBpbmRleD1iX3NlYXJjaChzdHVkZW50cyxpZCxoZWFkLChoZWFkK3RhaWwpLzIpOyAgCiAgICAgICAgICAgIHJldHVybiBpbmRleDsgICAgICAKICAgICAgICB9IAogICAgICAgIGVsc2UgaWYoc3R1ZGVudHNbKGhlYWQrdGFpbCkvMl0uaWQ8aWQpCiAgICAgICAgewogICAgICAgICAgICBpbmRleD1iX3NlYXJjaChzdHVkZW50cyxpZCwoKGhlYWQrdGFpbCkvMikrMSx0YWlsKTsgIAogICAgICAgICAgICByZXR1cm4gaW5kZXg7ICAgCiAgICAgICAgfQogICAgfQp9CgppbnQgYl9zZWFyY2hfbmFtZShzdHJ1Y3QgU1RVREVOVCBzdHVkZW50c1tdLGNoYXIqIG5hbWUsaW50IGhlYWQsaW50IHRhaWwpCnsKICAgIGludCBjb21wX3Jlc3VsdDsKICAgIGludCBpbmRleD0tMTsKICAgIGlmKGhlYWQ9PXRhaWwpCiAgICB7CiAgICAgICAgaWYoc3RyY21wKG5hbWUsc3R1ZGVudHNbaGVhZF0ubmFtZSk9PTApCiAgICAgICAgICAgIHJldHVybiBoZWFkOwogICAgICAgIGVsc2UKICAgICAgICAgICAgcmV0dXJuIGluZGV4OyAgICAgICAgICAgICAgCiAgICB9CiAgICAKICAgICAgICBjb21wX3Jlc3VsdD1zdHJjbXAobmFtZSxzdHVkZW50c1soaGVhZCt0YWlsKS8yXS5uYW1lKTsKICAgICAgICAKICAgICAgICBpZihjb21wX3Jlc3VsdDwwKQogICAgICAgIHsKICAgICAgICAgICAgaW5kZXg9Yl9zZWFyY2hfbmFtZShzdHVkZW50cyxuYW1lLGhlYWQsKGhlYWQrdGFpbCkvMik7ICAKICAgICAgICAgICAgcmV0dXJuIGluZGV4OyAgICAgICAgICAgICAgICAKICAgICAgICB9IAogICAgICAgIGVsc2UgaWYoY29tcF9yZXN1bHQ9PTApCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gKGhlYWQrdGFpbCkvMjsgICAgIAogICAgICAgIH0gICAKICAgICAgICBlbHNlIGlmKGNvbXBfcmVzdWx0PjApCiAgICAgICAgewogICAgICAgICAgICBpbmRleD1iX3NlYXJjaF9uYW1lKHN0dWRlbnRzLG5hbWUsKChoZWFkK3RhaWwpLzIpKzEsdGFpbCk7ICAgCiAgICAgICAgICAgIHJldHVybiBpbmRleDsgICAgICAgICAgICAgICAKICAgICAgICB9Cn0KCnZvaWQgbWFrZV90cmVlKHN0cnVjdCBTVFVERU5UKiBzdHVkZW50X3B0cixzdHJ1Y3QgU1RVREVOVCogaGVhZCkKewogICAgaWYoaGVhZD09TlVMTCkKICAgIHsKICAgICAgICBoZWFkPXN0dWRlbnRfcHRyOwogICAgICAgIHByaW50ZigiJWRcbiIsaGVhZC0+aWQpOwogICAgICAgIC8qY3VycmVudD1zdHVkZW50X3B0cjsgCiAgICAgICAgcHJldmlvdXM9c3R1ZGVudF9wdHI7Ki8gICAgICAgICAgICAgICAgIAogICAgfQogICAgZWxzZSBpZihoZWFkIT1OVUxMKQogICAgewogICAgICAgIGlmKGhlYWQtPmlkPD1zdHVkZW50X3B0ci0+aWQpCiAgICAgICAgewogICAgICAgICAgICAvKnByZXZpb3VzPWN1cnJlbnQ7CiAgICAgICAgICAgIGN1cnJlbnQ9aGVhZC0+bGVmdF9uZXh0OyovCiAgICAgICAgICAgIGlmKGhlYWQtPmxlZnRfbmV4dD09TlVMTCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaGVhZC0+bGVmdF9uZXh0PXN0dWRlbnRfcHRyOyAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZihoZWFkLT5sZWZ0X25leHQhPU5VTEwpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIG1ha2VfdHJlZShzdHVkZW50X3B0cixoZWFkLT5sZWZ0X25leHQpOwogICAgICAgICAgICB9ICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgIH0KICAgICAgICBlbHNlIGlmKGhlYWQtPmlkPnN0dWRlbnRfcHRyLT5pZCkKICAgICAgICB7CiAgICAgICAgICAgIGlmKGhlYWQtPnJpZ2h0X25leHQ9PU5VTEwpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGhlYWQtPnJpZ2h0X25leHQ9c3R1ZGVudF9wdHI7ICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgfSAgICAgCiAgICAgICAgICAgIGVsc2UgaWYoaGVhZC0+cmlnaHRfbmV4dCE9TlVMTCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbWFrZV90cmVlKHN0dWRlbnRfcHRyLGhlYWQtPnJpZ2h0X25leHQpOyAgICAgCiAgICAgICAgICAgIH0KICAgICAgICB9ICAgICAKICAgIH0gICAKICAgIC8vcHJpbnRmKCIlcyAlZFxuIixoZWFkLT5uYW1lLGhlYWQtPmlkKTsKfQoKdm9pZCB0cmF2ZXJzZV90cmVlKHN0cnVjdCBTVFVERU5UKiBoZWFkKQp7CiAgICBpZihoZWFkPT1OVUxMKQogICAgewogICAgICAgIHJldHVybjsgICAgICAgICAgICAgIAogICAgfQogICAgZWxzZQogICAgewogICAgICAgIHByaW50ZigiJXMgJWRcbiIsaGVhZC0+bmFtZSxoZWFkLT5pZCk7CiAgICAgICAgdHJhdmVyc2VfdHJlZShoZWFkLT5sZWZ0X25leHQpOwogICAgICAgIHRyYXZlcnNlX3RyZWUoaGVhZC0+cmlnaHRfbmV4dCk7ICAKICAgIH0gICAKfQo=