#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);
int search(struct STUDENT students[],int id);
struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num);
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=-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);
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);
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,select_num);
//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");
}
}
}
struct STUDENT* b_search(struct STUDENT* head,int id,char* name,int select_num)
{
//printf("%d ",select_num);
if(select_num==1)
{
if(head==NULL)
{
printf("%s",head->name);
return NULL;
}
else if(head!=NULL)
{
if(strcmp((head)->name,name)>0)
{
b_search(head->left_next,-1,name,1);
}
else if(strcmp((head)->name,name)<0)
{
b_search(head->right_next,-1,name,1);
}
else if(strcmp((head)->name,name)==0)
{
printf("%s %d ",head->name,head->id);
return head;
}
}
}
}
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,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));
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBEQVRBX0ZJTEUgImRhdGEudHh0IgoKc3RydWN0IFNUVURFTlQKewogICAgaW50IGlkOwogICAgY2hhciBuYW1lWzgwXTsKICAgIGludCB0YWtlX251bTsKICAgIGludCB0YWtlc1s2XTsKICAgIHN0cnVjdCBTVFVERU5UKiBsZWZ0X25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y9pZD1uYW1lX3RvX2lkKHN0dWRlbnRzLG5hbWUpOyAgCiAgICAgICAgcHJpbnRmKCIlZCAiLGNvbnNlcXVlbmNlLT5pZCk7CiAgICAgICAgcHJpbnRmKCIlcyIsY29uc2VxdWVuY2UtPm5hbWUpOyAgICAgICAgICAKICAgIH0KICAgIGVsc2UgaWYoc2VsZWN0X251bT09MikKICAgIHsKICAgICAgICBsb2FkKGZwLHNlbGVjdF9udW0pOwogICAgICAgIHByaW50ZigicGxlYXNlIGVudGVyIGlkOiIpOwogICAgICAgIHNjYW5mKCIlZCIsJmlkKTsKICAgICAgICAgICAgICAgIAogICAgICAgIGluZGV4PXNlYXJjaChzdHVkZW50cyxpZCk7CiAgICAgICAgCiAgICAgICAgaWYoaW5kZXg9PS0xKQogICAgICAgIHsKICAgICAgICAgICAgcHJpbnRmKCJpZCBkb2VzbmB0IGV4aXN0LHByb2dyYW0gZW5kc1xuIik7CiAgICAgICAgICAgIGV4aXQoMSk7ICAgICAgICAgICAgCiAgICAgICAgfQogICAgfQogICAgCiAgICB3aGlsZSgxKQogICAgewogICAgICAgIHNob3coc3R1ZGVudHMsaWQsaW5kZXgpOwogICAgICAgIGlmKHN0dWRlbnRzW2luZGV4XS50YWtlX251bT09NSkKICAgICAgICB7CiAgICAgICAgICAgIHByaW50ZigiYWxsIGNvdXJzZXMgYXJlIHNlbGVjdGVkLHByb2dyYW0gZW5kcyIpOwogICAgICAgICAgICBicmVhazsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgfSAgICAgICAgCiAgICAgICAgcHJpbnRmKCLoq4vovLjlhaXoqrLnqIvku6PomZ/vvIwtMee1kOadn+mBuOiqsiIpOwogICAgICAgIHNjYW5mKCIlZCIsJmNvdXJzZSk7CiAgICAgICAgaWYoY291cnNlPT0tMSkKICAgICAgICB7CiAgICAgICAgICAgIHByaW50Zigi56iL5byP57WQ5p2fXG4iKTsgCiAgICAgICAgICAgIGJyZWFrOyAgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcmVzdWx0PWFkZChzdHVkZW50cyxpZCxpbmRleCxjb3Vyc2UpOwogICAgICAgIGlmKHJlc3VsdD09MSkKICAgICAgICB7CiAgICAgICAgICAgIHByaW50Zigi6YG46Kqy5oiQ5YqfXG4iKTsgICAgICAgICAgICAgCiAgICAgICAgfQogICAgICAgIGVsc2UgaWYocmVzdWx0PT0yKQogICAgICAgIHsKICAgICAgICAgICAgcHJpbnRmKCLph43opIfpgbjoqrJcbiIpOyAgICAgICAgICAgIAogICAgICAgIH0KICAgICAgICBlbHNlIGlmKHJlc3VsdD09MykKICAgICAgICB7CiAgICAgICAgICAgIHByaW50Zigi54Sh5q2k6Kqy56iLXG4iKTsgICAgIAogICAgICAgIH0KICAgIH0KICAgIAogICAgZnA9Zm9wZW4oREFUQV9GSUxFLCJ3Iik7CiAgICBzYXZlKGZwLHN0dWRlbnRzKTsKICAgIGZjbG9zZShmcCk7CiAgICAKICAgIAogICAgcmV0dXJuIDA7Cn0KCi8qdm9pZCBpbml0KHN0cnVjdCBTVFVERU5UIHN0dWRlbnRzW10pCnsKICAgIGludCBpLGo7CiAgICBmb3IoaT0xO2k8MjU2O2krKykKICAgIHsKICAgICAgICBzdHVkZW50c1tpXS5pZD0tMTsgCiAgICAgICAgLy9zdHVkZW50c1tpXS5uYW1lWzgwXT17MH07CiAgICAgICAgbWVtc2V0KHN0dWRlbnRzW2ldLm5hbWUsMCxzaXplb2Yoc3R1ZGVudHNbaV0ubmFtZSkpOyAgICAgICAgICAgICAgICAKICAgIH0gICAgIAp9Ki8KCnZvaWQgbG9hZChGSUxFKiBmcCxpbnQgc2VsZWN0X251bSkKewogICAgaW50IGksaixrOwogICAgaT0wOwogICAgLy9pZihzZWxlY3RfbnVtPT0yKS8qZW50ZXIgaWQqLwogICAgewogICAgICAgIHdoaWxlKCFmZW9mKGZwKSkKICAgICAgICB7CiAgICAgICAgICAgIGk9aSsxOwogICAgICAgICAgICBzdHJ1Y3QgU1RVREVOVCogc3R1ZGVudF9wdHI9KHN0cnVjdCBTVFVERU5UKiltYWxsb2Moc2l6ZW9mKHN0cnVjdCBTVFVERU5UKSk7CiAgICAgICAgICAgLy8gcHJpbnRmKCIlZFxuIixpKTsKICAgICAgICAgICAgZnNjYW5mKGZwLCIlZCIsJihzdHVkZW50X3B0ci0+aWQpKTsKICAgICAgICAgICAgZnNjYW5mKGZwLCIlcyIsc3R1ZGVudF9wdHItPm5hbWUpOwogICAgICAgICAgICBmc2NhbmYoZnAsIiVkIiwmKHN0dWRlbnRfcHRyLT50YWtlX251bSkpOwogICAgICAgICAgIC8vIHByaW50ZigiJWQgIixzdHVkZW50X3B0ci0+aWQpOwogICAgICAgICAgIC8vIHByaW50ZigiJWRcbiIsc3R1ZGVudF9wdHItPnRha2VfbnVtKTsKICAgICAgICAgICAgZm9yKGo9MTtqPD0oc3R1ZGVudF9wdHItPnRha2VfbnVtKTtqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGZzY2FuZihmcCwiJWQiLCYoc3R1ZGVudF9wdHItPnRha2VzW2pdKSk7ICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgc3R1ZGVudF9wdHItPmxlZnRfbmV4dD1OVUxMOwogICAgICAgICAgICBzdHVkZW50X3B0ci0+cmlnaHRfbmV4dD1OVUxMOwogICAgICAgICAgICAKICAgICAgICAgICAgbWFrZV90cmVlKHN0dWRlbnRfcHRyLCZoZWFkLHNlbGVjdF9udW0pOwogICAgICAgICAgICAvL3ByaW50ZigiJWRcbiIsaSk7ICAgICAgIAogICAgICAgIH0gCiAgICAgICAgdHJhdmVyc2VfdHJlZSgmaGVhZCk7CiAgICB9CiAgICAKfQoKaW50IHNlYXJjaChzdHJ1Y3QgU1RVREVOVCBzdHVkZW50c1tdLGludCBpZCkKewogICAgaW50IGksaW5kZXg7CiAgICBpbnQgajsKICAgIHN0cnVjdCBTVFVERU5UIHRlbXA7Ly/os4fmlpnmmqvlrZjomZUgCiAgICBpbmRleD0tMTsKICAgIC8qZm9yKGk9MTtpPDI1NjtpKyspCiAgICB7CiAgICAgICAgaWYoc3R1ZGVudHNbaV0uaWQ9PWlkKQogICAgICAgIHsKICAgICAgICAgICAgaW5kZXg9aTsKICAgICAgICAgICAgYnJlYWs7ICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgIH0gICAgICAgICAgICAgICAgICAKICAgIH0qLwogICAgCiAgICAvL2luc2VydGlvbiBzb3J0CiAgICBmb3IoaT0yO2k8MjU2O2krKykKICAgIHsKICAgICAgICAvKnRlbXAuaWQ9c3R1ZGVudHNbaV0uaWQ7Ly/mmqvlrZjos4fmlpkgCiAgICAgICAgdGVtcC5uYW1lPXN0dWRlbnRzW2ldLm5hbWU7CiAgICAgICAgdGVtcC50YWtlX251bT1zdHVkZW50c1tpXS50YWtlX251bTsKICAgICAgICB0ZW1wLnRha2VzPXN0dWRlbnRzW2ldLnRha2VzOyovCiAgICAgICAgLy9wcmludGYoIiVkICVkXG4iLGksc3R1ZGVudHNbaV0uaWQpOwogICAgICAgIHRlbXA9c3R1ZGVudHNbaV07CiAgICAgICAgZm9yKGo9aS0xO2o+PTE7ai0tKQogICAgICAgIHsKICAgICAgICAgICAgaWYoc3R1ZGVudHNbal0uaWQ8dGVtcC5pZCkKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBlbHNlIGlmKHN0dWRlbnRzW2pdLmlkPj10ZW1wLmlkKSAgCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHN0dWRlbnRzW2orMV09c3R1ZGVudHNbal07ICAgIAogICAgICAgICAgICB9ICAgICAgICAgICAgICAgICAgICAKICAgICAgICB9IAogICAgICAgIAogICAgICAgIGlmKGo9PWktMSkKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgZWxzZSAgICAKICAgICAgICAgICAgc3R1ZGVudHNbaisxXT10ZW1wOyAgICAgICAgICAgICAgICAgCiAgICB9LyppbnNlcnRpb24gc29ydCovCiAgICAKICAgIGZvcihpPTE7aTwyNTY7aSsrKQogICAgewogICAgICAvL3ByaW50ZigiJWQgJWRcbiIsaSxzdHVkZW50c1tpXS5pZCk7ICAgICAgICAgICAgICAgCiAgICB9CiAgICAvL3N5c3RlbSgicGF1c2UiKTsKICAgIC8vYmluYXJ5IHNlYXJjaCBzdHVkZW50CiAgICAvL2luZGV4PWJfc2VhcmNoKHN0dWRlbnRzLGlkLDEsMjU1KTsKICAgIHByaW50ZigiJWQiLGluZGV4KTsKICAgIHJldHVybiBpbmRleDsKfQoKaW50IHNlYXJjaF9ieV9uYW1lKHN0cnVjdCBTVFVERU5UIHN0dWRlbnRzW10sY2hhciogbmFtZSkKewogICAgaW50IGksaixpbmRleD0tMTsKICAgIGludCBjb21wX3Jlc3VsdDsKICAgIHN0cnVjdCBTVFVERU5UIHRlbXA7CiAgICAvKmZvcihpPTE7aTwyNTY7aSsrKQogICAgewogICAgICAgIGlmKHN0cmNtcChzdHVkZW50c1tpXS5uYW1lLG5hbWUpPT0wKQogICAgICAgIHsKICAgICAgICAgICAgaW5kZXg9aTsKICAgICAgICAgICAgYnJlYWs7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgfSAgICAgICAgICAgICAgICAgIAogICAgfSAgICovCiAgICAKICAgIC8qc29ydGluZyBzdHJpbmcgYnkgYnViYmxlIHNvcnQqLwogICAgZm9yKGo9MjU1O2o+PTI7ai0tKQogICAgewogICAgICAgIGZvcihpPTE7aTxqO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGNvbXBfcmVzdWx0PXN0cmNtcChzdHVkZW50c1tpXS5uYW1lLHN0dWRlbnRzW2krMV0ubmFtZSk7IAogICAgICAgICAgICBpZihjb21wX3Jlc3VsdD09MSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdGVtcD1zdHVkZW50c1tpXTsKICAgICAgICAgICAgICAgIHN0dWRlbnRzW2ldPXN0dWRlbnRzW2krMV07CiAgICAgICAgICAgICAgICBzdHVkZW50c1tpKzFdPXRlbXA7ICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZihjb21wX3Jlc3VsdD09MHx8Y29tcF9yZXN1bHQ9PS0xKQogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgfSAKICAgIH0KICAgIGZvcihpPTE7aTwyNTY7aSsrKQogICAgewogICAgICAvL3ByaW50ZigiJWQgJXNcbiIsaSxzdHVkZW50c1tpXS5uYW1lKTsgICAgICAgICAgICAgICAKICAgIH0KICAgIGluZGV4PWJfc2VhcmNoX25hbWUoc3R1ZGVudHMsbmFtZSwxLDI1NSk7CiAgICAvL3ByaW50ZigiJWQiLGluZGV4KTsKICAgIC8vc3lzdGVtKCJwYXVzZSIpOwogICAgcmV0dXJuIGluZGV4Owp9CgoKdm9pZCBzaG93KHN0cnVjdCBTVFVERU5UIHN0dWRlbnRzW10saW50IGlkLGludCBpbmRleCkKewogICAgaW50IGk7CiAgICBwcmludGYoIuaCqOW3sumBuOS7peS4i+iqsueoizpcbiIpOwogICAgZm9yKGk9MTtpPD1zdHVkZW50c1tpbmRleF0udGFrZV9udW07aSsrKQogICAgewogICAgICAgIHByaW50ZigiJWQiLHN0dWRlbnRzW2luZGV4XS50YWtlc1tpXSk7CiAgICAgICAgcHJpbnRmKCIgIik7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgIH0gICAgIAogICAgcHJpbnRmKCJcbiIpOwp9CgppbnQgYWRkKHN0cnVjdCBTVFVERU5UIHN0dWRlbnRzW10saW50IGlkLGludCBpbmRleCxpbnQgY291cnNlKQp7CiAgICBpbnQgaTsKICAgIGlmKGNvdXJzZT49MTAxJiZjb3Vyc2U8PTEwNSkKICAgIHsKICAgICAgICBmb3IoaT0xO2k8PXN0dWRlbnRzW2luZGV4XS50YWtlX251bTtpKyspCiAgICAgICAgewogICAgICAgICAgICBpZihzdHVkZW50c1tpbmRleF0udGFrZXNbaV09PWNvdXJzZSkKICAgICAgICAgICAgICAgIHJldHVybiAyOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICB9ICAKICAgICAgICBzdHVkZW50c1tpbmRleF0udGFrZXNbaV09Y291cnNlOwogICAgICAgIHN0dWRlbnRzW2luZGV4XS50YWtlX251bT1zdHVkZW50c1tpbmRleF0udGFrZV9udW0rMTsKICAgICAgICByZXR1cm4gMTsgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgfSAgICAKICAgIHJldHVybiAzOwp9Cgp2b2lkIHNhdmUoRklMRSogZnAsc3RydWN0IFNUVURFTlQgc3R1ZGVudHNbXSkKewogICAgaW50IGksajsKICAgIGZvcihpPTE7aTwyNTY7aSsrKQogICAgewogICAgICAgIGlmKHN0dWRlbnRzW2ldLmlkIT0tMSkKICAgICAgICB7CiAgICAgICAgICAgIGZwcmludGYoZnAsIiVkIixzdHVkZW50c1tpXS5pZCk7CiAgICAgICAgICAgIGZwcmludGYoZnAsIlx0Iik7CiAgICAgICAgICAgIGZwcmludGYoZnAsIiVzIixzdHVkZW50c1tpXS5uYW1lKTsKICAgICAgICAgICAgZnByaW50ZihmcCwiXHQiKTsKICAgICAgICAgICAgZnByaW50ZihmcCwiJWQiLHN0dWRlbnRzW2ldLnRha2VfbnVtKTsKICAgICAgICAgICAgZnByaW50ZihmcCwiXHQiKTsKICAgICAgICAgICAgZm9yKGo9MTtqPD1zdHVkZW50c1tpXS50YWtlX251bTtqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGZwcmludGYoZnAsIiVkIixzdHVkZW50c1tpXS50YWtlc1tqXSk7CiAgICAgICAgICAgICAgICBmcHJpbnRmKGZwLCJcdCIpOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICB9ICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgZnByaW50ZihmcCwiXG4iKTsgICAgIAogICAgICAgIH0gICAgICAgICAgICAgICAgICAKICAgIH0gICAgIAp9CgpzdHJ1Y3QgU1RVREVOVCogYl9zZWFyY2goc3RydWN0IFNUVURFTlQqIGhlYWQsaW50IGlkLGNoYXIqIG5hbWUsaW50IHNlbGVjdF9udW0pCnsKICAgIC8vcHJpbnRmKCIlZCAiLHNlbGVjdF9udW0pOwogICAgaWYoc2VsZWN0X251bT09MSkKICAgIHsgICAgCiAgICAgICAgaWYoaGVhZD09TlVMTCkKICAgICAgICB7CiAgICAgICAgICAgIHByaW50ZigiJXMiLGhlYWQtPm5hbWUpOwogICAgICAgICAgICByZXR1cm4gTlVMTDsgICAgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihoZWFkIT1OVUxMKQogICAgICAgIHsKICAgICAgICAgICAgaWYoc3RyY21wKChoZWFkKS0+bmFtZSxuYW1lKT4wKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBiX3NlYXJjaChoZWFkLT5sZWZ0X25leHQsLTEsbmFtZSwxKTsKICAgICAgICAgICAgfSAgICAgICAgIAogICAgICAgICAgICBlbHNlIGlmKHN0cmNtcCgoaGVhZCktPm5hbWUsbmFtZSk8MCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgYl9zZWFyY2goaGVhZC0+cmlnaHRfbmV4dCwtMSxuYW1lLDEpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYoc3RyY21wKChoZWFkKS0+bmFtZSxuYW1lKT09MCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcHJpbnRmKCIlcyAlZCAiLGhlYWQtPm5hbWUsaGVhZC0+aWQpOwogICAgICAgICAgICAgICAgcmV0dXJuIGhlYWQ7ICAgICAKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIAp9CgppbnQgYl9zZWFyY2hfbmFtZShzdHJ1Y3QgU1RVREVOVCBzdHVkZW50c1tdLGNoYXIqIG5hbWUsaW50IGhlYWQsaW50IHRhaWwpCnsKICAgIGludCBjb21wX3Jlc3VsdDsKICAgIGludCBpbmRleD0tMTsKICAgIGlmKGhlYWQ9PXRhaWwpCiAgICB7CiAgICAgICAgaWYoc3RyY21wKG5hbWUsc3R1ZGVudHNbaGVhZF0ubmFtZSk9PTApCiAgICAgICAgICAgIHJldHVybiBoZWFkOwogICAgICAgIGVsc2UKICAgICAgICAgICAgcmV0dXJuIGluZGV4OyAgICAgICAgICAgICAgCiAgICB9CiAgICAKICAgICAgICBjb21wX3Jlc3VsdD1zdHJjbXAobmFtZSxzdHVkZW50c1soaGVhZCt0YWlsKS8yXS5uYW1lKTsKICAgICAgICAKICAgICAgICBpZihjb21wX3Jlc3VsdDwwKQogICAgICAgIHsKICAgICAgICAgICAgaW5kZXg9Yl9zZWFyY2hfbmFtZShzdHVkZW50cyxuYW1lLGhlYWQsKGhlYWQrdGFpbCkvMik7ICAKICAgICAgICAgICAgcmV0dXJuIGluZGV4OyAgICAgICAgICAgICAgICAKICAgICAgICB9IAogICAgICAgIGVsc2UgaWYoY29tcF9yZXN1bHQ9PTApCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gKGhlYWQrdGFpbCkvMjsgICAgIAogICAgICAgIH0gICAKICAgICAgICBlbHNlIGlmKGNvbXBfcmVzdWx0PjApCiAgICAgICAgewogICAgICAgICAgICBpbmRleD1iX3NlYXJjaF9uYW1lKHN0dWRlbnRzLG5hbWUsKChoZWFkK3RhaWwpLzIpKzEsdGFpbCk7ICAgCiAgICAgICAgICAgIHJldHVybiBpbmRleDsgICAgICAgICAgICAgICAKICAgICAgICB9Cn0KCnZvaWQgbWFrZV90cmVlKHN0cnVjdCBTVFVERU5UKiBzdHVkZW50X3B0cixzdHJ1Y3QgU1RVREVOVCoqIGhlYWQsaW50IHNlbGVjdF9udW0pCnsKICAgICBpZihzZWxlY3RfbnVtPT0xKQogICAgIHsKICAgICAgICAgaWYoKmhlYWQ9PU5VTEwpICAgICAgICAgICAgICAgICAKICAgICAgICAgewogICAgICAgICAgICAgKmhlYWQ9c3R1ZGVudF9wdHI7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgfQogICAgICAgICBlbHNlIGlmKCpoZWFkIT1OVUxMKQogICAgICAgICB7CiAgICAgICAgICAgICBpZihzdHJjbXAoKCpoZWFkKS0+bmFtZSxzdHVkZW50X3B0ci0+bmFtZSk+MCkKICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICBtYWtlX3RyZWUoc3R1ZGVudF9wdHIsJigoKmhlYWQpLT5sZWZ0X25leHQpLHNlbGVjdF9udW0pOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgIH0gICAgIAogICAgICAgICAgICAgZWxzZSBpZihzdHJjbXAoKCpoZWFkKS0+bmFtZSxzdHVkZW50X3B0ci0+bmFtZSk8PTApCiAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgbWFrZV90cmVlKHN0dWRlbnRfcHRyLCYoKCpoZWFkKS0+cmlnaHRfbmV4dCksc2VsZWN0X251bSk7ICAgICAKICAgICAgICAgICAgIH0KICAgICAgICAgfQogICAgIH0KICAgICBlbHNlIGlmKHNlbGVjdF9udW09PTIpCiAgICAgeyAgIAogICAgICAgIGlmKCpoZWFkPT1OVUxMKQogICAgICAgIHsKICAgICAgICAgICAgKmhlYWQ9c3R1ZGVudF9wdHI7CiAgICAgICAgICAgLy8gcHJpbnRmKCIlZFxuIiwoKmhlYWQpLT5pZCk7CiAgICAgICAgICAgIC8qY3VycmVudD1zdHVkZW50X3B0cjsgCiAgICAgICAgICAgIHByZXZpb3VzPXN0dWRlbnRfcHRyOyovICAgICAgICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZWxzZSBpZigqaGVhZCE9TlVMTCkKICAgICAgICB7CiAgICAgICAgICAgIGlmKCgqaGVhZCktPmlkPj1zdHVkZW50X3B0ci0+aWQpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8qcHJldmlvdXM9Y3VycmVudDsKICAgICAgICAgICAgICAgIGN1cnJlbnQ9aGVhZC0+bGVmdF9uZXh0OyovCiAgICAgICAgICAgICAgICBpZigoKmhlYWQpLT5sZWZ0X25leHQ9PU5VTEwpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgKCpoZWFkKS0+bGVmdF9uZXh0PXN0dWRlbnRfcHRyOyAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlIGlmKCgqaGVhZCktPmxlZnRfbmV4dCE9TlVMTCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBtYWtlX3RyZWUoc3R1ZGVudF9wdHIsJigoKmhlYWQpLT5sZWZ0X25leHQpLHNlbGVjdF9udW0pOwogICAgICAgICAgICAgICAgfSAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKCgqaGVhZCktPmlkPHN0dWRlbnRfcHRyLT5pZCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYoKCpoZWFkKS0+cmlnaHRfbmV4dD09TlVMTCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAoKmhlYWQpLT5yaWdodF9uZXh0PXN0dWRlbnRfcHRyOyAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB9ICAgICAKICAgICAgICAgICAgICAgIGVsc2UgaWYoKCpoZWFkKS0+cmlnaHRfbmV4dCE9TlVMTCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBtYWtlX3RyZWUoc3R1ZGVudF9wdHIsJigoKmhlYWQpLT5yaWdodF9uZXh0KSxzZWxlY3RfbnVtKTsgICAgIAogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9ICAgICAKICAgICAgICB9ICAKICAgICB9IAogICAgLy9wcmludGYoIiVzICVkXG4iLGhlYWQtPm5hbWUsaGVhZC0+aWQpOwp9Cgp2b2lkIHRyYXZlcnNlX3RyZWUoc3RydWN0IFNUVURFTlQqKiBoZWFkKQp7CiAgICBpZigqaGVhZD09TlVMTCkKICAgIHsKICAgICAgICByZXR1cm47ICAgICAgICAgICAgICAKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBwcmludGYoIiVzICVkXG4iLCgqaGVhZCktPm5hbWUsKCpoZWFkKS0+aWQpOwogICAgICAgIHRyYXZlcnNlX3RyZWUoJigoKmhlYWQpLT5sZWZ0X25leHQpKTsKICAgICAgICB0cmF2ZXJzZV90cmVlKCYoKCpoZWFkKS0+cmlnaHRfbmV4dCkpOyAgCiAgICB9ICAgCn0K