int main(void) { DARR *handle = NULL; int n; int *val = NULL; handle = darr_create(sizeof(int)); while (1) { printf("please input num : "); scanf("%d", &n); if (n == -1) break; /*darr_append(&n, handle);*/ /*darr_prepend(&n, handle);*/ darr_insert(&n, PREPEND, handle); } if (!darr_store("./db", handle)) printf("存储成功!\n"); /*n = 9999;*/ /*darr_insert(&n, 3, handle);*/ val = darr_ind(2, handle); printf("val = %d\n", *val); darr_travel(ls, handle); printf("\n"); darr_ind_del(1, handle); darr_travel(ls, handle); printf("\n"); darr_sort(cmp, handle); darr_travel(ls, handle); printf("\n"); darr_destroy(&handle); return 0; }
/* * NAME: sortfiles() * DESCRIPTION: arrange files in order according to sort selection */ static void sortfiles(darray *files, int flags, int options) { int (*compare)(const queueent *, const queueent *); switch (options & S_MASK) { case S_NAME: compare = compare_names; break; case S_TIME: switch (options & T_MASK) { case T_MOD: compare = compare_mtimes; break; case T_CREATE: compare = compare_ctimes; break; default: abort(); } break; case S_SIZE: compare = compare_sizes; break; default: return; } reverse = (flags & HLS_REVERSE) ? -1 : 1; darr_sort(files, (int (*)(const void *, const void *)) compare); }
int main(void) { int i; DARR *sturoot; if ((sturoot = load_darr("db.db")) == NULL){ printf("load db error.\n"); if ((sturoot = create_darr(sizeof(struct stu))) == NULL) { printf("create date error.\n"); return -1; } } /* ** 0,q for exit; 1,f for find; 2, i for insert; 3,d for delete, 4, l for list, 5, e for erase; ** 6, a for delete duplication; 7, u for update, 8, s for sort, 9 for Save data, @ for Load data; */ while ((i = read_select()) != '0' && i != 'q' && i != 'Q') /* 主循环,获取控制输入*/ switch (i){ case '1': case 'F': case 'f': printf("Please enter the search condition: [ID],[Name],[Scope]\n"); { struct stu tmp, *ret; if (getline(&tmp) == 0) { printf("Input Format wrong!\n"); } else if ((ret =(struct stu *)find_node(sturoot,&tmp,cmp)) == NULL) printf("Record is not exist!"); else print_node(ret); } any_key(); break; case '2': case 'I': case 'i': if (sturoot == NULL) { printf("Datebase is empty, System will re-create it.\n"); if ((sturoot = create_darr(sizeof(struct stu))) == NULL) { printf("DB is empty,create date error.\n"); return -1; } } printf("Please enter the new record: 'ID,Name,Scope:'\n"); { struct stu tmp; if (getline(&tmp) == 0) printf("Input Format wrong!\n"); else if (inserd_node(sturoot,&tmp,0) == -1) printf("Input Error!"); else printf("Insert OK!"); } any_key(); break; case '3': case 'D': case 'd': printf("Please enter the condition : [ID],[Name],[Scope]\n"); { struct stu tmp; if (getline(&tmp) == 0) printf("Input Format wrong!"); else delete_node(sturoot,&tmp, &cmp); } any_key(); break; case '4': case 'L': case 'l': travel(sturoot,print_node); any_key(); break; case '5': case 'E': case 'e': erase(sturoot); printf("All data has erase!!"); sturoot = NULL; any_key(); break; #if 0 case '6': case 'A': case 'a': printf("Please enter the condition: [ID],[Name],[Scope]\n"); { struct stu tmp; if (getline(&tmp) == 0) { printf("Input Format wrong!"); } else if (delete(&sturoot, &tmp, DELETE_ALL_YES) == 0) printf("Record is not exist!"); } any_key(); break; #endif case '7': case 'U': case 'u': { struct stu tmp_from, tmp_to, *tmp_find; printf("Please enter the condition: [ID],[Name],[Scope]\n"); if (getline(&tmp_from) == 0) { printf("Input Format wrong!"); } else if ((tmp_find = (struct stu *)find_node(sturoot,&tmp_from,cmp)) != NULL) { print_node(tmp_find); printf("Please enter the new data: [ID],[Name],[Scope]\n"); if (getline(&tmp_to) == 0) printf("Input Format wrong!"); else if (update_node(tmp_find, &tmp_to) == 0) printf("Update Succeed."); } else printf("Record is not exist!"); } any_key(); break; case '8': case 'S': case 's': { int sel; int (*fun)(const void *, const void*); printf("Please select sort by:\n1[by ID]\t2[by name]\t3[by scope]:"); if ((sel=get_num()) <= 3 && sel >= 1) { switch (sel) { case 1: fun = cmp_id; break; case 2: fun = cmp_name; break; case 3: fun = cmp_scope; break; } darr_sort(sturoot,fun); printf("Sort Succeed."); } else printf("Select wrong."); } any_key(); break; case '9': if (save_darr(sturoot, "db.db") == -1) fprintf(stderr, "Open file failed.\n"); any_key(); break; default: break; } return 0; }