• switch 实现年月转换


    #include "stdafx.h"
    #include <stdlib.h>
    #include <stdio.h>
    int DayofYear(int year,int month, int day);
    void MonthDay(int year,int yearDay,int *pMonth,int *pDay);
    void Menu(void);
    int dayTab[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
                        {0,31,29,31,30,31,30,31,31,30,31,30,31}};
    int main()
    {
     int year,month,day,yearDay;
     char c;
     Menu();
     c = getchar();
     switch(c)
     {
     case '1':printf_s("please enter year,month,day:");
           scanf_s("%d,%d,%d",&year,&month,&day,3*sizeof(year));
        yearDay = DayofYear(year,month,day);
        printf_s("yearDay = %d ",yearDay);
        break;
     case '2':printf_s("Please enter year,yearDay:");
           scanf_s("%d,%d",&year,&yearDay);
        MonthDay(year,yearDay,&month,&day);
        printf_s("month = %d,day = %d ",month,day);
        break;
     case '3':exit(0);
     default :printf_s("input the error");
     }
        system("pause");
     return 0;
    }
    int DayofYear(int year,int month,int day)
    {
     int i, leap;
     leap = ((year % 4 == 0)&&(year % 100 !=0))||(year % 400 == 0);
     for(i = 1;i < month ;i++)
     {
      day = day +dayTab[leap][i];
     }
     return day;
    }
    void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
    {
     int i, leap;
     leap = ((year % 4 ==0)&&(year %100 !=0))||(year % 400 == 0);
     for(i = 1;yearDay > dayTab[leap][i];i++)
     {
      yearDay = yearDay - dayTab[leap][i];
     }
     *pMonth = i;
     *pDay = yearDay;
    }
    void Menu(void)
    {
     printf_s("1. year/month/day ->yearDay ");
     printf_s("2. yearDay -> year/month/day ");
     printf_s("3. Exit ");
     printf_s("please enter your choice:");
    }

  • 相关阅读:
    一些话
    把视频文件拆成图片保存在沙盒中
    相册视频保存进沙盒
    数据库
    C 计算数组长度
    iOS之与JS交互通信
    iOS之duplicate symbols for architecture x86_64错误
    iOS之隐藏状态栏
    iOS之开发程序之间的跳转及跳转到appStore
    iOS之常用的判NULL的方法
  • 原文地址:https://www.cnblogs.com/joyclub/p/4422422.html
Copyright © 2020-2023  润新知