Pascal/Turbo Pascal - Problema arboles.

 
Vista:
sin imagen de perfil

Problema arboles.

Publicado por diego (98 intervenciones) el 17/10/2013 19:00:56
Buenas tardes, tengo un problema que aunque no pide generar la estructura que contiene la información, yo la quiero generar, para poder hacer pruebas y ver si funciona como se debe.

El problema es el siguiente:
Un comercio de pastas frescas, dispone de una estructura con la información de las ventas que se
realizaron durante un mes. De cada venta se conoce: el código de pasta, cantidad, fecha y número de
cliente. Esta información no tiene ningún orden.

a)Se pide generar una estructura que almacene por cada código de pasta, la cantidad total vendida
durante dicho mes y los números de los clientes que la solicitaron (si el cliente solicitó más de una
vez un código de pasta, debe aparecer una sola vez para ese código de pasta). Esta estructura
debe estar ordenada por código de pasta y ser eficiente para la búsqueda por dicho criterio.

Una aclaración mia: el problema debe resolverse solo con arboles, la estructura que contiene la informacion no tiene que ser necesariamente un arbol. por eso yo elegi una lista para contener la información. y luego usar esa lista para generar el arbol.

No me funciona bien el programa, pero no se bien porque. De estar bien y solo necesitar unas correcciones me gustaria saberlas, si está casi todo mal estonces me gustaria que me mostracen como se tendría que hacer, para asi lo analizo bien y veo en que me equivoque. Desde ya muchas gracias.
Dev-Pascal

program ejercicio4;
type
info_individual = record
cod : integer;
cant : integer;
fecha: string;
numero_cliente : integer;
end;
lista_info = ^nodo_info;
nodo_info = record
elemen_info : info_individual;
dirSig_info : lista_info;
end;
lista = ^nodo_lista;
nodo_lista = record
cliente: integer;
dirSig: lista;
end;
info = record
cod_pasta : integer;
cant_vendida : integer;
nros_clientes : lista;
end;

T_arbol = ^nodo;
nodo = record
hijo_izq : T_arbol;
elemen : info;
hijo_der : T_arbol;
end;
procedure crear_arbol(var a: T_arbol);
begin
a := nil;
end;
procedure crear_lista(var L : lista_info);
begin
L := nil;
end;
procedure cargar_datos(var reg : info_individual);
begin
with (reg) do
begin
write('Ingrese el código de pasta: ');
readln(cod);
if (cod <> -1) then
begin
write('Ingrese la cantidad vendida: ');
readln(cant);
write('Ingrese la fecha: ');
readln(fecha);
write('Ingrese el número de cliente: ');
readln(numero_cliente);
end;
end;
end;
procedure agregar_cliente (var L : lista; num_cliente : integer);
var
ant, act : lista;
nue : lista;
begin
new(nue);
nue^.cliente:= num_cliente;
nue^.dirSig := nil;
act := L;
ant := act;
if (L = nil) then
L^.dirSig := nue
else
begin
while (act <> nil) do
begin
ant := act;
act := act^.dirSig;
end;
ant^.dirSig := nue;
end;
end;
procedure agregar_atras(var L: lista_info; reg: info_individual);
var
nue : lista_info;
begin
new(nue);
nue^.elemen_info := reg;
nue^.dirSig_info := nil;
L^.dirSig_info := nue;
end;


procedure cargar_lista(var L : lista_info);
var
reg : info_individual;
inicio_lista : lista_info;
begin
cargar_datos(reg);
if (reg.cod <> -1) then
begin
new(L);
L^.elemen_info := reg;
L^.dirSig_info := nil;
inicio_lista := L;
cargar_datos(reg);
while (reg.cod <> -1) do
begin
agregar_atras(L, reg);
L:= L^.dirSig_info;
cargar_datos(reg);
end;
L:= inicio_lista;
end;
end;
procedure imprimir_lista(L: lista_info);
begin
while (L <> nil) do
begin
writeln('Codigo de pasta: ', L^.elemen_info.cod);
writeln('Cantidad vendida: ', L^.elemen_info.cant);
writeln('Fecha: ', L^.elemen_info.fecha);
writeln('Codigo de cliente: ', L^.elemen_info.numero_cliente);
writeln();
L := L^.dirSig_info;
end;
end;
procedure imprimir_clientes(L : lista);
begin
write('Numeros de cliente: ');
while (L <> nil) do
begin
write(L^.cliente, ' ');
L := L^.dirSig;
end;
end;
procedure esta(list : lista; var ok: boolean; cod: integer);
begin
ok := false;
while ((list<> nil) and (ok <> true)) do
begin
if (list^.cliente = cod) then
ok := true;
list := list^.dirSig;
end;
end;

procedure actualizar_nodo_arbol(var list: lista_info; var a: T_arbol);
var
ok : boolean;
begin
if (list^.elemen_info.cod = a^.elemen.cod_pasta) then
begin
a^.elemen.cant_vendida := a^.elemen.cant_vendida + list^.elemen_info.cant;
esta(a^.elemen.nros_clientes, ok, list^.elemen_info.numero_cliente);
if (ok) then
agregar_cliente(a^.elemen.nros_clientes, list^.elemen_info.numero_cliente);
list := list^.dirSig_info;
end
end;
procedure crear_nodo_arbol(var a : T_arbol; list: lista_info);
begin
new(a);
a^.elemen.cod_pasta := list^.elemen_info.cod;
a^.elemen.cant_vendida := list^.elemen_info.cant;
a^.elemen.nros_clientes := nil;
agregar_cliente(a^.elemen.nros_clientes, list^.elemen_info.numero_cliente);
end;
procedure cargar_estructura(var a : T_arbol; var informacion : lista_info);
begin
if (a = nil) then
begin
new(a);
a^.elemen.cod_pasta := informacion^.elemen_info.cod;
a^.elemen.cant_vendida := informacion^.elemen_info.cant;
a^.elemen.nros_clientes := nil;
agregar_cliente(a^.elemen.nros_clientes, informacion^.elemen_info.numero_cliente);
end
else
begin
if (a^.elemen.cod_pasta = informacion^.elemen_info.cod) then
actualizar_nodo_arbol(informacion, a)
else
if (informacion^.elemen_info.cod >= a^.elemen.cod_pasta) then
cargar_estructura(a^.hijo_der, informacion)
else
cargar_estructura(a^.hijo_izq, informacion);
end;
end;
procedure imprimir_nodo_arbol(a: T_arbol);
begin
writeln('Codigo de pasta: ', a^.elemen.cod_pasta);
writeln('Cantidad vendida: ', a^.elemen.cant_vendida);
imprimir_clientes(a^.elemen.nros_clientes);
writeln();
end;
procedure recorrido_in_orden(a: T_arbol);
begin
if (a <> nil) then
begin
recorrido_in_orden(a^.hijo_izq);
imprimir_nodo_arbol(a);
recorrido_in_orden(a^.hijo_der);
end;
end;
{procedure recorrido_post_orden(a: T_arbol);
begin
if (a <> nil) then
begin
recorrido_post_orden(a^.hijo_izq);
recorrido_post_orden(a^.hijo_der);
write(a^.elemen, ' ');
end;
end;
procedure recorrido_pre_orden(a: T_arbol);
begin
if (a <> nil) then
begin
write(a^.elemen, ' ');
recorrido_pre_orden(a^.hijo_izq);
recorrido_pre_orden(a^.hijo_der);
end;
end;}
var
list : lista_info;
arbol : T_arbol;
inicio : lista_info;
begin
crear_lista(list);
crear_arbol(arbol);
cargar_lista(list);
imprimir_lista(list);
inicio := list;
crear_nodo_arbol(arbol, list);
cargar_estructura(arbol, list^.dirSig_info);
list:= inicio;
writeln();
recorrido_in_orden(arbol);
writeln();
readln();
readln();
end.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Problema arboles.

Publicado por ramon (2158 intervenciones) el 19/10/2013 16:13:24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{A ver si esto te ayuda}
 
program clientearbol;
 uses
    crt;
  type
     pastas_frescas = record
        codigo_pasta : longint;
            cantidad : integer;
               fecha : string[12];
         num_cliente : word;
              end;
 
       arbol = ^arbolcliente;
       arbolcliente = record
               datos : pastas_frescas;
               der, izq : arbol;
              end;
 
      ventas = record
             codig : longint;
             venta : integer;
             clien : word;
           end;
 
    nodoventa = ^lasventas;
          lasventas = record
              ven : ventas;
              iz, de : nodoventa;
            end;
 
   var
    dat : pastas_frescas;
    nodo : arbol;
    exis : boolean;
    vent : nodoventa;
 
    procedure entradacleente(var nodo : arbol; prod : pastas_frescas);
    begin
       if nodo = nil then
       begin
          new(nodo);
          nodo^.datos := prod;
          nodo^.izq := nil;
          nodo^.der := nil;
       end
     else
        if nodo^.datos.codigo_pasta < prod.codigo_pasta then
        entradacleente(nodo^.izq,prod);
        if nodo^.datos.codigo_pasta > prod.codigo_pasta then
        entradacleente(nodo^.der,prod);
    end;
 
    procedure Existe(nodo : arbol;co : longint;cl : word);
    var
      canti : integer;
      feh : string[12];
    begin
        Exis := false;
        if nodo <> nil then
        begin
   if (nodo^.datos.codigo_pasta = co) and (nodo^.datos.num_cliente = cl) then
   begin
      write('   Entre Cantidad : ');
      readln(canti);
      nodo^.datos.cantidad := nodo^.datos.cantidad + canti;
      Exis := true;
   end
 else
    Existe(nodo^.izq,co,cl);
    Existe(nodo^.der,co,cl);
    end;
  end;
 
    procedure datoscliente;
    begin
       clrscr;
       writeln('**** Entrada Cliente ****');
       writeln;
       write('   Entre Codigo Pasta : ');
       readln(dat.codigo_pasta);
       write('   Entre Num. Cliente : ');
       readln(dat.num_cliente);
       existe(nodo,dat.codigo_pasta,dat.num_cliente);
       if exis = true then
       begin
          writeln('  El codigo Pasta y Cliente Existe Actualizado ');
          writeln;
          writeln('   Pulse Una Tecla');
          readkey;
       end
     else
        begin
       write('   Cantidad           : ');
       readln(dat.cantidad);
       write('   Fecha              : ');
       readln(dat.fecha);
       entradacleente(nodo,dat);
      end;
   end;
 
   procedure presentanodos(nodo : arbol);
   begin
      if nodo <> nil then
      begin
        presentanodos(nodo^.izq);
        presentanodos(nodo^.der);
        write('  ',nodo^.datos.codigo_pasta,'       ',nodo^.datos.cantidad,
       '         ',nodo^.datos.num_cliente,'         ',nodo^.datos.fecha);
          writeln;
      end;
   end;
 
  procedure generareporteventas(var ve : nodoventa; da : pastas_frescas);
  begin
     if ve = nil then
     begin
        new(ve);
        ve^.ven.codig := da.codigo_pasta;
        ve^.ven.venta := da.cantidad;
        ve^.ven.clien := da.num_cliente;
        ve^.iz := nil;
        ve^.de := nil;
     end
  else
      if ve^.ven.codig < da.codigo_pasta then
        generareporteventas(ve^.iz,da)
      else
        generareporteventas(ve^.de,da);
  end;
 
  procedure generacionventas(me : arbol);
  begin
     if me <> nil then
     begin
         dat.codigo_pasta := me^.datos.codigo_pasta;
         dat.cantidad := me^.datos.cantidad;
         dat.num_cliente := me^.datos.num_cliente;
         generareporteventas(vent,dat);
         generacionventas(me^.izq);
         generacionventas(me^.der);
     end;
  end;
 
  procedure presentaenorden(e : nodoventa);
  begin
     if e <> nil then
     begin
         presentaenorden(e^.iz);
         presentaenorden(e^.de);
         write('  ',e^.ven.codig,'        ',e^.ven.venta,'       ',
                    e^.ven.clien);
         writeln;
     end;
  end;
 
  procedure menu;
  var
    opc, teb : char;
    sal : boolean;
  begin
      sal := false;
    repeat
       clrscr;
       writeln('***** Menu Principal *****');
       writeln;
       writeln('  1 = Entradas Cliente');
       writeln('  2 = Presenta Entradas ');
       writeln('  3 = Jenera Ventas');
       writeln('  4 = Salir');
       writeln;
       writeln('<<<<<< Elija Opcion >>>>>>');
       repeat
           teb := readkey;
       until teb in['1','2','3','4'];
       clrscr;
     case teb of
  '1' : begin
           datoscliente;
        end;
  '2' : begin
           writeln('  Cod. Prod.    Cantidad      Num. Clien.       Fecha ');
           writeln;
           presentanodos(nodo);
           writeln;
           writeln('   Pulse Una Tecla ');
           readkey;
        end;
  '3' : begin
           generacionventas(nodo);
           writeln('  Cod. Prod.       Cantidad     Num. Clien.');
           writeln;
           presentaenorden(vent);
           writeln;
           writeln('   Pulse Una Tecla ');
           readkey;
        end;
  '4' : sal := true;
    end;
    until sal = true;
  end;
 
  begin
     nodo := nil;
     vent := nil;
     menu;
  end.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar