• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

用C语言在控制台打印日历

OC/C/C++ 水墨上仙 1460次浏览

用C语言在控制台打印日历

/***************************************
Student:赵忠印 032-25
Date:  2006.9.23
Purpose: Coursework 1
这个程序能打印一年的日历
***************************************/
#include<stdio.h>
int isLeapyear(int year)/*this function is used to calculate that if a year is a leap year*/
{
    if(year%4==0)
        {
            if(year%100!=0)
                return 1;
            else if(year%100==0&&year%400==0)
                return 1;
            else
                return 0;
        }
    else
        return 0;
}
void printCalendar(int year,int day)/*this function is used to print the calendar of a year*/
{
    int month;
    int days;
    int FristDayOfMonth;
    int LastDayOfMonth;
    int i;
    for(month=1;month<=12;month++)
    {
         printf("\n\n");
         switch(month)
         {
               case 1:printf("\tJanuary %d\n",year);
                      days=31;/*how many days in this month*/
                      break;
               case 2:printf("\tFebruary %d\n",year);
                      days=(isLeapyear(year))? 29:28;/*this is used to calculate if there are 29 or 28 days in february*/
                      break;
               case 3:printf("\tMarch %d\n",year);
                      days=31;
                      break;
               case 4:printf("\tApril %d\n",year);
                      days=30;
                      break;
               case 5:printf("\tMay %d\n",year);
                      days=31;
                      break;
               case 6:printf("\tJune %d\n",year);
                      days=30;
                      break;
               case 7:printf("\tJuly %d\n",year);
                      days=31;
                      break;
               case 8:printf("\tAugust %d\n",year);
                      days=31;
                      break;
               case 9:printf("\tSeptember %d\n",year);
                      days=30;
                      break;
               case 10:printf("\tOctober %d\n",year);
                       days=31;
                       break;
               case 11:printf("\tNovember %d\n",year);
                       days=30;
                       break;
               case 12:printf("\tDecember %d\n",year);
                       days=31;
                       break;
               default:printf("error");
         }
         printf("  S  M  T  W  T  F  S\n");/*print the days of a week*/
         if(month==1)/*January*/
                {
                 FristDayOfMonth=day;/*this is used to calculate the day of week that it is the frist day of the month*/
                 LastDayOfMonth=(days-8+day)%7;/*this is used to calculate the day of week that it is the last day of the month*/
             }
         else/*other months*/
             {
                FristDayOfMonth=(LastDayOfMonth==7)?1:LastDayOfMonth+1;/*this is used to calculate the day of week that it is the frist day of the month*/
                LastDayOfMonth=(days-8+FristDayOfMonth)%7;/*this is used to calculate the day of week that it is the last day of the month*/
             }
         for(i=1;i<FristDayOfMonth*3-2;i++)/*calculate the location of the frist day of a month*/
             printf(" ");
         for(i=1;i<=days;i++)/*print the calendar of a month*/
             {
                 printf("%3d",i);
                 if((i-(7-FristDayOfMonth+1))%7==0)
                     printf("\n");/* because it is Saturday*/
             }
    }
}
int main()
{
    int choose;/*what is day today*/
    int year;/*which year,you'd better input a interger*/
    printf("\n\n\t\tWelcome You!\n\n");
    printf("please input a year\n");
    scanf("%d",&year);
    printf("\nplease choose the day of the week that is January 1st\n");
    printf("\n1:Sunday  2:Monday  3:Tuseday 4:Wednesday  5:Thrusday 6;Friday  7:Saturday\n");
    printf("\nyour choose:[ ]\b\b");
    scanf("%d",&choose);
    if(choose<1||choose>7)
        {
            printf("choose error");
            return 1;
        }
    printCalendar(year,choose);/*this function will print the whole calendar of this year that it input by you*/
    getchar();
    getchar();
    return 1;/*because my English is poor,it is difficulty to read,please excuse you,I swear I will study it well*/
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明用C语言在控制台打印日历
喜欢 (0)
加载中……