我想在C中使用带偏移的日历,以便下个月从结束前一个月的那天开始。一切工作正常,但是我想在case操作中执行printf,以便可以打印月份的名称。如果没有printf以防万一,那是正确的,但是在放置好printf以防我的偏移量被保存下来之后,该行从星期一开始。
我尝试了很多;这就是为什么我的代码中有一些注释的原因。
#include <stdio.h>
int main(){
//enum monat {Januar = 1, Februar, Maerz, April, Mai, Juni, Juli, August, September, Oktober, November, Dezember};
int monat;
int tag;
int maxtage;
int offset = 0;
int tab = 0;
for(monat = 1; monat <= 12; monat++){
//printf("\nMonat %i: \n", monat);
//printf("Mo\tDi\tMi\tDo\tFr\tSa\tSo\n");
for(tab = 1; tab <= offset; tab++){
printf("\t");
}
switch (monat){
case 1:
printf("\nJanuar\n");
maxtage = 31;
break;
case 2:
printf("\nFebruar\n");
maxtage = 28;
break;
case 3:
printf("\nMärz\n");
maxtage = 31;
break;
case 4:
printf("\nApril\n");
maxtage = 30;
break;
case 5:
printf("Mai");
maxtage = 31;
break;
case 6:
printf("Juni");
maxtage = 30;
break;
case 7:
printf("Juli");
maxtage = 31;
break;
case 8:
printf("August");
maxtage = 31;
break;
case 9:
printf("September");
maxtage = 30;
break;
case 10:
printf("Oktober");
maxtage = 31;
break;
case 11:
printf("November");
maxtage = 30;
break;
case 12:
printf("Dezember");
maxtage = 31;
break;
}
for(tag = 1; tag <= maxtage; tag++){ /*Tage printen*/
printf("%i\t", tag);
if ((tag + offset) %7 == 0){ /*Einrücken für Monate*/
printf("\n");
}
}
offset += maxtage % 7;
if(offset >= 7){
offset -=7;
}
printf("\n");
}
return 0;
}
收到您的反馈后,现在效果很好,谢谢大家。
int main(){
int monat;
int tag;
int maxtage;
int offset = 0;
int tab = 0;
for(monat = 1; monat <= 12; monat++){
switch(monat){
case 1:
maxtage = 31;
printf("\nJanuar\n");
break;
case 2:
maxtage = 28;
printf("\nFebruar\n");
break;
case 3:
maxtage = 31;
printf("\nMärz\n");
break;
case 4:
maxtage = 30;
printf("\nApril\n");
break;
case 5:
maxtage = 31;
printf("\nMai\n");
break;
case 6:
maxtage = 30;
printf("\nJuni\n");
break;
case 7:
maxtage = 31;
printf("\nJuli\n");
break;
case 8:
maxtage = 31;
printf("\nAugust\n");
break;
case 9:
maxtage = 30;
printf("\nSeptember\n");
break;
case 10:
maxtage = 31;
printf("\nOktober\n");
break;
case 11:
maxtage = 30;
printf("\nNovember\n");
break;
case 12:
maxtage = 31;
printf("\nDezember\n");
break;
}
printf("Mo\tDi\tMi\tDo\tFr\tSa\tSo\t\n");
for(tab = 1; tab <= offset; tab++){
printf("\t");
}
for(tag = 1; tag <= maxtage; tag++){ /*Tage printen*/
printf("%i\t", tag);
if ((tag + offset) %7 == 0){ /*Einrücken für Monate*/
printf("\n");
}
}
offset += maxtage % 7;
if(offset >= 7){
offset -=7;
}
printf("\n");
}
return 0;
}