Προς το περιεχόμενο

eyresh algorithmoy..


dekra

Προτεινόμενες αναρτήσεις

Δημοσ.

Re paidia mipws borei kaneis na voh8isei..?

 

8elw na vrw ena tropo na ypologizw thn diafora se meres metaxy 2 hmeromhniwn. (den einai anagkh na lamvanei ypopsin ta disekta eth).

 

apla ftiaxnw ena programma se C kai exw kolisei sto pws (algorithmika) na to ypologizw.

 

thx ek twn protairwn.

Δημοσ.

an katalava kalaa einai aplo...

toulaxiston an to kaneis se xarti einai aplo

 

afaireis apo tin pio megali imerominia tin pio palia xekinontas apo tis meres,meta tous mines kai meta ta xronia.kaneis kanoniki afairesi dld exeis kai kratoumena ktl.

Δημοσ.

Gia na ypologiseis thn diafora meta3y dyo opoiondhpote pragmatwn, o pio efkolos tropos einai na ta fereis sthn pio xamhlh monada pou mporeis. Etsi gia thn hmeromhnia kalh idea einai na ypologiseis p.x. meres, afou 8eleis na vreis th diafora se meres.

Estw loipon oti h hmeromhnia sou pou dhlwnei thn hmera 0 einai h 1/1/2000 (h h 1/1/1970 pou legetai kai Unix Epoch kai xrhsimopoieitai evrews sto UNIX kai oxi mono).

8eleis loipon na vreis poses meres apexei h hmeromhnia 5/7/2003 apo thn hmeromhnia 2/10/2001.

Pairneis loipon tis hmeromhnies, afaireis ta xronia (px 2003-2000) kai ara exeis 5 meres, 7 mhnes kai 3 xronia meta to 0, dhladh 5+7*30+3*365 (peripou, opws eipes). To idio kai gia thn allh kai etsi exeis 2 times pou einai hmeres, tis afaireis kai vriskeis ton ari8mo pou 8eleis!

 

Filika

Dionisos

Δημοσ.

O tropos upologismou pou lete den einai akrivis giati den lambanetai upopsin disekta eti, oti oi mines borei na exoun 30 i 31 meres ktl.

 

Pada se tetoia problimata pou exoun na kanoun me imerominies, kalo einai o xristis na ekmetalevetai tis idi uparxouses sunartiseis pou exoun pronoisei olous tous poluplokous upologismous apo prin.

 

Stin sugekrimeni periptwsi tis C++, arkei na ekmetaleutei kaneis tin thavmatourgi time.h.

 

O kwdikas gia ton upologismo tis diaforas se meres 2 imerominiwn, einai arketa aplos kai fainetai parakatw. Ston kwdika einai fanero oti upothetw imerominies 8.3.1978 kai 8.3.2003, diladi o arithmos twn merwn pou exw zisei ews simera! :-)

 

>  
#include <stdio.h>
#include <time.h>

int main(int argc, char** argv)
{
	struct tm* startinfo;
	struct tm* endinfo;
	time_t starttime, endtime;
	double dif;

	time(&starttime);
	startinfo= localtime(&starttime);
	// 8.3.1978
	startinfo->tm_year= 1978 - 1900;
	startinfo->tm_mon= 3 - 1;
 	startinfo->tm_mday= 8;
	starttime= mktime(startinfo);
	
	time(&endtime);
	endinfo= localtime(&starttime);
	// 8.2.2003
	endinfo->tm_year= 2003 - 1900;
	endinfo->tm_mon= 2 - 1;
	endinfo->tm_mday= 8;
	endtime= mktime(endinfo);

	dif = difftime (endtime, starttime);

	
	printf ("Time difference in days: %f\n", dif/(24*3600));		
	
	return 0;
}

 

(Dustuxws bariemai na bazw sxolia se kwdika, alla elpizw oti me tin boitheia kapoiou manual tis C, tha einai katanoitos <img src="http://www.insomnia.gr/ubbthreads/images/graemlins/grin.gif" alt="" />)

Δημοσ.

to format twn hmeromhniwn 8a einai eite dd/mm/yyyy or dd-mm-yyyy.

 

Sxetika me thn lysh pou edwse o filos Dionisos einai nai men swsth alla ekei sto * 30 exei san apotelesma sto teliko noumero na xanode meres.

 

Oso gia ton wdika toy GrMikeD swstos einai alla den 8elw na xrhsimopoihsw synarthseis mesa apo thn idia th C etoimes.

 

Padws vrika ena tropo na to ypologizeis swsta. (Ennoeitai oti den lamvanoyme ypopsin ta disekta eth)

 

1. pernoume thn 1h hmeromhnia kai vriskoume poses meres apexei apo thn arxh toy xronoy pou do8hke. Meta oti vroyme se meres to afairoyme apo to 365.

 

2. vriskoyme poses meres apo thn arxh toy xronoy toy opoioy do8hke apexei h deyterh hmeromhnia.

 

3. pros8etoyme ta dyo parapanw noymera mazi

 

4. telos afairoyme ton ena xrono apo ton allo kai oti vroyme toy afairoyme 1 kai pollaplasiazoyme epi 365. To apotelesma afto to pros8etoyme me to apotelesma toy vhmatos 3 kai exoume th diafora twn hmerwmhniwn se meres.

 

afta...kai efxaristw gia ta reply sas paides. <img src="http://www.insomnia.gr/ubbthreads/images/graemlins/wink.gif" alt="" />

Δημοσ.

xmmm ok.

 

Tote estw oti exeis d1.m1.y1 kai d2.m2.y2

 

tote i diafora se imeres gia kathe mia apo tin imerominia anaforas tha einai isws (elpizw na lambanw swsta upopsin febrouario pou exei 28 meres kai avgousto pou exei 31 me ta if pou perigrafw):

 

dif1 = d1 + (ceil(m1/2)*31+(m1-ceil(m1/2))*30) + (y1*365)

if m1>2 then dif1-=2

if m1==9 then dif1+=1

if m1==11 then dif1+=1

 

dif2 = d2 + (ceil(m2/2)*31+(m2-ceil(m2/2))*30) + (y2*365)

if m2>2 then dif2-=2

if m2==9 then dif2+=1

if m2==11 then dif2+=1

 

opote dif = abs(dif1-dif2)

 

simeiwsi: o oros ceil(m/2)*31+(m-ceil(m/2) dinei pada ton arithmo twn imerwn tou mina apo tin arxi tou xronou afou ceil(m/2) einai o arithmos twn monwn arithmwn apo to 0 ews to m (diladi twn minwn me 31 meres), kai to upoloipo m2-ceil(m2/2) einai oi upoloipoi mines me tis 30 imeres.

 

 

Δημοσ.

autos o kodikas pou les file einai sostos mono an oi imerominies einai diaforetikou etous. an einai idiou etous tote einai lathos giati as poume oti i proti imeromini einai 5/1/2003 kai i deuteri 6/1/2003 tote i diafora einai isi me ena alla etsi opos ta les i proti imerominia apexei 5 meres apo tin arxi tou etous i deuteri 6 meres ara to athroisma 11. i diafora ton eton einai miden opote tha vgalei diafora 11 pou einai lathos...

Δημοσ.

an ennoeis gia to tropo poy proteina egw, to ekana se kwdika kai to exw testarei leitoyrgei kanonika. to apotelesma pou vgazei gia tis hmeromhnies pou edwses ws paradeigma einai 1.

 

ama 8es soy stelnw to .exe na dokimaseis kai monos soy.

 

ostoso mallon den katalaves afto pou eipa. Gia thn prwth hmeromhnia vriskeis poses meres apexei apo thn arxh toy xronoy kai oti noumero vreis to afaireis apo to 365 gia na deis poses meres ypoleiponte ston xrono ayto apo ayth thn hmeromhnia.

 

opote 365 - 5 = 360

 

meta symfwna me to 2o vhma pou anefera

exoume 360 + 6 = 366

 

kai telos 365 * ((2003-2003) - 1) = -365

366 + (-365) = 1

 

afta..

Δημοσ.

Vasika den katalava oti 8eleis akriveia... eipes proseggistika, opote 8ewrhsa oti to 30 mas kanei.

Mia veltiwsh sthn skepsh mou einai na exeis enan pinaka me tis hmeres twn mhnwn. Etsi anti gia ton pollaplasiasmo epi 30, exeis mia pros8esh twn hmerwn twn mhnwn pairnontas ta apo ton pinaka me ena for loop.

Epishs mporeis na laveis ypopsin sou kai ta disekta poly efkola me afton ton tropo, exontas 2 pinakes, ton ena me ton fevrouario na exei 28 meres kai ton allo na exei 29 meres kai me ena if na elegxeis an einai disekto h oxi to etos kai na ypologizeis etsi tis hmeres analoga.

 

Filika

Dionisos

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...