【问题标题】:A bit of help making an ordinal clock conversion帮助进行序数时钟转换
【发布时间】:2021-12-27 06:17:22
【问题描述】:

目前,我的任务是编写一段代码,在将其转换为序数时钟之前询问用户的输入,以及闰年的例外情况。唯一的问题是,在将数组放入函数的情况下,我不知道自己在做什么。

/*The purpose of this program is to make the user enter a date and return the info as ordinal*/


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define MAXDAYSINMONTH 12 /*Define maximums for dates*/
#define MAXDAYS 365

int main()
{
    void findDate(int[], int *, int *); /*Prototype*/
int daysInMonth[MAXDAYSINMONTH]; /*Variables for function*/

int days, months, year, dayTest; /*Variables for user*/

printf("This Program will ask you for the date\n");
printf("Please enter the Day: "); /*Get User input*/
scanf("%d", &days);
printf("\nPlease enter the Month: ");
scanf("%d", &months);
printf("\nPlease enter the Year: ");
scanf("%d", &year);

findDate(days, months, &dayTest);/*call the function*/


/**
* Input:   Actual Date
* Process: Conversion
* Output:  Ordinal Date
*/

printf("The ordinal date for the date entered is: ", dayTest);/*Get Output*/
if (year / 4 == 0) { /*Look for Leap Year*/
    if (dayTest > 59) {
        dayTest = dayTest + 1;
    }
    printf("This date is a leap year");
}
else
    printf("This date is not a leap year");

return 0;
}
/*Convert Regular Date to Ordinal*/
void findDate(int days, int months, int* dayTest, int daysInMonths[]) {
    int daysInMonth[MAXDAYSINMONTH], dayTest; /*Variables for function*/
    daysInMonth[0] = 31;
    daysInMonth[1] = 28;
    daysInMonth[2] = 31;
    daysInMonth[3] = 30;
    daysInMonth[4] = 31;
    daysInMonth[5] = 30;
    daysInMonth[6] = 31;
    daysInMonth[7] = 31;
    daysInMonth[8] = 30;
    daysInMonth[9] = 31;
    daysInMonth[10] = 30;
    daysInMonth[11] = 31;
    if (months > 31) {
        *dayTestaddr = days + daysInMonth;
    }
}

新代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define MAXDAYSINMONTH 12 /*Define maximums for dates*/
#define MAXDAYS 365

void findDate(int days, int months, int* dayTest, int daysInMonths[]); /*Prototype*/

int main()
{

    int daysInMonth[MAXDAYSINMONTH]; /*Variables for function*/

    int days, months, year, dayTest; /*Variables for user*/

    printf("This Program will ask you for the date\n");
    printf("Please enter the Day: "); /*Get User input*/
    scanf("%d", &days);
    printf("\nPlease enter the Month: ");
    scanf("%d", &months);
    months = months - 1;
    printf("\nPlease enter the Year: ");
    scanf("%d", &year);

    findDate(days, months, &dayTest, daysInMonth); /*call the function*/


    /**
    * Input:   Actual Date
    * Process: Conversion
    * Output:  Ordinal Date
    */

    printf("The ordinal date for the date entered is: %d%d", year, dayTest);/*Get Output*/

    if (year % 4 == 0) { /*Look for Leap Year*/
        if (dayTest > 59) {
            dayTest = dayTest + 1;
        }
        printf("\nThis date is a leap year");
    }
    else
        printf("\nThis date is not a leap year");
    
    return 0;
}
/*Convert Regular Date to Ordinal*/
void findDate(int days, int months, int* dayTest, int daysInMonths[]) {
    daysInMonths[0] = 31;
    daysInMonths[1] = 28;
    daysInMonths[2] = 31;
    daysInMonths[3] = 30;
    daysInMonths[4] = 31;
    daysInMonths[5] = 30;
    daysInMonths[6] = 31;
    daysInMonths[7] = 31;
    daysInMonths[8] = 30;
    daysInMonths[9] = 31;
    daysInMonths[10] = 30;
    daysInMonths[11] = 31;
    if (months > 11 || months < 0)
    {
        // Illegal call
        printf("Your month is over 12 or under 0, try again\n");
    }
    if (months == 0) {
        *dayTest = days;
    }
    else 
        *dayTest = days + (daysInMonths[months] - daysInMonths[1]);
}

我只是保存了这个,以便我的进步更加明显,我确实认为我有一个通用的句柄,但我仍然需要继续前进,以 YYYY-DDD 的格式制作序数日期。我们将不胜感激,谢谢。

【问题讨论】:

    标签: c visual-studio


    【解决方案1】:

    findDate的声明不一致,原型应该放在main之前。

    你的原型:

    void findDate(int[], int *, int *); /*Prototype*/
    

    您的电话:

    findDate(days, months, &dayTest);
              ^       ^
              |       Not an int*
              Not an array
    

    你的定义:

    void findDate(int days, int months, int* dayTest, int daysInMonths[])
    

    这里有 4 个参数,但之前只有 3 个。此外,单个参数与之前的定义不匹配。

    您需要解决此不一致问题。

    main 之前放:

    void findDate(int days, int months, int* dayTest, int daysInMonths[]);
    

    main 中这样称呼它:

    findDate(days, months, &dayTest, daysInMonth);
    

    并且不要在函数体中重新定义 daysInMonthdayTest 之类的变量。只需这样做:

    void findDate(int days, int months, int* dayTest, int daysInMonths[]) {
        daysInMonth[0] = 31;
        daysInMonth[1] = 28;
        daysInMonth[2] = 31;
        daysInMonth[3] = 30;
        daysInMonth[4] = 31;
        daysInMonth[5] = 30;
        daysInMonth[6] = 31;
        daysInMonth[7] = 31;
        daysInMonth[8] = 30;
        daysInMonth[9] = 31;
        daysInMonth[10] = 30;
        daysInMonth[11] = 31;
        if (months >= 12 || months < 0) 
        {
            // Illegal call
            .. error handling ..
        }
        *dayTest = days + daysInMonth[months];
    }
    

    也就是说,为什么每次调用函数时都将daysInMonths 数组作为变量填充?这是对 CPU 周期的浪费。而是将其设为全局常量。

    所以之前main做:

    const int daysInMonth[] = {31, 28, 31, 30, ....};
    

    那么你根本不需要传递数组并且你的函数变得更加简单:

    void findDate(int days, int months, int* dayTest) {
        if (months >= 12 || months < 0) 
        {
            // Illegal call
            .. error handling ..
        }
        *dayTest = days + daysInMonth[months];
    }
    

    或者通过使用返回值而不是指针来进一步简化:

    int findDate(int days, int months) {
        assert(months >= 0 && months < 12); 
        return days + daysInMonth[months];
    }
    

    然后这样称呼它:

    dayTest = findDate(days, months);
    

    简单得多...没有指针,没有要传递给函数的数组。

    最后注意...数组索引从零开始,但您的用户可能会在 1 月输入 1。所以你有一个必须处理的不一致。

    【讨论】:

    • 好的,到目前为止一切顺利,没有重大错误,但我希望它以 YYYY-DDD 格式输出。另外,我希望数组将上个月的天数添加到函数中,但我不想完整的 yandev 并发布每个月的 if else 语句,所以如果可以的话,请你看一下吗?它应该在这篇文章的“新代码”下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多