MSFN Forum: Problem with % in C, detecting LEAP YEAR. plz hlp - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with % in C, detecting LEAP YEAR. plz hlp Rate Topic: -----

#1 User is offline   ke3pup 

  • Newbie
  • Group: Members
  • Posts: 17
  • Joined: 07-September 04

Posted 04 October 2005 - 08:28 PM

hi everyone

can someone plz help me. as a small assigment i have to make a caldender that gets year and month and then prints out the number of days, starting day under the right day name..etc... thats all done .

BUT to detect leap year the instruction says , Its ONLY leap year if the year (eg, 1900, 1800) is divisable by 400 and if its leap year and if the month is 2 (FEB) then add 1 to month days (28 becomes 29). . i can do but here is the probelm.. how cum 1100 is a leap year when it's not divisable by 400?? the sample output shows that year 1100 has 29 days so its leap year but how is that possible? plz help guys. thanx


#2 User is offline   Viper_666 

  • Group: Members
  • Posts: 3
  • Joined: 24-August 04

Posted 04 October 2005 - 09:10 PM

did this a few years ago when I was in the first year of my Computer sciences graduate.
The variables in the code are named in Dutch but you should get no problem seeing how the thing works or even just translate the strings and variable names for your own use...
Coding isn't perfect at some places but it works 100% (as far as I can remember :P )

#include <stdio.h>
#include <conio.h>
#include <string.h>


typedef struct Datum
{
    unsigned int weekdag;       
    unsigned int dag;
    unsigned int maand;
    unsigned int jaar;
}Datum;

//maanden
enum{foo_dm1,januari,februari,maart,april,mei,juni,
     juli,augustus,september,oktober,november,december};
enum{foo_dm2,jan,feb,maa,apr,_mei,jun,jul,aug,sep,okt,nov,dec}; 

//dagen
enum{zondag,maandag,dinsdag,woensdag,donderdag,vrijdag,zaterdag};

//formaat
enum{kort,voluit_kort,voluit_lang};
 
#define isschrikkel (x) (!(x%4)&&((x%100) || !(x%400)))

int main()
{
 void code();

code();
puts("\n\n- Press ENTER to exit -");
getch();
}


void code()
{ 
 unsigned int lees_dag();
 unsigned int lees_maand();
 unsigned int lees_jaar();
 Datum lees_datum();
 
 int valse_datum(Datum datum);
 int strieq(char * string1,char * string2);
 int weekdag(Datum datum);
 unsigned int schrikkel(unsigned int jaar);
 
 void print_weekdag(unsigned int weekdag);
 void print_datum(Datum d,unsigned int formaat);
 Datum datumvandaag();
 
 
 
 Datum datum;
 
 datum=lees_datum();
 
 datum.weekdag=weekdag(datum);
 
 puts("");
 print_datum(datum,voluit_lang);

}


/*  Leest een dag in en controleert of deze wel kan bestaan
*/
unsigned int lees_dag()
{
 unsigned int dag;

 do
  {
   printf("  dag : "); scanf("%u",&dag); fflush(stdin);
   if(dag<1 || dag>31) printf("\t *** dag moet tussen 1 en 31 liggen *** \n");
  }
 while(dag<1 || dag>31);  
 
 return (dag);
}

//_________________________________________________________


/*  leest een maand in en controleert of deze wel kan bestaan
*/
unsigned int lees_maand()
{
 char string[20]="";
 unsigned int maand;
 
 do
  {   
   printf("maand : "); scanf("%s",string); fflush(stdin);
   
   if(strieq("januari",string)) maand=januari;
   else if(strieq("jan",string)) maand=jan;
   else if(strieq("01",string)) maand=1;
   else if(strieq("1",string)) maand=1;
 
   else if(strieq("februari",string)) maand=februari;
   else if(strieq("feb",string)) maand=feb;
   else if(strieq("02",string)) maand=2;
   else if(strieq("2",string)) maand=2;
 
   else if(strieq("maart",string)) maand=maart;
   else if(strieq("maa",string)) maand=maa;
   else if(strieq("03",string)) maand=3;
   else if(strieq("3",string)) maand=3;
 
   else if(strieq("april",string)) maand=april;
   else if(strieq("apr",string)) maand=apr;
   else if(strieq("04",string)) maand=4;
   else if(strieq("4",string)) maand=4;
 
   else if(strieq("mei",string)) maand=mei;
   else if(strieq("05",string)) maand=5;
   else if(strieq("5",string)) maand=5;
 
   else if(strieq("juni",string)) maand=juni;
   else if(strieq("jun",string)) maand=jun;
   else if(strieq("06",string)) maand=6;
   else if(strieq("6",string)) maand=6;
 
   else if(strieq("juli",string)) maand=juli;
   else if(strieq("jul",string)) maand=juli;
   else if(strieq("07",string)) maand=7;
   else if(strieq("7",string)) maand=7;
 
   else if(strieq("augustus",string)) maand=augustus;
   else if(strieq("aug",string)) maand=aug;
   else if(strieq("08",string)) maand=8;
   else if(strieq("8",string)) maand=8;
 
   else if(strieq("september",string)) maand=september;
   else if(strieq("sep",string)) maand=sep;
   else if(strieq("09",string)) maand=9;
   else if(strieq("9",string)) maand=9;
 
   else if(strieq("oktober",string)) maand=oktober;
   else if(strieq("okt",string)) maand=okt;
   else if(strieq("10",string)) maand=10;
 
   else if(strieq("november",string)) maand=november;
   else if(strieq("nov",string)) maand=nov;
   else if(strieq("11",string)) maand=11;
 
   else if(strieq("december",string)) maand=december;
   else if(strieq("dec",string)) maand=dec;
   else if(strieq("12",string)) maand=12;
   else maand=0;

   
   if(maand<jan || maand>dec) printf("\n\t*** maand moet tussen 1 en 12 liggen, ***"
                                     "\n\t*** voluit zijn geschreven of de 3 eerste letters *** \n\n");
  }
 while(maand<jan || maand>dec);  
 
 return (maand);
}

//_________________________________________________________


/*  leest een jaar in en controleert of deze wel kan bestaan
*/
unsigned int lees_jaar()
{
  unsigned int jaar;

 do
  {
   printf(" jaar : "); scanf("%u",&jaar); fflush(stdin);
   if(jaar<1900 || jaar>2100) printf("\t ***  jaar moet tussen 1900 en 2100 liggen *** \n");
  }
 while(jaar<1900 || jaar>2100);  
 
 return (jaar);
}

//_________________________________________________________


/*  leest een datum in en stuurt deze terug in een struct Datum
*/ 
Datum lees_datum()
{
 Datum d;
  
 d.dag=lees_dag();
 d.maand=lees_maand();
    
 while(valse_datum(d))
  {
   printf("\n\t*** de ingevoerde dag en maand zijn niet geldig : ***"
          "\n\t*** '%u' is te groot voor maand '%u' ***\n\n",d.dag,d.maand);
   d.dag=0; d.maand=0; d.dag=lees_dag(); d.maand=lees_maand();
  }
 
 d.jaar=lees_jaar();
 
 return(d);  
}

//_________________________________________________________



int valse_datum(Datum datum)
{
 int SW=0;
 
 switch(datum.maand)
  {
   case januari :   if(datum.dag>31) SW=1; break;
   case februari :  if(datum.dag>29) SW=1; break;
   case maart :     if(datum.dag>31) SW=1; break;
   case april :     if(datum.dag>30) SW=1; break;
   case mei :       if(datum.dag>31) SW=1; break;
   case juni :      if(datum.dag>30) SW=1; break;
   case juli :      if(datum.dag>31) SW=1; break;
   case augustus :  if(datum.dag>31) SW=1; break;
   case september : if(datum.dag>30) SW=1; break;
   case oktober :   if(datum.dag>31) SW=1; break;
   case november :  if(datum.dag>30) SW=1; break;
   case december :  if(datum.dag>31) SW=1; break;
  }
  
 return(SW);
}

//_________________________________________________________


unsigned int schrikkel(unsigned int jaar)
{ 
 if(jaar%4==0) return(1);
 if(jaar%400==0) return(1);
 if(jaar%100==0) return(0);
  else return(0);
}

//_________________________________________________________


int weekdag(Datum datum)
{
 int juliaanse,vieren,honderden,vierhonderden,rangorde;
 
 switch(datum.maand)
  {
   case januari :   juliaanse=datum.dag;       break;
   case februari :  juliaanse=datum.dag + 31;  break;
   case maart :     juliaanse=datum.dag + 59;  break;
   case april :     juliaanse=datum.dag + 90;  break;
   case mei :       juliaanse=datum.dag + 120; break;
   case juni :      juliaanse=datum.dag + 151; break;
   case juli :      juliaanse=datum.dag + 181; break;
   case augustus :  juliaanse=datum.dag + 212; break;
   case september : juliaanse=datum.dag + 243; break;
   case oktober :   juliaanse=datum.dag + 273; break;
   case november :  juliaanse=datum.dag + 304; break;
   case december :  juliaanse=datum.dag + 334; break;
  }
 
 if(datum.maand>februari && schrikkel(datum.jaar)) juliaanse++;
 
 vieren=(datum.jaar-1)/4;
 honderden=(datum.jaar-1)/100;
 vierhonderden=(datum.jaar-1)/400;
 
 rangorde=((datum.jaar -1) + juliaanse + vieren - honderden + vierhonderden) %7;
 
 return(rangorde);
}

//_________________________________________________________


int strieq(char * string1,char * string2)
{
 int i,lengte1=strlen(string1),lengte2=strlen(string2),lengte,SW=0; 

 strlwr(string1); strlwr(string2);


 if(lengte1==lengte2) 
  {
   lengte=lengte1; 
   for(i=0;i<=lengte;i++)
    {
     if(string1[i]==string2[i]) SW=1;
      else {SW=0; i=lengte+1;}
    }     
  }
 else SW=0;

 return (SW);
}

//_________________________________________________________


/* leest de standaard systeemtijd uit en 
   slaagt deze op in een structuur datum
*/
Datum datumvandaag()
{
 char string[9];
 unsigned int dag,maand,jaar;
 Datum d;
 
 _strdate(string);

 dag=(unsigned int)  ( ((string[3]-48)*10) + (string[4]-48) );
 maand=(unsigned int)( ((string[0]-48)*10) + (string[1]-48) );
 jaar=(unsigned int) ( ((string[6]-48)*10) + (string[7]-48) + 2000);

 d.dag=dag; 
 d.maand=maand;
 d.jaar=jaar; 

 return(d);
}

//_________________________________________________________


/*  Print een datum structuur uit
*/
void print_datum(Datum d,unsigned int formaat)
{ 
 char dagnaam[7][10]={"zondag","maandag","dinsdag","woensdag","donderdag","vrijdag","zaterdag"};
 Datum vandag=datumvandaag();

 
 switch(formaat)
  {      
   case kort : printf("%u/%u/%u",d.dag,d.maand,d.jaar); break;
   case voluit_kort : {
                       printf("%s ",dagnaam[d.weekdag]);
                       printf("%u ",d.dag);
   
                       switch(d.maand)
                        {
                         case jan : printf("jan");   break;
                         case feb : printf("feb");   break;
                         case maa : printf("maa");   break;
                         case apr : printf("apr");   break;
                         case mei : printf("mei");   break;
                         case jun : printf("jun");   break;
                         case jul : printf("jul");   break;
                         case aug : printf("aug");   break;
                         case sep : printf("sep");   break;
                         case okt : printf("okt");   break;
                         case nov : printf("nov");   break;
                         case dec : printf("dec");   break;
                        }
                       printf(" %u",d.jaar);  
                      }break;
   case voluit_lang : {
                       printf("%s ",dagnaam[d.weekdag]);
                       printf("%u ",d.dag);
  
                       switch(d.maand)
                        {
                         case januari :   printf("januari");   break;
                         case februari :  printf("februari");  break;
                         case maart :     printf("maart");     break;
                         case april :     printf("april");     break;
                         case mei :       printf("mei");       break;
                         case juni :      printf("juni");      break;
                         case juli :      printf("juli");      break;
                         case augustus :  printf("augustus");  break;
                         case september : printf("september"); break;
                         case oktober :   printf("oktober");   break;
                         case november :  printf("november");  break;
                         case december :  printf("december");  break;
                        }
                
                       printf(" %u",d.jaar);
                      } break;
  
  }

}


#3 User is offline   spazmire11 

  • MicrowinX Devoloper
  • PipPip
  • Group: Members
  • Posts: 275
  • Joined: 27-June 05

Posted 04 October 2005 - 09:27 PM

heres my simple coding of it in a visual basic function

Function isleapyear(intyear As Integer) As Boolean
'you dident have a leap year before the system was created
If intyear <= 1582 Then
Exit Function
End If
'if divisible by 4 then
If intyear Mod 4 = 0 Then
    'if divisible by 100 then
    If intyear Mod 100 = 0 Then
        'if divisible by 400 then
        If intyear Mod 400 = 0 Then
            'it is a leap year
            isleapyear = True
            Exit Function
        End If
        'it is not a leap year
        isleapyear = False
        Exit Function
    End If
        'it is a leap year
        isleapyear = True
    Exit Function
End If
'if all of these equal false then it is not a leap year.
isleapyear = False
End Function


this works with the folowing statment:

The current calendar, called the Gregorian Calendar, was introduced in the year 1582. Every year divisible by 4 was declared to be a leap year with the exception of years ending in 00. (that is, those divisible by 100) and not divisible by 400. For instance, the years 1600 and 2000 are leap years, but 1700, 1800, and 1900 are not.

i hope this helps

#4 User is offline   ke3pup 

  • Newbie
  • Group: Members
  • Posts: 17
  • Joined: 07-September 04

Posted 04 October 2005 - 09:44 PM

spazmire11, on Oct 4 2005, 09:27 PM, said:

heres my simple coding of it in a visual basic function

Function isleapyear(intyear As Integer) As Boolean
'you dident have a leap year before the system was created
If intyear <= 1582 Then
Exit Function
End If
'if divisible by 4 then
If intyear Mod 4 = 0 Then
    'if divisible by 100 then
    If intyear Mod 100 = 0 Then
        'if divisible by 400 then
        If intyear Mod 400 = 0 Then
            'it is a leap year
            isleapyear = True
            Exit Function
        End If
        'it is not a leap year
        isleapyear = False
        Exit Function
    End If
        'it is a leap year
        isleapyear = True
    Exit Function
End If
'if all of these equal false then it is not a leap year.
isleapyear = False
End Function


this works with the folowing statment:

The current calendar, called the Gregorian Calendar, was introduced in the year 1582.  Every year divisible by 4 was declared to be a leap year with the exception of years ending in 00. (that is, those divisible by 100) and not divisible by 400.  For instance, the years 1600 and 2000 are leap years, but 1700, 1800, and 1900 are not. 

i hope this helps
<{POST_SNAPBACK}>



thanx so much, that really helps. but then why 1100 is a leap year when 1100 mod 400 isnt 0? my book says 1100 is a leap year, but with this logic it wouldnt be. can u plzl clear me on this.. thanx

#5 User is offline   ke3pup 

  • Newbie
  • Group: Members
  • Posts: 17
  • Joined: 07-September 04

Posted 04 October 2005 - 10:40 PM

i also forgot to mention that by the project guidlines year 1992 is a leap year as well! for example if the user enters 2 as month and 1992 as year then the caldender would be 29 days becuase leap year add 1 to number of days in Feb..its the samecase for years: 1100 (month 2) and 2000(month 2)

im not sure what i mdoing wrong here. can anyone help.

#6 User is offline   ke3pup 

  • Newbie
  • Group: Members
  • Posts: 17
  • Joined: 07-September 04

Posted 05 October 2005 - 04:10 AM

done deal :) i fixed it all up, now works, thanx for ur help everyone

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy