//CompareByNum.h
#pragma once
#include "Stu.h"
class CompareByNum
{
int num;
public:
CompareByNum(int num)
:num(num)
{}
bool operator()(Stu*stu)
{
return (stu&&stu->GetNum() == num);
}
};
class CompareByName
{
string name;
public:
CompareByName(string name)
:name(name)
{}
bool operator()(Stu*stu)
{
return (stu&&stu->GetName() == name);
}
};
//EHGlobal.h
#pragma once
#pragma warning(disable:4996)
#include <string>
#include <iostream>
using namespace std;
enum keydata
{
NO_DEFINED, F1, F2, F3, F4, F5,F6,F7, ESC
};
//맴버변수가 없는 클래스
//==> 상태값이 없다.
//==> C언어라면 전역함수들..
class ehglobal
{
//개체 생성을 못하게 하기 위해
//private으로 지정
private:
ehglobal();
~ehglobal();
public:
static void clrscr();
static void timeflow(int millisecond);
static int getnum();
static string getstr();
static keydata getkey();
};
//Stu.h
#pragma once
#include "EHGlobal.h" //?
class Stu
{
const int num; //학생번호
string name; //이름
int age; //나이
public:
Stu(int num, string name, int age)
:num(num),name(name),age(age)
{}
~Stu() {}
public:
int GetNum() const { return num; }
string GetName() const { return name; }
int GetAge() const { return age; }
void SetAge(int v) { age = v; }
public:
friend ostream &operator<<(ostream &os, Stu *stu)
{
os << "[번호]" << stu->num <<
" [이름]" << stu->name <<
" [나이]" << stu->age << endl;
return os;
}
};
//StuManager.h
#pragma once
#include <vector>
using namespace std;
#include "EHGlobal.h"
#include "Stu.h"
class StuManager
{
vector<Stu*> base;
public:
StuManager();
~StuManager();
public:
void Run();
private:
keydata SelectMenu();
bool Exist(int num);
void AddStu();
void RemoveStu();
void ListAll();
void SearchStuByNum();
void SearchStuByName();
};
//CompareByNum.cpp
#include "CompareByNum.h"
//EHGlobal.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "EHGlobal.h"
#include <conio.h>
#include <windows.h>
ehglobal::ehglobal()
{
}
ehglobal::~ehglobal()
{
}
void ehglobal::clrscr()
{
system("cls");
}
void ehglobal::timeflow(int millisecond)
{
Sleep(millisecond);//windows.h
}
int ehglobal::getnum()
{
int num;
char buf[255 + 1];
cin.getline(buf, 255);
cin.clear();
sscanf(buf, "%d", &num);
return num;
}
string ehglobal::getstr()
{
char buf[255 + 1];
cin.getline(buf, 255);
cin.clear();
return buf;
}
keydata ehglobal::getkey()
{
int key = _getch();
if (key == 27)//ESC를 누를때의 key값
return ESC;
if (key == 0) //기능키를 눌렀을때 반환값
{
key = _getch();
switch (key)
{
case 59: return F1;
case 60: return F2;
case 61: return F3;
case 62: return F4;
case 63: return F5;
case 64: return F6;
case 65: return F7;
}
}
return NO_DEFINED;
}
//Stu.cpp
#include "Stu.h"
//StuManager.cpp
#include <algorithm>
using namespace std;
#include "StuManager.h"
#include "CompareByNum.h"
StuManager::StuManager()
{
}
StuManager::~StuManager()
{
}
void StuManager::Run()
{
keydata key = keydata::NO_DEFINED;
while ((key = SelectMenu()) != ESC)
{
switch (key)
{
case F1: AddStu(); break;
case F2: RemoveStu(); break;
case F3: SearchStuByNum(); break;
case F4: SearchStuByName(); break;
case F5: ListAll(); break;
default: cout << "잘못된 메뉴를 선택하였습니다." << endl;
}
cout << "아무키나 누르세요" << endl;
ehglobal::getkey();
}
}
keydata StuManager::SelectMenu()
{
ehglobal::clrscr();
cout << "메뉴 [ESC]:종료" << endl;
cout << "[F1]:학생 추가 [F2]:학생 삭제 [F3]:번호로 검색";
cout << "[F4]:이름으로 검색 [F5]:전체 보기" << endl;
cout << "메뉴를 선택하세요" << endl;
return ehglobal::getkey();
}
bool StuManager::Exist(int num)
{
Stu *stu = NULL;
for (int i=0; i< (int)base.size(); i++)
{
stu = base[i];
if (stu->GetNum() == num)
return true;
}
return false;
}
void StuManager::AddStu()
{
int num = 0;
cout << "추가할 학생 번호를 입력하세요" << endl;
num = ehglobal::getnum();
if (Exist(num))
{
cout << "이미 보관된 학생이 있습니다." << endl;
return;
}
string name = "";
cout << "이름을 입력하세요" << endl;
name = ehglobal::getstr();
int age;
cout << "나이를 입력하세요" << endl;
age = ehglobal::getnum();
//저장
base.push_back(new Stu(num, name, age));
}
void StuManager::ListAll()
{
Stu *stu = NULL;
for (int i = 0; i< (int)base.size(); i++)
{
stu = base[i];
cout << stu << endl;
}
}
void StuManager::RemoveStu()
{
int num = 0;
cout << "삭제할 학생 번호를 입력하세요" << endl;
num = ehglobal::getnum();
Stu *stu;
for (int i = 0; i < (int)base.size(); i++){
stu = base[i];
if (stu->GetNum() == num)
{
base.erase(base.begin() + i);
cout << "삭제되었습니다." << endl;
return;
}
}
cout << num << "번 학생 자료는 보관되지 않았습니다." << endl;
}
void StuManager::SearchStuByNum()
{
int num = 0;
cout << "검색할 학생 번호를 입력하세요: " << endl;
num = ehglobal::getnum();
CompareByNum sbn(num);
vector<Stu*>::iterator seek;
seek = find_if(base.begin(), base.end(), sbn);
if (seek == base.end())
{
cout << num << "번 학생 자료는 보관되지 않았습니다." << endl;
return;
}
Stu *stu = *seek;
cout << stu << endl;
}
void StuManager::SearchStuByName()
{
string name = "";
cout << "검색할 학생 이름을 입력하세요: " << endl;
name = ehglobal::getstr();
CompareByName sbn(name);
vector<Stu*>::iterator seek;
seek = find_if(base.begin(), base.end(), sbn);
if (seek == base.end())
{
cout << name << "학생 자료는 보관되지 않았습니다." << endl;
return;
}
Stu *stu = *seek;
cout << stu << endl;
}
//소스.cpp
#include <iostream>
using namespace std;
#include "StuManager.h"
int main()
{
StuManager *pman = new StuManager;
pman->Run();
delete pman;
return 0;
}
'00.Project' 카테고리의 다른 글
자판기프로그램(mini) (0) | 2018.04.30 |
---|---|
야구게임(mini) (0) | 2018.04.28 |
간단한 관리프로그램(mini) (0) | 2018.04.28 |
댓글