본문 바로가기
코스웨어/10년 스마트폰BSP

[BSP]업무일지-정호영-20100715

by 알 수 없는 사용자 2010. 7. 15.
728x90
반응형

C++ 도서관리 구조체로 작성중.


//#include "books.h"
#include "menu.h"
//#define "list.h"
#include <iostream>
#include <iomanip>  //setw를 쓰기 위해 사용하는 헤더파일 
#include <string>

using namespace std;


struct Books    //책의 구조체 설정. 
{
      
      string book_title;
      string author;
      string publish_date;
      string publisher;
      string price;
      string amount;
};



struct Node
{
  Node* prev; // 이전 Node 구조체
  Node* next;  // 다음 Node 구조체
  void* data;  // 데이타에 대한 포인터
};



struct List    // 전체 리스트를 관리하는 구조체   //list구조체  
{
  Node* head;  //list 구조체가 head노드를 가르킨다. 
};

List* books = NULL;    //링크드리스트 포인터를 정의. 

List* CreateList();  //링크드 리스트 생성 및 초기화 함수
void RemoveList(List* list, bool deleteData);//링크드 리스트 제거 함수
Node* GetTail(List* list);//함수 포인터 , 

void InsertNodeAfter(Node* node, void* data);
void RemoveNode(Node* node);
Node* GetNext(Node* node);
Node* GetPrev(Node* node);
void* GetData(Node* node);







List* CreateList()  // 새로운 링크드 리스트를 만든다.   // 반환값 : 새로운 링크드 리스트의 포인터
{
  
  Node* head = new Node;        // 헤드 노드를 만들고 초기화한다.
  head->prev = head;
  head->next = head;
  head->data = 0;


  // List 구조체를 만들고 초기화한다.
  List* list = new List;
  list->head = head;

  // List 구조체를 반환한다.
  return list;
}

// 리스트를 제거한다. 
// 리스트의 모든 노드와 데이터도 제거한다.
// list : 리스트
// deleteData : true면 노드의 데이터도 제거한다.
void RemoveList(List* list, bool deleteData)
{
  // head 노드부터 마지막 노드까지 제거한다.
  Node* current = list->head;

  do
  {
    // 현재 노드를 지울 것이므로
    // 다음 노드를 구해둔다.
    Node* next = current->next;

    // 현재 노드와 노드의 데이터를 제거한다.
    if (deleteData)
      delete current->data;
    delete current;

    // 현재 노드를 갱신한다.
    current = next;
  }
  while( current != list->head); 
  // while() 문에 도달한 상태에서
  // list->head가 가리키는 곳의 메모리는
  // 해제된 상태이지만, list->head처럼
  // 그곳의 주소값만 사용하는 것은 상관없다.

  // 리스트를 제거한다.
  delete list;
}

// 마지막 노드를 구한다.
// list : 리스트
// 반환값 : 리스트의 마지막 노드
Node* GetTail(List* list)
{
  // 헤드 노드의 이전 노드를 반환한다.
  // (헤드 노드 자기 자신일 수도 있다.)
  return list->head->prev;
}

// 새로운 노드를 추가한다.
// node : 이 노드 뒤에 새 노드가 추가된다.
// data : 새 노드의 데이타
void InsertNodeAfter(Node* node, void* data)
{
  // 삽입되려는 곳의 앞, 뒤 노드를 구해놓으면
  // 읽기 쉬운 소스 코드를 만들 수 있다.
  Node* before = node;
  Node* after = node->next;

  // 새 노드를 만들고 초기화한다.
  Node* newNode = new Node;
  newNode->data = data;
  newNode->next = after;
  newNode->prev = before;

  // node의 뒤에 있던 노드가 새 노드를 가리키도록 만든다.
  after->prev = newNode;

  // 매개변수 node가 새 노드를 가리키도록 만든다.
  before->next = newNode;
}

// 노드를 제거한다.
// node : 제거할 노드
// deleteData : true면 노드가 가리키는 데이타도 제거한다.
void RemoveNode(Node* node, bool deleteData)
{
  // node의 앞, 뒤 노드를 구해놓으면
  // 읽기 쉬운 소스 코드를 만들 수 있다.
  Node* before = node->prev;
  Node* after = node->next;

  // node의 이전 노드와 다음 노드를 연결해준다.
  before->next = after;
  after->prev = before;

  // node의 데이타와 node 자신을 제거한다.
  if (deleteData)
    delete node->data;
  delete node;
}

// 노드의 다음 노드를 반환한다.
// node : 이 노드의 다음 노드가 반환된다.
// 반환값 : node의 다음 노드
Node* GetNext(Node* node)
{
  return node->next;
}

// 노드의 이전 노드를 반환한다.
// node : 이 노드의 이전 노드가 반환된다.
// 반환값 : node의 이전 노드
Node* GetPrev(Node* node)
{
  return node->prev;
}

// 노드에 보관된 데이터를 반환한다.
// node : 이 노드의 데이터를 반환한다.
// 반환값 : node에 보관된 데이타
void* GetData(Node* node)
{
  return node->data;
}









void Setup()
{
     //리스트를 생성한다.
     books = CreateList();                  //students 리스트를 초기화하는 함수.
                                            //AddStudent()나 ShowAll() 함수를 호출하기 전에 반드시 이 함수를 
}                                           //호출해야 한다. 

void Teardown()                             //students 리스트를 정리하는 함수. 프로그램을 종료하기 전에 반드시 
{                                           // 이 함수를 호출해야 메모리가 올바르게 해제된다. 
     //리스트를 정리한다.
     RemoveList(books, true);
     
}
 

//메뉴를 보여주고 고를 때 까지 반복한다.
  MENU ShowMenu()
  {
       while(1)
       {
            //menu 
            cout <<"##############################\n";
            cout <<"      도서관리 프로그램       \n";
            cout <<"------------------------------\n";
            cout <<"           메  뉴             \n";
            cout <<"------------------------------\n";
            cout <<"         1.입  력             \n";
            cout <<"         2.출  력             \n";
            cout <<"         3.수  정             \n";
            cout <<"         4.검  색             \n";
            cout <<"         5.삭  제             \n";
            cout <<"         0.종  료             \n";
            cout <<"##############################\n";
            cout <<"메뉴선택 : ";
            
            
            //작업 입력 받기.
            char select;
            cin >> select;
            
            
            //선택된 값에 따라 처리
           switch(select)
           {
                         case '1' :
                              return MENU_ADD_BOOK;
                         case '2' :
                              return MENU_PRINT_BOOK;
                         case '3' :
                              return MENU_MODIFY_BOOK;
                         case '4' :
                              return MENU_SEARCH_BOOK;
                         case '5' :
                              return MENU_DELETE_BOOK;
                         case '0' :
                              return MENU_QUIT;
                         default:
                                 // 그 외의 선택
                         cout << "\n올바른 값을 입력하세요.\n"
                         break;
           }
         
           
    }
                                 
                         // 이 문장은 절대 출력할 일 없다.
                         return MENU_QUIT;
}
int sNo;                 





int NumberOfBooks = 0;  //현재까지 입력된 책의 권 수.

//책 한권을 추가한다.
//반환값: 더 이상 입력할 수 없는 경우 false.


bool AddBooks()          //bool을 쓴 이유가 조건이 성립하지 않으면 flase를 반환하기 위해서. 
{
     books = CreateList();
     Books* bk = new Books;

//해당 도서의 도서번호를 자동으로 입력한다.
//bk->sNo = NumberOfBooks + 1;

//해당 도서의 도서명   저자    츨판일    출판사     가격    수량을 입력하세요 : \n";
cout << "도서명:"
cin >> bk->book_title;

cout << "저  자:";
cin >>  bk->author;

cout << "출 판 일 :"
cin>> bk->publish_date; 

cout <<"출 판 사 :";
cin>> bk->publisher;

cout <<"책 가격 :";
cin >> bk->price;

cout <<"수량:";
cin >> bk->amount;

NumberOfBooks++;

//books 리스트에 새 노드를 추가한다.
InsertNodeAfter( GetTail(books), bk); 

return true;

 




//전체 도서리스트를 보여준다.
void ShowAll()
{
     cout.precision(2);     //실수출력시에 소수점이하 두자리만 출력한다. 
     cout << fixed;
     
     cout << "\n              <전체 도서목록 보기>";
     cout << "\n  도서명  저자    츨판일    출판사  가격  수량\n";
     
     
     Node* current = books->head->next;
     
     while( current != books ->head)
     {
            const Books* bk = (Books*)GetData(current);
            
            //해당도서의 정보를 출력한다.
             cout <<setw(7<<bk->book_title<<setw(7);
             cout << setw(7<<bk->author <<setw(9<<bk->publish_date;
             cout << setw(9<<bk->publisher <<setw(7<< bk->price<<setw(7);
             cout << bk->amount <<"\n";
            
             current = GetNext(current);     //current가 다음 노드를 가르키게 만든다.
                                             //GetNext()는 입력한 노드의 next멤버의 값을 반환해주는 함수. 
     }
     
     
     
  


void Search()
{
     string str;
     
     cout<< "제목을 입력하세요 : "
     cin >>str;
     cout.precision(2);     //실수출력시에 소수점이하 두자리만 출력한다. 
     cout << fixed;
     
     cout << "\n              <검색 도서목록 보기>";
     cout << "\n  도서명  저자    츨판일    출판사  가격  수량\n";
     
     
     Node* current = books->head->next;    //Node의 포인터형 변수에다가 books 
     
     while( current != books ->head)
     {
            const Books* bk = (Books*)GetData(current);
            
            if(bk->book_title == str)
            {
            
            //해당도서의 정보를 출력한다.
             cout <<setw(7<<bk->book_title<<setw(7);
             cout << setw(7<<bk->author <<setw(9<<bk->publish_date;
             cout << setw(9<<bk->publisher <<setw(7<< bk->price<<setw(7);
             cout << bk->amount <<"\n";
             
             }
            
             current = GetNext(current);     //current가 다음 노드를 가르키게 만든다.
                                             //GetNext()는 입력한 노드의 next멤버의 값을 반환해주는 함수. 
     }
     
     
     
  
}
/*
void Modify()
{
     string str;
     
     cout<< "수정할 책의 제목을 입력하세요 : "; 
     cin >>str;
     cout.precision(2);     //실수출력시에 소수점이하 두자리만 출력한다. 
     cout << fixed;
     
     cout << "\n              <검색 도서목록 보기>";
     cout << "\n  도서명  저자    츨판일    출판사  가격  수량\n";
     
     
     Node* current = books->head->next;    //Node의 포인터형 변수에다가 books 
     
     while( current != books ->head)
     {
            const Books* bk = (Books*)GetData(current);
            
            if(bk->book_title == str)
            {
            
            //해당도서의 정보를 출력한다.
             cout <<setw(7) <<bk->book_title<<setw(7);
             cout << setw(7) <<bk->author <<setw(9) <<bk->publish_date;
             cout << setw(9) <<bk->publisher <<setw(7) << bk->price<<setw(7);
             cout << bk->amount <<"\n";
             
             }
            
             current = GetNext(current);     //current가 다음 노드를 가르키게 만든다.   
                                          //GetNext()는 입력한 노드의 next멤버의 값을 반환해주는 함수. 
                                          
              }                  
            
            Node* current          
            
                 books = CreateList();
                 Books* bk = new Books;

                 //해당 도서의 도서번호를 자동으로 입력한다.
                 //bk->sNo = NumberOfBooks + 1;

                 //해당 도서의 도서명   저자    츨판일    출판사     가격    수량을 입력하세요 : \n";
                 cout << "도서명:"; 
                 cin >> bk->book_title;

                 cout << "저  자:";
                 cin >>  bk->author;

                 cout << "출 판 일 :"; 
                 cin>> bk->publish_date; 

                 cout <<"출 판 사 :";
                 cin>> bk->publisher;

                 cout <<"책 가격 :";
                 cin >> bk->price;

                 cout <<"수량:";
                 cin >> bk->amount;

                 NumberOfBooks++;

                 //books 리스트에 새 노드를 추가한다.
                 InsertNodeAfter( GetTail(books), bk); 

                 return true;
                 }
     
     
     
  
}  

*/

int main()
{   
    //프로그램을 종료할 떄까지 메뉴를 보여주고 작업을 처리한다.  
    while(1)
    {
            
            //메뉴를 보여준다.
            MENU select;     //열거체형 변수를 선언... 
            select = ShowMenu();       //변수에다가 Showmenu() 함수를 대입. 
            
            //선택된 값에 따라 처리 
            switch(select) 
            {
                           case MENU_ADD_BOOK:       //책 데이터 추가
                                {
                                     bool succeeded;
                                     succeeded = AddBooks();
                                
                                if(succeeded)
                                             cout << "\n 책의 내용이 올바르게 입력되었습니다. \n";
                                else
                                             cout << "\n 더 이상 입력을 할 수 없습니다. \n";
                                             
                                break;
                                }
                                
                                
                           case MENU_PRINT_BOOK:
                                {
                                      ShowAll();
                                      
                                      break;
                                }
                                
                           case MENU_SEARCH_BOOK:
                                {
                                      Search();
                                      
                                      break;
                                }
                                
                          /* case MENU_MODIFY_BOOK:
                                {
                                      Modify();
                                      
                                      break;
                                } 
                                
                                */

                           case MENU_QUIT:
                           //종료
                           cout << "\n프로그램을 종료합니다.\n";
                           system("PAUSE");
                           return EXIT_SUCCESS;
                           return 0;
                           }
            }
            
            
            
            return 0;
                                 
                                 
             
             
    //ShowMenu();
    system("PAUSE");
    return EXIT_SUCCESS;
}

728x90