⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 books.c

📁 图书管理系统  给予文件的 可以运行的  在MINGW上运行
💻 C
字号:
/* file_name:	books.c
 * author:	wangtiezhen miuliang @ Xidian University
 * description:	
 */

/* Begin:
 * read books data from the file;
 */
extern int readbook(void);
static struct book *inputbook(int , char* ,char* ,int );

int readbook(void) {
        FILE *fp_book;
        int i = 0;
        if( (fp_book = fopen("book", "r") ) == NULL) {
                printf("It cann't opean file\n");
                return 0;
                /* Cannot open the file */
        }
        while( !feof(fp_book) ) {
                int num, price;
                char name[20], writer[20];

                if(fscanf(fp_book, "%d%s%s%d", &num, name, writer, &price) == 4){
//              	printf("reading..\t%d\t%s\t%s\t%d\n", num, name, writer, price);
                	inputbook(num, name, writer,price);
                }
                
                if(!feof(fp_book)) {
                        i++;
                }
        }
        fclose(fp_book);
        return i;
}
struct book *inputbook(int num, char name[],  char writer[],int price) {
        struct book *p,*q;
        p=(struct book *)malloc(sizeof(struct book));
        p->bnum = num;
        strcpy(p->bname,name);
        strcpy(p->bwriter,writer);
        p->bprice = price;
        p->lchild=NULL;
        p->rchild=NULL;
        if((p->bnum)!=0) {
                q=bookbt;
                if(q==NULL)
                        bookbt=p;

                else {
                        while((q->lchild!=p)&&(q->rchild)!=p) {
                                if((p->bnum)<(q->bnum)) {

                                        if(q->lchild!=NULL)
                                                q=q->lchild;
                                        else
                                                q->lchild=p;
                                } else {
                                        if(q->rchild!=NULL) {
                                                q=q->rchild;
                                        } else
                                                q->rchild=p;
                                }
                        }

                }
        }
        //        print_book(p);
        return bookbt;
}
/* End:
 * read books data from the file;
 */


/* Begin:
 * tree travel;
 */
int print_book(struct book* root) {
        if(root == NULL){
        	printf("No find\n");
        	return -1;
        }
        //	putchar('p');
        printf("!!!num:%d\tname:%s\twriter:%s\tprice:%d\n", root->bnum, root->bname,
               root->bwriter, root->bprice);
        return 1;
}
int tree_visit_book(struct book* root, int f(), int style) {
        //	putchar('j');
        if(root == NULL) {
                return 0;
        }
        if(style == 0)
                (*f)(root);
        tree_visit_book (root->lchild, f, style );
        if(style == 1)
                (*f)(root);
        tree_visit_book (root->rchild, f, style );
        if(style == 2)
                (*f)(root);
}
/* End:
 * tree travel;
 */


struct book *find_book_num(struct book *bookbt,int bnum) {
        struct book *p;
        p=bookbt;
        if(p==NULL)
                return NULL;
        if(bnum==p->bnum)
                return(p);
        else if(bnum >  p ->bnum) {
                return find_book_num(p->rchild, bnum );
        } else if(bnum <  p ->bnum) {
                return find_book_num(p->lchild, bnum );
        }
}

struct book* find_book_name(struct book* root, char* find_name) {
        struct book* t = NULL;
        //	putchar('j');
        if(root == NULL) {
                return NULL;
        }
        if(!strcmp(find_name , root->bname)) {
                //		printf("find it!");
                return root;
        }
//	putchar('J');
        if((t = find_book_name (root->lchild, find_name )) != NULL) {
                return t;
        }
        if((t = find_book_name (root->rchild, find_name )) != NULL) {
                return t;
        }
        //	printf("\t");
        return NULL;
}

struct book* find_book_writer(struct book* root, char* writer_name) {
        struct book* t = NULL;
        //	putchar('j');
        if(root == NULL) {
                return NULL;
        }
        if(!strcmp(writer_name , root->bwriter)) {
                //		printf("find it!");
                return root;
        }
        //	putchar('J');
        if((t = find_book_writer (root->lchild, writer_name )) != NULL) {
                return t;
        }
        if((t = find_book_writer (root->rchild, writer_name )) != NULL) {
                return t;
        }
        //	printf("\t");
        return NULL;
}


/* Begin:
 * book del
 */
extern int book_del(bookpointer *);
extern int book_del_value(bookpointer *, int );

int book_del_value(bookpointer *proot, int num){
	bookpointer root = *proot;
	if(root == NULL){
		return 0;
	}
	if(root ->bnum == num){
		book_del(proot);
	}else if(num >  root ->bnum){ 
		if(root -> rchild == NULL){
			return 0;
		}
		if(root ->rchild -> bnum == num){
			return book_del( &(root->rchild));
		}else{
			return book_del_value(&(root->rchild), num );
		}
	}else if(num <  root ->bnum){
		if(root -> lchild == NULL){
			return 0;
		}
		if(root ->lchild -> bnum == num){
			return book_del( &(root->lchild));
		}else{
			return book_del_value(&(root->lchild), num );
		}	
	}
}
int book_del(bookpointer *proot){
	bookpointer root = *proot;
	if(root == NULL)
		return 0;
	if(root -> lchild == NULL){
		*proot = root -> rchild;
		free(root);
	}else if(root -> rchild == NULL){
		*proot = root -> lchild;
		free(root);
	}else{
		bookpointer temp = root -> lchild;
		while (temp->rchild != NULL){
			temp = temp -> rchild;
		}
		temp -> rchild = root -> rchild;
		free(root);
	}
}
 
/* End:
 * book del
 */
 
 
 
 
int book_destory(bookpointer root){
	if(root == NULL){
		return 0;
	}
	book_destory (root->lchild);
	book_destory (root->rchild);
//	printf("Killing num:\t%ld\t\n", root->bnum);
	free(root);
	
}
 
/* Begin:
 * book update;
 */
struct book * book_update(int src_num,
			char* pname, char* pwriter,
			int *pprice, int* pnum)
{
//	putchar('a');
	struct book* p = find_book_num(bookbt, src_num);
//	putchar('s');
	char n_name[20], n_writer[20];
	int n_price, n_num;
	
	if(pname != NULL){
		strcpy(n_name, pname);
	}else{
		strcpy(n_name, p->bname);
	}
	
	if(pwriter != NULL){
		strcpy(n_writer, pwriter);
	}else{
		strcpy(n_writer, p->bwriter);
	}
	
	if(pnum != NULL){
		n_num = *pnum;
	}else{
		n_num = src_num;
	}
	
	if(pprice != NULL){
		n_price = *pprice;
	}else{
		n_price = p->bprice;
	}
	
	book_del_value(&bookbt, src_num);
	inputbook(n_num, n_name, n_writer, n_price);
	
}
 
 
/* End:
 * book update;
 */



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -