Tuesday, March 22, 2011

Structures and Unions

Add a function called nextdate to the program designed previously to perform the follwing task;
  • Accepts two arguments, one of the structure data containing the present data and the second and integer that represents the number of days to be added to the present date.
  • Adds the days to the present date and returns the structure containing the next date correctly
Note that the next date may be in the next month or even the next year.

#include <stdio.h>
struct date
{
int day;
int month;
int year;
}dat;
void nextdate(struct date dat, int x);
void main()
{
int y;
clrscr();
printf("Enter day:");
scanf("%d",&dat.day);
printf("Enter month:");
scanf("%d",&dat.month);
printf("Enter year:");
scanf("%d",&dat.year);
printf("Enter days to add:");
scanf("%d",&y);
nextdate(dat,y);
getch();
}
/***********nextdate function****************/
void nextdate(struct date dat, int x)
{
dat.day=dat.day+x;
if(dat.month==2)
{
if(dat.year%4==0)
{
dat.day=dat.day-29;
dat.month++;
}
if(dat.year%4!=0)
{
dat.day=dat.day-28;
dat.month++;
}
}
if(dat.month==1||dat.month==3||dat.month==5||dat.month==7
||dat.month==8||dat.month==10||dat.month==12)
while(dat.day>31)
{
dat.day=dat.day-31;
dat.month=dat.month+1;
}
if(dat.month==4||dat.month==6||dat.month==9||dat.month==11)
while(dat.day>30)
{
dat.day=dat.day-30;
dat.month=dat.month+1;
}
if(dat.month>12)
while(dat.month>12)
{
dat.month=dat.month-12;
dat.year=dat.year+1;
}
printf("Updated date: %d/%d/%d",dat.day,dat.month,dat.year);
end:
}

OUTPUT
Enter day: 25
Enter month: 12
Enter year: 2010
Enter days to add: 10
Updated date: 4/1/2011

Enter day: 25
Enter month: 5
Enter year: 2011
Enter days to add: 10
Updated date: 4/6/2011

No comments:

Post a Comment