๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ๋น ๋ฅด๊ฒŒ ๋ณ€ํ™”ํ•˜๋Š” ์„ธ์ƒ ์†์—์„œ ๋‚˜์˜ ์ƒํ™œ์„ ๊ธฐ๋กํ•˜๊ณ , ๋‚˜๋งŒ์˜ ์†Œ์ค‘ํ•œ ์ˆœ๊ฐ„๋“ค์„ ๋‹ด์•„๋‚ด์–ด ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋น ๋ฅด๊ฒŒ ๋ณ€ํ™”ํ•˜๋Š” ์„ธ์ƒ ์†์—์„œ ๋‚˜์˜ ์ƒํ™œ์„ ๊ธฐ๋กํ•˜๊ณ , ๋‚˜๋งŒ์˜ ์†Œ์ค‘ํ•œ ์ˆœ๊ฐ„๋“ค์„ ๋‹ด์•„๋‚ด์–ด ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.
  • Life is Simple..โค๏ธŽ
# PRIVATE

10์›” 01์ผ ํ”„๋กœ์ ํŠธ student ver1 ๊ป์งˆ

by ROHA__ 2010. 10. 1.



#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