본문 바로가기

PROFILE/Independent project

Star Dic - Dictionary / Dos based / C, Algorithm(sort, linked list, etc), File DB / April. 2008


---- 130 Lines without DB ---
#include <iostream>
using namespace std;
#define MAX 8
struct Data{ char* Name;
int Health, Mana;
};
struct Node{ Data* data;
Node *prev,*next;
};
struct List{ Node* head;
};
List* Start();
void Insert(char* Name,int Health,int Mana);
void PrintAll();
void Del(char* Name);
void MaxHealth();
void SortByHealth();
List* UnitList= Start();

void main(){ FILE* f = fopen("unit.txt","r");
Data* data = new Data;
data->Name = new char;
for(int i=0;i<MAX;i++){
fscanf(f," %s %d %d " , data->Name, &data->Health, &data->Mana);
Insert(data->Name, data->Health, data->Mana);
}
RESTART: int key;
cout << " key" <<endl;
cin >> key;
switch(key){ case 1:{ cout << "1.출력" << endl;
PrintAll();
break;}
case 2:{ cout << "2.입력(이름체력마나순)" << endl;
cin>>data->Name;
cin>>data->Health;
cin>>data->Mana;
Insert(data->Name,data->Health,data->Mana);
break;}
case 3:{ cout << "3.삭제(이름)" << endl;
cin >> data->Name;
Del(data->Name);
break;}
case 4:{ cout << "4.최대체력" << endl;
MaxHealth();
break;}
case 5:{ cout << "5.체력정렬" << endl;
SortByHealth();
break;}
}
system("pause");
system("cls");
goto RESTART;
}

List* Start(){
Node* head = new Node;
head->data= 0;
head->next= head;
head->prev= head;
List* list = new List;
list->head=head;
return list;
}
void Insert(char* Name,int Health,int Mana){
Node* after = UnitList->head;
Node* before = UnitList->head->prev;
Data* data= new Data;
data->Name = new char;
strcpy(data->Name,Name); //
data->Health=Health;
data->Mana=Mana;
Node* now = new Node;
now->data = data;
now->prev = before;
now->next = after;
after->prev = now;
before->next = now;
}
void PrintAll(){
Node* current = UnitList->head->next;
while(current!=UnitList->head){
cout << current->data->Name <<"\t"<< current->data->Health <<"\t"<< current->data->Mana<<endl;
current=current->next;
}
}
void Del(char* Name){
Node* current = UnitList->head->next;
while(current!=UnitList->head){
if(strcmp(Name,current->data->Name)==0){ //
Node* after = current->next;
Node* before = current->prev;
after->prev = before;
before->next = after;
break; //
} current=current->next;
}
}
void MaxHealth(){
Node* current = UnitList->head->next;
int buff=0;
while(current!=UnitList->head){
if(buff<current->data->Health)
buff=current->data->Health;
current=current->next;
}
cout<<buff <<"가 최대체력입니다"<<endl;
}
void SortByHealth(){
Node* current = UnitList->head->next;
for(int i=0;i<MAX;i++)
for(int j=0;j<MAX;j++){
if(current->next->data==0)
current=current->next;
if(current->data==0)
current=current->next;
if(current->data->Health>current->next->data->Health){
Node* before = current->prev;
Node* now = current;
Node* after = current->next;
Node* afterafter = current->next->next;
before->next=after;
after->prev=before;
after->next=now;
now->prev=after;
now->next=afterafter;
afterafter->prev=now;
}current=current->next;
}
}