|  | 
 
| 
#include <stdio.h>
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 int main()
 {
 long count1 = 0, count2;
 int year1, year2, year3;
 int month1, month2, month3;
 int day1, day2, day3;
 int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
 printf("请输入你的生日(如1988-05-20):");
 scanf("%d-%d-%d", &year1, &month1, &day1);
 
 printf("请输入今天的日期(如2016-03-28):");
 scanf("%d-%d-%d", &year2, &month2, &day2);
 
 year3 = year1 + 80;// 为什么要做这一步处理?
 month3 = month1;// 为什么要做这一步处理?
 day3 = day1;// 为什么要做这一步处理?
 
 while (1)//为什么做这一步处理呢?
 {
 days[1] = (year1 % 400 == 0 || (year1 % 4 == 0 && year1 % 100 != 0)) ? 29 : 28;
 while (month1 <= 12)
 {
 while (day1 <= days[month1 - 1])
 {
 if (year1 == year2 && month1 == month2 && day1 == day2)
 {
 count2 = count1;// 为什么要做这一步处理?
 printf("你在这个世界上总共生存了%d天\n", count2);
 }
 
 if (year1 == year3 && month1 == month3 && day1 == day3)
 {
 printf("如果能活到80岁,你还剩下%d天\n", count1 - count2);
 printf("你已经使用了%.2f%%的生命,请好好珍惜剩下的时间!\n", (double)count2 / count1 * 100);
 goto FINISH;
 }
 
 day1++;
 count1++;
 }
 day1 = 1;
 month1++;
 }
 month1 = 1;
 year1++;
 }
 
 FINISH: return 0;
 }
 
首先你得明白这个程序的原理是通过循环,从你输入的 出生日期 开始模拟一天一天累计时间。
 year3 = year1 + 80;// 为什么要做这一步处理?
 month3 = month1;// 为什么要做这一步处理?
 day3 = day1;// 为什么要做这一步处理?
 
 这里是计算了 80岁 的推算日期, 为的是之后的循环中进行判断
 
 while(1) 是一个死循环,是为了模拟时间不断累计,直到循环体中的某一步跳出循环,即这个例子中的  goto FINISH;
 值得一提的是主流的观点是尽量不用 goto... 所以此处直接 return 也行...
 
 count2 = count1;// 为什么要做这一步处理?
 这里的count2是为了记录你此时活了多少时间,以便与80岁时间的累计时间相减
 
 总的来说,你只要理解了这个例子中 day1 month1 year1 count1 的值在循环体中不断变化, 我们为了保存几个我们需要的特定值才在特定的时候把它们的值通过其他在循环体中不变的量存放了起来
 | 
 |