|
pet.h 헤더파일
#include <iostream.h>
class Pet
{
public:
typedef char PetName[20];
enum PetType {Dog, Cat, Monkey};
private:
PetType kind; // 종류
PetName name; // 이름
public:
Pet (PetType x= Dog, char* n =" "); //생성자
PetType Type(void) {return (kind); } //종류를 반환
char* Name(void) {return (name); } //이름을 반환
void Input(void); //입력
void Sintro(void); //자기소개
};
ostream& operator << (ostream& s, Pet& p); //문자열 출력 연산자
petlist.h 헤더파일
#include <iostream.h>
#include "pet.h"
//클레스
class PetNode
{
friend class PetList;
private:
Pet* pet; //애완동물 데이터
PetNode* next;
public:
PetNode(Pet* ptr) : pet(ptr) {next=NULL;} //생성자
~PetNode(void) {delete pet;}
PetNode* Next(void) {return (next);}
operator Pet*(void) {return (pet);}
class PetList
{
friend ostream& operator <<(ostream& s, PetList& x);
private:
PetNode* top; //선두 노드로의 포인트
PetNode* bottom; // 마지막 더미노등로의 포인트
public:
PetList(void); //생성자
~PetList(void); //소멸자
PetNode* Top(void) { return ((top == bottom) ? NULL : top); }
PetNode* Bottom(void);
PetList& Insert(PetNode*); //선두에 노드 삽입
PetList& Append(PetNode*);
PetList& Delete(void);
PetList& Remove(void);
PetList& Clear(void);
PetList& Introduce(void);
};
pet.cpp 파일
#include <string.h>
#include "pet.h"
//생성자
Pet::Pet(PetType t, char* n)
{
kind=t;
strcpy(name,n);
}
void Pet::Input(void)
{
int k;
do {
cout << "종류 [0.개, 1.고양이, 2.원숭이]:";
cin >> k;
} while (k < Dog || k > Monkey);
kind = PetType(k);
cout << "이름:" ;
cin >> name;
}
//자기소개
void Pet::Sintro(void)
{
cout << "나는";
switch (kind) {
case Dog : cout << "개"; break;
case Cat : cout << "고양이"; break;
case Monkey : cout << "원숭이"; break;
}
cout <<"이고, 이름은 " << name << "입니다!!\n";
}
//애완동물 클레스 문자영 출력 연산자
ostream& operator << (ostream& s, Pet& p)
{
s<< "{";
switch (p.Type()){
case Pet::Dog :s<<"개"; break;
case Pet::Cat :s<<"고양이"; break;
case Pet::Monkey :s<<"원숭이"; break;
}
s<< p.Name() <<"}";
return(s);
}
petlist.cpp 파일
#include <iostream.h>
#include "petlist.h"
//생성자
PetList::PetList(void)
{
Pet* x = new Pet(Pet::Dog,"DUMMY");
top = bottom = new PetNode(x);
}
//해제자
PetList::~PetList(void)
{
Clear(); //모든 요소 삭제
delete top; //더미 노드 해제
}
// 리스트의 마지막 요소로의 포인터
PetNode* PetList::Bottom(void)
{
if (top == bottom)
return (NULL);
else{
PetNode* ptr = top;
while (ptr -> next != bottom)
ptr = ptr -> next;
return (ptr);
}
}
//리스트의 선두에 요소를 삽입
PetList& PetList::Insert(PetNode*x)
{
PetNode* ptr =top;
top = x;
top -> next =ptr;
return (*this);
}
//리스트의 마지막에 요소를 추가
PetList& PetList::Append(PetNode* x)
{
if(top == bottom)
Insert(x);
else{
PetNode* ptr = Bottom();
ptr -> next = x;
x -> next = bottom;
}
return (*this);
}
//리스트의 선두요소 삭제
PetList& PetList::Delete(void)
{
if(top !=bottom){
PetNode* ptr=top ->next;
delete top;
top = ptr;
}
return (*this);
}
//리스트의 마지막 요소 삭제
PetList& PetList::Remove(void)
{
if(top == bottom);
else if (top->next ==bottom)
Delete();
else{
PetNode* now = top;
PetNode* pre;
while (now->next != bottom) {
pre = now;
now = now ->next;
}
pre->next=bottom;
delete now;
}
return (*this);
}
// 리스트의 모든 요소 삭제
PetList& PetList::Clear(void)
{
PetNode* ptr = top;
while (ptr != bottom){
PetNode* next = ptr -> next;
delete ptr;
ptr = next;
}
top = bottom;
return (*this);
}
//자기소개
PetList& PetList::Introduce(void)
{
PetNode* ptr = top;
cout <<"<<-----------자기소개----------->>\n";
while (ptr !=bottom){
ptr -> pet -> Sintro();
ptr = ptr -> next;
}
cout <<"<<------------------------------->>\n";
return (*this);
}
//petlist stream 삽입 연산자
ostream& operator <<(ostream& s, PetList& x)
{
PetNode* ptr = x.top;
s<<"{\n";
while (ptr != x.bottom) {
s<<'\t'<<*(Pet*)(*ptr)<<'\n';
ptr = ptr->Next();
}
S<<"}\n";
return (s);
}
main.cpp
#include <iostream.h>
#include "petlist.h"
enum Menu{
Terminate, Insert, Append, DspFirst, DspLast, Delete,
Remove, Clear, Print, Intro, MenuOver
};
Menu SelectMenu(void) // 메뉴선택
{
int ch;
do{
cout<< "(1) 선두에 삽입 (2) 마지막에 추가"
<< "(3) 선두를 표시 (4) 마지막을 표시\n";
cout<< "(5) 선두를 삭제 (5) 마지막을 삭제"
<< "(7) 모두 삭제 (8) 모두 표시\n";
cout<< "(9) 리스트순 자기소개 (0) 종료\n";
cin>> ch;
} while (ch < Terminate || ch >= MenuOver);
return ((Menu)ch);
}
int main(void)
{
Menu menu;
PetList list;
do{
menu = SelectMenu();
switch (menu){
PetNode* p;
case Insert : { cout <<"선두에 삽입할 데이터를 입력하시오\n";
Pet* x = new Pet();
x -> Input();
PetNode* n = new PetNode(x);
list.Insert(n);
} break;
case Append : { cout <<"마지막에 추가할 데이터를 입력하시오\n";
Pet* x = new Pet();
x ->Input();
PetNode* n = new PetNode (x);
list.Append(n);
}break;
case DspFirst : if(p = list.Top())
((Pet*)*p)->Sintro();
break;
case DspLast : if (p = list.Bottom())
((Pet*)*p)->Sintro();
break;
case Delete : list.Delete(); break;
case Remove : list.Remove(); break;
case Clear : list.Clear(); break;
case Print : cout << list; break;
case Intro : list.Introduce(); break;
}
} while (menu != Terminate);
return(0);
}
컴파일시
Compiling...
main.cpp
c:\program files\microsoft visual studio\myprojects\last\main.cpp(4) : error C2059: syntax error : 'PCH creation point'
c:\program files\microsoft visual studio\myprojects\last\main.cpp(4) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
c:\program files\microsoft visual studio\myprojects\last\main.cpp(9) : error C2059: syntax error : 'PCH creation point'
c:\program files\microsoft visual studio\myprojects\last\main.cpp(10) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
c:\program files\microsoft visual studio\myprojects\last\main.cpp(24) : error C2059: syntax error : 'PCH creation point'
c:\program files\microsoft visual studio\myprojects\last\main.cpp(25) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
c:\program files\microsoft visual studio\myprojects\last\main.cpp(60) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
이런 에러가 뜹니다.. ㅠㅜ 왜그러는지 수정 좀 부탁 드립니다!!!
|