153 lines
2.6 KiB
C
153 lines
2.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
void read_input(char *buf,size_t size){
|
|
int ret ;
|
|
ret = read(0,buf,size);
|
|
if(ret <=0){
|
|
puts("Error");
|
|
_exit(-1);
|
|
}
|
|
}
|
|
|
|
struct heap {
|
|
size_t size ;
|
|
char *content ;
|
|
};
|
|
|
|
struct heap *heaparray[10];
|
|
|
|
void menu(){
|
|
puts("--------------------------------");
|
|
puts(" Heap Creator ");
|
|
puts("--------------------------------");
|
|
puts(" 1. Create a Heap ");
|
|
puts(" 2. Edit a Heap ");
|
|
puts(" 3. Show a Heap ");
|
|
puts(" 4. Delete a Heap ");
|
|
puts(" 5. Exit ");
|
|
puts("--------------------------------");
|
|
printf("Your choice :");
|
|
}
|
|
|
|
void create_heap(){
|
|
int i ;
|
|
char buf[8];
|
|
size_t size = 0;
|
|
for(i = 0 ; i < 10 ; i++){
|
|
if(!heaparray[i]){
|
|
heaparray[i] = (struct heap *)malloc(sizeof(struct heap));
|
|
if(!heaparray[i]){
|
|
puts("Allocate Error");
|
|
exit(1);
|
|
}
|
|
printf("Size of Heap : ");
|
|
read(0,buf,8);
|
|
size = atoi(buf);
|
|
heaparray[i]->content = (char *)malloc(size);
|
|
if(!heaparray[i]->content){
|
|
puts("Allocate Error");
|
|
exit(2);
|
|
}
|
|
heaparray[i]->size = size ;
|
|
printf("Content of heap:");
|
|
read_input(heaparray[i]->content,size);
|
|
puts("SuccessFul");
|
|
break ;
|
|
}
|
|
}
|
|
}
|
|
|
|
void edit_heap(){
|
|
int idx ;
|
|
char buf[4];
|
|
printf("Index :");
|
|
read(0,buf,4);
|
|
idx = atoi(buf);
|
|
if(idx < 0 || idx >= 10){
|
|
puts("Out of bound!");
|
|
_exit(0);
|
|
}
|
|
if(heaparray[idx]){
|
|
printf("Content of heap : ");
|
|
read_input(heaparray[idx]->content,heaparray[idx]->size+1);
|
|
puts("Done !");
|
|
}else{
|
|
puts("No such heap !");
|
|
}
|
|
}
|
|
|
|
void show_heap(){
|
|
int idx ;
|
|
char buf[4];
|
|
printf("Index :");
|
|
read(0,buf,4);
|
|
idx = atoi(buf);
|
|
if(idx < 0 || idx >= 10){
|
|
puts("Out of bound!");
|
|
_exit(0);
|
|
}
|
|
if(heaparray[idx]){
|
|
printf("Size : %ld\nContent : %s\n",heaparray[idx]->size,heaparray[idx]->content);
|
|
puts("Done !");
|
|
}else{
|
|
puts("No such heap !");
|
|
}
|
|
|
|
}
|
|
|
|
void delete_heap(){
|
|
int idx ;
|
|
char buf[4];
|
|
printf("Index :");
|
|
read(0,buf,4);
|
|
idx = atoi(buf);
|
|
if(idx < 0 || idx >= 10){
|
|
puts("Out of bound!");
|
|
_exit(0);
|
|
}
|
|
if(heaparray[idx]){
|
|
free(heaparray[idx]->content);
|
|
free(heaparray[idx]);
|
|
heaparray[idx] = NULL ;
|
|
puts("Done !");
|
|
}else{
|
|
puts("No such heap !");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
int main(){
|
|
char buf[4];
|
|
setvbuf(stdout,0,2,0);
|
|
setvbuf(stdin,0,2,0);
|
|
while(1){
|
|
menu();
|
|
read(0,buf,4);
|
|
switch(atoi(buf)){
|
|
case 1 :
|
|
create_heap();
|
|
break ;
|
|
case 2 :
|
|
edit_heap();
|
|
break ;
|
|
case 3 :
|
|
show_heap();
|
|
break ;
|
|
case 4 :
|
|
delete_heap();
|
|
break ;
|
|
case 5 :
|
|
exit(0);
|
|
break ;
|
|
default :
|
|
puts("Invalid Choice");
|
|
break;
|
|
}
|
|
|
|
}
|
|
return 0 ;
|
|
}
|