-
Notifications
You must be signed in to change notification settings - Fork 0
/
Activity manipulate structures with functions
90 lines (79 loc) · 2.91 KB
/
Activity manipulate structures with functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* In this problem you will continue developing the data feature which you started implementing in the previous problem. You will implement a "tomorrow" feature in the C programming language via a function called "advanceDay()". The function advanceDay() should take as input a date (stored in a struct date) and return the date of the following day. You do not have to take into account leap years (although you may if you wish to). That is, it is okay for your function to always return March 1 as the day following February 28, no matter the year.
You are provided with a familiar date structure definition, a main function as well as the function prototypes for the readDate(), printDate(), and advanceDate() functions. Do not modify any of the given code. Simply add your function definitions underneath the main() function. For the readDate() and printDate() functions you may simply copy and paste the code you developed in the previous task.
#include <stdio.h>
struct date {
int year;
int month;
int day;
};
// function prototypes
void printDate(struct date);
void readDate(struct date *);
struct date advanceDay(struct date);
int main(void) {
struct date today, tomorrow;
readDate(&today);
printDate(today);
tomorrow = advanceDay(today);
printDate(tomorrow);
return 0;
}
*/
#include <stdio.h>
struct date {
int year;
int month;
int day;
};
/* function prototypes */
void printDate(struct date);
void readDate(struct date *);
struct date advanceDay(struct date);
int main(void) {
struct date today, tomorrow;
readDate(&today);
printDate(today);
tomorrow = advanceDay(today);
printDate(tomorrow);
return 0;
}
void readDate(struct date *todptr){
scanf("%d %d %d", &(*todptr).year,&(*todptr).month, &(*todptr).day);
}
void printDate(struct date tod){
printf("%02d/%02d/%4d\n", tod.month, tod.day, tod.year);
}
struct date advanceDay(struct date tomptr){
if(tomptr.month == 1 || tomptr.month == 3 ||tomptr.month == 5 ||tomptr.month == 7 ||tomptr.month == 8 ||tomptr.month == 10){
if(tomptr.day<31){
tomptr.day = tomptr.day+1;
}else {
tomptr.day = 1;
tomptr.month = tomptr.month + 1;
}
}else if(tomptr.month == 4 || tomptr.month == 6 ||tomptr.month == 9 || tomptr.month == 11){
if(tomptr.day<30){
tomptr.day = tomptr.day+1;
}else {
tomptr.day = 1;
tomptr.month = tomptr.month + 1;
}
}else if(tomptr.month == 2){
if(tomptr.day<28){
tomptr.day = tomptr.day + 1;
}else {
tomptr.day = 1;
tomptr.month = tomptr.month + 1;
}
}else if(tomptr.month == 12){
if(tomptr.day<31){
tomptr.day = tomptr.day + 1;
}else {
tomptr.day = 1;
tomptr.month = 1;
tomptr.year = tomptr.year + 1;
}
}else{
}
return(tomptr);
}