Tuesday, March 22, 2011

Structure and Unions

Design a function update that accepts the date structure designed previously to increment the date by one day and return the new date. The following rules are applicable:
  • If the date is the last day in a month, month should  be incremented
  • If it is the last day in December, the year should be incremented
  • There are 29 days in February of a leap year
#include <stdio.h>
struct date
{
int day;
int month;
int year;
}dat;
void update(struct date dat);
void main()
{
clrscr();
printf("Enter day:");
scanf("%d",&dat.day);
printf("Enter month:");
scanf("%d",&dat.month);
printf("Enter year:");
scanf("%d",&dat.year);
update(dat);
getch();
}
void update(struct date dat)
{
dat.day=dat.day+1;
if(dat.day>32)
{
printf("Wrong input");
goto end;
}
else
if(dat.month==4||dat.month==6||dat.month==9||dat.month==11)
{
if(dat.day==31)
{
dat.day=1;
dat.month=dat.month+1;
}
if(dat.day>31)
{
printf("This month has only 30 days",dat.month);
goto end;
}
}
if(dat.month==1||dat.month==3||dat.month==5||dat.month==7||dat.month==8||dat.month==10||dat.month==12)
if(dat.day==32)
{
dat.day=1;
dat.month=dat.month+1;
}
if(dat.month==13)
{
dat.day=1;
dat.month=1;
dat.year=dat.year+1;
}
if(dat.month==2)
{
if(dat.day>30)
{
printf("This month has 28 or 29 days");
goto end;
}
if(dat.day==29)
{
dat.day=1;
dat.month=dat.month+1;
}
}
printf("Updated date: %d/%d/%d",dat.day,dat.month,dat.year);
end:
}

OUTPUT
Enter day: 31
Enter month: 12
Enter year: 2010
Updated date: 1/1/2011

Enter day: 28
Enter month: 2
Enter year: 2002
Updated date:  1/3/2002

No comments:

Post a Comment