1、last day (错误程序)
#include <stdio.h> void lastday(int *y, int *m, int *d) { if(*d > 1) { *d -= 1; } if(*d == 1 && *m == 2 || *m == 4 || *m == 6 || *m == 9 || *m == 11) // 这种写法不正确,需要将逻辑或运算符用括号括起来 { *d = 31; *m -= 1; } if(*d == 1 && *m == 1) { *d = 31; *m = 12; *y -= 1; } if(*d == 1 && *m == 5 || *m == 7 || *m == 10 || *m == 12) { *d = 30; *m -= 1; } if(*d == 1 && *m == 3) { if(*y % 4 == 0 || *y % 400 == 0 && *y % 100 != 0) { *d = 29; *m -= 1; } else { *d = 28; *m -= 1; } } } int main(void) { int year, month, day; puts("please input three integers."); printf("year = "); scanf("%d", &year); printf("month = "); scanf("%d", &month); printf("day = "); scanf("%d", &day); printf(" today: %d-%d-%d ",year, month, day); lastday(&year, &month, &day); printf("lastday: %d-%d-%d ", year, month, day); return 0; }
↓
修改程序
#include <stdio.h> void lastday(int *y, int *m, int *d) { if(*d > 1) { *d -= 1; } if(*d == 1 && (*m == 2 || *m == 4 || *m == 6 || *m == 9 || *m == 11)) { *d = 31; *m -= 1; } if(*d == 1 && *m == 1) { *d = 31; *m = 12; *y -= 1; } if(*d == 1 && (*m == 5 || *m == 7 || *m == 10 || *m == 12)) { *d = 30; *m -= 1; } if(*d == 1 && *m == 3) { if((*y % 4 == 0 || *y % 400 == 0) && *y % 100 != 0) { *d = 29; *m -= 1; } else { *d = 28; *m -= 1; } } } int main(void) { int year, month, day; puts("please input three integers."); printf("year = "); scanf("%d", &year); printf("month = "); scanf("%d", &month); printf("day = "); scanf("%d", &day); printf(" today: %d-%d-%d ",year, month, day); lastday(&year, &month, &day); printf("lastday: %d-%d-%d ", year, month, day); return 0; }
2、tomorrow
#include <stdio.h> void tomo(int *y, int *m, int *d) { if(*m == 12) { if(*d < 31) *d += 1; else { *d = 1; *m = 1; *y += 1; } return; } if(*m == 1 || *m == 3 || *m == 5 || *m == 7 || *m == 8 || *m == 10) { if(*d < 31) *d+=1; else { *d = 1; *m += 1; } return; } if(*m == 4 || *m == 6 || *m == 9 || *m == 11) { if(*d < 30) *d += 1; else { *d = 1; *m += 1; } return; } if(*m == 2 && ((*y % 4 == 0 || *y % 400 == 0) && *y % 100 != 0)) { if(*d < 29) *d += 1; else { *d = 1; *m += 1; } return; } if(*m == 2 && ((*y % 4 != 0 && *y % 400 != 0) || *y % 100 == 0)) { if(*d < 28) *d += 1; else { *d = 1; *m += 1; } return; } } int main(void) { int year, month, day; puts("please input three integers."); printf("year = "); scanf("%d", &year); printf("month = "); scanf("%d", &month); printf("day = "); scanf("%d", &day); printf(" today: %d-%d-%d ", year, month, day); tomo(&year, &month, &day); printf(" tomorrow: %d-%d-%d ", year, month, day); return 0; }
2、
#include <stdio.h> void tomor(int *y, int *m, int *d) { if(*m == 1 || *m == 3 || *m == 5 || *m == 7 || *m == 8 || *m == 10) { if(*d < 31) *d += 1; else { *d = 1; *m += 1; } return; } if(*m == 12) { if(*d < 31) *d += 1; else { *d = 1; *m = 1; *y += 1; } return; } if(*m == 4 || *m == 6 || *m == 9 || *m == 11) { if(*d < 30) *d += 1; else { *d = 1; *m += 1; } return; } if(*m == 2 && ((*y % 4 == 0 || *y % 400 == 0) && *y % 100 != 0)) { if(*d < 29) *d += 1; else { *d = 1; *m += 1; } return; } if(*m == 2 &&((*y % 4 != 0 && *y % 400 != 0) || *y % 100 == 0)) { if(*d < 28) *d += 1; else { *d = 1; *m += 1; } return; } } int main(void) { int year, month, day; puts("please input three integers."); printf("year = "); scanf("%d", &year); printf("month = "); scanf("%d", &month); printf("day = "); scanf("%d", &day); printf(" today: %d-%d-%d ", year, month, day); tomor(&year, &month, &day); printf(" tomorrow: %d-%d-%d ", year, month, day); return 0; }