Oppure

Loading
02/08/06 17:43
thedifferent
Salve a tutti

Devo svolgere un programma pascal che mi permetta di inserire,eliminare e stampare la lista di vari libri dati da tastiera.
Per ogni libro deve essere inoltre inserito:
-il titolo
-l'autore
-la casa editrice
-il prezzo
...che devono essere poi stampate.
Fino ad ora sono riuscito a realizzare solo un programma che aggiunge,elimina e stampa solo i libri a cui però riesco a dare solamente il titolo.Se provo a inserire le altre caratteristiche il programma mi restituisce o caratteri incomprensibili o inverte a caso l'ordine delle caratteristiche(es.sotto la voce titolo viene inserito il prezzo e così via).
Ecco il programma che aggiunge,elimina e stampa libri provvisti solo di titolo

program libreria;
uses crt;

type
dati=string;
pnodi=^nodi;
nodi=record
      dato,dato2: dati;
      pros: pnodi
     end;
var
   primo,secondo:pnodi;
   scelta:char;
   finito:boolean;
procedure init;
begin
   primo:=nil;
end;
function listavuota:boolean;
begin
   listavuota:=primo=nil
end;
procedure creanuovo(var punt:pnodi;info:dati);
begin
   new(punt);
   punt^.dato:=info;
   punt^.pros:=nil
end;
procedure inserisci;
var nuovo,temp,precedente:pnodi;
    datoinput,datoinput2:dati;
begin
    write('Inserisci il titolo del libro: ');
    readln(datoinput);
    creanuovo(nuovo,datoinput);
    if listavuota then
      primo:=nuovo
    else
    if primo^.dato>nuovo^.dato then
      begin
         temp:=primo;
         primo:=nuovo;
         primo^.pros:=temp
      end
    else
      begin
         temp:=primo;
         while (temp<>nil)and(nuovo^.dato>temp^.dato) do
          begin
             precedente:=temp;
             temp:=temp^.pros
          end;
         nuovo^.pros := temp;
         precedente^.pros:=nuovo
    end;
end;
procedure elimina;
var temp,precedente:pnodi;
    datoinput:dati;
    trovato:boolean;
begin
    write('Inserisci il nome del libro da cancellare: ');
    readln(datoinput);
    if listavuota then
       writeln('Non Š possibile cancellare alcun libro in quanto la lista Š vuota')
    else
        begin
             trovato:=false;
             precedente:=nil;
             temp:=primo;
             if primo^.dato=datoinput then
                begin
                     primo:=primo^.pros;
                     dispose(temp);
                     trovato:=true
                end
             else
                 while(not trovato)and(temp<>nil)do
                    if temp^.dato=datoinput then
                      begin
                           trovato:=true;
                           precedente^.pros:=temp^.pros;
                           dispose(temp)
                      end
                    else
                      begin
                           precedente:=temp;
                           temp:=temp^.pros
                      end;
             if not trovato then
                begin
                     writeln('Il libro inserito non esiste');
                     readln
                end
    end
end;
procedure stampa;
var temp:pnodi;
begin
     clrscr;
     writeln;
     writeln('-- ELENCO COMPLETO --');
     writeln;
     temp:=primo;
     while temp<>nil do
       begin
          writeln('TITOLO: ',temp^.dato);
          temp:=temp^.pros;
       end;
     writeln('-------------------------');
     readln
end;

begin
     init;
     finito:=false;
     repeat
           clrscr;
           gotoxy(1,50);
           textbackground(blue);
           textcolor(yellow);write(' Premi 0 ');
           textcolor(white);write('per uscire');
           gotoxy(1,1);
           writeln;
           writeln;
           writeln;
           writeln;
           writeln('               ----------BENVENUTO NELL''ARCHIVIO LIBRI----------');
           writeln;
           writeln('               1: Inserisci un libro');
           writeln('               2: Cancella un libro');
           writeln('               3: Visualizza la lista completa dei libri');
           writeln;
           repeat
                 write('Scelta: ');
                 readln(scelta)
           until(scelta>='0')and(scelta<='3');
           case scelta of
               '0':finito:=true;
               '1':inserisci;
               '2':elimina;
               '3':stampa;
           end;
     until finito
end.



Come faccio a far visualizzare anche le altre voci?(autore,casa e prezzo)
Spero possiate aiutarmi :k:
Ultima modifica effettuata da thedifferent 02/08/06 17:53
aaa
05/08/06 8:43
AndRyu™
ho dato solo un'occhiata al codice ma se non sbaglio non è possibile fare un confronto fra stringhe con > o <...

prova a cambiare quello, se ancora non funziona dimmelo che lo guardo + attentamente ;)
aaa
05/08/06 11:18
thedifferent
Il programma che ho postato sopra è quello funzionante in quanto aggiunge,elimina e stampa libri provvisti di solo titolo.
Ora posto il programma in cui ho tentato di fargli aggiungere,oltre al titolo, anche autore,casa editrice,prezzo,ecc.
Ecco cosa sono riuscito a fare
uses crt;

type
stringacorta=string[30];
stringalunga=string[50];
dati=string;
pnodi=^nodi;
nodi = record
         titolo:stringalunga;
         autore:stringacorta;
         ediz:real;
         cased:stringacorta;
         prezzo:real;
         pros:pnodi
        end;
var testo:nodi;
    biblioteca:array[1..100] of nodi;
    primo:pnodi;
    scelta:char;
    finito:boolean;
procedure init;
begin
   primo:=nil;
end;
function listavuota:boolean;
begin
   listavuota:=primo=nil
end;
procedure creanuovotit(var punt:pnodi;info:stringalunga);
begin
   new(punt);
   punt^.titolo:=info;
   punt^.pros:=nil
end;

procedure creanuovoaut(var punt:pnodi;info:stringacorta);
begin
   new(punt);
   punt^.autore:=info;
   punt^.pros:=nil
end;

procedure creanuovoediz(var punt:pnodi;info:real);
begin
   new(punt);
   punt^.ediz:=info;
   punt^.pros:=nil
end;


procedure creanuovocased(var punt:pnodi;info:stringalunga);
begin
   new(punt);
   punt^.cased:=info;
   punt^.pros:=nil
end;


procedure creanuovoprez(var punt:pnodi;info:real);
begin
   new(punt);
   punt^.prezzo:=info;
   punt^.pros:=nil
end;

procedure inserisci;
var nuovo,temp,precedente:pnodi;
    datoinput,datoinput2:stringalunga;
    datoinput3,datoinput5:real;
    datoinput4:stringacorta;
begin
    write('Inserisci il titolo del libro: ');
    readln(datoinput2);
    creanuovotit(nuovo,datoinput2);
    if listavuota then
      primo:=nuovo
    else
    if primo^.titolo>nuovo^.titolo then
      begin
         temp:=primo;
         primo:=nuovo;
         primo^.pros:=temp
      end
    else
      begin
         temp:=primo;
         while (temp<>nil)and(nuovo^.titolo>temp^.titolo) do
          begin
             precedente:=temp;
             temp:=temp^.pros
          end;
         nuovo^.pros := temp;
         precedente^.pros:=nuovo
      end;
    write('Inserisci l''autore del libro: ');
    readln(datoinput);
    creanuovoaut(nuovo,datoinput);
    if listavuota then
      primo:=nuovo
    else
    if primo^.autore>nuovo^.autore then
      begin
         temp:=primo;
         primo:=nuovo;
         primo^.pros:=temp
      end
    else
      begin
         temp:=primo;
         while (temp<>nil)and(nuovo^.autore>temp^.autore) do
          begin
             precedente:=temp;
             temp:=temp^.pros
          end;
         nuovo^.pros := temp;
         precedente^.pros:=nuovo
      end;
    write('Inserisci l''edizione del libro: ');
    readln(datoinput3);
    creanuovoediz(nuovo,datoinput3);
    if listavuota then
      primo:=nuovo
    else
    if primo^.ediz>nuovo^.ediz then
      begin
         temp:=primo;
         primo:=nuovo;
         primo^.pros:=temp
      end
    else
      begin
         temp:=primo;
         while (temp<>nil)and(nuovo^.ediz>temp^.ediz) do
          begin
             precedente:=temp;
             temp:=temp^.pros
          end;
         nuovo^.pros := temp;
         precedente^.pros:=nuovo
      end;
    write('Inserisci la casa editrice del libro: ');
    readln(datoinput4);
    creanuovocased(nuovo,datoinput4);
    if listavuota then
      primo:=nuovo
    else
    if primo^.cased>nuovo^.cased then
      begin
         temp:=primo;
         primo:=nuovo;
         primo^.pros:=temp
      end
    else
      begin
         temp:=primo;
         while (temp<>nil)and(nuovo^.cased>temp^.cased) do
          begin
             precedente:=temp;
             temp:=temp^.pros
          end;
         nuovo^.pros := temp;
         precedente^.pros:=nuovo
      end;
    write('Inserisci il prezzo del libro: ');
    readln(datoinput5);
    creanuovoprez(nuovo,datoinput5);
    if listavuota then
      primo:=nuovo
    else
    if primo^.prezzo>nuovo^.prezzo then
      begin
         temp:=primo;
         primo:=nuovo;
         primo^.pros:=temp
      end
    else
      begin
         temp:=primo;
         while (temp<>nil)and(nuovo^.prezzo>temp^.prezzo) do
          begin
             precedente:=temp;
             temp:=temp^.pros
          end;
         nuovo^.pros := temp;
         precedente^.pros:=nuovo
      end;
end;
procedure elimina;
var temp,precedente:pnodi;
    datoinput:stringalunga;
    datoinput2,datoinput4:stringacorta;
    datoinput3,datoinput5:real;
    trovato:boolean;
begin
    write('Dato da cancellare');
    readln(datoinput);
    if listavuota then
       writeln('La lista Š vuota')
    else
        begin
             trovato:=false;
             precedente:=nil;
             temp:=primo;
             if primo^.titolo=datoinput then
                begin
                     temp:=temp^.pros;
                     dispose(temp);
                     trovato:=true
                end
             else
                 while(not trovato)and(temp<>nil)do
                    if temp^.titolo=datoinput then
                      begin
                           trovato:=true;
                           precedente^.pros:=temp^.pros;
                           dispose(temp)
                      end
                    else
                      begin
                           precedente:=temp;
                           temp:=temp^.pros
                      end;
             if not trovato then
                begin
                     writeln('Il dato richiesto non esiste');
                     readln
                end;
    end;
end;

procedure stampa;
var temp:pnodi;
begin
     clrscr;
     writeln;
     writeln('²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²');
     writeln;
     writeln('-- ELENCO COMPLETO --');
     writeln;
     temp:=primo;
     while temp<>nil do
       begin
          writeln('TITOLO: ',temp^.titolo);
          temp:=temp^.pros;
          writeln('AUTORE: ',temp^.autore);
          temp:=temp^.pros;
          writeln('EDIZIONE: ',temp^.ediz);
          temp:=temp^.pros;
          writeln('CASA EDITRICE: ',temp^.cased);
          temp:=temp^.pros;
          writeln('PREZZO: ',temp^.prezzo);
          temp:=temp^.pros;
       writeln('-------------------------.')
       end;

     writeln('²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²');
     readln
end;


begin
     init;
     finito:=false;
     repeat
           clrscr;
           gotoxy(1,50);
           textbackground(blue);
           textcolor(yellow);write(' Premi 0 ');
           textcolor(white);write('per uscire');
           gotoxy(1,1);
           writeln;
           writeln;
           writeln;
           writeln;
           writeln('               ----------BENVENUTO NELL''ARCHIVIO LIBRI----------');
           writeln;
           writeln('               1: Inserisci un libro');
           writeln('               2: Cancella un libro');
           writeln('               3: Visualizza la lista completa dei libri');
           writeln;
           repeat
                 write('Scelta: ');
                 readln(scelta)
           until(scelta>='0')and(scelta<='3');
           case scelta of
               '0':finito:=true;
               '1':inserisci;
               '2':elimina;
               '3':stampa;
           end;
     until finito
end.

Con turbo pascal il programma viene eseguito ma ,quando immetto più di 1 libro,le caratteristiche si mischiano(es. al posto del titolo il prezzo e così via).
Con ipcute,invece, me lo esegue fino all'immissione del titolo e,quando inserisco l'autore, mi da un errore alla riga(senza virgolette):

"while (temp<>nil)and(nuovo^.autore>temp^.autore) do"
(per la precisione tra "temp^." e la parola "autore"

l'errore è:referencement not allowed on a pointer outside the heap

Grazie in anticipo:k:
aaa