Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Visual C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 12.06.2014, 18:58   #1
Валера1984
Пользователь
 
Регистрация: 10.02.2014
Сообщений: 14
По умолчанию Структура дерево

Программ должна выводить Игроков основного и резервного состава в виде дерева... она выводит основной и виснет... Подскажите где ошибка?
Код:
#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;
        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");
    }
 
  /*  void napad_osnov()
    {
    cout<<"Osnovnoi sostav "<<endl; 
    TreeNode *node = head->child;
    TreeNode *first = node->child;
    while(node != NULL)
        {
    if ((node->sport.Sostav=="Main") && (node->sport.MestoVkomande=="Forward"))
       printf("%20s!%2d.%2d.%4d \n",node->sport.fio,node->sport.d_birth.day,node->sport.d_birth.month,node->sport.d_birth.year);
       first=node->child;
    node = node->next;
 
    
   
        }
        
      printf("Основной состав", first->sport.fio);  
        
    }*/
};
 
int main()
{
    
    
    osnovnoi f;
    for(int i = 2; i < 4; i++)
    {   
        f.add_node();
    };
    
    f.print_tree();
    //f.napad_osnov();
    _getch();
 
    return 0;
}

Последний раз редактировалось Stilet; 12.06.2014 в 19:40.
Валера1984 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Структура и бинарное дерево(С++) igor1024 Помощь студентам 0 13.06.2011 15:24
Структура minato91 Общие вопросы C/C++ 1 11.03.2011 00:12
структура klykovka Помощь студентам 1 31.05.2010 13:03
Структура IF в С++ lebrosha Помощь студентам 3 22.05.2009 00:09
структура cr1mson Общие вопросы C/C++ 3 14.01.2009 18:08