C++/디딤돌 자료구조와 알고리즘 with C++

STL list 사용, 순차 보관 [C++ 소스]

언제나휴일 2016. 6. 11. 08:24
반응형

STL list 사용, 순차 보관 [C++ 소스]


3.2 list에 순차보관.zip





"본문 내용"은 언제나 휴일 본 사이트에 있습니다.


//ehglobal.h

#pragma once

#pragma warning(disable:4996)

#include <string>

using std::string;

#include <iostream>

using std::cout;

using std::cin;

using std::ostream;

using std::endl;

#include <conio.h>

#include <windows.h>

enum keydata

{

    NO_DEFINED,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,ESC

};

 

//공통적으로 사용할 정적 메서드를 캡슐화한 클래스

class ehglobal

{

public:

    static void clrscr();//화면을 지우는 메서드

    static void timeflow(int millisecond); //원하는 시간동안 지연시키는 메서드

    static int getnum();//정수를 입력받는 메서드

    static string getstr();//문자열을 입력받는 메서드

    static int getkey();//기능 키를 입력받는 메서드

private:

    ehglobal(void){ }//개체를 생성하지 못하게 하기 위해 private으로 접근 지정

    ~ehglobal(void){}

};

 


//ehglobal.cpp

#include "ehglobal.h"

 

void ehglobal::clrscr()//화면을 지우는 메서드

{

    system("cls");

}

 

void ehglobal::timeflow(int millisecond) //원하는 시간동안 지연시키는 메서드

{

    Sleep(millisecond);

}

 

int ehglobal::getnum()//정수를 입력받는 메서드

{

    int num;

    char buf[255+1];

    cin.getline(buf,255);

    cin.clear();

    sscanf(buf,"%d",&num);

    return num;

}


//Genre.h

#pragma once

#include "ehglobal.h"

class Genre

{

    string name;

    const int num;

public:

    Genre(int num,string name);

    int GetNum()const;

    string GetName()const;

    void View()const;

};


//Genre.cpp

#include "Genre.h"

 

Genre::Genre(int num,string name):num(num)

{

    this->name = name;

}

int Genre::GetNum()const

{

    return num;

}

string Genre::GetName()const

{

    return name;

}

void Genre::View()const

{

    cout<<"장르 No."<<num<<장르명:"<<name<<endl;

}


//App.h

#pragma once

#include "Genre.h"

#include "ehglobal.h"

#include <list>

#include <algorithm>

using std::list;

using std::find;

using std::find_if;

typedef list<Genre *> Genres;

typedef Genres::iterator GIter;

typedef Genres::const_iterator GCIter;

 

class App

{

    Genres genres;

    int last_gnum;//가장 최근에 부여한 장르 번호

public:

    App(void);

    ~App(void);

    void Run();

private:

    int SelectMenu();

    void AddGenre(); //장르 추가

    void RemoveGenreByNum();//번호로 장르 삭제

    void RemoveGenreByName();//이름으로 장르 삭제

    void FindGenreByNum()const; //번호로 장르 검색

    void FindGenreByName()const; //이름으로 장르 검색

    void ListGenre()const; //장르 목록 보기

};


//App.cpp

#include "App.h"

 

class EqualerByNum

{

    int num;

public:

    EqualerByNum(int num)

    {

        this->num=num;

    }

    bool operator()(const Genre *genre)const

    {

        return num == genre->GetNum();

    }

};

 

class EqualerByName

{

    string name;

public:

    EqualerByName(string name)

    {

        this->name = name;

    }

    bool operator()(const Genre *genre)const

    {

        return name.compare(genre->GetName())==0;

    }

};

 

App::App(void)

{

    last_gnum = 0;

}

 

App::~App(void)

{

    GIter seek = genres.begin(); //보관한 시작 위치

    GIter last = genres.end(); //보관한 마지막(다음) 위치

    const Genre *genre;

    for(   ;seek!=last; ++seek)

    {

        genre = (*seek); //간접 연산으로 seek위치에 보관한 장르 확인

        delete genre;

    }

}

 

void App::Run()

{

    int key=NO_DEFINED;

    while((key = SelectMenu())!=ESC)

    {

        switch(key)

        {

        case F1: AddGenre(); break;

        case F2: RemoveGenreByNum(); break;

        case F3: RemoveGenreByName(); break;

        case F4: FindGenreByNum(); break;

        case F5: FindGenreByName(); break;

        case F6: ListGenre(); break;

        default: cout<<"잘못 선택하셨습니다."<<endl; break;

        }

        cout<<"아무 키나 누르세요."<<endl;

        ehglobal::getkey();

    }

}

 

int App::SelectMenu()

{

    ehglobal::clrscr();//콘솔 화면을 지우기

    cout<<"장르 관리 프로그램 [ESC]: 종료"<<endl;

    cout<<"F1: 장르 추가 F2: 번호로 장르 삭제 F3: 이름으로 장르 삭제"<<endl;

    cout<<"F4: 번호로 장르 검색 F5: 이름으로 장르 검색 F6: 장르 목록 보기"<<endl;

 

    return ehglobal::getkey();//사용자가 입력한 기능 키 반환

}

void App::AddGenre() //장르 추가

{

    last_gnum++;

    cout<<last_gnum<<"번째 추가할 장르명:";

    string name = ehglobal::getstr();//장르명 입력

    Genre *genre = new Genre(last_gnum,name); //장르 생성

    genres.push_back(genre);//순차 보관

}

 

 

 

void App::RemoveGenreByNum()//번호로 장르 삭제

{

    int num=0;

    cout<<"삭제할 장르 번호:";

    num = ehglobal::getnum();

    EqualerByNum ebnum(num);

 

    GIter seek = find_if(genres.begin(), genres.end(), ebnum);

    if(seek != genres.end())

    {

        delete (*seek);

        genres.erase(seek);       

        cout<<"삭제하였습니다."<<endl;

    }

    else

    {

        cout<<"존재하지 않는 장르 번호입니다."<<endl;

    }

}

 

void App::RemoveGenreByName()//이름으로 장르 삭제

{

    string name;

    cout<<"삭제할 장르명:";

    name = ehglobal::getstr();

    EqualerByName ebname(name);

 

    GIter seek = find_if(genres.begin(), genres.end(), ebname);

    if(seek != genres.end())

    {

        Genre *genre = (*seek);

        delete genre;

        genres.erase(seek);

        cout<<"삭제하였습니다."<<endl;

    }

    else

    {

        cout<<"존재하지 않는 장르명입니다."<<endl;

    }

}

void App::FindGenreByNum()const //번호로 장르 검색

{

    int num=0;

    cout<<"검색할 장르 번호:";

    num = ehglobal::getnum();

    EqualerByNum ebnum(num);

 

    GCIter seek = find_if(genres.begin(), genres.end(), ebnum);

    if(seek != genres.end())

    {

        Genre *genre = (*seek);

        genre->View();

    }

    else

    {

        cout<<"존재하지 않는 장르 번호입니다."<<endl;

    }

}

void App::FindGenreByName()const //이름으로 장르 검색

{

    string name;

    cout<<"검색할 장르명:";

    name = ehglobal::getstr();

    EqualerByName ebname(name);

 

    GCIter seek = find_if(genres.begin(), genres.end(), ebname);

    if(seek != genres.end())

    {

        Genre *genre = (*seek);

        genre->View();

    }

    else

    {

        cout<<"존재하지 않는 장르명입니다."<<endl;

    }

}

void App::ListGenre()const //장르 목록 보기

{

    GCIter seek = genres.begin(); //보관한 시작 위치

    GCIter last = genres.end(); //보관한 마지막(다음) 위치

    const Genre *genre;

    for(   ;seek!=last; ++seek)

    {

        genre = (*seek); //간접 연산으로 seek위치에 보관한 장르 확인

        genre->View(); //장르 정보 출력

    }

}


//Program.cpp

#include "App.h"

int main()

{

    App *app = new App();

    app->Run();

    delete app;

    return 0;

}



프로그래밍 언어 및 기술 학습, 무료 동영상 강의 언제나 휴일

반응형