어디가 에러인지 모르겠습니다. 설명좀해주세요.
움찬
질문 제목 : 질문 내용 :
이 소스가 트리로 짠 소스인데요...트리노드가 저장이 안됩니다. 도서정보를 저장해야하는데 저장이 안되요
저장인 됬는데 조회가 안되는지도 모르겠고....
소스 설명좀 부탁드립니다. 특히 빨간부분좀....
#include stdio.h
#include stdlib.h
#include string.h
typedef struct{
int book_num;
int checkout;
int book_locat;
char title[256];
char borrower[256];
}bookinfo;
typedef struct treenode{
bookinfo key;
struct treenode *left, *right;
}treenode;
//int model_init()
void model_insert_book(treenode **root, bookinfo key)
{
treenode *p, *t;
treenode *n;
t=*root;
p=null;
while(t != null){
if(key.book_num == t-key.book_num);
p=t;
if(key.book_num t-key.book_num) t=t-left;
else t=t-right;
}
n=(treenode *)malloc(sizeof(treenode));
if(n==null);
n-key=key;
n-left=n-right = null;
if(p!=null){
if(key.book_num p-key.book_num)
p-left=n;
else p-right=n;
}else{
*root=n;
printf(성공\n);
}
}
int model_delete_book(treenode **root, int key)
{
treenode *p, *child, *succ, *succ_p, *t;
p=null;
t=*root;
while(t!=null && t-key.book_num != key){
p=t;
t=(key t-key.book_num) ? t-left : t-right;
}
if(t==null){
printf(book is not in the tree\n);
return 0;
}
if((t-left==null) && (t-right==null)){
if(p!=null){
if(p-left==t)
p-left=null;
else p-right=null;
}
else
*root=null;}
else if((t-left==null) || (t-right==null)){
child=(t-left != null) ? t-left : t-right;
if(p != null){
if(p-left==t)
p-left=child;
else p-right=child;
}
else
*root=child;}
else{
succ_p=t;
succ=t-right;
while(succ-left != null){
succ_p=succ;
succ=succ-left;
}
if(succ_p-left == succ)
succ_p-left=succ-right;
else
succ_p-right=succ-right;
t-key=succ-key;
t=succ;
}
free(t);
return 1;
}
int model_get_num_of_books(treenode *root)
{
int count=0;
if(root !=null)
count = 1 + model_get_num_of_books(root-left) + model_get_num_of_books(root-right);
return count;
}
treenode *model_get_books(int idx, treenode *root)
{
while(root != null){
if(root-key.book_num == idx) return root;
else if(root-key.book_num idx)
root=root-left;
else
root=root-right;
}
return null;brnull;
}
int model_set_checkout(bookinfo idx,treenode *root, int checkout)
{
if(checkout !=0 && checkout != 1)
return 0;
while(root != null){
if(root-key.book_num == idx.book_num){
root-key.checkout=checkout;
strcpy(root-key.borrower, idx.borrower);//root-key.borrower=idx.borrower;
return 1;
}
else if(root-key.book_num idx.book_num)
root=root-left;
else
root=root-right;
}
return 0;
}
int view_init(treenode *root)
{
printf(도서관리 시스템: 도서수 %d\n, model_get_num_of_books(root));
return 0;
}
int view_top_menu()
{
int input;
while(1)
{
printf(==상위 메뉴==\n);
printf([1] 조회\n[2] 대출\n[3] 반납\n[4] 추가\n[5] 삭제\n[6] 종료\n);
printf(입력 : );
scanf(%d, &input);
if(input==1 || input==2 || input==3 || input==4 || input==5 || input==6)
return input;
}
return 0;
}
bookinfo view_insert_menu(bookinfo *b)
{
while(1){
printf(==추가 메뉴==\n);
printf(도서정보입력(번호, 위치, 책이름 순) : \n);
scanf(%d %d %s, &b-book_num, &b-book_locat, b-title);
}
}
int view_delete_menu()
{
int input;
while(1){
printf(==삭제 메뉴==\n);
printf(도서번호입력:\n);
scanf(%d, &input);
if (input 0)
return input;
}
return 0;
}
void view_checkout_menu(bookinfo *b)
{
while(1){
printf(==대출 메뉴==\n);
printf(도서번호입력:\n);
scanf(%d, &b-book_num);
printf(대출자입력: \n);
scanf(%s, b-borrower); }
return;
}
bookinfo view_checkin_menu()
{
bookinfo input;
strcpy(input.borrower,없음);
while(1){
printf(==반납 메뉴==\n);
printf(도서번호입력:\n);
scanf(%d, &input.book_num);
return input;
}
return;
}
int view_info_menu()
{
int input;
while(1){
printf(==조회 메뉴==\n);
printf(도서번호입력:\n);
scanf(%d, &input);
if (input 0)
return input;
}
return 0;
}
int view_book_info(int idx, treenode *root)
{
treenode *info;
info=model_get_books(idx, root);
if(info){
printf(도서 결과 조회\n);
printf(책 번호: %d\n책 위치: %d\n책 이름: %s\n대출자: %s\n대출상태: %d\n,
info-key.book_num, info-key.book_locat, info-key.title, info-key.borrower, info-key.checkout ? 대출중 : 대출가능);
return 1;
}else{
return 0;
}
}
int view_message(char *msg)
{
printf(메시지 : %s\n, msg);
return 1;
}
int con_insert(treenode *root, bookinfo *in)
{
model_insert_book(&root, in);
view_message(추가하였습니다.\n);
return 1;
}
int con_delete(treenode *root, int idx)
{
if(model_delete_book(&root, idx)){
view_message(삭제하였습니다.\n);
return 1;
}
return 0;
}
int con_checkin(bookinfo idx, treenode *root)
{
if (model_set_checkout(idx, root, 0)) {
view_message(반납하였습니다.\n);
return 1;
}
return 0;
}
int con_checkout(bookinfo checkout, treenode *root)
{
if (model_set_checkout(checkout, root, 1)) {
view_message(대출하였습니다.\n);
return 1;
}
return 0;
}
int con_info(int idx, treenode *root)
{
if (idx 0)
return view_book_info(idx, root);
else
return 0;
}
int main(int argc, char *argv[])
{
int input=0;
bookinfo a;
bookinfo *b=null;
treenode *root=null;
while(input != 6){
view_init(root);
input=view_top_menu();
switch(input){
case 1:
input=view_info_menu();
con_info(input, root);
break;
case 2:
view_checkout_menu(b);
con_checkout(a, root);
break;
case 3:
a=view_checkin_menu();
con_checkin(a, root);
break;
case 4:
view_insert_menu(b);
con_insert(root, b);
break;
case 5:
input=view_delete_menu();
con_delete(root, input);
break;
case 6:
break;
}
}
return 0;
}
-
플랜 2026-05-01
이거 작성하시는데 되게 오래 걸리 셨을거 같은데 문제도 같이 올려주시면 저도 재밌게 짜볼 수 있을거 같은데 올려주실 수 있으신가요?
-
치킨마루 2026-05-01
int con_insert(TreeNode *root, bookinfo *in)
{
model_insert_book(&root, in); - int con_insert(TreeNode *root, bookinfo *in)
view_message(\추가하였습니다.\\n\);
return 1;
}
-부근 바꾸니 실행은 되네요
| 번호 | 제 목 | 글쓴이 | 날짜 |
|---|---|---|---|
| 2752085 | 문자열을 랜덤하게 출력해서 대체,삽입,삭제 횟수를 각가 표시하고 그걸 표 형태로 출력 | 슬아s | 2026-10-20 |
| 2752056 | 포인터 && 배열 질문요~ (2) | 해찬나래 | 2026-10-20 |
| 2752031 | x^n 구하는 코드 짜봤는데 값이 틀려요 ㅠㅠ | 세실 | 2026-10-20 |
| 2752000 | 나 정말 미치겠어요..ㅠㅜ (4) | 다이 | 2026-10-20 |
| 2751978 | 문자열 질문이요.. | 초시계 | 2026-10-19 |
| 2751945 | 숫자 맞추는 프로그램이요 ㅠㅠ (1) | 동은 | 2026-10-19 |
| 2751917 | C언어 이중 연결 관련 질문합니다. | 유우 | 2026-10-19 |
| 2751888 | return의 정확한 의미좀 알려주세요 (3) | 올해1살 | 2026-10-19 |
| 2751857 | 포인터 배열 (2) | 터큰 | 2026-10-18 |
| 2751831 | 연산자? 질문입니다 (2) | 진주 | 2026-10-18 |
| 2751800 | C언어 기초질문입니다.. (3) | 핫블랙 | 2026-10-18 |
| 2751739 | 파일 출력을 구조체로 받을 때, 파일의 끝을 판별하는 방법을 알려주세요. (2) | 얼 | 2026-10-17 |
| 2751715 | unresolved external symbol 에러... | 이룩 | 2026-10-17 |
| 2751686 | 변수 설정에 관한 질문입니다. | 꽃달 | 2026-10-17 |
| 2751655 | 자료구조 연습문제 | 한무릎 | 2026-10-17 |
| 2751631 | gets 함수 질문이요. (4) | 해늘 | 2026-10-16 |
| 2751553 | 퀵정렬 소스인데 주석좀 달아주세요 (1) | 싴흐한세여니 | 2026-10-16 |
| 2751528 | 저기 이거하는법좀 질문.. (2) | 가람슬기 | 2026-10-15 |
| 2751445 | 도저히 풀 수가 없네요.... (2) | 신당 | 2026-10-15 |
| 2751412 | 어제부터 계속 해도.........안되는.... (1) | 족장 | 2026-10-14 |
