voidmenu(){ puts("----------------------------"); puts("Bamboobox Menu"); puts("----------------------------"); puts("1.show the items in the box"); puts("2.add a new item"); puts("3.change the item in the box"); puts("4.remove the item in the box"); puts("5.exit"); puts("----------------------------"); printf("Your choice:"); }
voidshow_item(){ int i ; if(!num){ puts("No item in the box"); }else{ for(i = 0 ; i < 100; i++){ if(itemlist[i].name){ printf("%d : %s",i,itemlist[i].name); } } puts(""); } }
intadd_item(){
char sizebuf[8] ; int length ; int i ; int size ; if(num < 100){ printf("Please enter the length of item name:"); read(0,sizebuf,8); length = atoi(sizebuf); if(length == 0){ puts("invaild length"); return0; } for(i = 0 ; i < 100 ; i++){ if(!itemlist[i].name){ itemlist[i].size = length ; itemlist[i].name = (char*)malloc(length); printf("Please enter the name of item:"); size = read(0,itemlist[i].name,length); itemlist[i].name[size] = '\x00'; num++; break; } } }else{ puts("the box is full"); } return0; }
voidchange_item(){
char indexbuf[8] ; char lengthbuf[8]; int length ; int index ; int readsize ;
if(!num){ puts("No item in the box"); }else{ printf("Please enter the index of item:"); read(0,indexbuf,8); index = atoi(indexbuf); if(itemlist[index].name){ printf("Please enter the length of item name:"); read(0,lengthbuf,8); length = atoi(lengthbuf); printf("Please enter the new name of the item:"); readsize = read(0,itemlist[index].name,length); *(itemlist[index].name + readsize) = '\x00'; }else{ puts("invaild index"); } }
}
voidremove_item(){ char indexbuf[8] ; int index ;
if(!num){ puts("No item in the box"); }else{ printf("Please enter the index of item:"); read(0,indexbuf,8); index = atoi(indexbuf); if(itemlist[index].name){ free(itemlist[index].name); itemlist[index].name = 0 ; itemlist[index].size = 0 ; puts("remove successful!!"); num-- ; }else{ puts("invaild index"); } } }