标签:
实验:输入今天求明天日期
#include<stdio.h> int main() { int year,month,day; printf("请输入今天的日期,格式如:2016-10-04。\n"); scanf("%d-%d-%d",&year,&month,&day); if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { if(day>0&&day<=30) { day=day+1; printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } else if(day==31) { day=1,month=month+1; if(month<=12) { printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } else { year=year+1,month=1; printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } } else { printf("日期错误,请关闭程序,重新输入。\n"); } } else if(month==4||month==6||month==5||month==9||month==11) { if(day>0&&day<=29) { day=day+1; printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } else if(day==30) { day=1,month=month+1; if(month<=12) { printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } else { year=year+1,month=1; printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } } else { printf("日期错误,请关闭程序,重新输入。\n"); } } else if(month==2) { if(year%4==0&&year%100!=0||year%400==0) { if(day>0&&day<=28) { day=day+1; printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } else if(day==29) { day=1,month=month+1; if(month<=12) { printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } else { year=year+1,month=1; printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } } else { printf("日期错误,请关闭程序,重新输入。\n"); } } else { if(day>0&&day<=27) { day=day+1; printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } else if(day==28) { day=1,month=month+1; if(month<=12) { printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } else { year=year+1,month=1; printf("明天的日期为%4d-%02d-%02d。\n",year,month,day); } } else { printf("日期错误,请关闭程序,重新输入。\n"); } } } else { printf("日期错误,请关闭程序,重新输入。\n"); } return 0; }
实验总结:注意等于是==,而=是赋值,考虑闰年、二月、月底、年底、日期错误的情况,做之前一定要先想好思路。
标签:
原文地址:http://www.cnblogs.com/TX980502/p/5931505.html