기능은 모두 구현하였습니다.
단,
insert시 순차정렬 미적용,
sortByHp시 memcpy를 통한 값 교환으로 구현하였습니다.
 
기본적인 챔피언의 리스트를 연결리스트를 이용해서 구현

1. 챔피언의 이름 검색
2. 새로운 챔피언의 삽입
3. 챔피언의 삭제
4. 특정 챔피언의 삭제
5. 특정 포지션의 챔피언 모두 삭제
6. 저장된 챔피언 출력
7. 최대 HP인 챔피언 호출
8. 체력 순서대로 재정렬

해당 기능이 포함되어 있습니다.
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
 
struct lolCham
{
char name[50];
int hp;
int mp;
int range;
int speed;
char position[50];
lolCham *next;
};
 
struct linkedList
{
lolCham *head = NULL;
};
 
void arr2LinkedList(linkedList *list, lolCham *cham)
{
int i = 0;
struct lolCham *nnew;
struct lolCham *now = list->head;
 
for (i = 0; i < 10; i++)
{
nnew = (lolCham*)malloc(sizeof(lolCham));
*nnew = cham[i];
 
if (list->head == NULL)
{
list->head = nnew;
now = list->head;
}
else
{
now->next = nnew;
now = now->next;
}
}
now->next = list->head;
}
 
void search(linkedList *list, char *findCham)
{
lolCham *now = list->head;
lolCham *i = 0;
 
if (list->head == NULL)
{
printf("챔피언이 한명도 없습니다.\\n");
return;
}
if (strcmp(now->name, findCham) == 0)
{
printf("이름\\t체력\\t마나\\t사거리\\t이속\\t포지션\\n");
printf("%s\\t%d\\t%d\\t%d\\t%d\\t%s\\n", now->name, now->hp, now->mp, now->range, now->speed, now->position);
return;
}
now = list->head->next;
 
while (now != list->head)
{
if (strcmp(now->name, findCham) == 0)
{
printf("이름\\t체력\\t마나\\t사거리\\t이속\\t포지션\\n");
printf("%s\\t%d\\t%d\\t%d\\t%d\\t%s\\n", now->name, now->hp, now->mp, now->range, now->speed, now->position);
return;
}
else
{
now = now->next;
}
}
printf("해당 챔피언은 없습니다.\\n");
}
 
void insertCham(linkedList *list, char *insertChamName)
{
lolCham *now = list->head;
lolCham *nnew;
 
while (now->next != list->head)
{
if (strcmp(now->name, insertChamName) == 0)
{
printf("해당 챔피언이 이미 존재합니다.\\n");
return;
}
else
{
now = now->next;
}
}
 
nnew = (lolCham*)malloc(sizeof(lolCham));
 
strcpy(nnew->name, insertChamName);
printf("체력 : ");
scanf("%d", &nnew->hp);
printf("마나 : ");
scanf("%d", &nnew->mp);
printf("사거리 : ");
scanf("%d", &nnew->range);
printf("이속 : ");
scanf("%d", &nnew->speed);
printf("포지션 : ");
scanf("%s", &nnew->position);
 
now->next = nnew;
nnew->next = list->head;
return;
}
 
void deleteCham(linkedList *list, char *deleteChamName)
{
lolCham *now = list->head;
lolCham *temp;
lolCham *nnew = list->head;
lolCham *prev = list->head;
 
while (now->next != list->head)
{
if (strcmp(now->name, deleteChamName) == 0)
{
temp = now->next;
printf("삭제되었습니다.\\n");
break;
}
 
if (now->next == list->head)
{
printf("해당 이름이 존재하지 않습니다.\\n");
return;
}
else
{
now = now->next;
}
}
 
if (now->next == list->head) // 맨뒤삭제
{
while (prev->next != now)
{
prev = prev->next;
}
free(now);
prev->next = list->head;
 
return;
}
 
if (list->head == now) // 맨앞삭제
{
while (nnew->next != list->head)
{
nnew = nnew->next; // tmep는 맨앞의 다음꺼
}
nnew->next = list->head->next;
free(now);
list->head = temp;
nnew = list->head;
}
else
{
while (nnew->next != now)
{
nnew = nnew->next;
}
free(now);
nnew->next = temp;
}
}
 
void deletePos(linkedList *list, char *deletePosName)
{
struct lolCham *temp[100];
int i = 0;
int j = 0;
 
struct lolCham *now = list->head;
 
if (list->head != NULL)
{
if (strcmp(now->position, deletePosName) == 0)
{
temp[i++] = now;
}
now = now->next;
 
while (now != list->head)
{
if (strcmp(now->position, deletePosName) == 0)
{
temp[i++] = now;
}
now = now->next;
}
}
for (j = 0; j < i; j++)
{
deleteCham(list, temp[j]->name);
}
}
 
void printfAll(linkedList *list)
{
lolCham *now = list->head;
 
if (now->hp < 0)
{
printf("챔피언이 없어요!\\n");
return;
}
 
if (list->head != NULL)
{
printf("이름\\t체력\\t마나\\t사거리\\t이속\\t포지션\\n");
printf("%s\\t%d\\t%d\\t%d\\t%d\\t%s\\n", now->name, now->hp, now->mp, now->range, now->speed, now->position);
now = list->head->next;
while (now != list->head)
{
if (now->hp == 0)
{
printf("챔피언을 삽입해주세요.");
}
printf("%s\\t%d\\t%d\\t%d\\t%d\\t%s\\n", now->name, now->hp, now->mp, now->range, now->speed, now->position);
now = now->next;
}
}
}
 
void hpMax(linkedList *list)
{
struct lolCham *max = list->head;
struct lolCham *now = list->head->next;
 
while (now->next != list->head)
{
if (now->hp > max->hp)
{
max = now;
}
now = now->next;
}
printf("가장 체력이 높은 챔피언은 %s입니다.\\n", max->name);
}
 
void sortByHp(linkedList *list)
{
struct lolCham temp;
struct lolCham *i;
struct lolCham *j;
 
for (i = list->head; i->next != list->head; i = i->next)
{
for (j = i->next; j != list->head; j = j->next)
{
if (i->hp < j->hp)
{
memcpy(&temp, i, 116);
memcpy(i, j, 116);
memcpy(j, &temp, 116);
}
}
}
printf("정렬되었습니다.\\n");
}
 
int main()
{
linkedList list;
struct lolCham cham[10] =
{
{ "루시우", 111, 123, 123, 123, "탑", NULL },
{ "티모", 121, 123, 123, 123, "정글", NULL },
{ "맥북", 1511, 123, 123, 123, "미드", NULL },
{ "사과", 11511, 123, 123, 123, "원딜", NULL },
{ "형준", 1311, 123, 123, 123, "서포터", NULL },
{ "파판", 1111, 123, 123, 123, "탑", NULL },
{ "메이플", 1911, 123, 123, 123, "정글", NULL },
{ "마우스", 15611, 123, 123, 123, "미드", NULL },
{ "키보드", 1121, 123, 123, 123, "원딜", NULL },
{ "비주얼", 1111, 123, 123, 123, "서포터", NULL },
};
arr2LinkedList(&list, cham);
 
char selectOption = 0;
char findCham[50];
char insertChamName[50];
char deleteChamName[50];
char deletePosName[50];
 
while (1)
{
system("cls");
 
printf("1.search\\n");
printf("2.insert\\n");
printf("3.delete\\n");
printf("4.deletePos\\n");
printf("5.printAll\\n");
printf("6.hpMax\\n");
printf("7.sortByHp\\n");
printf("실행할 기능은 : ");
scanf("%d", &selectOption);
 
if (selectOption > 7 || selectOption < 1)
{
printf("잘못된 수를 입력\\n");
system("pause");
continue;
}
 
switch (selectOption)
{
case 1:
{
printf("찾으려는 챔피언의 이름 : ");
scanf("%s", &findCham);
search(&list, findCham);
system("pause");
break;
}
case 2:
{
printf("추가하려는 챔피언의 이름 : ");
scanf("%s", &insertChamName);
insertCham(&list, insertChamName);
system("pause");
break;
}
case 3:
{
printf("지우려는 챔피언의 이름 : ");
scanf("%s", &deleteChamName);
deleteCham(&list, deleteChamName);
system("pause");
break;
}
case 4:
{
printf("지우려는 포지션의 이름 : ");
scanf("%s", &deletePosName);
 
if (strcmp(deletePosName, "탑") == 0
|| strcmp(deletePosName, "정글") == 0
|| strcmp(deletePosName, "미드") == 0
|| strcmp(deletePosName, "원딜") == 0
|| strcmp(deletePosName, "서포터") == 0)
{
deletePos(&list, deletePosName);
}
else
{
printf("그런 포지션은 존재하지 않습니다.\\n");
system("pause");
break;
}
system("pause");
break;
}
case 5:
{
printfAll(&list);
system("pause");
break;
}
case 6:
{
hpMax(&list);
system("pause");
break;
}
case 7:
{
sortByHp(&list);
system("pause");
break;
}
}
 
}
 
return 0;
}


'공부용 > 프로그래밍' 카테고리의 다른 글

C언어 오목  (0) 2018.01.25

+ Recent posts