Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Arbol c++

Estas en el tema de Arbol c++ en el foro de C/C++ en Foros del Web. Buenos días a todos, Necesito ayuda con este código, ya que no estoy ducho en lo que es programacion, estuve intentando dar con el error ...
  #1 (permalink)  
Antiguo 18/04/2016, 08:03
 
Fecha de Ingreso: abril-2016
Ubicación: Caracas
Mensajes: 2
Antigüedad: 8 años
Puntos: 0
Arbol c++

Buenos días a todos,

Necesito ayuda con este código, ya que no estoy ducho en lo que es programacion, estuve intentando dar con el error y hasta los momentos nada. El codigo debe guardar y leer, la función de leer es la que no me queda clara, Aqui se los dejo a ver si me pueden ayudar


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<fstream>
#define TRUE 1
#define FALSE 0
using namespace std;


/*struct infantes {
int co_inf;
string nom_inf;
string apell_inf;
float mes_inf;
struct infantes *gizq;
struct infantes *gder;
};*/
ofstream fguar("Guardar datos.txt");
//ifstream fguar;
struct infantes {
int co_inf;
string nom_inf;
string apell_inf;
float mes_inf;
struct infantes *gizq;
struct infantes *gder;
};
typedef struct infantes *infante;


infante crearInfante(int co_inf, string nom_inf, string apell_inf, float mes_inf)
{
infante nuevoInfante = new(struct infantes);
nuevoInfante->co_inf = co_inf;
nuevoInfante->nom_inf = nom_inf;
nuevoInfante->apell_inf = apell_inf;
nuevoInfante->mes_inf = mes_inf;
nuevoInfante->gizq = NULL;
nuevoInfante->gder = NULL;

return nuevoInfante;
}


void cargar(infante &arbol, int co_inf, string nom_inf, string apell_inf, float mes_inf)
{
if(arbol==NULL){
arbol = crearInfante(co_inf,nom_inf,apell_inf,mes_inf);
}

else if(co_inf < arbol->co_inf){
cargar(arbol->gizq, co_inf,nom_inf,apell_inf,mes_inf);}
else if(co_inf > arbol->co_inf){
cargar(arbol->gder, co_inf,nom_inf,apell_inf,mes_inf); }
}


void preOrden(infante arbol)
{
if(arbol!=NULL)
{
cout <<" " << arbol->co_inf <<" " << arbol->nom_inf <<" " << arbol->apell_inf <<" " << arbol->mes_inf << endl;
preOrden(arbol->gizq);
preOrden(arbol->gder);
}
}


void posOrden(infante arbol)
{
if(arbol!=NULL)
{
preOrden(arbol->gizq);
cout <<" " << arbol->co_inf <<" " << arbol->nom_inf <<" " << arbol->apell_inf <<" " << arbol->mes_inf << endl;
preOrden(arbol->gder);
}
}

void guardar(infante arbol)
{
if(arbol!=NULL)
{
guardar(arbol->gizq);
cout <<" " << arbol->co_inf <<" " << arbol->nom_inf <<" " << arbol->apell_inf <<" " << arbol->mes_inf << endl;
// fprintf(pa,"%s",arbol->nom_inf);
fguar <<arbol->co_inf<< ","<<arbol->nom_inf<< ","<<arbol->apell_inf<< ","<<arbol->mes_inf<< endl;

guardar(arbol->gder);
}
}
void leer(infante arbol){
int co_inf1;
string nom_inf1;
string apell_inf1;
float mes_inf1;
ifstream fleer("Guardar datos.txt");

do {

}while(fleer.eof())

}
void dibujarArbol(infante arbol, int nivel){
if(arbol==NULL){
return;}
else{
dibujarArbol(arbol->gder, nivel+1);
for(int i=0; i<nivel; i++)
cout<<" ";
cout<< arbol->co_inf <<endl;
dibujarArbol(arbol->gizq, nivel+1);
}
}


void tituloArbol(){
printf ("Proyecto Arbol Guardar/Leer \n");

}


void menuArbol(){
printf ("Menu\n\n");
cout << " 1. Cargar Infante" << endl;
cout << " 2. Pre-orden" << endl;
cout << " 3. Post-orden" << endl;
cout << " 4. Salir" << endl;
cout << " 5. Guardar" << endl;
cout << " 6. Leer" << endl;
cout << " selecione una opcion: ";
}


void pausa(){
cout << "Pulse una [tecla] para continuar...";
getwchar();
getwchar();
}


int main()
{
bool bandera=false;
char tecla;

infante arbol = NULL;

int codigo;
string nombre,apellido;
float meses;

do
{
system("cls");
cin.clear();
tituloArbol();
menuArbol();
cin >> tecla;

switch(tecla)
{
case '1':
system("cls");
tituloArbol();

cout << " ingrese datos del infante \n";
cout << " Ingrese Codigo: ";
cout << " "; cin >> codigo;
cout << " Ingrese Nombre: ";
cout << " "; cin >> nombre;
cout << " Ingrese Apellido: ";
cout << " "; cin >> apellido;
cout << " Ingrese Edad : ";
cout << " "; cin >> meses;

cargar( arbol, codigo,nombre,apellido,meses);

cout << "\n "; pausa();
break;

case '2':
system("cls");
tituloArbol();

cout << " pre-orden\n";

cout <<"\n ESQUEMA INICIAL DEL ARBOL \n" << endl;
dibujarArbol(arbol,0);

preOrden(arbol);
cout << "\n "; pausa();
break;

case '3':
system("cls");
tituloArbol();

cout << "post orden \n";

cout <<"\n ESQUEMA INICIAL DEL ARBOL \n" << endl;
dibujarArbol(arbol,0);

posOrden(arbol);
cout << "\n "; pausa();
break;


case '4':
bandera=true;
break;

case '5':
system("cls");
tituloArbol();
ofstream fguar("Guard.txt");
cout << "Guardar \n";

cout <<"\n ESQUEMA INICIAL DEL ARBOL \n" << endl;
dibujarArbol(arbol,0);

guardar(arbol);
cout << "\n "; pausa();
fguar.close();
break;

case '6':
system("cls");
tituloArbol();
//ofstream fguar("Guard.txt");
cout << "Guardar \n";
cout <<"\n ESQUEMA INICIAL DEL ARBOL \n" << endl;
dibujarArbol(arbol,0);
guardar(arbol);
cout << "\n "; pausa();

break;

default:
system("cls");
tituloArbol();
cout << "\n DISCULPE, OPCION INVALIDA.\a\n";
cout << "\n "; pausa();
break;
}
}while(bandera!=true);
}

Etiquetas: arboles, c++
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:22.