# PRIVATE
10월 01일 프로젝트 student ver1 껍질
ROHA__
2010. 10. 1. 14:00
#include<iostream>
#include<iomanip> //조작
using namespace std;
const int MAX_NAME=51;
struct STUDENT //응용프로그램에서 필요한 것들(Client_)
{
int sNo;
char name[MAX_NAME];
int kor, eng, math1;
float ave;
};
struct NODE //노드구성(student + nomd*), 응용프로그램에서 필요한 것들(Client_)
{
STUDENT data;
NODE* link; // 자가참조
};
class List //메타데이터 : head + count + 온갖method(삽입, 삭제 ,찾기등등을 가져간다)
{
public: //linked List의 Server역할
int count;
NODE* head;
};
List studentlist;
enum MENU{MENU_ADD_STUDENT, MENU_SHOW_ALL, MENU_QUIT}; //SHOW_ALL은 순회에 해당.
MENU ShowMenu();
int addStudent();
bool insertStudent(NODE* pPre, STUDENT datain);
void printList(); // 전체를 출력하는것!
void constructList(); // count를 초기화해주는 것! = 생성자로 바뀔거임
void destructList();
bool search(NODE* & pPre, NODE* & pLoc, int key);
int main() // 응용프로그램이 됨!
{
constructList(); // 초기화한다.
bool flag = false;
while(true)
{
MENU select; //enum데이터를 호출
select = ShowMenu();
switch(select)
{
case MENU_ADD_STUDENT:
{
//학생 성적 추가 함수를 호출한다.
int addResult;
addResult=addStudent();
if(addResult !=0)
{
if(addResult==-1)
{
cout<<"추가에서 메모리 넘침(overflow)\a\n";
exit(120);
}//리스트 넘침(overflow)
else
{
cout<<"중복된 학번임. 삽입되지 않음 \n\a";
}
}
else
{
cout<<"\n학생 성적이 올바르게 입력되었습니다.\n";
}
break;
}
case MENU_SHOW_ALL:
{
printList();
break;
}
case MENU_QUIT:
{
cout<<"\n프로그램을 종료합니다.\n";
flag=true;
break;
}
}
if(flag==true)
{
break;
}
}//while
return 0;
}
MENU ShowMenu()
{
while(true)
{
cout << "\n-------메뉴-------\n" ;
cout << "1. 학생 성적 추가 \n";
cout << "2. 전체 성적 보기 \n";
cout << "3. 프로그램 종료 \n";
cout << "\n------------------\n" ;
cout << "\n 원하는 작업 번호를 입력하세요 : " ;
char select;
cin>> select;
switch(select)
{
case '1':
return MENU_ADD_STUDENT;
case '2':
return MENU_SHOW_ALL;
case 'Q':
case 'q':
return MENU_QUIT;
default:
cout<< "\n 올바른 값을 입력해 주세요.\n";
break;
}
}
return MENU_QUIT;
}
void constructList()
{
studentlist.head = NULL;
studentlist.count = 0;
}
int addStudent()
{
//지역 정의
bool found;
bool success;
STUDENT* std=new STUDENT;
cout<<" 학번 이름 국어, 영어, 수학 입력: ";
cin>> std->sNo >> std->name >> std->kor >> std->eng >> std->math1;
std->ave = float(std->kor + std->eng + std->math1) / 3.0f;
NODE* pPre;
NODE* pLoc;
found = search(pPre, pLoc, std->sNo);
if (found==true)
{
return (+1);
}
else//insertStudent할 예정
{
success = insertStudent(pPre,*std);
if (success==true)
{
return (0);
}
else
{
return (-1);
}
}
}
bool search(NODE* & pPre,NODE* & pLoc,int key)
{
bool found;
pPre = NULL;
pLoc = studentlist.head;
while((pLoc != NULL) && (key > pLoc->data.sNo))
{
pPre = pLoc;
pLoc = pLoc->link;
}
if(pLoc==NULL)
{
found=false;
}
else
{
if (key == pLoc->data.sNo)
{
found=true;
}
else
{
found=false;
}
}
return found;
}//search
bool insertStudent(NODE* pPre, STUDENT datain)
{
NODE* pNew;
//문장들
if ( ! (pNew = new NODE))
return false; //memory 부족
pNew->data = datain;
pNew->link=NULL;
if(pPre==NULL) { //첫 노드 앞에 혹은 빈 리스트에 삽입
pNew->link=studentlist.head;
studentlist.head=pNew;
}//if pPre
else
{
//중간, 혹은 끝에 삽입
pNew->link=pPre->link;
pPre->link=pNew;
}//if else
studentlist.count=studentlist.count+1;
cout<<"insert()후: "<<"갯수: "<<studentlist.count<<endl;
return true;
}
void printList()
{
if( studentlist.count == 0)
{
cout << " 리스트에 아무것도 없음! " << endl;
}
else
{
STUDENT std;
NODE* np = studentlist.head;
while(np !=NULL)
{
std = np->data;
cout<<" " << std.sNo <<" " << std.name << " " << std.kor;
cout<<" "<< std.math1 << " " << std.ave << endl;
np = np->link;
}
}
}
728x90
반응형