Problem with Nodes, Ancestry!

This is a discussion on Problem with Nodes, Ancestry! within the C Programming forums, part of the General Programming Boards category; Good morning, l'm needing some ideas or help. I am doing a project, where I have to create a system... ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    23

    Problem with Nodes, Ancestry!

    Good morning, l'm needing some ideas or help.
    I am doing a project, where I have to create a system... like a simulation of a Explorer.
    I want to be able to create a node(folder), in the creation i want to control where
    that node(folder) goes in the first time, I will put to the root (C. Because I have nothing
    registered yet.
    so, if a want to create more nodes(folders) in the root or inside of another node(folder)
    I want to have an option to create files inside the nodes (folders).
    Just remember, this is a simulation of control of folders and files, I want only work with strings and types.

    What I did until now.
    I've done all this part of the creation and allocation of the nodes, but I'm always
    and mistake of leaving recorded ancestor who is who, displaying the list
    correctly, this folder is inside of this other that is at the root.
    I am providing my code so if someone can help me.


    Code:
    // Arvore_Estruturada.cpp : Defines the entry point for the console application.
    //
    
    
    //#include "stdafx.h"
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    #include <dos.h>
    
    
    
    
    
    class cls_arvore
    {
    	struct str_arvore
    	{
    		char nome[30];
    		char tipo;
    		int chave;
    		int ancestral;
    		int atributo;
    		int code;
    	};
    	str_arvore objarv[100];
    	int chave,pont,Ancestral;
    //_________________________________CONTRUTOR DAS VARIAVEIS__________________________________
    //******************************************************************************************
    	public:
    		cls_arvore::cls_arvore()
    		{
    			chave = 1;
    			pont = 0;
    			Ancestral = 0;
    		}
    		
    		
    //******************************************************************************************
    //_________________________________INSERIR ESTRUTURA________________________________________
    		
    		
    		
    		
    		void cls_arvore::inserir(char descricao[30],char tipo,int ancestra,int chave,char opca)
    		{
    			if(cls_arvore::limite() == 1)
    			{
    				printf(" O systema chegou ao seu limite!!\n\n");
    				system("pause");
    			}
    			int verificador=0;
    			if(descricao[0] == '\0')
    			{
    				printf("\n E obrigatorio um nome! ");
    				system("pause");
    			}
    			else
    			{
    				if(pont == 0)
    				{
    					objarv[pont].ancestral = 0;
    					strcpy(objarv[pont].nome,descricao);
    					objarv[pont].tipo = 'n';
    					objarv[pont].chave = chave;
    					objarv[pont].atributo = 2;
    					pont++;
    				}
    				else
    				{
    					for(int i=0;i<pont;i++)
    					{
    						if(objarv[i].chave == chave)
    						{
    							verificador++;
    							break;
    						}
    					}
    					if(verificador>0)
    					{
    						system("cls");
    						printf(" Essa pasta ja existe\n\n");
    						system("pause");
    					}
    					else
    					{
    						if(opca == '1')
    						{
    							objarv[pont].ancestral = objarv[0].chave;
    							strcpy(objarv[pont].nome,descricao);
    							objarv[pont].tipo = tipo;
    							objarv[pont].chave = chave;
    							objarv[pont].atributo = 2;
    							pont++;
    						}
    						if(opca == '2')
    						{
    							objarv[pont].ancestral = ancestra;
    							strcpy(objarv[pont].nome,descricao);
    							objarv[pont].tipo = tipo;
    							objarv[pont].chave = chave;
    							objarv[pont].atributo = 2;
    							pont++;
    						}
    					}
    				}
    			}
    		}
    		
    		
    //******************************************************************************************
    //_____________________________VERIFICA SE E A PRIMEIRA INSERÇAO____________________________
    		int cls_arvore::primeira()
    		{
    			if(pont==0)
    			{
    				return 1;
    			}
    		}
    		
    		
    //******************************************************************************************
    //_______________________________________EXIBIR_____________________________________________
    		
    		
    		void cls_arvore::exibir()
    		{
    			system("cls");
    			for(int i=0;i<pont;i++)
    			{
    				printf("\n Descricao = %s\n Tiop = %c\n Chave = %d\n Ancestral = %d\n Atributo = %d\n_________________________\n",
    						objarv[i].nome,objarv[i].tipo,objarv[i].chave,objarv[i].ancestral,objarv[i].atributo);
    			}
    			system("pause");
    		}
    //______________________________VERIFICADOR DO LIMITE_______________________________________
    //******************************************************************************************
    private:
    	int cls_arvore::limite()
    	{
    		if(pont == 99)
    		{
    			return 1;
    		}
    	}
    
    //_____________________________________ALTERAR ATRIBUTO____________________________________
    //*****************************************************************************************
    	void cls_arvore::Alterar_Atributo(int chav,int atri)
    	{
    		int i,verificador=1;
    		for(i=0;i<pont;i++)
    		{
    			if(chav == objarv[i].chave)
    			{
    				verificador = 0;
    				break;
    			}
    		}
    		if(verificador == 0)
    		{
    			objarv[i].atributo = atri;
    		}
    	}
    	
    	
    //______________________________Especificar_________________________________________________
    public:
    	int cls_arvore::expecificar()
    	{
    		int opc;
    		system("cls");
    		for(int i=0;i<pont;i++)
    		{
    			if(objarv[i].tipo =='n')
    			{
    				printf(" %d Descricao = %s  Tipo = %c\n\n",i+1,objarv[i].nome,objarv[i].tipo);
    			}
    		}
    		printf("\n\n Entre com o numero a esquerda para selecionar a pasta. ->  ");
    		scanf("%d",&opc);
    		
    		
    		return objarv[opc].chave;
    	}
    	
    	
    //******************************************************************************************
    //___________________________________FIM CLASSE_____________________________________________
    //******************************************************************************************
    };
    
    
    int main()
    {
    	cls_arvore obj_arvore;
    	char opc = '1',descri[30],tip,teste[20];
    	int ances = 0,y = 0,cha,atribu;
    	while(opc!='0')
    	{
    		descri[0] = '\0';
    		system("cls");
    		printf(" 1)- Insrir na Raiz\n 2)- Inserir em um local especifico\n 0)- Sair");
    		opc=getch();
    		if(opc == '1')
    		{
    			printf("\n Entre com o nome -> ");
    			fflush(stdin);
    			gets(descri);
    			printf("\n Entre com o numero da chave");
    			scanf("%d",&cha);
    			if(obj_arvore.primeira() != 1)
    			{
    				printf("\n Entre com o tipo, (n) para No e (i) pata Item -> ");
    				tip=getch();
    				if(tip != 'n' && tip != 'i')
    				{
    					system("cls");
    					printf(" Tipo invalido, entre com (n) para no ou (i) para item!!\n\n");
    					system("pause");
    				}
    				else
    				{
    					printf("\n Entre com o ancestral -> ");
    					scanf("%d",&ances);
    					obj_arvore.inserir(descri,tip,ances,cha,opc);
    				}
    			}
    			else
    			{
    				obj_arvore.inserir(descri,tip,ances,cha,opc);
    			}
    		}
    		if(opc=='2')
    		{
    			if(obj_arvore.primeira() != 1)
    			{
    				ances = obj_arvore.expecificar();
    				printf("\n Entre com o nome -> ");
    				fflush(stdin);
    				gets(descri);
    				printf("\n Entre com o numero da chave");
    				scanf("%d",&cha);
    				printf("\n Entre com o tipo, (n) para No e (i) pata Item -> ");
    				tip=getch();
    				if(tip != 'n' && tip != 'i')
    				{
    					system("cls");
    					printf(" Tipo invalido, entre com (n) para no ou (i) para item!!\n\n");
    					system("pause");
    				}
    				else
    				{
    					obj_arvore.inserir(descri,tip,ances,cha,opc);
    				}
    			}
    			else
    			{
    				system("cls");
    				printf(" Nao existe nenhuma pasta criada!!! ");
    				getch();
    			}
    		}
    		if(opc == '3')
    		{
    			obj_arvore.exibir();
    		}
    	}
    }
    Last edited by Salem; 04-14-2010 at 09:52 AM. Reason: Removed email / payment offers

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    First, decide which language you're programming in.

    It looks a lot like C (and that's the forum you posted in), so what's with the class (C++ only).

    It would help if you named your file Arvore_Estruturada.c so that the compiler will guess that this really is C code.
    Then you would have to re-write your class in C terms.


    Second, remove all these bogus header files which you shouldn't need for this program.
    #include <conio.h>
    #include <windows.h>
    #include <dos.h>


    Third, read the FAQs on fflush(stdin) and gets()
    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

    > int cls_arvore:rimeira()
    This returns a junk value (maybe even 1), if the condition fails.
    Ditto for your limite() function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. delete by copy only for non-children nodes?
    By patricio2626 in forum C++ Programming
    Replies: 17
    Last Post: 11-18-2006, 07:37 AM
  3. Programmatically creating nodes in a treeview problem
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 10-12-2006, 02:41 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21