Показать сообщение отдельно

  #7  
Старый 14.04.2010, 23:56
criptic
Новичок
Регистрация: 24.03.2009
Сообщений: 18
С нами: 9017234

Репутация: 12
По умолчанию

Ребят, пока еще не приходилось иметь дело с Си. помогите разобрать код пошагово.

Могу понять, что там что-то с деревьями, обход или что-то такое..




#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#define count 4

struct Tree
{
char *key;
Tree**child;
};
Tree*Root;
void Vsglad(Tree*);
void View_tree(Tree*,int);

int _tmain(int argc, _TCHAR* argv[])
{

Root = new Tree;
Root->key = new char[50];
Root->child = new Tree*[count];
for(int i=0;i<count;i++)Root->child[i]=NULL;
printf("Please enter root of the tree");
gets(Root->key);
Vsglad(Root);


return 0;
}
void Vsglad(Tree*ptr)
{
Tree*ptrN;
char mychoice;
int i;
do
{
system("cls");
system("echo 1 Посмотрите...");
system("echo 2 Пройдемте...");
system("echo 3 Добавьте...");
system("echo 4 Назад...");
system("echo q Выходите...");
View_tree(Root,1);
scanf("%c",&mychoice);
switch(mychoice)
{
case '1':
puts(ptr->key);
system("pause");
continue;
case '2':
system("echo Введите номер ветви");
printf("\n0<i<%d ",count);
scanf("%d",&i);
if(ptr->child[i]==NULL)
{
system("Тут ничего нет");
system("pause");
continue;
}
Vsglad(ptr->child[i]);
continue;
case '3':

char New_word[50];
int link;
printf("Please enter a new element of tree: ");
scanf("%s",New_word);

printf("number of link ");
scanf("%d",&link);
ptrN = new Tree;
ptrN->key = new char[50];
ptrN->child = new Tree*[count];
strcpy(ptrN->key,New_word);// ptrN->key = New_word
for(int i=0;i<count;i++)ptrN->child[i]=NULL;
ptr->child[link] = ptrN;
continue;
case '4':return;
case 'q':exit(1);
}
}while(mychoice!='q');

}
void View_tree(Tree*ptr,int glubina)
{
int n=0,i;
printf("%s\n",ptr->key);
while(1)
{
if(n==count)
{
n--;
if(ptr->child[n]==NULL)
{
return;
}
else
{

for(i=0;i<glubina;i++)printf("---");
printf("%d ",n);
glubina++;
View_tree(ptr->child[n],glubina);
glubina--;
n++;
}
return;
}
if(ptr->child[n]!=NULL)
{

for(i=0;i<glubina;i++)printf("---");
printf("%d ",n);
glubina++;
View_tree(ptr->child[n],glubina);
glubina--;
n++;

}else
{
n++;
continue;
}
}
}
 
Ответить с цитированием