Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印12个月的日历,仅输入年份。使用C_C_Algorithm_Calendar_Formatting - Fatal编程技术网

打印12个月的日历,仅输入年份。使用C

打印12个月的日历,仅输入年份。使用C,c,algorithm,calendar,formatting,C,Algorithm,Calendar,Formatting,我被一个程序卡住了,这个程序记录用户想要的年份,并打印出同一年的12个月日历。到目前为止,这就是我所拥有的一切,我非常确定我有正确的方法来判断这一年是否是闰年(在代码中),以及如何确定1月1日是什么时候(在第一个下方,仅填写变量)。此外,我还试图打印在正常的日历格式与上面的月,然后在下面的一周中的几天后面的日期数字。任何帮助都将不胜感激 查找第一天: h = (1 + [(13(m + 1))/5] + K + [K/4] + [J/4] - 2J) mod 7 h = (1 + [(13(1

我被一个程序卡住了,这个程序记录用户想要的年份,并打印出同一年的12个月日历。到目前为止,这就是我所拥有的一切,我非常确定我有正确的方法来判断这一年是否是闰年(在代码中),以及如何确定1月1日是什么时候(在第一个下方,仅填写变量)。此外,我还试图打印在正常的日历格式与上面的月,然后在下面的一周中的几天后面的日期数字。任何帮助都将不胜感激

查找第一天:

h = (1 + [(13(m + 1))/5] + K + [K/4] + [J/4] - 2J) mod 7

h = (1 + [(13(13 + 1))/5] + (year % 100) + [(year % 100)/4] + [(year/100)/4] - 2(year/100) % 7
H是起始日,M是月份,K是年百分比100,J是年/100

/* Calendar.c */

#include <stdio.h>

int main(void){

    int year, month, date;
    int startingDay; /* initfrom user input*/

    printf("Enter the year of your desired calendar: ");
    scanf("%d\n", &year);

    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
        int months[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    else 
        int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    for (month = 0; month < 12; month++) {
        const int daysInMonth = ; /* set # of days */
        int dayOfWeek;

        printf(…); //month name
        printf(…); //days of week

        for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++)
            printf(/*blanks*/);

        for (int date = 1; date <= daysInMonth; date++) {
            printf("…", date);

            if (++dayOfWeek>6) {
                printf("\n");
                dayOfWeek = 0;
            }
        } // for date

        if (dayOfWeek !=0)
            printf("\n");

        startingDay = dayOfWeek;
    } // for month
}

以下是汇编,但仍有改进的机会:

/* calender.c */

#include <stdio.h>
#include <stdlib.h>

#define JULIAN    1
#define GREGORIAN 2

/*
 * return the day of the week for particualr date
 * flag JULIAN or GREGORIAN
 * (the divisions are integer divisions that truncate the result)
 * Sun = 0, Mon = 1, etc.
 */
int get_week_day(int day, int month, int year, int mode) {
  int a, m, y;

  a = (14 - month) / 12;
  y = year - a;
  m = month + 12*a - 2;

  if (mode == JULIAN) {
    return (5 + day + y + y/4 + (31*m)/12) % 7;
  }

  return (day + y + y/4 - y/100 + y/400 + (31*m)/12) % 7; // GREGORIAN
}


int main(void) {

    int year, month, date;
    int startingDay;     /* of the week: init from user input*/
    char *names[] = {"January", "February", "March", "April", "May", "June",
                    "July", "August", "September", "October", "November", "December"};

    printf("Enter the year of your desired calendar: ");
    scanf("%d", &year);

    // could check whether input is valid here

    startingDay = get_week_day(1, 1, year,GREGORIAN);

    int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
       months[1] = 29;

    for (month = 0; month < 12; ++month) {
        const int daysInMonth = months[month];  /* set # of days */
        int dayOfWeek, date;

        printf("\n  ------------%s-------------\n", names[month]);   // month name
        printf("  Sun  Mon  Tue  Wed  Thu  Fri  Sat\n");         // days of week

        for (dayOfWeek = 0; dayOfWeek < startingDay; ++dayOfWeek)
            printf("     ");

        for (date = 1; date <= daysInMonth; ++date) {
            printf("%5d", date);

            if (++dayOfWeek > 6) {
                printf("\n");
                dayOfWeek = 0;
            }
        } // for date

        if (dayOfWeek != 0)
            printf("\n");

        startingDay = dayOfWeek;
    } // for month

    return EXIT_SUCCESS;
}
/*calender.c*/
#包括
#包括
#定义朱利安1
#定义公历2
/*
*返回特定日期的星期几
*朱利安还是格里高利
*(分段是截断结果的整数分段)
*太阳=0,周一=1,等等。
*/
int get_week_day(整数天、整数月、整数年、整数模式){
int a,m,y;
a=(14个月)/12;
y=年份-a;
m=月份+12*a-2;
if(mode==JULIAN){
回报率(5+天+y+y/4+(31*m)/12)%7;
}
返回(日+y+y/4-y/100+y/400+(31*m)/12)%7;//公历
}
内部主(空){
整数年、月、日;
int startingDay;/*周数:从用户输入初始化*/
字符*名称[]={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
printf(“输入所需日历的年份:”);
scanf(“%d”和“年”);
//无法在此检查输入是否有效
开始日=获得周日(1,1,年,公历);
整月[12]={31,28,31,30,31,30,31,30,31,30,31};
如果(年份%400==0 | |(年份%4==0和年份%100!=0))
月[1]=29;
对于(月=0;月<12;++月){
const int daysInMonth=月[月];/*集#天*/
int dayOfWeek,日期;
printf(“\n--------%s------\n”,名称[月份];//月份名称
printf(“星期一星期二星期三星期四星期五星期六”);//一周中的几天
对于(dayOfWeek=0;dayOfWeek
以下汇编,但仍有改进的机会:

/* calender.c */

#include <stdio.h>
#include <stdlib.h>

#define JULIAN    1
#define GREGORIAN 2

/*
 * return the day of the week for particualr date
 * flag JULIAN or GREGORIAN
 * (the divisions are integer divisions that truncate the result)
 * Sun = 0, Mon = 1, etc.
 */
int get_week_day(int day, int month, int year, int mode) {
  int a, m, y;

  a = (14 - month) / 12;
  y = year - a;
  m = month + 12*a - 2;

  if (mode == JULIAN) {
    return (5 + day + y + y/4 + (31*m)/12) % 7;
  }

  return (day + y + y/4 - y/100 + y/400 + (31*m)/12) % 7; // GREGORIAN
}


int main(void) {

    int year, month, date;
    int startingDay;     /* of the week: init from user input*/
    char *names[] = {"January", "February", "March", "April", "May", "June",
                    "July", "August", "September", "October", "November", "December"};

    printf("Enter the year of your desired calendar: ");
    scanf("%d", &year);

    // could check whether input is valid here

    startingDay = get_week_day(1, 1, year,GREGORIAN);

    int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
       months[1] = 29;

    for (month = 0; month < 12; ++month) {
        const int daysInMonth = months[month];  /* set # of days */
        int dayOfWeek, date;

        printf("\n  ------------%s-------------\n", names[month]);   // month name
        printf("  Sun  Mon  Tue  Wed  Thu  Fri  Sat\n");         // days of week

        for (dayOfWeek = 0; dayOfWeek < startingDay; ++dayOfWeek)
            printf("     ");

        for (date = 1; date <= daysInMonth; ++date) {
            printf("%5d", date);

            if (++dayOfWeek > 6) {
                printf("\n");
                dayOfWeek = 0;
            }
        } // for date

        if (dayOfWeek != 0)
            printf("\n");

        startingDay = dayOfWeek;
    } // for month

    return EXIT_SUCCESS;
}
/*calender.c*/
#包括
#包括
#定义朱利安1
#定义公历2
/*
*返回特定日期的星期几
*朱利安还是格里高利
*(分段是截断结果的整数分段)
*太阳=0,周一=1,等等。
*/
int get_week_day(整数天、整数月、整数年、整数模式){
int a,m,y;
a=(14个月)/12;
y=年份-a;
m=月份+12*a-2;
if(mode==JULIAN){
回报率(5+天+y+y/4+(31*m)/12)%7;
}
返回(日+y+y/4-y/100+y/400+(31*m)/12)%7;//公历
}
内部主(空){
整数年、月、日;
int startingDay;/*周数:从用户输入初始化*/
字符*名称[]={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
printf(“输入所需日历的年份:”);
scanf(“%d”和“年”);
//无法在此检查输入是否有效
开始日=获得周日(1,1,年,公历);
整月[12]={31,28,31,30,31,30,31,30,31,30,31};
如果(年份%400==0 | |(年份%4==0和年份%100!=0))
月[1]=29;
对于(月=0;月<12;++月){
const int daysInMonth=月[月];/*集#天*/
int dayOfWeek,日期;
printf(“\n--------%s------\n”,名称[月份];//月份名称
printf(“星期一星期二星期三星期四星期五星期六”);//一周中的几天
对于(dayOfWeek=0;dayOfWeek
对于某些输入,预期输出是什么,实际输出是什么?您是否尝试过在调试器中逐行遍历程序?输入是
int
,输出是整个12个月的日历。至于调试器部分,我还没有到可以编译任何东西的地步,我只是想把我知道需要的东西写下来。@melmore;显示您的输出格式。@haccks@Joachim Pileborg输出已添加到底部。它不应该是
scanf(“%d\n”,&year)?对于某些输入,预期输出是什么,实际输出是什么?您是否尝试过在调试器中逐行遍历程序?输入是
int
,输出是整个12个月的日历。至于调试器部分,我还没有到可以编译任何东西的地步,我只是想把我知道需要的东西写下来。@melmore;显示您的输出格式。@haccks@Joachim Pileborg输出已添加到底部。它不应该是
scanf(“%d\n”,&year)