Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
counting days/months/years Posted by buddingone on 24 Jun 2009 at 12:55 PM
hi guys,

I am trying to write a program that accpets any number and then counts years, months and days. The problem is of course with implementing leap years (February 28/29) and months (30/31). What I have managed to conjured up so far is quite simple and does not take into consideration the above-mentioned issues.
I was thinking of declaring months as month:array[1..12] of integer then assigning each month correct numbers of days. However, I don't know either whether I am going the right way or how to implement it.
I am just about to finish my BTEC course and programming is one of the units we have coverd. Quite enjoyed it and I would like to think that I could sort of try and learn more solving different problems (sometimes with your help)

You are more than welcome to throw some tasks at me that you think when solved will teach me some precious things about pascal!
I would really appreciate any tutorials!

This is not a homework or any assignment!


Program Pas3;
Uses crt;

var

year,month,week,day,hour,minute,second,number:longint;

BEGIN
clrscr;
       write('Enter the number of days: ');
       readln(number);

       {years}
       year:=number div 365;
       number:=number mod 365;

       {month}
       month:=number div 31;
       number:=number mod 31;

       {weeks}
       week:=number div 7;
       number:=number mod 7 ;

       {days}
       day:=number div 1;
       number:= number mod 1;

       {screen display}
       writeln(year,' years');
       writeln(month,' months');
       writeln(week,' weeks');
       writeln(day,' days');


readln;
END.


any help much appreciated!


Report
Re: counting days/months/years Posted by Actor on 24 Jun 2009 at 9:13 PM
Here's a program that I posted last year. It takes a person's birthday and today's date as input, then computes the person's age in days. That's the opposite of what you are trying to do, however, you may find some of the functions and procedures useful. Sorry I can't do more right now but it's late and I need to get to bed.
Program Days ;

      Function Julian (Year: Word ; Month, Day : Byte) : LongInt ;

            Function IsLeap (Year : LongInt) : Boolean ;
            {
               a year is a leap year IF it's divisible by 4
               UNLESS it's also divisible by 100, then it's
               a leap year ONLY if it divisible by 400
            }
            begin
                 if Year DIV 4 = 0 then begin
                     if Year DIV 100 = 0 then begin
                         if Year DIV 400 = 0 then
                             IsLeap := TRUE
                         else
                             IsLeap := FALSE
                     end
                     else
                         IsLeap := TRUE
               end
               else
                   IsLeap := FALSE
            end ;

            Function DaysPriorToThisYear (Year : LongInt) : LongInt ;
              
            Var
                  i, Count : LongInt ;

            begin
                  Count := 0 ;

                  for i := 1 to Year - 1 do
                     if IsLeap(i) then
                        Count := Count + 366
                     else
                        Count := Count + 365 ;

                  DaysPriorToThisYear := Count
            end ;

            Function DaysPriorToThisMonth (Year, Month : LongInt) : LongInt ;
               
            CONST
               MonthDays : Array [FALSE .. TRUE, 1 .. 12] of LongInt
                         = ((31,28,31,30,31,30,31,31,30,31,30,31),
                            (31,29,31,30,31,30,31,31,30,31,30,31)) ;

            Var
               i, Count : LongInt ;

            begin
               Count := 0 ;

               for i := 1 to Month - 1 do
                  if IsLeap(Year) then
                     Count := Count + MonthDays[TRUE, i]
                  else
                     Count := Count + MonthDays[FALSE, i]  ;

               DaysPriorToThisMonth := Count
            end ;
             
      begin
         Julian := DaysPriorToThisYear(Year) 
                 + DaysPriorToThisMonth(Year,Month) 
                 + Day
      end ;

Var
   Year0, Year1, Month0, Month1, Day0, Day1, BirthDay, Today : LongInt ;

begin
   Write ('Enter year of birth:   ') ;  ReadLn (Year0) ;
   Write ('Enter month of birth:  ') ;  ReadLn (Month0) ;
   Write ('Enter day of birth:    ') ;  ReadLn (Day0) ;
   WriteLn ;

   Write ('Enter today''s year:    ') ;  ReadLn (Year1) ;
   Write ('Enter today''s month:   ') ;  ReadLn (Month1) ;
   Write ('Enter today''s day:     ') ;  ReadLn (Day1) ;

   BirthDay := Julian (Year0, Month0, Day0) ;
   Today    := Julian (Year1, Month1, Day1) ; 

   WriteLn (Today   :10) ;
   WriteLn (Birthday:10) ;
    
   WriteLn ('The person is ', Today - BirthDay, ' days old.')
end.

Report
Re: counting days/months/years Posted by Atex on 24 Jun 2009 at 10:33 PM
: I am trying to write a program that accpets any number and then
: counts years, months and days. The problem is of course with
: implementing leap years (February 28/29) and months (30/31). What I
: have managed to conjured up so far is quite simple and does not take
: into consideration the above-mentioned issues.

I didn't fully tested the following code, but in any case should give you an idea how to do it...
program pas3;

const days_in_year:array[false..true] of word=(365,366);
      days_in_month:array[1..12] of byte=(31,28,31,30,31,30,31,31,30,31,30,31);

var number,years,months:longint;

function is_leap_year(y:word):boolean;
 begin
  is_leap_year:=(y mod 4=0) and not((y mod 100=0) and (y mod 400<>0));
 end;

begin
 write('Enter the number of days: ');readln(number);

 years:=0;
 while number>=days_in_year[is_leap_year(years+1)] do begin
  dec(number,days_in_year[is_leap_year(years+1)]);
  inc(years);
 end;

 months:=0;
 if is_leap_year(succ(years)) then inc(days_in_month[2]);
 while number>=days_in_month[months+1] do begin
  inc(months);
  dec(number,days_in_month[months]);
 end;

 writeln(years,' Years');writeln(months,' Months');writeln(number,' Days');
 readln;
end.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.