1 // 第三题 2 3 #include <stdio.h> 4 5 struct time{ 6 int hour; 7 int minutes; 8 int seconds; 9 }; 10 struct time elapsed_time(struct time t1,struct time t2){ 11 struct time result = {0,0,0}; 12 13 result.seconds = t2.seconds - t1.seconds; 14 if (result.seconds < 0){ 15 result.seconds += 60; 16 --t2.minutes; 17 } 18 19 result.minutes = t2.minutes - t1.minutes; 20 if (result.minutes < 0){ 21 result.minutes += 60; 22 --t2.hour; 23 } 24 25 result.hour = t2.hour - t1.hour; 26 if (result.hour < 0) 27 result.hour += 24; 28 29 return result; 30 } 31 int main (void){ 32 struct time elapsed_time (struct time t1,struct time t2); 33 struct time t1 = {3,45,15}, t2={9,44,03}, 34 t3 = {22,50,59}, t4={7,30,0}; 35 36 struct time result; 37 38 result = elapsed_time (t1, t2); 39 printf ("Time between %.2i:%.2i:%.2i and %.2i:%.2i:%.2i" 40 "is %.2i:%.2i:%.2i\n", 41 t1.hour,t1.minutes,t1.seconds,t2.hour,t2.minutes, 42 t2.seconds,result.hour,result.minutes,result.seconds); 43 44 result = elapsed_time (t2, t1); 45 printf ("Time between %.2i:%.2i:%.2i and %.2i:%.2i:%.2i" 46 "is %.2i:%.2i:%.2i\n", 47 t2.hour,t2.minutes,t2.seconds,t1.hour,t1.minutes, 48 t1.seconds,result.hour,result.minutes,result.seconds); 49 50 result = elapsed_time(t3,t4); 51 printf ("Time between %.2i:%.2i:%.2i and %.2i:%.2i:%.2i" 52 "is %.2i:%.2i:%.2i\n", 53 t3.hour,t3.minutes,t3.seconds,t4.hour,t4.minutes, 54 t4.seconds,result.hour,result.minutes,result.seconds); 55 56 return 0; 57 } 58 59 //第五题 60 61 #include <stdio.h> 62 struct dateAndTime clockKeeper (struct dateAndTime dt){ 63 struct time timeUpdate (struct time now); 64 struct date dateUpdate (struct date today); 65 66 dt.stime = timeUpdate(dt.stime); 67 68 if (dt.stime.hour == 0 && dt.stime.minutes == 0 && 69 dt.stime.seconds == 0) 70 dt.sdate = dateUpdate (dt.sdate); 71 72 return dt; 73 } 74 75 //第六题 76 77 #include <stdio.h> 78 #include <stdbool.h> 79 80 struct date{ 81 int month; 82 int day; 83 int year; 84 }; 85 86 struct date dateUpdate (struct date today){ 87 struct date tomorrow; 88 int numberOfDays (struct date d); 89 90 if (today.day != numberOfDays (today)) 91 tomorrow = (struct date){ 92 today.month, 93 today.day + 1, 94 today.year 95 }; 96 97 else if (today.month == 12) 98 tomorrow = (struct date){ 99 1,1,today.year + 1 100 }; 101 else 102 tomorrow = (struct date){ 103 today.month + 1,1,today.year 104 }; 105 return tomorrow; 106 } 107 108 int numberOfDays (struct date d){ 109 int days; 110 bool isLeapYear (struct date d); 111 const int daysPerMonth[12] = 112 {31,28,31,30,31,30,31,31,30,31,30,31}; 113 114 if (isLeapYear && d.month == 2) 115 days = 29; 116 else 117 days = daysPerMonth[d.month - 1]; 118 119 return days; 120 121 } 122 bool isLeapYear (struct date d){ 123 bool leapYearFlag; 124 125 if ((d.year % 4 == 0 && d.year % 100 != 0) || 126 d.year % 400 == 0) 127 leapYearFlag = true; 128 else 129 leapYearFlag = false; 130 return leapYearFlag; 131 } 132 int main (void){ 133 struct date dateUpdate (struct date today); 134 struct date thisDay, nextDay; 135 136 printf ("Enter today's date (mm dd yyyy):"); 137 scanf ("%i%i%i", &thisDay.month, &thisDay.day, 138 &thisDay.year); 139 nextDay = dateUpdate (thisDay); 140 141 printf ("Tomorrow's date is %i/%i/%.2i.\n", nextDay.month, 142 nextDay.day,nextDay.year % 100); 143 return 0; 144 }