/*
제 목 : 데이터구조 4주차 과제 DLL 활용한 코로나 확진자 리스트 만들기
기 능 : 정보의 입출력 및 삭제
파일이름 : 201622821_김영진_데이터구조_4주차과제.cpp
수정날짜 : 2020. 04. 22
작 성 자 : 김영진
*/
#include <iostream>
using namespace std;
class corona_world {
public:
string country = "empty";
int infected = 0, dead = 0;
double death_rate = 0.0;
corona_world* next, * prev;
};
void view(corona_world* p_head) { //출력기능 함수
corona_world* view = p_head;
cout << "국가명\t확진자\t사망자\t사망률" << endl;
for (; view != NULL; view = view->next)
cout << view->country << "\t" <<
view->infected << "\t" <<
view->dead << "\t" <<
view->death_rate << endl;
}
corona_world* AddtoSorted(corona_world* p_head) { //추가기능 함수
corona_world* p = new corona_world();
corona_world* temp;
cout << "정보를 추가합니다." << endl
<< "국가명:";
cin >> p->country;
cout << "확진자:";
cin >> p->infected;
cout << "사망자:";
cin >> p->dead;
p->death_rate = ((double)p->dead / p->infected) * 100;
p->next = NULL;
if (p_head == NULL) {
p_head = p;
p_head->next = NULL;
}
else {
temp = p_head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = p;
p->prev = temp;
p->next = NULL;
}
return p_head;
}
corona_world* delete_(corona_world* p_head) { //삭제기능 함수
corona_world* temp_1 = p_head;
corona_world* free_;
string d_country;
cout << "정보를 삭제합니다." << endl
<< "국가명:";
cin >> d_country;
if (temp_1->country.compare(d_country) == 0) {
free_ = p_head;
p_head = temp_1->next;
free(free_);
return p_head;
}
else {
for (; temp_1->country.compare(d_country) != 0; temp_1 = temp_1->next);
temp_1->prev->next = temp_1->next;
temp_1->next->prev = temp_1->prev;
free(temp_1);
return p_head;
}
}
int main(void) {
corona_world* head = NULL;
int sel = 0;
cout << "┌─────────────────────────┐" << endl
<< "│ 국가별 코로나 발병리스트│" << endl
<< "└─────────────────────────┘" << endl;
while (1) {
cout << "(0)시스템 종료\t(1)정보추가\t(2)정보확인\t(3)정보삭제" << endl
<< "입력:";
cin >> sel;
switch (sel)
{
case 0:
return 0;
case 1:
head = AddtoSorted(head);
break;
case 2:
view(head);
break;
case 3:
head = delete_(head);
break;
default:
cout << "잘못입력하셨습니다." << endl;
break;
}
}
}
댓글남기기