美文网首页
练习题:生命计算器

练习题:生命计算器

作者: mark_x | 来源:发表于2019-08-10 15:38 被阅读0次
    #include <stdio.h>
    
    int main(void)
    {
        int year1, month1, day1;
        int year2, month2, day2;
        int count, count_sp;
        int lifetime = 80;
        int year3, month3, day3; 
        
        
        printf("请输入你的生日:");
        scanf("%d-%d-%d",&year1, &month1, &day1);
        printf("请输入今天的日期:");
        scanf("%d-%d-%d",&year2, &month2, &day2);
        year3 = year1 + lifetime; 
        month3 = month1; 
        day3 = day1;
        
        int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 30};
        
        while (year1 <= year3)
        {
            if ((year1 % 400 == 0) || ((year1 % 4 == 0) && (year1 % 100 != 0)))  // 判断闰年
            {
                days[1] = 29;
            }
            while (month1 <= 12)
            {
                while (day1 <= days[month1 - 1])
                {
                    if (year1 == year2 && month1 == month2 && day1 == day2)
                    {
                        printf("你在这个世界上总共生存了%d天\n", count);
                        count_sp = count;
                    }
                    if (year1 == year3 && month1 == month3 && day1 == day3)
                    {
                        printf("如果你能活到80岁,你还剩下%d天\n", count - count_sp);
                        printf("你已经使用了%.2f%%的生命,请好好珍惜剩下的时间\n", (double)count_sp / count * 100);
                        
                        goto FINISH;
                    }
                    
                    
                    
                    day1++;
                    count++;
                    
                }
                day1 = 1;
                month1++;
            }
            month1 = 1;
            year1++;
            
        }
    FINISH: 
        return 0;
        
        
    }
    

    相关文章

      网友评论

          本文标题:练习题:生命计算器

          本文链接:https://www.haomeiwen.com/subject/wmyrjctx.html