Деревья

11.08.2014
1
0
#1
Здравствуйте. У меня возникли проблемы. Вот задание:
Описать массив записей "семья".
--------------------------------------------------------------
Отец ! Мать ! Ребенок ! ! Ребенок
--------------------------------------- -----------
Ф.И.О.!дата !Ф.И.О.!дата ! Имя !дата ! ...... ! Имя !дата
!рожд.! !рожд.! !рожд.! ! !рожд.
---------------------------------------------------------------
!__________________ ________________!
не более 5 детей

Найти и вывести имя младшего ребенка у Иванова И.И. по форме
_______________________________
имя ребенка ! дата рождения !
-------------------------------------------

Программа выдала необработанное исключение и вдобавок результат вывелся не совсем корректно. Что я сделал неправильно?

Вот мой код:

C++:
// laba 12-1.cpp: главный файл проекта.
#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

struct Date 		
{
unsigned day; 		
unsigned month;		
unsigned year;
};

struct Children
{
char name_of_child[30];
Date d_birth_child;
};

struct T_family 
{
char fio_father[30];
Date d_birth_father;
char fio_mother[30];
Date d_birth_mother;
Children ch[5];
int k;
}; 

struct TreeNode
{
int k;
T_family family;
Children ch[5];
TreeNode *next;
TreeNode *child;
};

class date_of_birth
{
private:
TreeNode *head;
public:
date_of_birth()
{
head = new TreeNode;
TreeNode *node1 = new TreeNode;
node1->k = 1;
node1->child = NULL;
node1->next = NULL;
head->child = node1;
TreeNode *node2 = new TreeNode;
node2->k = 2;
node2->child = NULL;
node2->next = NULL;
node1->next = node2;
TreeNode *node3 = new TreeNode;
node3->k = 3;
node3->child = NULL;
node3->next = NULL;
node2->next = node3;
T_family a = {"Ivanov I. I.", 21,5,1965,"Ivanova E.G.",13,2,1967,"Oleg",19,7,1994,"Olga",13,8,1996};
TreeNode *nodeA = new TreeNode;
nodeA->family = a;
nodeA->child = NULL;
nodeA->next = NULL;
node1->child = nodeA;
T_family b = {"Petrov P. P.", 12,4,1962,"Petrova E.O.",8,12,1965,"Pavel",12,4,1988,"Ekaterina",24,8,1992};
TreeNode *nodeB = new TreeNode;
nodeB->family = b;
nodeB->child = NULL;
nodeB->next = NULL;
node2->child = nodeB;
}

void add_node(int i)
{
int count;		
T_family a; 
char c;
cout << endl << " Enter data of" << i+1 << "person\n ";
cout << " fio of father ";
cin.getline (a.fio_father, 30);
cout << "Enter date of father's birthday: \n";
cout << " day (1-31) ";
cin >> a.d_birth_father.day;
cin.get(c);
cout << " month (1-12)";
cin >> a.d_birth_father.month;
cin.get(c);
cout << " year ";
cin >> a.d_birth_father.year;
cin.get(c);
cout << " fio of mother ";
cin.getline (a.fio_mother, 30);
cout << endl <<"Enter date of mother's birthday: \n";
cout <<" day (1-31) ";
cin >> a.d_birth_mother.day;
cin.get(c);
cout << " month (1-12)";
cin >> a.d_birth_mother.month;
cin.get(c);
cout << " year ";
cin >> a.d_birth_mother.year;
cin.get(c);
//запросить кол-во детей и запомнить в count
cout << "Enter count of children" << endl;
cin >> count;
cin.get(c);
if ((count > 5) && (count < 0)) cout << " Fatal error " << endl;
a.k = count;
for (int k=0; k < count; k++) {
cout << endl << " Name of " " " << k+1 << " " " child ";
cin.getline (a.ch[k].name_of_child, 30);
cout << endl << "Enter date of" " "<< k+1 << " " "child's birthday: \n";
cout <<" day (1-31) ";
cin >> a.ch[k].d_birth_child.day;
cin.get(c);
cout << " month (1-12)";
cin >> a.ch[k].d_birth_child.month;
cin.get(c);
cout << " year ";
cin >> a.ch[k].d_birth_child.year;
cin.get(c); 
}

TreeNode *node = head->child;
while(true)
{
if(node->k == a.k)
{
TreeNode *newNode = new TreeNode;
newNode->family = a;
newNode->child = NULL;
newNode->next = 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->k = a.k;
newNode->child = NULL;
newNode->next = NULL;
node->next = newNode;
TreeNode *nodeA = new TreeNode;
nodeA->family = a;
nodeA->child = NULL;
nodeA->next = NULL;
newNode->child = nodeA;
}

void print_tree()
{
printf("[ \n");
TreeNode *node = head->child;
while(node != NULL)
{
cout << " --> " << node->k << endl;
if(node->child != NULL)
{
TreeNode *child = node->child;
while(child != NULL)
{
printf("! %10s ! %2d.%2d.%4d !",child->family.fio_father, child->family.d_birth_father.day, child->family.d_birth_father.month, child->family.d_birth_father.year);
printf("! %10s ! %2d.%2d.%4d !",child->family.fio_mother, child->family.d_birth_mother.day, child->family.d_birth_father.month, child->family.d_birth_father.year);
for (int k = 0; k < child->family.k; k++) {
printf("! %10s ! %2d.%2d.%4d!", child->family.ch[k].name_of_child, child->family.ch[k].d_birth_child.day, child->family.ch[k].d_birth_child.month, child->family.ch[k].d_birth_child.year);
printf("\n");
}
child = child->next;
}
}
node = node->next;
}
printf("]\n");
}

void find_min()
{
int k_min = 0;
TreeNode *node = head->child;
TreeNode *young = node->child;
while(node != NULL)
{
if(node->k < young->k && node->child != NULL)
young = node->child;
node = node->next;
k_min = young->k;
}

printf("! %10s ! %2d.%2d.%4d !\n", young->family.ch[k_min].name_of_child, young->family.ch[k_min].d_birth_child.day, young->family.ch[k_min].d_birth_child.month, young->family.ch[k_min].d_birth_child.year);
}
};

int main()
{
cout << "	  Avtor - Shusharin A.V., student gr. ISEbd-11" << endl;
cout << "	  Variant N 1" << endl;
cout << "	  Programma sozdaet massiv structur, zapolnaet ego dannimi i vivodit na ekran etot massiv v vide tablici" << endl;
cout << "	  Programma opredelaet i vivodit imya mladshego rebenka u Ivanova I.I" << endl;
date_of_birth d;
for(int i = 2; i < 3; i++)
{ 	
d.add_node(i);
};
cout << "\nlist of structs\n!	  father		!	  mother		!		child		!\n";
d.print_tree();
d.find_min();
_getch();
return 0;
}
Ошибки и результат выполнения в скриншотах.
 

Вложения

  • 42.5 КБ Просмотры: 18
  • 78.1 КБ Просмотры: 20
R

rrrFer

Гость
#2
выполните программу по шагам и узнайте на какой строке ошибка.
В вижуал студии это кнопка F10 вроде бы.