list-소스한개
Posted 2012. 8. 13. 22:16#include <iostream>
#include <cstring>
#include <list>
using namespace std;
typedef struct PersonInfo{
char Name[20];
int Age;
char ID[20];
char PW[20];
}Person;
//유저 define type data인 경우 직접 조건을 만들어 줘야됨
bool Comp(Person first, Person Second) //적은숫자가 앞으로 가게 정렬(오름차순 정렬)
{
if(first.Age < Second.Age)
return true;
return false;
};
void MainMenu();
int SearchData();
void main()
{
int Select;
list<Person> Data; // list로 Data 선언
list<Person>::iterator iter; // list의 내부 참조자 변수만듬
while(1)
{
MainMenu();
cin >> Select;
switch(Select)
{
case 1: //입 력
Person temp;
cout << "이 름 : ";
cin >> temp.Name;
cout << "나 이 : ";
cin >> temp.Age;
cout << "I D : ";
cin >> temp.ID;
cout << "P W : ";
cin >> temp.PW;
Data.push_back(temp); //데이터 밀어넣기
cout <<"입력완료"<<endl;
break;
case 2: //출 력
if( Data.empty() )
{
cout<<"등록된 정보가 없다"<<endl;
continue;
}
else
{
for(iter=Data.begin() ; iter != Data.end() ; iter++) //Data.begin() -> 시작점, Data.end() ->끝점
{
cout<<"이 름:"<<(*iter).Name<<endl;; // iter로 데이터 접근
cout<<"나 이:"<<(*iter).Age<<endl;
cout<<"I D:"<<(*iter).ID<<endl;
cout<<"P W:"<<(*iter).PW<<endl;
}
cout<<"출력완료"<<endl;
}
break;
case 3: //나이순 정렬
if( Data.empty() ) //Data.empty() ->비었으면 false, 있으면 true
{
cout<<"정렬할 정보가 없다"<<endl;
continue;
}
else
{
Data.sort(Comp); //
cout<<"나이순 정렬완료"<<endl;
}
break;
case 4: //검색삭제
if(SearchData() == 1) //이 름
{
char strname[256];
cout << "이 름 : ";
cin >> strname;
for(iter = Data.begin() ; iter != Data.end() ; )
{
if( !strcmp((*iter).Name, strname) ) //같은게 있으면
{
iter = Data.erase(iter); //Data.erase() ->선택부분 삭제
cout<<"삭제 완료"<<endl;
}
else
iter++;
}
}
else if(SearchData() == 2) //아이디
{
char strid[256];
cout<<"I D : ";
cin>>strid;
for(iter = Data.begin() ; iter != Data.end() ; )
{
if( !strcmp( (*iter).ID, strid) ) //같은게 있으면
{
iter = Data.erase(iter);
cout<<"삭제 완료"<<endl;
}
else
iter++;
}
}
break;
case 5: //전체삭제
if( Data.empty() )
cout<<"등록된정보가없다"<<endl;
else
{
Data.clear(); //Data.clear() -> 전체 삭제
cout<<"전체삭제완료"<<endl;
}
break;
case 6: //종료
Data.clear();
cout<<"프로그램종료함"<<endl;
return;
break;
}
}
}
void MainMenu()
{
cout << "--------------" <<endl;
cout << " List " << endl;
cout << "--------------" <<endl;
cout << "1.입 력" << endl;
cout << "2.출 력" << endl;
cout << "3.나이순정렬"<<endl;
cout << "4.검색 삭제" << endl;
cout << "5.전체 삭제" << endl;
cout << "6.종 료" << endl;
cout << "--------------" <<endl;
cout << "선 택 : ";
}
int SearchData()
{
int i;
cout << "검색할 목록 선택" << endl;
cout << "1.이 름 2.I D" << endl;
cout << "선택 : ";
cin >> i;
return i;
}
- Filed under : C++/STL-list