#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
๋ฐ์ํ
SMALL
'# PRIVATE' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
WEB 10์ 01์ผ P84 ํผ ํ๊ทธ ์ฐ์ต (0) | 2010.10.01 |
---|---|
WEB 10์ 01์ผ (0) | 2010.10.01 |
์ฒซ๋ฒ์งธ ๋ด์ฌํ๋ : [2010๊ณ ์ ํํจ๋๋์ถ์ ] Volunteer Staff (0) | 2010.09.30 |
WEB 9์ 30์ผ ํ ์ด๋ธโ (0) | 2010.09.30 |
WEB 9์ 30์ผ ๋งํฌ๊ฑธ๊ธฐ (0) | 2010.09.30 |