C/C++
c언어 연결리스트로 전화번호부 만드는 과제 수정 부분 도와주세요...

c언어 피드백 받은 부분 수정 도와주실 분 찾습니다.도와주세요...ㅠ

 

//c언어 링트드리스트를 이용한 전화번호부입니다

피드백 부분-

1.

성별에 여러글자 입력 시 에러처리 추가

2.주소록 구조체 중 addr은 배열 말고 포인터로 선언하고 malloc로 메모리 할당할 것.
입력 받을 주소의 길이를 모르니까 고정 길이여도 상관 없음.
경험삼아 해보는 것.

3.

1번 메뉴. 출력
주소를 1000개 등록 한 경우 1000 * 1000 번 만큼 for문 반복 됨
[시간 복잡도] [빅 O] [빅오] 이런 키워드로 검색해서 어떤 의미인지 찾아보세요

4.

2번 메뉴. 삭제
리스트의 장점이 추가/삭제가 용이하다는 것인데..
굳이 for문이 필요 할까요???

5.

4번 메뉴. 검색
검색 결과로 삭제, 수정 할 수 있도록 해주세요.
일괄 삭제, 부분 삭제 등등

6.

메뉴 선택에 대한 처리를 if, else if 이런 것 보다는 switch 구문 쓰는게 더 나을것 같아요.
메뉴 선택에 대한 처리를 각각의 함수로 빼서 구현하면 코드가 좀 더 깔끔할듯 싶어요

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void add(void);
struct people * seek(int); 
void print(int);
void del(struct people *);
void modify(int);

void namesearch(void);
void agesearch(void);
void idsearch(void);
void gendersearch(void);
void phonesearch(void);
void addrsearch(void);
int kill(int ); 



struct people
{
   int id;
   int age;
   char name[11];
   char gender;
   char phone[20];
   char addr[100];

   struct people *next;

};
struct people *head = NULL;
struct people *tail = NULL;
int count = 0;
int delete = 0;


int main(void)

{
   int dele;
   int select;
   int pcount;
   system("clear");
   while(1)
   {

      
      while(1)
      {
         printf("\n\n");
         printf("┌-----------------------------┐\n");
         printf("            [0]주소록 삽입        \n\n");
         printf("            [1]주소록 출력        \n\n");
         printf("            [2]주소록 삭제        \n\n");
         printf("            [3]내용 추가,수정        \n\n");
         printf("            [4]주소록 검색        \n\n");
         printf("            [5]서비스 종료        \n\n");
         printf("└-----------------------------┘\n");
         printf(" 메뉴 선택 : ");


         fflush(stdin);
         scanf("%d", &select);

         if(select!=0 && select!=1 && select!=2 && select!=3 && select!=4 &&  select!=5 )
         {   system("clear"); 
            printf("\n\n\n\n");   
            printf("======================경    고=========================\n");
            printf("              0부터 5까지만 입력하세요 :\n\n\n\n");
            printf("=======================================================\n");
            fflush(stdin);
            continue;
         }

         break;

      }






      if(select == 0)
      {
         add();

      }

      else if(select == 1)
      {system("clear"); 
         printf("------------------------------주소록정보--------------------------------------  \n");
         printf("           번호   ID    이름   나이    성별     전화번호        주소              \n");
         for(pcount=1;pcount<=count;pcount++)
         {
            print(pcount);
         }
         printf("--------------------------------------------------------------------------------\n");


      }
      else if(select == 2)
      {
         if(count==0)
         {   system("clear");
            printf("삭제할 주소록정보가 없습니다\n");
            continue;
         }
         printf("삭제할 주소록 번호  :");
         scanf("%d",&dele);
         if(dele>count){
            system("clear");
            printf("해당 주소록 정보는 없으므로 삭제 불가능 \n");
            continue;
         }

         else if((dele<0) || (dele>1000000))
         {
            system("clear");
            printf("해당 주소록 정보는 없으므로 삭제 불가능 \n");
            fflush(stdin);
            continue;
         }   
         delete = dele;
         del(seek(dele));
         system("clear");
         printf("============주소록 삭 제 완 료============\n");

      }
      else if(select == 3) 
      { if(count == 0)
         {   system("clear");
            printf("추가 등록할 주소록 없습니다 입력후 추가하시오 \n");
            continue ;
         }

         printf("\n"); 
         printf("수정할 주소록의 번호를 입력하시오 : ");
         scanf("%d",&dele);
         if(dele>count){
            system("clear");
            printf("해당 주소록 정보는 없으므로 추가 불가능 \n");
            fflush(stdin);
            continue;
         }

         else if((dele<0) || (dele>1000000))
         {
            system("clear");
            printf("해당 주소록 정보는 없으므로 추가 불가능\n");
            fflush(stdin);
            continue;
         }
         fflush(stdin);    

         modify(dele);
         system("clear");

         printf("===========추가 및 수정 완 료============\n");
      }
    

      else if(select == 4)
      {
         if(count == 0){
            system("clear");
            printf("\n\n\n");
            printf("입력된 값이 아무것도 없습니다 \n\n\n\n\n");
            continue;
         }
         fflush(stdin);
         while(1)
         {
           
            printf("\n\n\n");
            printf("이름검색은 (1)번 :\n\n");
            printf("나이검색은 (2)번 :\n\n");
            printf("ID  검색은 (3)번 :\n\n");
            printf("성별검색은 (4)번 :\n\n");
            printf("전화번호검색은 (5)번 :\n\n");
            printf("주소검색은 (6)번 :\n\n");
            printf("select : ");
            scanf("%d",&select);
            if(select!=1 && select!=2 && select!=3 && select!=4 && select!=5 && select!=6)
            {  system("clear");
               printf("1~6번에서만 입력하세요\n");
               fflush(stdin);
               continue;
            }
            break;
         }

         if(select == 1)
         { 
            namesearch();
         }
         if(select == 2)
         {
            agesearch();
         }
         if(select == 3)
         {
            idsearch();
         }
         if(select == 4)
         {
            gendersearch();
         }
         if(select == 5)
         {
            phonesearch();
         }
	if(select == 6)
         {
            addrsearch();
         }
      
      }

      else if(select == 5)
      {  system("clear");
         printf("=======================================================================\n\n\n\n\n\n");
         printf("                      이 용 해 주 셔 서 감 사 합 니 다 \n\n\n\n\n");
         printf("                                               \n\n\n\n ");
         printf("=======================================================================\n");
         return 0;

      }
   }
   return 0;

}

void add(void)
{

   


 struct people * temp;

   temp = (struct people *)malloc (sizeof(struct people));


   printf("\n");
   printf("이름 : ");
   scanf("%10s",temp->name);
   
   fflush(stdin);

   printf("\n");
   while(1)
   {
   

      printf("ID(1 to 9999) :");
      scanf("%d",&temp->id);
      if((temp->id <=0) || ( temp->id > 9999) ){
         printf("올바른 숫자를 입력하시오\n ");
         fflush(stdin);
         continue;
      }

      if((kill(temp->id))){ 
         printf("중복된 ID가 있습니다 다시 입력하시오\n ");
         fflush(stdin);
         continue;
      }

      break;
   }

   printf("\n");
   fflush(stdin);


   while(1)
   {      
      printf("성별(여자 = f, 남자 = m) : ");
      scanf("%c", &temp->gender);
      if(temp->gender!='f' && temp->gender!='m') 
      {
         printf("여자 = f 남자 = m만 입력하시오\n");
         fflush(stdin);
         continue;
      }

      break;
   }
   fflush(stdin);


   printf("\n");

   while(1)
   {

      printf("나이 :");
      scanf("%d", &temp->age);
      if((temp->age)<=0 || (temp->age>150) )
      {
         printf("잘못된 나이를 입력했습니다\n");
         fflush(stdin);
         continue;
      }

      break;





   }
   fflush(stdin);





   printf("\n");

   while(1)
   {

      printf("전화번호 :");
      scanf("%s", temp->phone);
      

      break;





   }
   fflush(stdin);
   
   
   printf("\n");

   while(1)
   {

      printf("주소 :");
      scanf("%s", temp->addr);
      

      break;





   }
   fflush(stdin);



   system("clear");
   printf("               생   성  완  료  !              \n");
   printf("==============================================\n\n");


   temp->next = NULL;

   if(tail!=NULL) 

      tail->next = temp;

   if(tail == NULL)
      head = temp;
   tail = temp;
   count++;

   return;

}

struct people * seek(int number)
   
{

   int n;
   struct people *temp = head;

   if(number>count) 
      return NULL;

   for(n=1;n<number;n++)
   {
      temp = temp->next;
   }
   return temp;

}

void print(int number)
{
   int n;
   struct people *temp = head;

   if(number>count)
      return ;

   for(n=1;n<number;n++)
   {
      temp = temp->next;

   }

   printf("│%13d %10d %9s %8d %9c %13s %9s     │ \n",n++,temp->id,temp->name,temp->age,temp->gender,temp->phone, temp->addr);

}

void del(struct people *node)

{


   struct people *prev;
   if(count == delete)
   {


      for(prev=head;prev!=tail;prev=prev->next)
      {                                        
         if(prev->next == node)
         break;
         if(prev == node) 
         break;
      }

      if(node == head)
         head = node->next;
      else 
         tail = prev;
      prev->next = node->next;
   }    


   else 
   {

      for(prev=head;prev!=tail;prev=prev->next)
      {
         if(prev->next == node)
            break;
         if(prev == node)
            break;

      }

      if(node == head)
         head = node->next;
      else
         prev->next = node->next;
   }





   count--;


   free(node);

}

void modify(int number)
   
{
   int n,dele;
   struct people *temp = head;
   if(number>count){
      printf("그런 번호는 없습니다 \n");
      return ;
   }



   for(n=1;n<number;n++)
   {
      temp = temp->next;

   }
   printf("==============%d번 사용자 정보 추가================\n",number);
   printf("============== ID 는 추가와 수정이 불가능 =============\n");
   printf("\n");

   printf("수정할  이름 :");
   scanf("%10s",temp->name);
   fflush(stdin);

   printf("\n");




   while(1)
   {
      printf("수정할 사용자 성별(여자 = f 남자 = m):");
      scanf("%c",&temp->gender);
      if(temp->gender!='f' && temp->gender!='m')
      {
         printf("남자 = f 여자 = m만 입력하시오\n");
         fflush(stdin);
         continue;
      }
      break;
   }
   fflush(stdin);
   printf("\n");

   while(1)
   {

      printf("수정할 사용자 나이 :");
      scanf("%d",&temp->age);
      if((temp->age<=0) || (temp->age>150) )
      {
         printf("잘못된 나이를 입력했습니다\n");
         fflush(stdin);
         continue;
      }
      break;
   }
   fflush(stdin);









printf("\n");



   printf("수정할 사용자 전화번호 :");
   scanf("%10s",temp->phone);
fflush(stdin);

   printf("\n");



fflush(stdin);




 printf("\n");

   printf("수정할 사용자 주소 :");
   scanf("%10s",temp->addr);
   fflush(stdin);

   printf("\n");


   



   printf("             수  정  완  료  !              \n");



   return ;

}







void namesearch(void)
{
   struct people *temp = head;
   char names[11];
   printf("찾을 사람의 이름을 입력하시오 :");
   scanf("%10s",names);
   fflush(stdin);
   system("clear");
   printf("================================== 검   색   결   과 ===================================\n\n\n");
   while(1)
   {
      if(strstr(temp->name,names))
      {                           

         printf("이름  :%s    ID  :%d    성별  :%c    나이  :%d 전화번호  :%s 주소  :%s  \n",temp->name,temp->id,temp->gender,temp->age ,temp->phone,temp->addr);
         temp = temp->next;

         if(temp == NULL)
         {
            temp = head;
            break;
         }

      }
      else if(!strstr(temp->name,names))
         temp = temp->next;

      if(temp == NULL)
      {
         break;
      }


   }
   printf("\n\n\n\n");
   printf("==============================================================================================\n\n\n");
   return ;

}

void agesearch(void)

  


{ 

   int age;


   struct people *temp = head;

   while(1) 
   {

      printf("찾고자 하는 나이를 입력하시오 :");
      scanf("%d",&age);
      if((age<=0) || (age>150))
      {   printf("=====================================\n\n");
         printf("             잘못된 나이를 입력했습니다\n\n\n\n\n");
         printf("=========================================\n\n\n\n\n");
         fflush(stdin);
         continue;
      }

      break;
   }
   system("clear");
   printf("=================================== 검   색   결   과 =======================================\n\n\n\n"); 

   while(1)
   {
      if(age == temp->age)
      {

         printf("이름  :%s    ID  :%d    성별  :%c    나이  :%d 전화번호  :%s 주소  :%s  \n",temp->name,temp->id,temp->gender,temp->age ,temp->phone,temp->addr);
         temp = temp->next;
         if(temp == NULL)
         {
            temp = head;
            break;
         }

      }

      if(age != temp->age)
      {

         if(temp->next == NULL )
         {

            break;

         }
         temp = temp->next;
      }
   }
   printf("\n\n\n");
   printf("==============================================================================================\n\n\n");
   return ;

}

void idsearch(void)
{

   

   int id;
   struct people *temp  = head;

   while(1)
   {

      printf("찾고자 하는 ID 를 입력하시오 :");
      scanf("%d",&id);
      if((id<=0) || (id>9999)){
         printf("올바른 숫자를 입력하시오\n");
         fflush(stdin);
         continue;
      }
      break;

   }
   system("clear");
   printf("================================= 검   색   결   과 =================================\n\n\n");   

   while(1)
   {
      if(id == temp->id)
      {

         printf("이름  :%s    ID  :%d    성별  :%c    나이  :%d 전화번호  :%s 주소  :%s  \n",temp->name,temp->id,temp->gender,temp->age ,temp->phone,temp->addr);
         temp = temp->next;
         if(temp == NULL)
         {
            temp = head;
            break;
         }
      }
      if(id != temp->id)
      {
         temp = temp->next;
         if(temp==NULL)
         {
            temp = head;


            break;
         }
      }

   }
   printf("\n\n");
   printf("======================================================================================\n\n\n");
   return ;

}

void gendersearch(void)
{
   char gender;
   struct people *temp = head;

   fflush(stdin);
   while(1)

   {

      printf("찾고자 하는 성별을 입력하시오 :");

      scanf("%c", &gender);
      if(gender !='f' && gender !='m')
      {
         printf("남자 = f 여자 = m 만 입력하시오\n");
         fflush(stdin);
         continue;
      }
      break;

   }
   fflush(stdin);
   system("clear");
   printf("==================================== 검   색   결   과 ======================================\n\n\n");

   while(1)

   {
      fflush(stdin);
      if(gender == temp->gender)
      {

          printf("이름  :%s    ID  :%d    성별  :%c    나이  :%d 전화번호  :%s 주소  :%s  \n",temp->name,temp->id,temp->gender,temp->age ,temp->phone,temp->addr);
         temp = temp->next;
         if(temp == NULL)
         {
            temp = head;
            break;
         }

      }

      if(gender !=temp->gender)
      {
         temp = temp->next;
         if(temp ==NULL)
         {
            temp = head;
            break;
         }
      }
   }
   printf("\n\n\n");
   printf("========================================================================================================\n\n\n");
   return ;

}





void phonesearch(void)

{
	struct people *temp = head;
	char phone[20];
	printf("찾을 사람의 번호를 입력하시오 :");
	scanf("%20s",phone);
	fflush(stdin);
	system("clear");
	printf("================================== 검   색   결   과 ===================================\n\n\n");
	while(1)
	{
		if(strstr(temp->phone,phone))
		{                           
		 printf("이름  :%s    ID  :%d    성별  :%c    나이  :%d 전화번호  :%s 주소  :%s  \n",temp->name,temp->id,temp->gender,temp->age ,temp->phone,temp->addr);
			temp = temp->next;

			if(temp == NULL)
			{
				temp = head;
				break;
			}

		}
		else if(!strstr(temp->phone,phone))
			temp = temp->next;

		if(temp == NULL)
		{
			break;
		}


	}
	printf("\n\n\n\n");
	printf("==============================================================================================\n\n\n");
	return ;

}




void addrsearch(void)

{
	struct people *temp = head;
	char addr[20];
	printf("찾을 사람의 주소를 입력하시오 :");
	scanf("%20s",addr);
	fflush(stdin);
	system("clear");
	printf("================================== 검   색   결   과 ===================================\n\n\n");
	while(1)
	{
		if(strstr(temp->addr,addr))
		{                           
		 printf("이름  :%s    ID  :%d    성별  :%c    나이  :%d 전화번호  :%s 주소  :%s  \n",temp->name,temp->id,temp->gender,temp->age ,temp->phone,temp->addr);
			temp = temp->next;

			if(temp == NULL)
			{
				temp = head;
				break;
			}

		}
		else if(!strstr(temp->addr,addr))
			temp = temp->next;

		if(temp == NULL)
		{
			break;
		}


	}
	printf("\n\n\n\n");
	printf("==============================================================================================\n\n\n");
	return ;

}







int kill(int number)
{
   struct people *temp = head;
   while(1)

   {

      if(count == 0)
         break;

      if(temp->id == number)
         return 1;



      if(temp->next == NULL)
         break;

      temp = temp->next;

   }









   return 0 ;

}

 

댓글 0