zoukankan      html  css  js  c++  java
  • c语言10-2

    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;
    }

  • 相关阅读:
    robotframework-requests--中文注解版
    Python猜数小游戏
    走进Selenium新世界
    HTML
    Web测试方法_02
    3.线程死锁
    2.线程--线程安全(synchronized)
    1.线程--线程创建方式
    使用Android-studio开发移动app与weex结合开发详细步骤
    Weex 简介
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14824945.html
Copyright © 2011-2022 走看看