This is an old revision of the document!


HW 9 개요

파일 입출력과 map 클래스를 활용하여 다음과 같은 데이터베이스를 만들어라.

  1. 저장 데이터는 (이름, 점수)
  2. "scorebook.txt"라는 파일에 데이터를 저장한다 (프로그램 종료후에도 남아있음)
  3. 다음과 같은 기능을 제공한다
    1. 데이터 출력 (print)
      1. 현재 데이터베이스에 저장되어 있는 모든 (이름, 점수)를 출력한다.
    2. 데이터 추가 (insert)
      1. 이름과 점수를 입력하면 그 이름이 데이터베이스에 존재하지 않으면 새로운 데이터를 추가하고 이미 존재하면 점수를 새로 입력된 값으로 수정한다. 추가가 되었는지 수정이 되었는지를 메시지 출력.
    3. 데이터 삭제 (remove)
      1. 이름을 입력하면 해당 인물의 데이터를 삭제한다. 존재하지 않으면 해당 메시지 출력.
    4. 데이터 검색 (search)
      1. 이름을 입력하면 해당 인물의 점수를 출력한다. 존재하지 않으면 해당 메시지 출력.
    5. 종료 (quit)
      1. 프로그램을 종료한다.

실행 예

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 1
<name>  : <score>
----------------
jiyoo   : 95
minho   : 90
miok    : 85
yoowon  : 96
----------------

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 2
Enter name : minho
Enter score : 100
minho already exists in the database.

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 1
<name>  : <score>
----------------
jiyoo   : 95
minho   : 90
miok    : 85
yoowon  : 96
----------------

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 2
Enter name : cinamorole
Enter score : 50

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 1
<name>  : <score>
----------------
jiyoo   : 95
minho   : 90
miok    : 85
yoowon  : 96
cinamorole      : 50
----------------

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 3
Enter name : cinamon
cinamon does not exist in the database.

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 3
Enter name : cinamorole

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 1
<name>  : <score>
----------------
jiyoo   : 95
minho   : 90
miok    : 85
yoowon  : 96
----------------

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 4
Enter name : jiyoo
jiyoo's score is 95

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 4
Enter name : jiyoon
jiyoon does not exist in the database.

#### MENU ####
1. print all
2. insert data
3. remove data
4. search
5. quit
Choose >> 5
Good Bye

제출할 것

다음 파일에서 class ScoreBook을 정의한 후, 전체 파일을 cpphw9-<학번>.cpp로 제출한다. 코드의 다른 부분은 건들지 말 것. (단 오류가 있으면 수정하고 수정한 내용을 적어서 제출)

#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

class ScoreBook {
    ... // 스스로 정의할 것
public:
    ScoreBook( string filename );
    void printall();
    void insert( string name, string score );
    void remove( string iname );
    void search( string iname );
    ... // 그 외 필요한 함수들
};

int getSelection()
{
	cout << "#### MENU ####" << endl;
    cout << "1. print all" << endl;
    cout << "2. insert data" << endl;
    cout << "3. remove data" << endl;
    cout << "4. search" << endl;
    cout << "5. quit" << endl;
    cout << "Choose >> ";
    
    string line;
    getline( cin, line );
    return stoi( line );
 }

string getName() 
{
	string name;
	cout << "Enter name : ";
	getline( cin, name );
	return name;
}

string getScore() 
{
	string score;
	cout << "Enter score : ";
	getline( cin, score );
	return score;
}

int main()

{
    int selection;
    string name, score;

    ScoreBook sb("scorebook.txt");
    
    while( (selection = getSelection()) ) {
        switch (selection) {
            case 1:
		sb.printall();
                break;
            case 2: 
		name = getName();
		score = getScore();
                sb.insert( name, score );
                break;
            case 3:
		sb.remove( getName() );
                break;
            case 4:
                sb.search( getName() );
                break;
            case 5:
                cout << "Good bye" << endl;
		system("pause");
                exit(1);
            default:
                continue;
        }
    }
}
 
class/os2014f/hw9.1416511350.txt.gz · Last modified: 2025/10/13 12:59 (external edit) · [Old revisions]
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki