虽然循环在C日历程序中无法正常工作?

时间:2015-09-24 15:55:02

标签: c while-loop

我的代码的目标是能够让用户输入一个整数值(delta),而delta是代码显示日期之日的天数现在。例如,今天的日期为September 24, 2015,如果用户输入10delta,则计算机将输出新日期October 4,2015。我知道可以使用time.h标准库,但我试图在不使用这些函数的情况下完成这一部分,我希望真正理解基础知识。我在使用while循环时遇到问题,我可以更改天数,但更改月份/年份对我来说不起作用。我在下面输入了当前代码的这一部分。我是C的新手,所以任何帮助都将不胜感激。感谢

#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <stdbool.h>

int domToday();
int monthToday();
int yeartoday();
int calcDays();

void printDate(int year, int month, int dom);

struct tm *now;
time_t timer;

int numberOfDays;

int main(int argc, char **argv) {

int delta = 0;

if (argc!=2) {
    printf("Expect a single argument... number of days from today.\n");
    printf("Assuming today's date (delta=0)\n");
    printf("Enter the number of days\n"); // change later
    scanf("%i", &delta);
} else delta=atoi(argv[1]);

int year=yearToday();
int month=monthToday();
int dom=domToday();


while(delta>0) {

    if (month == 12 && dom>numberOfDays) {
        ++year;
        month = 1;
        dom = 0;
        ++dom;
        --delta;
    }
    else if(dom>numberOfDays) {
        ++month;
        --delta;
        dom = 0;
        ++dom;
    }
    else {
        ++dom;
        --delta;
    }
}


printDate(year,month,dom);

}

int calcDays(month,year){
int numberOfDays;  
    if (month == 4 || month == 6 || month == 9 || month == 11)  
        numberOfDays = 30;  
    else if (month == 2)  {
        bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);  
    if (isLeapYear)  
         numberOfDays = 29;  
    else  
            numberOfDays = 28;  
    }  
    else  
        numberOfDays = 31; 
    }



int domToday() {
    timer=time(NULL);
    now = localtime(&timer);
    return now->tm_mday;
}

int monthToday() {
    timer=time(NULL);
    now = localtime(&timer);
    return now->tm_mon+1;
}

int yearToday() {
    timer=time(NULL);
    now = localtime(&timer);
    return now->tm_year+1900;
}

void printDate(int year, int month, int dom) {
    /* The following assertions make sure that the arguments are valid numbers */
    assert(year>=0);
    assert(month>0);
    assert(month<=12);
    assert(dom>0);
    assert(dom<=31);
    char* monthNames[12]={"January","February","March","April","May",
            "June","July","August","September","October","November","December"};
    printf("New date is: %s %2d, %d\n",monthNames[month-1],dom,year);
}

1 个答案:

答案 0 :(得分:-2)

正如HappyCoder所说,“

”上缺少括号

您的代码:

while(delta > 0)

    if (month == 12 && dom > numberOfDays) {
        ++year;
        month = 1;
        dom = 1;
        ++dom;
        --delta;
    }

    else if(dom > numberOfDays) {
        ++month;
        --delta;
        dom = 1;
        ++dom;
    }

    else {
        ++dom;
        --delta;
    }

你应该拥有什么:

while(delta > 0) {

    if (month == 12 && dom > numberOfDays) {
        ++year;
        month = 1;
        dom = 1;
        ++dom;
        --delta;
    }

    else if(dom > numberOfDays) {
        ++month;
        --delta;
        dom = 1;
        ++dom;
    }

    else {
        ++dom;
        --delta;
    }
}