欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c語言實現(xiàn)一個簡單日歷

 更新時間:2015年03月09日 16:05:54   投稿:hebedich  
本文給大家分享的是一則使用C語言來實現(xiàn)的一個簡單日歷的代碼,根據(jù)項目需求,實現(xiàn)了3個簡單的小功能,推薦給大家,有需要的小伙伴可以參考下。

滿足三個需求:

1.輸入一個年份,輸出是在屏幕上顯示該年的日歷。假定輸入的年份在1940-2040年之間。
2.輸入年月,輸出該月的日歷。
3.輸入年月日,輸出距今天還有多少天,星期幾,是否是公歷節(jié)日。

最終完善版代碼:

復(fù)制代碼 代碼如下:

#include<stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <windows.h>
int year , month , day ;
int day_of_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
//char wek[7]={'周日','周一','周二','周三','周四','周五','周六'};
int current_year,current_month,current_day;
/************************************通用關(guān)鍵函數(shù)**************************************************/
void cls_screen()
{
    printf("請按任意鍵返回!\n");
    getchar();
    getchar();
    system("cls");
}
void get_current_time()   //獲取當(dāng)前時間
{
    time_t timep;
    struct tm *p;
    time(&timep);
    p = gmtime(&timep);
    current_year=1900+p->tm_year;
    current_month=1+p->tm_mon;
    current_day=p->tm_mday;
}
int judgement (int y)
{
    if (y % 400 == 0 || (y % 100 !=0 && y %4 ==0))
        return 1 ;
    else return 0 ;
}
int show_week (int year , int month , int day)
{
/*
公式:w=(y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1)%7
    */
    int w ,k;   //記錄周幾
    int year_last=year %100,c=year/100 , m = month;
    if (month==1 )
    {
        year_last-=1 ;
        m=13 ;
    }
    else if (month==2)
    {
        year_last-=1;
        m=14;
    }
    w = (year_last + year_last/4 + c/4 - 2*c +26*(m+1)/10+day-1); // abs  絕對值
    if (w<0)
    {
        k=(w%7+7)%7;
    }
    else k=w%7;
    return k ;
}
/************************************第一部分**************************************************/
void print_year (int year)
{
    int i , k ,x ,first_week;
    printf ("請輸入想要查詢的年月(格式如2012):");
    scanf ("%d",&year);
    printf("=======================%d年===========================\n",year);
    printf("\n");
    if (judgement(year))
    {
        day_of_month[1]=29;
    }
    else day_of_month[1]=28;
    for (i=1 ; i <13 ; i++)
    {
        first_week = show_week(year,i,1);
        printf("=====================%d月日歷如下========================\n",i);
        printf ("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
        for (x=1;x<=first_week;x++)
        {
            printf("\t");
            if (x%7==0) printf ("\n");
        }
        for (k=1;k<=day_of_month[i-1];k++)
        {
            printf("%d\t",k);
            if (x%7==0) printf ("\n");
            x++;
        }
        printf("\n");
        printf("\n");
        printf("\n");
    }
}
/************************************第二部分**************************************************/
void print_year_month ()
{
    int k ,x ,first_week;
    printf ("請輸入想要查詢的年月(格式如2012 12):");
    do
    {
        scanf ("%d %d",&year,&month);
        if (month<1||month>12)
        {
            printf("您輸入的月份有誤哦~請輸入正確的月份\n");
            printf ("請輸入想要查詢的年月(格式如2012 12):");
        }
    }while(1>month||month>12);
    printf("=====================%d年%d月======================\n",year,month);
    if (judgement(year))
    {
        day_of_month[1]=29;
    }
    else day_of_month[1]=28;
    first_week = show_week(year,month,1);
    printf ("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
    for (x=1;x<=first_week;x++)
    {
        printf("\t");
        if (x%7==0) printf ("\n");
    }
    for (k=1;k<=day_of_month[month-1];k++)
    {
        printf("%d\t",k);
        if (x%7==0) printf ("\n");
        x++;
    }
    printf("\n");
}
/************************************第三部分**************************************************/
int year_before_sumdays (int year,int month, int day)
{
    int days=0 ,i,judgement1;
    int temp_day=0 ,sum_days;       //
    //printf ("%d,%d\n",current_year,current_month);
    judgement1=judgement(year);
    /*===================比當(dāng)前少=====================*/
    if (year < current_year )
    {
        for (i = year+1;i < current_year ;i++)     
        {
            if (judgement(i))
            {
                days=days+356;
            }
            else days+=355;
        }
        for (i = month+1;i<=12;i++)
        {
            days=days+day_of_month[i-1];
        }
        days = days + day_of_month[month-1]-day;           //指定日子距離當(dāng)年結(jié)束還有多少天
        //printf("去年還有%d\n",days);
        for (i = 0;i < current_month-1;i++ )           
        {  
            if (judgement1)
            {
                day_of_month[1]=29;
            }
            temp_day = temp_day + day_of_month[i];
        }
        //當(dāng)前日子是這一年的多少天
        temp_day = temp_day + current_day;
        //printf("今天是第%d天\n",temp_day);
        sum_days=temp_day + days ;
    }
    /*===================比當(dāng)前多=====================*/
    if (year > current_year )
    {
        for (i =current_year+1;i < current_year ;i++)     
        {
            if (judgement(i))
            {
                days=days+356;
            }
            else days+=355;
        }
        for (i = current_month+1;i<=12;i++)
        {
            days=days+day_of_month[i-1];
        }
        days = days + day_of_month[month-1]-current_day;           //指定日子距離當(dāng)年結(jié)束還有多少天
        //printf("去年還有%d\n",days);
        for (i = 0;i <month-1;i++ )           
        {  
            if (judgement1)
            {
                day_of_month[1]=29;
            }
            temp_day = temp_day + day_of_month[i];
        }
        //當(dāng)前日子是這一年的多少天
        temp_day = temp_day + day;
        //printf("今天是第%d天\n",temp_day);
        sum_days=temp_day + days ;
    }
    /*===================比當(dāng)前一樣=====================*/
    if (year == current_year )
    {
        if(month <current_month)
        {
            for (i=month+1;i<current_month;i++)
            {
                if (judgement1)
                {
                    day_of_month[1]=29;
                }
                days = days + day_of_month[i];
            }
            sum_days = days + current_day + day_of_month[month-1] - day ;
        }
        if (month>current_month)
        {
            for (i=current_month+1;i<month;i++)
            {
                if (judgement1)
                {
                    day_of_month[1]=29;
                }
                days = days + day_of_month[i];
            }
            sum_days = days + day + day_of_month[month-1] - current_day ;
            printf("%d\n",days);
        }
        if (month==current_month)
        {
            sum_days= abs(day-current_day);
        }
    }
    return sum_days ;
}
void print(int year,int month,int day)
{
    int week;
    printf ("請輸入想要查詢的年月(格式如2012 12 12 ):");
    do
    {
        scanf ("%d %d %d",&year,&month,&day);
        if (judgement(year))
        {
            day_of_month[1]=29;
        }
        printf("\n");
        if (day<=0 || day >day_of_month[month-1])
            printf ("%d月沒有%d,請重新輸入(格式如2012 12 12 ):",month,day);
    }while(day<=0 || day >day_of_month[month-1]);
    week=show_week (year,month ,day);
    printf("\n");
    switch(month)//判斷查找天是否陽歷節(jié)日先由月份判斷是否有節(jié)日的月份再由日判斷是否為節(jié)日
    {
    case 1:switch(day)
           {
    case 1:printf("元旦") ;break;
    default:printf("不是陽歷節(jié)日");
           }break;
    case 2:switch(day)
           {
    case 14:printf("情人節(jié)(Valentines Day)");break;
    default:printf("不是陽歷節(jié)日");
           }break;
    case 3:switch(day)
           {
             case 8:printf("婦女節(jié)¨(Women's Day)");break;
             case 12:printf("植樹節(jié)(Arbor Day)");break;
             default:printf("不是陽歷節(jié)日");
           }break;
    case 4:switch(day)
           {
             case 1:printf("愚人節(jié)(April Fools Day)");break;
             case 5:printf("清明節(jié)(Tomb-sweeping Day)");break;
             default:printf("不是陽歷節(jié)日");
           }break;
    case 5:switch(day)
           {
    case 1:printf("勞動節(jié)(Labor Day)");break;
    case 4:printf("中國青年節(jié)(Chinese Youth Day)");
    default:printf("不是陽歷節(jié)日");
           }break;
    case 6:switch(day)
           {
    case 1:printf("兒童節(jié)(Children's Day)");break;
    default:printf("不是陽歷節(jié)日");
           }break;
    case 8:switch(day)
           {
    case 1:printf("建軍節(jié)(the Army's Day)");break;
    default:printf("不是陽歷節(jié)日");
           }break;
    case 9:switch(day)
           {
    case 10:printf("教師節(jié)(Teacher's Day)");break;
    default:printf("不是陽歷節(jié)日");
           }break;
    case 10:switch(day)
            {
    case 1:printf("國慶節(jié)(National Day)");break;
    case 31:printf("萬圣節(jié)(Helloween Day)");break;
    default:printf("不是陽歷節(jié)日");
            }break;
    case 12:switch(day)
            {
    case 25 :printf("圣誕節(jié)(Christmas Day)");break;
    default:printf("不是陽歷節(jié)日");
            }break;
    }
    printf("\n");
    printf("%d年%d月%d號是:",year,month,day);
    switch(week)//判斷所查找天是星期幾
    {
    case 0:printf("Sunday");break;
    case 1:printf("Monday");break;
    case 2:printf("Tuesday");break;
    case 3:printf("Wednesday");;break;
    case 4:printf("Thursday");break;
    case 5:printf("Friday");break;
    case 6:printf("Saturday");break;
    }
    printf("\n");
    printf("距離今天有%d天\n",year_before_sumdays ( year, month, day));
    printf("\n");
}
/********************************************************************************************/
void main ()
{
    int choice,year,month,day,flag=1;
    char c , k;
    for(;1;)//顯示程序菜單,為永真,每次查找完回到程序菜單ì
    {
        printf("===================================菜單選項===================================\n");
        printf("請選擇:\n");
        printf("* * * * * * * *               1.查找某一年的年歷              * * * * * * * *\n");
        printf("* * * * * * * *               2.查找某一月的月歷              * * * * * * * *\n");
        printf("* * * * * * * *               3.查找某一天                    * * * * * * * *\n");
        printf("* * * * * * * *               0.退出                          * * * * * * * *\n");
        printf("==============================================================================\n");
        printf("請輸入您的選擇:   ");
        do
        {
            if (flag)
            {
            c=getche();
            printf("\n");
            printf("確定選擇%c嗎? (y/Y )或者(n/N):   ",c);
            }  
            if (flag )
            {
            k=getche();
        //  printf("\n");
            }
            if (k == 'y' || k == 'Y')
            {
                printf("\n");
                if (c=='\n')
                {
                    printf("\n");
                    printf("輸入有誤,請輸入數(shù)字\n");
                    printf("\n");
                    printf("請輸入您的選擇:   ");
                }
                else if (!isdigit(c))
                {
                    printf("\n");
                    printf("輸入有誤,請輸入數(shù)字\n");
                    printf("\n");
                    printf("請輸入您的選擇:   ");
                }
                else if (isdigit(c))
                {
                    choice = c-'0' ;
                    if (choice < 0 || choice > 3 )
                    {
                        printf("輸入有誤,請輸入0-3之間的數(shù)字\n");
                    }
                    else break ;
                }
            }
            else if (k=='n' || k=='N')
            {
                printf("\n");
                printf("你選擇了否,請重新輸入選項:   ");
                flag=1;
            }
             if ((k != 'y'&& k!='Y'&& k!= 'n'&& k!='N'))
            {
                 printf("\n");
                printf("請輸入y/n:   ");
                flag=0;
                if (flag == 0)
            {
            k=getche();
            printf("\n");
            //getchar();
            }
            }
        }while(1);
        if (choice==1)
        {
            print_year(year);
            cls_screen();
        }
        else if (choice==2)
        {
            print_year_month ();
            cls_screen();
        }
        else if (choice==3)
        {
            get_current_time();
            print(year,month,day);
            cls_screen();
        }
        else if (choice==0)
            break;
        else
        {
            printf ("您的輸入有誤,請重新輸入\n");
        }
    }
}

以上就是本文所述的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

  • VScode+cuda編程常見環(huán)境問題的解決

    VScode+cuda編程常見環(huán)境問題的解決

    本文主要介紹了VScode+cuda編程常見環(huán)境問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C語言中函數(shù)返回字符串的方法匯總

    C語言中函數(shù)返回字符串的方法匯總

    C語言返回字符串函數(shù)共有四種方式,分別如下:使用堆空間,返回申請的堆地址,注意釋放、函數(shù)參數(shù)傳遞指針,返回該指針、返回函數(shù)內(nèi)定義的靜態(tài)變量(共享)、返回全局變量
    2017-05-05
  • C++事件處理中的__hook與__unhook用法詳解

    C++事件處理中的__hook與__unhook用法詳解

    這篇文章主要介紹了C++事件處理中__hook與__unhook的用法,C++中的COM類主要支持事件處理,需要的朋友可以參考下
    2016-01-01
  • C++?system()函數(shù)的常用用法(全網(wǎng)最新)

    C++?system()函數(shù)的常用用法(全網(wǎng)最新)

    system()用于從C?/C++程序調(diào)用操作系統(tǒng)命令,這里給大家講解下C++?system()函數(shù)的常用用法,感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • C++異常處理入門(try和catch)

    C++異常處理入門(try和catch)

    C++ 提供了異常機制,讓我們能夠捕獲運行時錯誤,本文就詳細(xì)的介紹了C++異常處理入門,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C++ 刪除字符串的2種方法小結(jié)

    C++ 刪除字符串的2種方法小結(jié)

    這篇文章主要介紹了C++ 刪除字符串的2種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Qt重寫QTreeView自繪實現(xiàn)酷炫樣式

    Qt重寫QTreeView自繪實現(xiàn)酷炫樣式

    QTreeView,顧名思義,就是一種樹形的控件,在我們需要做類似于文件列表的視圖時,是一個不錯的選擇,下面我們就來看看qt如何重寫QTreeView實現(xiàn)酷炫樣式,感興趣的可以了解一下
    2023-08-08
  • Opencv EigenFace人臉識別算法詳解

    Opencv EigenFace人臉識別算法詳解

    這篇文章主要為大家詳細(xì)介紹了Opencv EigenFace人臉識別算法的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C++利用遞歸實現(xiàn)走迷宮

    C++利用遞歸實現(xiàn)走迷宮

    這篇文章主要為大家詳細(xì)介紹了C++利用遞歸實現(xiàn)走迷宮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Qt實現(xiàn)邊加載數(shù)據(jù)邊顯示頁面的示例代碼

    Qt實現(xiàn)邊加載數(shù)據(jù)邊顯示頁面的示例代碼

    無論是MFC框架還是QT框架,實現(xiàn)加載數(shù)據(jù)的等待效果都是很麻煩的,不像WEB端輕輕松松一句代碼就搞定了。本文將通過Qt實現(xiàn)邊加載數(shù)據(jù)邊顯示頁面的功能,需要的可以參考一下
    2022-01-01

最新評論