我想找几年,几周和几年。 C中的日子。我不知道我的代码有什么问题。请帮忙:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define DAYS_PER_WEEK 7
#define WEEKS_PER_YEAR 52
#define DAYS_PER_YEAR 364
int main(void)
{
int a; int y; double w; double days; int weeks;
printf("Enter the time in days: ");
scanf("%d", &a);
y = a / DAYS_PER_YEAR;
w = ((a/ DAYS_PER_YEAR) - y)* WEEKS_PER_YEAR;
weeks = ((a / DAYS_PER_YEAR) - y)* WEEKS_PER_YEAR;
days = (w - weeks)* DAYS_PER_WEEK;
printf(" %d days is %d years, %d weeks and %d days", a, y, weeks, days);
return 0;
}
谁能告诉我哪里出错了?我怀疑问题出在printf
函数。
答案 0 :(得分:2)
首先,DAYS_PER_YEAR应该是365(没有餐饮闰年)。
其次,使用运算符'%'
来简化计算:
#include <stdio.h>
#define DAYS_PER_WEEK 7
#define WEEKS_PER_YEAR 52
#define DAYS_PER_YEAR 365
int main(void)
{
int a, years, weeks, days, remains;
printf("Enter the time in days: ");
scanf("%d", &a);
years = a / DAYS_PER_YEAR;
remains = a % DAYS_PER_YEAR;
weeks = remains / DAYS_PER_WEEK;
days = remains % DAYS_PER_WEEK;
printf("%d days is %d years, %d weeks and %d days\n", a, years, weeks, days);
return 0;
}