我正在尝试汇编我的C日历程序,使其看起来像真正的Unix程序。具体来说,我正在尝试使前3个月显示如下:
2017
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31
这是我的代码,它是草稿,稍后我将尝试对其进行简化。我正在逐行打印
void printJanFebMar(int year)
{
printf(" %d\n",year);
//print months from January to March
printf(" January February March\n");
for(int i = 0;i<3;i++){
printf(" Su Mo Tu We Th Fr Sa ");
}
printf("\n");
//January, February and March
//first line of days...
int record1 = 0;
int record2 = 0;
int record3 = 0;
for(int k = 0;k<findStartDateInMonth(1,year);k++){
printf(" ");
}
for(int j = 1;j<=daysInMonth[1];j++){
printf("%3d",j);
if((j+findStartDateInMonth(1,year)) % 7 == 0){
break;
record1 = j + findStartDateInMonth(1,year) +1;
printf("%d",record1);
}
}
printf(" ");
for(int k = 0;k<findStartDateInMonth(2,year);k++){
printf(" ");
}
for(int j = 1;j<=daysInMonth[2];j++){
printf("%3d",j);
if((j+findStartDateInMonth(2,year)) % 7 == 0){
break;
record2 = j + findStartDateInMonth(2,year) +1;
printf("%d",record2);
}
}
printf(" ");
for(int k = 0;k<findStartDateInMonth(3,year);k++){
printf(" ");
}
for(int j = 1;j<=daysInMonth[3];j++){
printf("%3d",j);
if((j+findStartDateInMonth(3,year)) % 7 == 0){
break;
record3 = j + findStartDateInMonth(3,year) +1;
printf("%d",record3);
}
}
printf("\n");
//second line
/*
for(int i = record1;i<record1+7;i++){
printf("%3d",i);
}
printf(" ");
for(int i = record2;i<record2+7;i++){
printf("%3d",i);
}
for(int i = record2;i<record2+7;i++){
printf("%3d",i);
}
*/
printf("\n");
它会打印出几天的第一行,但是,当我尝试保存将在换行符中的那一天的值以用于将来的printf语句时,这些值不会如
所示那样打印出来printf("%d",record1);
这是我程序的当前输出:
2017
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
将值分配给record1,record2和record3时出现什么错误,我该如何解决。
很长的帖子对不起,我将非常感谢您的帮助
答案 0 :(得分:1)
break
语句在当天的打印循环中放错了位置。在更新运行日编号后,您应该跳出循环:
if ((j + findStartDateInMonth(1, year)) % 7 == 0) {
record1 = j + findStartDateInMonth(1, year) + 1;
break;
}
您的方法可以简化并变得更通用。出于说明目的,这是一个更完整的版本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char *MonthNames[] = {
NULL,
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
};
/* Print the Proleptic* Gregorian calendar
(*) extended into the past before its adoption */
static void printYear(int year) {
int DaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const char *DayHeadings = "Su Mo Tu We Th Fr Sa";
int d1, d2, d3, y1, day;
y1 = year - 1;
day = y1 * 1461 / 4 - y1 / 100 + y1 / 400 + 1;
if (!(year % 4) && ((year % 100) || !(year % 400)))
DaysInMonth[2] = 29;
printf("%33d\n\n", year);
for (int m = 1; m <= 12; m += 3) {
int pad = 0;
for (int i = 0; i < 3; i++) {
int len = strlen(MonthNames[m + i]);
int pad1 = len + (20 - len) / 2;
printf("%*s", pad + pad1, MonthNames[m + i]);
pad = 22 - pad1;
}
printf("\n%s %s %s\n", DayHeadings, DayHeadings, DayHeadings);
d1 = 1 - day % 7;
day += DaysInMonth[m];
d2 = 1 - day % 7;
day += DaysInMonth[m + 1];
d3 = 1 - day % 7;
day += DaysInMonth[m + 2];
for (int j = 0; j < 6; j++) {
pad = -1;
for (int i = 0; i < 7; i++, d1++) {
pad += 3;
if (d1 > 0 && d1 <= DaysInMonth[m])
pad -= printf("%*d", pad, d1);
}
pad += 1;
for (int i = 0; i < 7; i++, d2++) {
pad += 3;
if (d2 > 0 && d2 <= DaysInMonth[m + 1])
pad -= printf("%*d", pad, d2);
}
pad += 1;
for (int i = 0; i < 7; i++, d3++) {
pad += 3;
if (d3 > 0 && d3 <= DaysInMonth[m + 2])
pad -= printf("%*d", pad, d3);
}
printf("\n");
}
}
}
int main(int argc, char *argv[]) {
if (argc > 1) {
for (int i = 1; i < argc; i++) {
int year = strtol(argv[i], NULL, 0);
printYear(year);
}
} else {
printYear(2018);
}
return 0;
}
答案 1 :(得分:0)
首先,这看起来像一个很酷的程序。
到目前为止,您的代码还不错-您只需将break;
语句移至其他命令之后,否则它将在执行之前结束循环。记住要连续三个月这样做。
for(int j = 1;j<=daysInMonth[1];j++){
printf("%3d",j);
if((j+findStartDateInMonth(1,year)) % 7 == 0){
record1 = j + findStartDateInMonth(1,year) +1;
break; // Moved this line to AFTER the record update
}
}
您可能会遇到的另一个问题是:
record1 = j + findStartDateInMonth(1,year) +1;
此行应为:
record1 = j + 1;
Try it online!只需点击“运行”,然后滚动以查看输出。