【发布时间】: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