Forum - Pascal
- Distanza date
Pagine: [ 1 2 ]
|
Petenausen (Normal User)
Newbie
Messaggi: 11
Iscritto: 26/04/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
program dat;
var
data, s: string ;
anno: word ;
giorno, mese: byte ;
new_data, c: string ;
n, f, l, i: byte ;
begin
writeln ( 'inserisci una data' ) ;
readln ( data) ;
for f: = 1 to 3 do
begin
i: = pos ( '/' , data) ;
c: = copy ( data, 1, i- 1) ;
val ( c, n) ;
delete ( data, 1, i) ;
if ( f= 1) then
giorno: = n;
if ( f= 2) then
mese: = n;
if ( f= 3) then
anno: = n;
readln ;
end ;
end .
Ultima modifica effettuata da Phi il 10/05/2011 alle 16:27
HeDo (Founder Member )
Guru^2
Messaggi: 2765
Iscritto: 21/09/2007
aspetta e spera
Petenausen (Normal User)
Newbie
Messaggi: 11
Iscritto: 26/04/2011
cioé??
()
Newbie
Messaggi:
Iscritto:
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!)
Petenausen (Normal User)
Newbie
Messaggi: 11
Iscritto: 26/04/2011
ok ragazzi l'ho fatto
Codice sorgente - presumibilmente Pascal
program pre_ver;
var st_data, st_dist, st1, st1dist, st2dist, st2, c: string ;
giorni, giornidist, mesi, mesidist: byte ;
anni, totale, totaledist, tot, annidist: word ;
posiz, posizione, posdist, posizionedist: byte ;
begin
writeln ( 'inserisci la data di oggi gg/mm/aaaa' ) ;
readln ( st_data) ;
writeln ( 'inserisci la data rispetto a cui vuoi calcolare la distanza' ) ;
readln ( st_dist) ;
posiz:= pos( '/' , st_data) ;
posdist:= pos( '/' , st_dist) ;
//writeln(posiz);
st1:= copy( st_data, 1, posiz- 1) ;
st1dist:= copy( st_dist, 1, posdist- 1) ;
//writeln (st1);
//readln;
val ( st1, giorni) ;
val ( st1dist, giornidist) ;
//writeln (giorni); //trovato i giorni
delete ( st_data, 1, posiz) ;
delete ( st_dist, 1, posdist) ;
posizione:= pos( '/' , st_data) ;
posizionedist:= pos( '/' , st_dist) ;
//writeln (st_data);
//readln;
st2:= copy( st_data, 1, posizione- 1) ;
st2dist:= copy( st_dist, 1, posizionedist- 1) ;
delete ( st_data, 1, posizione) ;
delete ( st_dist, 1, posizionedist) ;
val( st_data, anni) ;
val( st_dist, annidist) ;
//writeln (st2);
//readln;
val ( st2, mesi) ;//trovato che mese Š stato digitato
val ( st2dist, mesidist) ;
//ora cerchiamo di calcolare i giorni totali che sono passati dal 1 gennaio :) ù
if mesi = 1 then totale:= giorni+ ( 31* mesi) + ( 365* anni) ;
if mesi = 2 then totale:= giorni+ 31+ 28+ ( 365* anni) ;
if mesi = 3 then totale:= giorni+ ( 31* 2) + 28+ ( 365* anni) ;
if mesi = 4 then totale:= giorni+ ( 31* 2) + 28+ 30+ ( 365* anni) ;
if mesi = 5 then totale:= giorni+ ( 31* 3) + 28+ 30+ ( 365* anni) ;
if mesi = 6 then totale:= giorni+ ( 31* 3) + ( 30* 2) + 28+ ( 365* anni) ;
if mesi = 7 then totale:= giorni+ ( 31* 4) + ( 30* 2) + 28+ ( 365* anni) ;
if mesi = 8 then totale:= giorni+ ( 31* 5) + ( 30* 2) + 28+ ( 365* anni) ;
if mesi = 9 then totale:= giorni+ ( 31* 5) + ( 30* 3) + 28+ ( 365* anni) ;
if mesi = 10 then totale:= giorni+ ( 31* 6) + ( 30* 3) + 28+ ( 365* anni) ;
if mesi = 11 then totale:= giorni+ ( 31* 6) + ( 30* 4) + 28+ ( 365* anni) ;
if mesi = 12 then totale:= giorni+ ( 31* 7) + ( 30* 4) + 28+ ( 365* anni) ;
if mesidist = 1 then totaledist:= giorni+ ( 31* mesi) + ( 365* annidist) ;
if mesidist = 2 then totaledist:= giorni+ 31+ 28+ ( 365* annidist) ;
if mesidist = 3 then totaledist:= giorni+ ( 31* 2) + 28+ ( 365* annidist) ;
if mesidist = 4 then totaledist:= giorni+ ( 31* 2) + 28+ 30+ ( 365* annidist) ;
if mesidist = 5 then totaledist:= giorni+ ( 31* 3) + 28+ 30+ ( 365* annidist) ;
if mesidist = 6 then totaledist:= giorni+ ( 31* 3) + ( 30* 2) + 28+ ( 365* annidist) ;
if mesidist = 7 then totaledist:= giorni+ ( 31* 4) + ( 30* 2) + 28+ ( 365* annidist) ;
if mesidist = 8 then totaledist:= giorni+ ( 31* 5) + ( 30* 2) + 28+ ( 365* annidist) ;
if mesidist = 9 then totaledist:= giorni+ ( 31* 5) + ( 30* 3) + 28+ ( 365* annidist) ;
if mesidist = 10 then totaledist:= giorni+ ( 31* 6) + ( 30* 3) + 28+ ( 365* annidist) ;
if mesidist = 11 then totaledist:= giorni+ ( 31* 6) + ( 30* 4) + 28+ ( 365* annidist) ;
if mesidist = 12 then totaledist:= giorni+ ( 31* 7) + ( 30* 4) + 28+ ( 365* annidist) ;
tot:= totale- totaledist;
writeln ( 'la distanza ' , tot) ;
readln ;
end .
come potrei renderlo più semplice??
Ultima modifica effettuata da Phi il 10/05/2011 alle 19:12
Phi (Member )
Expert
Messaggi: 241
Iscritto: 30/12/2009
In primo luogo quando crei posto ricordati di inserire il codice del programma così:
Codice sorgente - presumibilmente Plain Text
[ code ]
..
Programma
..
[ /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
giornimesi : array [ 1..12] of byte = ( 31, 28, 31, 30, 31, 30, 31, .... ) ;
ed poi usare un ciclo
for :
Codice sorgente - presumibilmente Pascal
for i := 1 to mesi do totale := totale + giornimesi[ i] ;
E ti conviene scrivere
Codice sorgente - presumibilmente Plain Text
tot:=abs(totale-totaledist);
Usando word(0..65536) conviene non avere numeri negativi !
Così prenderà il valore assoluto.
Ultima modifica effettuata da Phi il 10/05/2011 alle 19:48
Goblin (Member )
Expert
Messaggi: 375
Iscritto: 02/02/2011
Forse mi sono perso qualche pezzo ... cosa c'e' che non va utilizzando il tipo TDateTime ??
Codice sorgente - presumibilmente Delphi
program calcdate;
uses sysutils;
Var dDataInizio, dDataFine: TDateTime ;
nTotal: Integer ;
sTemp: String ;
begin
Writeln ( 'Data inizio gg/mm/yyyy' ) ; // 15/10/1970
readln ( stemp) ;
dDataInizio : = StrTodate ( sTemp) ;
Writeln ( 'Data fine gg/mm/yyyy' ) ; // 10/10/2011
readln ( stemp) ;
dDataFine : = StrTodate ( sTemp) ;
nTotal : = Trunc ( dDataFine - dDataInizio) ;
writeln ( 'distanza in giorni ' , nTotal) ;
readln ( stemp) ;
end .
PS: scritto al volo non testato, ma dovrebbe funzionare
G.
Ibis redibis non morieris in bello
Phi (Member )
Expert
Messaggi: 241
Iscritto: 30/12/2009
Non sapendo di questa possibilità avrei fatto così:
Codice sorgente - presumibilmente Delphi
program calcologiornidata;
var
st : string ;
t1, t2: longword ;
const giornimese : array [ 1.. 12] of byte = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) ;
function numdata( s: string ) : longword ;
var
i, p, nn, tot: longwors;
ns : string ;
procedure trovann;
begin
p : = pos ( '/' , s) ;
ns: = copy ( s, 1, p- 1) ;
val ( ns, nn) ;
delete ( s, 1, p) ;
end ;
begin
numdata: = 0 ;
if s= '' then exit ;
trovann;
tot: = nn;
if s= '' then exit ;
trovann;
if ( s= '' ) or ( nn>12) or ( nn<1) then exit ;
if ( tot<0) or ( tot>giornimese[ nn] ) then exit ;
for i : = 1 to nn do tot: = tot+ giornimese[ i] ;
val ( s, nn) ;
numdata: = tot+ 365* nn;
end ;
Begin
writeln ( 'inserisci la data di oggi gg/mm/aaaa' ) ;
repeat
readln ( st) ;
t1: = numdata( st) ;
until t1<>0 ;
writeln ( 'inserisci la data rispetto a cui vuoi calcolare la distanza' ) ;
repeat
readln ( st) ;
t2: = numdata( st) ;
until t1<>0 ;
writeln ( 'la distanza e' '' , abs ( t1- t2) , ' giorni' ) ;
readln ;
End .
Ultima modifica effettuata da Phi il 10/05/2011 alle 21:13
Goblin (Member )
Expert
Messaggi: 375
Iscritto: 02/02/2011
non vorrei fare il pignolo ma gli anni bisestili ???
Ibis redibis non morieris in bello