Tuesday, March 22, 2011

Structures and Unions

Modify the input function used in previous program such that it reads a value that represents the date in the form of a long integer, like 19450815 for the date 15-8-1945 (August 15, 1945) and assigns suitable values to the members day, month and year. Use suitable algorithm to convert the long integer 19450815 into year, month and day.

#include <stdio.h>
struct date
{
int day;
int month;
int year;
}dat;
long int input();
void validate(struct date dat);
void print(struct date dat);
void main()
{
long int x,rem;
clrscr();
printf("Enter the date in the format of 19910815 for 1991 August 15:");
x=input();
dat.year=x/10000;
rem=x%10000;
dat.month=rem/100;
dat.day=rem%100;
validate(dat);
getch();
}
long int input()
{
long int a;
scanf("%ld",&a);
return(a);
}
void validate(struct date dat)
{
if(dat.day>31||dat.day<1)
printf("The day should 1 to 31!");
else
if(dat.month>12||dat.month<1)
printf("Month should be 1 to 12 only!");
else
if(dat.month==2)
{
if(dat.day==29)
{
if(dat.year%4!=0)
printf("%d is not a leap year",dat.year);
else
print(dat);
}
else
if(dat.day>29)
printf("February has only 28 or 29 days");
else
print(dat);
 }
 else
if(dat.month==4)
{
if(dat.day>30)
printf("April has only 30 days");
 }
 else
if(dat.month==6)
{
if(dat.day>30)
printf("June has only 30 days");
 }
 else
if(dat.month==9)
{
if(dat.day>30)
printf("September has only 30 days");
}
else
if(dat.month==11)
{
if(dat.day>30)
printf("November has only 30 days");
}
else
print(dat);
}
void print(struct date dat)
{
switch(dat.month)
{
case 1:printf("January %d, %d",dat.day,dat.year);
break;
case 2:printf("February %d, %d",dat.day,dat.year);
break;
case 3:printf("March %d, %d",dat.day,dat.year);
break;
case 4:printf("April %d, %d",dat.day,dat.year);
break;
case 5:printf("May %d, %d",dat.day,dat.year);
break;
case 6:printf("June %d, %d",dat.day,dat.year);
break;
case 7:printf("July %d, %d",dat.day,dat.year);
break;
case 8:printf("August %d, %d",dat.day,dat.year);
break;
case 9:printf("September %d, %d",dat.day,dat.year);
break;
case 10:printf("October %d, %d",dat.day,dat.year);
break;
case 11:printf("November %d, %d",dat.day,dat.year);
break;
case 12:printf("December %d, %d",dat.day,dat.year);
break;
}
}

OUTPUT
Enter the date in ther format of 19910815 for 1991 August 15: 20100520
May 20, 2010

No comments:

Post a Comment