Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Pascal - Distanza date
Forum - Pascal - Distanza date

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
Petenausen (Normal User)
Newbie


Messaggi: 11
Iscritto: 26/04/2011

Segnala al moderatore
Postato alle 14:56
Martedì, 10/05/2011
Ragazzi mi servirebbe qualcuno che mi faccia un programma che date due date indica il numero di giorni che le separano il problema è che deve funzionare  sia se scrivo le date nel modo gg/mm/aaaa es 20/12/2010 sia che le scriva così 1/3/2010

Ho provato a fare una cosa del genere intanto per scrivermi in modo corretto i giorni, i mesi e gli anni ma non va
Codice sorgente - presumibilmente Delphi

  1. program dat;
  2. var
  3. data,s:string;
  4. anno:word;
  5. giorno,mese:byte;
  6. new_data,c:string;
  7. n,f,l,i:byte;
  8. begin
  9. writeln('inserisci una data');
  10. readln(data);
  11.  
  12.  
  13.  
  14. for f:=1 to 3 do
  15. begin
  16. i:=pos('/',data);
  17. c:=copy(data,1,i-1);
  18. val(c,n);
  19. delete(data,1,i);
  20. if (f=1) then
  21. giorno:=n;
  22. if (f=2) then
  23. mese:=n;
  24. if (f=3) then
  25. anno:=n;
  26. readln;
  27. end;
  28. end.


Ultima modifica effettuata da Phi il 10/05/2011 alle 16:27
PM Quote
Avatar
HeDo (Founder Member)
Guru^2


Messaggi: 2765
Iscritto: 21/09/2007

Segnala al moderatore
Postato alle 15:49
Martedì, 10/05/2011
aspetta e spera

PM Quote
Avatar
Petenausen (Normal User)
Newbie


Messaggi: 11
Iscritto: 26/04/2011

Segnala al moderatore
Postato alle 16:17
Martedì, 10/05/2011
cioé??

PM Quote
Avatar
()
Newbie


Messaggi:
Iscritto:

Segnala al moderatore
Postato alle 16:28
Martedì, 10/05/2011
Non puoi usare le funzioni di SysUtils?
Comunque in questo forum si può chiedere come, ma non aspettarti
di ricevere direttamente il programma.
Buona fortuna se non puoi :) (Non che sia difficile, ma è lungo se non usi la libreria!)

PM Quote
Avatar
Petenausen (Normal User)
Newbie


Messaggi: 11
Iscritto: 26/04/2011

Segnala al moderatore
Postato alle 17:54
Martedì, 10/05/2011
ok ragazzi l'ho fatto
Codice sorgente - presumibilmente Pascal

  1. program pre_ver;
  2. var st_data,st_dist, st1,st1dist,st2dist, st2,c:string;
  3. giorni,giornidist, mesi,mesidist: byte;
  4. anni, totale,totaledist,tot,annidist: word;
  5. posiz,posizione,posdist,posizionedist:byte;
  6.  
  7.  
  8. begin
  9. writeln ('inserisci la data di oggi gg/mm/aaaa');
  10. readln (st_data);
  11. writeln('inserisci la data rispetto a cui vuoi calcolare la distanza');
  12. readln(st_dist);
  13. posiz:=pos('/',st_data);
  14. posdist:=pos('/',st_dist);
  15. //writeln(posiz);
  16. st1:=copy(st_data,1, posiz-1);
  17. st1dist:=copy(st_dist,1,posdist-1);
  18. //writeln (st1);
  19. //readln;
  20.  
  21. val (st1, giorni);
  22. val (st1dist,giornidist);
  23. //writeln (giorni); //trovato i giorni
  24. delete (st_data, 1, posiz);
  25. delete (st_dist, 1,posdist);
  26. posizione:=pos('/',st_data);
  27. posizionedist:=pos('/',st_dist);
  28. //writeln (st_data);
  29. //readln;
  30. st2:=copy(st_data,1,posizione-1);
  31. st2dist:=copy(st_dist,1,posizionedist-1);
  32. delete (st_data,1,posizione);
  33. delete (st_dist,1,posizionedist);
  34.  
  35. val(st_data,anni);
  36. val(st_dist,annidist);
  37.  
  38.  
  39. //writeln (st2);
  40. //readln;
  41. val (st2, mesi);//trovato che mese Š stato digitato
  42. val (st2dist,mesidist);
  43.  
  44. //ora cerchiamo di calcolare i giorni totali che sono passati dal 1 gennaio :) ù
  45.  
  46.  
  47. if mesi =1 then totale:= giorni+(31*mesi)+(365*anni);
  48. if mesi =2 then   totale:= giorni+31+28+(365*anni);
  49. if mesi =3 then   totale:= giorni+(31*2)+28+(365*anni);
  50. if mesi =4 then   totale:= giorni+(31*2)+28+30+(365*anni);
  51. if mesi =5 then  totale:= giorni+(31*3)+28+30+(365*anni);
  52. if mesi =6 then  totale:= giorni+(31*3)+(30*2)+28+(365*anni);
  53. if mesi =7 then  totale:= giorni+(31*4)+(30*2)+28+(365*anni);
  54. if mesi =8 then  totale:= giorni+(31*5)+(30*2)+28+(365*anni);
  55. if mesi =9 then totale:= giorni+(31*5)+(30*3)+28+(365*anni);
  56. if mesi =10 then  totale:= giorni+(31*6)+(30*3)+28+(365*anni);
  57. if mesi =11 then  totale:= giorni+(31*6)+(30*4)+28+(365*anni);
  58. if mesi =12 then totale:= giorni+(31*7)+(30*4)+28+(365*anni);
  59.  
  60.  
  61.  
  62. if mesidist =1 then totaledist:= giorni+(31*mesi)+(365*annidist);
  63. if mesidist =2 then   totaledist:= giorni+31+28+(365*annidist);
  64. if mesidist =3 then   totaledist:= giorni+(31*2)+28+(365*annidist);
  65. if mesidist =4 then   totaledist:= giorni+(31*2)+28+30+(365*annidist);
  66. if mesidist =5 then  totaledist:= giorni+(31*3)+28+30+(365*annidist);
  67. if mesidist =6 then  totaledist:= giorni+(31*3)+(30*2)+28+(365*annidist);
  68. if mesidist =7 then  totaledist:= giorni+(31*4)+(30*2)+28+(365*annidist);
  69. if mesidist =8 then  totaledist:= giorni+(31*5)+(30*2)+28+(365*annidist);
  70. if mesidist =9 then totaledist:= giorni+(31*5)+(30*3)+28+(365*annidist);
  71. if mesidist =10 then  totaledist:= giorni+(31*6)+(30*3)+28+(365*annidist);
  72. if mesidist =11 then  totaledist:= giorni+(31*6)+(30*4)+28+(365*annidist);
  73. if mesidist =12 then totaledist:= giorni+(31*7)+(30*4)+28+(365*annidist);
  74.  
  75.  
  76.  
  77. tot:=totale-totaledist;
  78. writeln('la distanza ',tot);
  79. readln   ;
  80.  
  81.  
  82. end.


come potrei renderlo più semplice??

Ultima modifica effettuata da Phi il 10/05/2011 alle 19:12
PM Quote
Avatar
Phi (Member)
Expert


Messaggi: 241
Iscritto: 30/12/2009

Segnala al moderatore
Postato alle 19:29
Martedì, 10/05/2011
In primo luogo quando crei posto ricordati di inserire il codice del programma così:
Codice sorgente - presumibilmente Plain Text

  1. [ code ]
  2. ..
  3. Programma
  4. ..
  5. [ /code ]



Il tuo programma presenta qualche errorino.
Innanzitutto non puoi definire totale e togtaledist Word, così arriveranno al massimo a 65536(mi pare). Consigli di definirle Longword.

Inoltre sarebbe più semplice, a mio parere creare una funzione che dia il "totale" data una stringa come parametro.

Potresti poi aggiungere un controllo per verificare il formato delle date.

E, al posto del codice if mesi=1 then ... potresti usare un array costante:
Codice sorgente - presumibilmente Pascal

  1. giornimesi : array[1..12]of byte = (31,28,31,30,31,30,31,.... );


ed poi usare un ciclo for:
Codice sorgente - presumibilmente Pascal

  1. for i := 1 to mesi do totale := totale + giornimesi[i];



E ti conviene scrivere
Codice sorgente - presumibilmente Plain Text

  1. tot:=abs(totale-totaledist);


Usando word(0..65536) conviene non avere numeri negativi ! :nono:
Così prenderà il valore assoluto.


Ultima modifica effettuata da Phi il 10/05/2011 alle 19:48
PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 20:56
Martedì, 10/05/2011
Forse mi sono perso qualche pezzo ... cosa c'e' che non va utilizzando il tipo TDateTime ??
Codice sorgente - presumibilmente Delphi

  1. program calcdate;
  2. uses sysutils;
  3.  
  4. Var dDataInizio, dDataFine: TDateTime;
  5.     nTotal: Integer;
  6.     sTemp: String;
  7. begin
  8.   Writeln('Data inizio gg/mm/yyyy');  // 15/10/1970
  9.   readln (stemp);
  10.   dDataInizio := StrTodate(sTemp);
  11.   Writeln('Data fine gg/mm/yyyy');   // 10/10/2011
  12.   readln (stemp);
  13.   dDataFine := StrTodate(sTemp);
  14.   nTotal := Trunc(dDataFine - dDataInizio);
  15.   writeln('distanza in giorni ', nTotal);
  16.   readln(stemp);
  17. end.




PS: scritto al volo non testato, ma dovrebbe funzionare
G.


Ibis redibis non morieris in bello
PM Quote
Avatar
Phi (Member)
Expert


Messaggi: 241
Iscritto: 30/12/2009

Segnala al moderatore
Postato alle 21:08
Martedì, 10/05/2011
Non sapendo di questa possibilità avrei fatto così:

Codice sorgente - presumibilmente Delphi

  1. program calcologiornidata;
  2.  
  3. var
  4. st :string;
  5. t1, t2: longword;
  6.  
  7. const giornimese : array[1..12]of byte=(31,28,31,30,31,30,31,31,30,31,30,31);
  8.  
  9. function numdata(s:string): longword;
  10. var
  11.  i,p,nn, tot:longwors;
  12.  ns :string;
  13. procedure trovann;
  14.  begin
  15.   p := pos('/', s);
  16.   ns:=copy(s, 1, p-1);
  17.   val(ns,nn);
  18.   delete(s,1,p);
  19.  end;
  20. begin
  21. numdata:=0;
  22. if s='' then exit;
  23. trovann;
  24. tot:=nn;
  25. if s='' then exit;
  26. trovann;
  27. if (s='') or (nn>12) or (nn<1) then exit;
  28. if (tot<0)or(tot>giornimese[nn]) then exit;
  29. for i := 1 to nn do tot:=tot+giornimese[i];
  30. val(s,nn);
  31. numdata:=tot+365*nn;
  32. end;
  33.  
  34.  
  35. Begin
  36. writeln ('inserisci la data di oggi gg/mm/aaaa');
  37. repeat
  38.  readln (st);
  39.  t1:=numdata(st);
  40. until t1<>0;
  41. writeln('inserisci la data rispetto a cui vuoi calcolare la distanza');
  42. repeat
  43.  readln (st);
  44.  t2:=numdata(st);
  45. until t1<>0;
  46. writeln('la distanza e''',abs(t1-t2), ' giorni');
  47. readln;
  48. End.


Ultima modifica effettuata da Phi il 10/05/2011 alle 21:13
PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 21:33
Martedì, 10/05/2011
non vorrei fare il pignolo :blush::heehee: ma gli anni bisestili ???


Ibis redibis non morieris in bello
PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo