Программа должна используя Дерево выводить список игроков сначала всех играющих в основном составе ниже играющих в резерве.... она же выводит всех в перемешку.... Почему?
Код:
#include"stdafx.h"
#include"iostream"
#include"conio.h"
#include <string.h>
using namespace std;
struct Data
{
unsigned day;
unsigned month;
unsigned year;
};
struct T_sport
{
char fio[40];
Data d_birth;
unsigned v_CSKA ;
char MestoVkomande[20] ;
char Sostav [20];
};
struct TreeNode
{
//char MestoVkomande [20];
string Sostav ;
T_sport sport;
TreeNode *next;
TreeNode*child;
};
class osnovnoi
{
private:
TreeNode *head;
public:
osnovnoi()
{
head = new TreeNode;
TreeNode *node1 = new TreeNode;
// node1->MestoVkomande="Forward";
node1->Sostav="Main";
node1->child = NULL;
node1->next = NULL;
head->child = node1;
TreeNode *node2 = new TreeNode;
//node2->MestoVkomande="Forward";
node2->Sostav="Reserve";
node2->child = NULL;
node2->next = NULL;
node1->next = node2;
//TreeNode *node3 = new TreeNode;
//node3->MestoVkomande="Defender";
T_sport a = {"Ivanov I. I.",3,1,1984,2000,"Defender","Main"};
TreeNode *nodeA = new TreeNode;
nodeA->sport = a;
nodeA->child = NULL;
nodeA->next = NULL;
node1->child = nodeA;
TreeNode *nodeB = new TreeNode;
T_sport b ={"Petrov A.A.",8,12,1987,2002, "Forward","Reserved"};
nodeB->sport = b;
nodeB->child = NULL;
nodeB->next = NULL;
node2->child = nodeB;
}
void add_node()
{
T_sport a;
char c;
cout<<endl<<"Enter data person\n";
cout<<" fio ";
cin.getline(a.fio,40);
cout<<endl<<"Enter date of birthday: \n";
cout<<" day (1-31) ";
cin>>a.d_birth.day;
cin.get(c);
cout<<" month (1-12)";
cin>>a.d_birth.month;
cin.get(c);
cout<<" year ";
cin>>a.d_birth.year;
cin.get(c);
cout<<" v_CSKA ";
cin>>a.v_CSKA;
cin.get(c);
cout<<" MestoVkomande ";
cin.getline(a.MestoVkomande,20);
cout<<" Sostav ";
cin.getline(a.Sostav,20);
TreeNode*node= head->child;
while(true)
{
if (strcmp(node->Sostav.c_str(),a.Sostav)==0)
{
TreeNode*newNode= new TreeNode;
newNode->sport=a;
newNode->child=NULL;
if(node->child == NULL)
node->child = newNode;
else
{
TreeNode *elem = node->child;
while(elem->next != NULL)
elem = elem->next;
elem->next = newNode;
}
return;
}
if(node->next == NULL)
break;
node = node->next;
}
TreeNode *newNode = new TreeNode;
//newNode->MestoVkomande = a.MestoVkomande;
newNode->Sostav= a.Sostav;
newNode->child = NULL;
newNode->next = NULL;
node->next = newNode;
TreeNode *nodeA = new TreeNode;
nodeA->sport = a;
nodeA->child = NULL;
nodeA->next = NULL;
newNode->child = nodeA;
}
void print_tree()
{
printf("[\n ");
TreeNode *node = head->child;
while(node != NULL)
{
cout<<" --> "<< node->Sostav.c_str()<<endl;
if(node->child != NULL)
{
TreeNode *child = node->child;
while(child != NULL)
{
printf("%20s ! %2d.%2d.%4d ! %4d ! %15s ! %15s\n",child->sport.fio,child->sport.d_birth.day,child->sport.d_birth.month,child->sport.d_birth.year,child->sport.v_CSKA,child->sport.MestoVkomande,child->sport.Sostav);
child= child->next;
}
}
node=node->next;
}
printf("]\n");
}
};
int main()
{
osnovnoi f;
for(int i = 2; i < 5; i++)
{
f.add_node();
};
f.print_tree();
_getch();
return 0;
}