国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

用C語言實現(xiàn)萬年歷的代碼及思路(詳細教程)

這篇具有很好參考價值的文章主要介紹了用C語言實現(xiàn)萬年歷的代碼及思路(詳細教程)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

萬年歷程序要求:

1.當選擇1的時候,輸入,打印輸入的這一年12月的日歷。
2.當選擇2的時候,輸入年-月,打印輸入這一年這一月的日歷。
實現(xiàn)效果:
選擇1時

*********************Please chose*************************
*************1.Print a year's calendar *******************
*************2.Print a month's calendar*******************
**********************************************************
your chose:>1
input(year)>2022
2022-1
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                                                        1
        2       3       4       5       6       7       8
        9       10      11      12      13      14      15
        16      17      18      19      20      21      22
        23      24      25      26      27      28      29
        30      31
2022-2
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                        1       2       3       4       5
        6       7       8       9       10      11      12
        13      14      15      16      17      18      19
        20      21      22      23      24      25      26
        27      28
2022-3
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                        1       2       3       4       5
        6       7       8       9       10      11      12
        13      14      15      16      17      18      19
        20      21      22      23      24      25      26
        27      28      29      30      31
2022-4
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                                                1       2
        3       4       5       6       7       8       9
        10      11      12      13      14      15      16
        17      18      19      20      21      22      23
        24      25      26      27      28      29      30
2022-5
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
        1       2       3       4       5       6       7
        8       9       10      11      12      13      14
        15      16      17      18      19      20      21
        22      23      24      25      26      27      28
        29      30      31
2022-6
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                                1       2       3       4
        5       6       7       8       9       10      11
        12      13      14      15      16      17      18
        19      20      21      22      23      24      25
        26      27      28      29      30
2022-7
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                                                1       2
        3       4       5       6       7       8       9
        10      11      12      13      14      15      16
        17      18      19      20      21      22      23
        24      25      26      27      28      29      30
        31
2022-8
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                1       2       3       4       5       6
        7       8       9       10      11      12      13
        14      15      16      17      18      19      20
        21      22      23      24      25      26      27
        28      29      30      31
2022-9
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                                        1       2       3
        4       5       6       7       8       9       10
        11      12      13      14      15      16      17
        18      19      20      21      22      23      24
        25      26      27      28      29      30
2022-10
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                                                        1
        2       3       4       5       6       7       8
        9       10      11      12      13      14      15
        16      17      18      19      20      21      22
        23      24      25      26      27      28      29
        30      31
2022-11
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                        1       2       3       4       5
        6       7       8       9       10      11      12
        13      14      15      16      17      18      19
        20      21      22      23      24      25      26
        27      28      29      30
2022-12
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                                        1       2       3
        4       5       6       7       8       9       10
        11      12      13      14      15      16      17
        18      19      20      21      22      23      24
        25      26      27      28      29      30      31

選擇2時

*********************Please chose*************************
*************1.Print a year's calendar *******************
*************2.Print a month's calendar*******************
**********************************************************
your chose:>2
input(year-month)>2020-2
2020-2
        Sun     Mon     Tue     Wed     Thu     Fri     Sat
                                                        1
        2       3       4       5       6       7       8
        9       10      11      12      13      14      15
        16      17      18      19      20      21      22
        23      24      25      26      27      28      29

程序中所用到的函數(shù):

1.參考公式:

C語言根據(jù)日期判斷星期幾(使用基姆拉爾森計算公式)
算法如下:
基姆拉爾森計算公式
W= (d+2m+3(m+1)/5+y+y/4-y/100+y/400)%7
在公式中d表示日期中的日數(shù),m表示月份數(shù),y表示年數(shù)。
注意:在公式中有個與其他公式不同的地方
把一月和二月看成是上一年的十三月和十四月,
例:如果是2004-1-10則換算成:2003-13-10來代入公式計算。
以公元元年為參考,公元元年1月1日為星期一
改進后代碼如下:
iY=2022, iM=7, iD=17 iWeekDay=0
iY=2022, iM=7, iD=18 iWeekDay=1

iY=2022, iM=7, iD=23 iWeekDay=6文章來源地址http://www.zghlxwxcb.cn/news/detail-509198.html

int getWeekdayByYearday(int iY, int iM, int iD)
{
	/*
	iY=2022, iM=7, iD=17  iWeekDay=0
	iY=2022, iM=7, iD=18  iWeekDay=1
	...
	iY=2022, iM=7, iD=23  iWeekDay=6
	*/
    int iWeekDay = -1;
    if (1 == iM || 2 == iM)
    {
        iM += 12;
        iY--;
    }
    iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / 100 + iY / 400) % 7;
    return iWeekDay;
}

2.得到某年某月有幾天,并返回天數(shù)

int getmonthday(int year,int month)
{
    int day=0;
    switch(month)
    {
    	//當月是1,3,5,7,8,10,12時,天數(shù)返回31天
    	//當月是4,6,9,11時,天數(shù)返回30天
    	//當月是2月時,判斷進行平閏年判斷,
    	//如果是閏年返回29天,平年返回28天
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
        day=31;
        break;
        case 4:
        case 6:
        case 9:
        case 11:
        day=30;
        break;
        case 2:
        {
            if(year%400==0||(year%4==0&&year%100!=0))
            {
                day=29;
            }
            else
            {
                day=28;
            }
            break;
        }
    }
    return day;
}

3.通過傳遞的參數(shù)輸出某年某一月的日歷

int printfmonth(int year,int month)
{
	//week是某年某月的第一天的是星期幾
    int i,j,week=getWeekdayByYearday(year, month, 1);
    printf("%d-%d\n",year,month);
    printf("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
    //當小于等于第一天時,輸出tab鍵進
    for(i=0;i<=week;i++)
    {
        printf("\t");
    }
    //當小于等于月份天數(shù)時輸出日歷幾號
    for(i=1,j=week;i<=getmonthday(year,month);i++)
    {	
    	//如果日期等于7時進行換行并輸入tab鍵對齊
    	//j賦值為0
        if(j==7)
        {
            printf("\n");
            printf("\t");
            j=0;
        }
        printf("%d",i);//輸入月份日期
        printf("\t");
        j++;
    }
    printf("\n");//當打完一個月份日歷后進行換行
}

4.主函數(shù)

int main(int argc,const char * argv[])
{
    int chose=0;
    int ret=0;
    int year,month;
    int i;
    //打印表頭
    printf("*********************Please chose*************************\n");
    printf("*************1.Print a year's calendar *******************\n");
    printf("*************2.Print a month's calendar*******************\n");
    printf("**********************************************************\n");
    printf("your chose:>");
    //判斷輸入的選項個數(shù)是否為一個,不是的話輸出輸入錯誤。
    ret=scanf("%d",&chose);
    if(ret!=1)
    {
        printf("input chose error\n");
        return -1;
    }
    while((getchar()!='\n'));//吃掉垃圾字符
    //chose one時執(zhí)行目的1
    //chose two時執(zhí)行目的2
    switch(chose)
    {
        case one:
        printf("input(year)>");
        ret=scanf("%d",&year);
        if(ret!=1)
        {
            printf("input error\n");
            return -1;
        }
        while((getchar()!='\n'));
        //判斷輸入的年份是否正確,不正確的話輸出輸入錯誤
        if(year<=0)
        {
            printf("input error\n");
            return -1;
        }
        //通過循環(huán)打印目的1的一年日歷要求,12個月
        for(i=1;i<=12;i++)
        {
            printfmonth(year,i);
        }
        break;
        case two:
        printf("input(year-month)>");
        ret=scanf("%d-%d",&year,&month);
        if(ret!=2)
        {
            printf("input error\n");
            return -1;
        }
        while((getchar()!='\n'));
        //判斷輸入的年和月是否正確,不正確輸出輸入錯誤
        if(year<=0||month<0||month>12)
        {
            printf("input error\n");
            return -1;
        }
        //打印輸入的年-月的日歷
        printfmonth(year,month);
    }
    return 0;
}

源代碼如下:

#include <stdio.h>

#define one 1
#define two 2

int getWeekdayByYearday(int iY, int iM, int iD)
{
    int iWeekDay = -1;
    if (1 == iM || 2 == iM)
    {
        iM += 12;
        iY--;
    }
    iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / 100 + iY / 400) % 7;
    return iWeekDay;
}
int getmonthday(int year,int month)
{
    int day=0;
    switch(month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
        day=31;
        break;
        case 4:
        case 6:
        case 9:
        case 11:
        day=30;
        break;
        case 2:
        {
            if(year%400==0||(year%4==0&&year%100!=0))
            {
                day=29;
            }
            else
            {
                day=28;
            }
            break;
        }
    }
    return day;
}

int printfmonth(int year,int month)
{
    int i,j,week=getWeekdayByYearday(year, month, 1);
    printf("%d-%d\n",year,month);
    printf("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
    for(i=0;i<=week;i++)
    {
        printf("\t");
    }
    for(i=1,j=week;i<=getmonthday(year,month);i++)
    {
        if(j==7)
        {
            printf("\n");
            printf("\t");
            j=0;
        }
        printf("%d",i);
        printf("\t");
        j++;
    }
    printf("\n");
}

int main(int argc,const char * argv[])
{
    int chose=0;
    int ret=0;
    int year,month;
    int i;
    //打印表頭
    printf("*********************Please chose*************************\n");
    printf("*************1.Print a year's calendar *******************\n");
    printf("*************2.Print a month's calendar*******************\n");
    printf("**********************************************************\n");
    printf("your chose:>");
    ret=scanf("%d",&chose);
    if(ret!=1)
    {
        printf("input chose error\n");
        return -1;
    }
    while((getchar()!='\n'));
    switch(chose)
    {
        case one:
        printf("input(year)>");
        ret=scanf("%d",&year);
        if(ret!=1)
        {
            printf("input error\n");
            return -1;
        }
        while((getchar()!='\n'));
        if(year<=0)
        {
            printf("input error\n");
            return -1;
        }
        for(i=1;i<=12;i++)
        {
            printfmonth(year,i);
        }
        break;
        case two:
        printf("input(year-month)>");
        ret=scanf("%d-%d",&year,&month);
        if(ret!=2)
        {
            printf("input error\n");
            return -1;
        }
        while((getchar()!='\n'));
        if(year<=0||month<0||month>12)
        {
            printf("input error\n");
            return -1;
        }
        printfmonth(year,month);
    }
    return 0;
}

到了這里,關于用C語言實現(xiàn)萬年歷的代碼及思路(詳細教程)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯(lián)網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 51單片機實訓項目之“萬年歷”代碼原理詳解

    51單片機實訓項目之“萬年歷”代碼原理詳解

    讀者若需要工程源碼,可以私信我,收到后會第一時間回復。這是仿真效果? 51單片機萬年歷程序設計(附源碼+仿真分享)_嗶哩嗶哩_bilibili STC89C52 DS18B20(溫度傳感器) DS1302(時鐘芯片) LCD1602液晶顯示 獨立按鍵 杜邦線 (一).子程序 EEPROM.h LCD1602.h DS1302時鐘模塊的三個引腳:

    2024年02月08日
    瀏覽(22)
  • 基于ego1開發(fā)板的萬年歷自動數(shù)字日歷設計verilog代碼

    基于ego1開發(fā)板的萬年歷自動數(shù)字日歷設計verilog代碼

    名稱:基于ego1開發(fā)板的萬年歷自動數(shù)字日歷設計verilog代碼 軟件:VIVADO 語言:Verilog 代碼功能: 自動數(shù)字日歷設計? 設計自動數(shù)字日歷,用七段數(shù)字顯示器顯示年(后2位)、月、日和星期數(shù),在計日脈沖的作用下,自動完成1-12月的月、日及星期的計數(shù)和顯示。 FPGA代碼Verilog/VHDL代碼

    2024年02月03日
    瀏覽(78)
  • STM32制作萬年歷

    ? ? STM32萬年歷制作指南 一、概述 STM32是一種常用的微控制器,具有強大的處理能力和低功耗特性,非常適合用于制作各種電子設備。本文將介紹如何使用STM32制作一款簡易的萬年歷,幫助您輕松查看日期、時間和農歷等信息。 二、所需材料 1. STM32微控制器(建議使用STM32F

    2024年02月03日
    瀏覽(27)
  • 6-6 萬年歷顯示函數(shù)

    題主為武理的學生 網上沒有對應答案 僅以學習用途上傳 設計一個萬年歷,當用戶輸入年份和月份時,顯示這個月的日歷表。程序重點是這個月的第一天是星期幾和這個月有幾天,有了這兩個值,只需通過排列,就可以顯示這個日歷。程序要求用戶輸入的年份是從1900年開始,

    2024年01月18日
    瀏覽(23)
  • 基于Java的萬年歷(課設)

    基于Java的萬年歷(課設)

    資源鏈接:基于Java的萬年歷(課設) 摘 要 Java編程語言自誕生十幾年來,已經成功地運用在網絡計算及移動等各個領域。對于開發(fā)者來說,它具有簡單、面向對象、健壯、安全、結構中立、可移植和高效能等眾多優(yōu)點。此次我們用JAVA來設計一個萬年歷程序,該程序以網頁形

    2024年02月11日
    瀏覽(25)
  • 萬年歷【小游戲】(Java課設)

    萬年歷【小游戲】(Java課設)

    Java實現(xiàn)的小游戲 適合作為Java課設?。?! jdk1.8+Idea或eclipse 本系統(tǒng)源碼地址:https://download.csdn.net/download/qq_50954361/87801830 更多Java課設系統(tǒng)源碼地址:更多Java課設系統(tǒng)源碼地址 更多Java小游戲運行效果展示:更多Java小游戲運行效果展示 Java課設部署教程:Java課設部署教程 注意事

    2024年02月15日
    瀏覽(14)
  • 基于FPGA的電子萬年歷設計

    基于FPGA的電子萬年歷設計

    quartusii12.1 系統(tǒng)的整個結構框圖: ?然后,設置控制輸入有5個腳,分析功能如下所示: i_Function_Controller=0 ;顯示年月日 i_sel:選擇需要調整的某位數(shù)字。 i_set:計數(shù)器,調整需要調整的位置的數(shù)字。 具體調整的時候,首先選擇i_sel,按鍵按一下,需要調整的位置會移動一次,

    2024年02月03日
    瀏覽(25)
  • 基于51單片機的萬年歷設計

    基于51單片機的萬年歷設計

    目? 錄 前言....................................................................... 1 1 緒論..................................................................... 3 1.1 課題研究的背景..................................................... 3 1.2課題的研究目的與意義................................................ 3 1.3課題解決的主要內

    2024年02月02日
    瀏覽(32)
  • PTA6-6 萬年歷顯示函數(shù)

    設計一個萬年歷,當用戶輸入年份和月份時,顯示這個月的日歷表。程序重點是這個月的第一天是星期幾和這個月有幾天,有了這兩個值,只需通過排列,就可以顯示這個日歷。程序要求用戶輸入的年份是從1900年開始,已知1900年1月1日是星期一。 日歷中每個具體的日期占5個

    2024年01月16日
    瀏覽(31)
  • FPGA項目(12)——基于FPGA的萬年歷設計

    FPGA項目(12)——基于FPGA的萬年歷設計

    ? ? ? ? 首先稱述一下所實現(xiàn)的功能:可以顯示年、月、日、時、分、秒,有鬧鐘設置功能,鬧鐘時間到時,蜂鳴器響,報警。用6位數(shù)碼管進行顯示,分三個顯示頁面,第一個頁面顯示年月日,第二個界面顯示時分秒,第三個頁面顯示鬧鐘時間??梢杂冒存I進行翻頁,按鍵進

    2024年02月07日
    瀏覽(31)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包