言語:C
OS:Windows7
コンパイラ:VisualStudio2010
#include<stdio.h>
#include<stdlib.h>
#include<crtdbg.h>
int nNum = 1;
struct CharactorData{
int Hp;
int Mp;
struct CharactorData *next;
};
void InputData(CharactorData*);
void OutputData(const CharactorData*, int);
void main(){
CharactorData* pRoot = NULL;
CharactorData* pTemp = NULL;
int nSelect;
while(1){
printf("=====1:追加 2:表示 3:終了=====\n");
printf("何を選びますか:");
scanf("%d", &nSelect);
if(nSelect == 1){
CharactorData* pPlayer;
pPlayer = (CharactorData*)malloc(sizeof(CharactorData));
InputData(pPlayer);
pPlayer->next = NULL;
if(nNum == 1){ pRoot = pPlayer; }
else{ (pPlayer - 6)->next = pPlayer; }
nNum++;
}
else if(nSelect == 2){
if(nNum > 1){
int i = 1;
pTemp = pRoot;
while(pTemp != NULL){
OutputData(pTemp, i);
pTemp = pTemp->next;
i++;
}
printf("\n");
}else{
printf("表示させるべきデータがありません\n");
}
}else if(nSelect == 3){
pTemp = pRoot;
for(int i = 0; i < nNum - 1; i++){
free(pTemp + i * 6);
}
break;
}else{
printf("追加か表示か終了以外は出来ません\n");
}
}
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
}
void InputData(CharactorData* C){
printf("\n");
printf("%d人目のHPを入力してください:", nNum);
scanf("%d", &C->Hp);
printf("%d人目のMPを入力してください:", nNum);
scanf("%d", &C->Mp);
printf("\n");
}
void OutputData(const CharactorData* C, int i){
printf("\n");
printf("%d人目のHP:%d\n", i, C->Hp);
printf("%d人目のMP:%d\n", i, C->Mp);
}