Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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
使用月份和年份输入在C中打印儒略日历?_C_Loops_Calendar - Fatal编程技术网

使用月份和年份输入在C中打印儒略日历?

使用月份和年份输入在C中打印儒略日历?,c,loops,calendar,C,Loops,Calendar,我正在尝试用C语言输出日历,我想我已经把格式搞定了,但是我遇到了以下问题: 正在验证用户输入(我已经注释掉了可能的解决方案,但它尚未正确编译。它总是打印“所选的年/月无效”,并且break语句不起作用。) 在一周的正确日期开始日历日(2018年5月从周二开始,而不是周日) 我的当前输出: Enter month: 5 Enter year: 2018 Su M Tu W Th F Sa 1 2 3 4 5 6 7 8 9 10 11

我正在尝试用C语言输出日历,我想我已经把格式搞定了,但是我遇到了以下问题:

  • 正在验证用户输入(我已经注释掉了可能的解决方案,但它尚未正确编译。它总是打印“所选的年/月无效”,并且break语句不起作用。)
  • 在一周的正确日期开始日历日(2018年5月从周二开始,而不是周日)
  • 我的当前输出:

    Enter month: 5
    Enter year: 2018
    
     Su   M  Tu   W  Th   F  Sa
      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
    
    我的期望输出:

    Enter month: 5 
    Enter year: 2018 
    
    Su   M  Tu   W  Th   F  Sa
             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 
    
    以下是我的节目:

    #include <stdio.h>
    #include <stdlib.h>
    
    /* #defines */
    #define BEGIN 1900
    #define DAYS_IN_WEEK 7
    
    /* function prototypes */
    void getMonthYear(int *month, int *year);
    int toJulian(int month, int day, int year);
    int daysInMonth(int month, int year);
    int leapYear(int year);
    long yearsToDays(int year);
    void printCalendar(int startDay, int numDays);
    void printHeader();
    
    /* Calendar.c:  Prints monthly calendar.  Lab 4 / Week 9 */
    void main(void)
    {
        int month, year;
        int startDay;   // what day is first day of month.  1/1/1900 was Monday, so...
                        // day 1 is Mon, day 2 is Tue, ... , day 0 is Sun
    
        getMonthYear(&month, &year);
        startDay = (toJulian(month, 1, year) + yearsToDays(year)) % DAYS_IN_WEEK;
        printCalendar(startDay, daysInMonth(month, year));
    }
    void getMonthYear(int *month, int *year) {
        printf("Enter month: ");
        scanf("%d", month);
        /*if (month < 1 || month > 12) {
            printf("Invalid month selected");
            //break;
        }*/
        printf("Enter year: ");
        scanf("%d", year);
        /* if (year < BEGIN) {
            printf("Invalid year selected");
            //break;
        }*/
        printf("\n"); 
    }
    int toJulian(int month, int day, int year) {
        int count;
        for(count = 1; count < month; ++count) {
            day += daysInMonth(month, year);    
        }
        return day;
    }
    int daysInMonth(int month, int year) {
        int numDays;
    
    
    switch (month) {
        case 1: numDays = 31;
            break;
        case 2: numDays = 28;
            break;
        case 3: numDays = 31;
            break;
        case 4: numDays = 30;
            break;
        case 5: numDays = 31;
            break;
        case 6: numDays = 30;
            break;
        case 7: numDays = 31;
            break;
        case 8: numDays = 31;
            break;
        case 9: numDays = 30;
            break;
        case 10: numDays = 31;
            break;
        case 11: numDays = 30;
            break;
        case 12: numDays = 31;
            break;
        }
        return numDays;
    }
    int leapYear(int year) {
        if (year % 400 == 0 && (year % 100 != 0 || year % 400 == 0)) {
            return 1;
        }
        else {
            return 0;
        }
    }
    long yearsToDays(int year) {
        int count;
        long days;
        for (count = BEGIN; count < year; ++count) {
            days = 365 + leapYear(year);
        }
        return days;
    }
    void printCalendar(int startDay, int numDays) {
        int dayid;
        printHeader();
        // Shifts position for the first date... sort of
            for ( dayid = 0; dayid < startDay; dayid++ ) {
                printf(" ");
            }
    
            // Supposedly prints all the dates for one month
            for ( dayid = 1; dayid <= numDays; dayid++ ) {
                printf("%3d", dayid );
    
                // If the day is not before Saturday, start next line on Sun
                if ( ( dayid + startDay ) % DAYS_IN_WEEK > 0 ) {
                    printf(" ");
                }
                else {
                    printf("\n" );
                }
            }
    }
    void printHeader() {
        printf(" Su   M  Tu   W  Th   F  Sa\n");
    }
    
    #包括
    #包括
    /*#定义*/
    #定义开始1900
    #定义第7周的天数
    /*功能原型*/
    void getMonthYear(整数*月,整数*年);
    int toJulian(int月、int日、int年);
    int daysInMonth(int month,int year);
    国际年(国际年);
    长年天数(整年);
    作废打印日历(int startDay,int NUMDDAY);
    作废打印头();
    /*日历c:打印月历。实验4/第9周*/
    真空总管(真空)
    {
    int月,年;
    int startDay;//月的第一天是星期几。1900年1月1日是星期一,所以。。。
    //第一天是星期一,第二天是星期二,…,第0天是太阳
    getMonthYear(月份和年份);
    startDay=(toJulian(月、年)+yearsToDays(年))%周内天数;
    打印日历(startDay,daysInMonth(月,年));
    }
    void getMonthYear(整数*月,整数*年){
    printf(“输入月份:”);
    scanf(“%d”,月);
    /*如果(月<1 | |月>12){
    printf(“所选月份无效”);
    //中断;
    }*/
    printf(“输入年份:”);
    scanf(“%d”,年份);
    /*如果(年<开始){
    printf(“选择的无效年份”);
    //中断;
    }*/
    printf(“\n”);
    }
    整数toJulian(整数月、整数日、整数年){
    整数计数;
    用于(计数=1;计数<月份;++计数){
    日+=日/月(月,年);
    }
    回归日;
    }
    int daysInMonth(int月,int年){
    国际数日;
    开关(月){
    案例1:numDays=31;
    打破
    案例2:numDays=28;
    打破
    案例3:numDays=31;
    打破
    案例4:numDays=30;
    打破
    案例5:numDays=31;
    打破
    案例6:numDays=30;
    打破
    案例7:numDays=31;
    打破
    案例8:numDays=31;
    打破
    案例9:numDays=30;
    打破
    案例10:numDays=31;
    打破
    案例11:numDays=30;
    打破
    案例12:numDays=31;
    打破
    }
    返回天数;
    }
    整数年(整数年){
    如果(年份%400==0&(年份%100!=0 | |年份%400==0)){
    返回1;
    }
    否则{
    返回0;
    }
    }
    长年份天数(整年){
    整数计数;
    漫长的日子;
    对于(计数=开始;计数<年份;++计数){
    天数=365+年(年);
    }
    返程天数;
    }
    作废打印日历(整数开始日、整数天数){
    int dayid;
    printHeader();
    //为第一次约会改变位置…有点
    对于(dayid=0;dayid
    break
    用于退出循环或开关语句。它只能在当前函数范围内的循环中使用

    因此,即使在
    main()
    中的循环内调用了
    getMonthYear()
    (它不是),它内部的
    中断也不会退出该循环

    实际上,无论是在
    getMonthYear()
    中,还是在导致它的调用序列中,都没有循环,因此
    中断将是不可操作的

    你必须问自己“当他们输入无效条目时,我实际上想做什么?”

    一种可能是从
    getMonthYear()返回有效性


    要处理正确的一天的问题,您可能需要自己进行更多的调试:将打印语句放在所有有计算的地方,打印出中间结果,并以这种方式找出错误所在。

    以下函数中存在错误:

  • toJulian

    int toJulian(int month, int day, int year) {
        int count;
        for(count = 1; count < month; ++count) {
            day += daysInMonth(month, year);    
        }
        return day;
    }
    
  • leapYear

    int leapYear(int year) {
        if (year % 400 == 0 && (year % 100 != 0 || year % 400 == 0)) {
            return 1;
        }
        else {
            return 0;
        }
    }
    
    if
    语句中的逻辑不正确。它需要:

    (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
    
    您可以使用以下方法使函数更具可读性,更不容易出错:

    int leapYear(int year) {
       int ret = 0;
       if ( year % 4 != 0 )
       {
          ret = 0;
       }
       else 
       {
          if ( year % 100 != 0 )
          {
             ret = 1;
          }
          else 
          {
             ret = (year % 400 == 0);
          }
       }
    
       return ret;
    }
    
  • yearsToDays

    long yearsToDays(int year) {
        int count;
        long days;
        for (count = BEGIN; count < year; ++count) {
            days = 365 + leapYear(year);
        }
        return days;
    }
    
  • printCalendar

    void printCalendar(int startDay, int numDays) {
        int dayid;
        printHeader();
        // Shifts position for the first date... sort of
            for ( dayid = 0; dayid < startDay; dayid++ ) {
                printf(" ");
            }
    
            // Supposedly prints all the dates for one month
            for ( dayid = 1; dayid <= numDays; dayid++ ) {
                printf("%3d", dayid );
    
                // If the day is not before Saturday, start next line on Sun
                if ( ( dayid + startDay ) % DAYS_IN_WEEK > 0 ) {
                    printf(" ");
                }
                else {
                    printf("\n" );
                }
            }
    }
    
  • main

    线路

    startDay = (toJulian(month, 1, year) + yearsToDays(year)) % DAYS_IN_WEEK;
    
    应该是

    startDay = (1 + toJulian(month, year) + yearsToDays(year)) % DAYS_IN_WEEK;
    
    解释(1)2001年1月19日是星期一,以及(2)更改为
    toJulian
    的界面


  • 通过使用
    switch
    语句,而不是返回
    numDays
    的多个
    if-else
    语句,您可以稍微改进代码。好的,为了清晰起见,我在程序中添加了switch语句!谢谢我是否将此有效性检查用于我现有的if语句?非常感谢R Sahu!你真的帮助我了解了我需要简化的日期间隔,以及我在循环中错误使用的变量!
    void printCalendar(int startDay, int numDays) {
        int dayid;
        printHeader();
        // Shifts position for the first date... sort of
            for ( dayid = 0; dayid < startDay; dayid++ ) {
                printf(" ");
            }
    
            // Supposedly prints all the dates for one month
            for ( dayid = 1; dayid <= numDays; dayid++ ) {
                printf("%3d", dayid );
    
                // If the day is not before Saturday, start next line on Sun
                if ( ( dayid + startDay ) % DAYS_IN_WEEK > 0 ) {
                    printf(" ");
                }
                else {
                    printf("\n" );
                }
            }
    }
    
            for ( dayid = 0; dayid < startDay; dayid++ ) {
                printf("    ");
            }
    
    startDay = (toJulian(month, 1, year) + yearsToDays(year)) % DAYS_IN_WEEK;
    
    startDay = (1 + toJulian(month, year) + yearsToDays(year)) % DAYS_IN_WEEK;