第十一课作业 万年历的实现
本帖最后由 wai1216 于 2016-4-22 10:25 编辑先开始的时候,申请了12个空间存放月份对应的天数的时候。直接在main()里面申请。然后在函数里面在进行调用导致按上下左右的时候总有问题,后来发现是void show2()里
对于闰年二月的问题(不返回但是在下一次进行的时候的确月份所对应的天数发生了变化,之后好好研究一下)
于是就各个模块中申请了12个连续的空间。2000年之后哈,判断是否为正确的天数之后有时间补上哈
代码如下:
int days_1(int year,int month,int days)
{
int c = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int i;
if ( ( !(year%4) && year%100 || !(year%100) ) && month >= 3)
{
days += 1;
}
for (i = 0; i < 12; i++)
{
if (month == i + 1)
{
break;
}
else
{
days += *(c + i);
}
}
for (; year != 2000; year--)
{
days += 365;
if ((year - 2001) % 4 == 0)
{
days += 1;
}
}
return days;
}
void show2(int days,int year,int month, int day)
{
int c = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int i;
int q = 3;
int w = 8;
int j = 1;
for( i =0; i < 7; i++)
{
if ((days + 6) % 7 == i)
{
q += i * 4;
gotoxy(q, w);
printf("%d", j++);
}
}
if( !(year%4) && year%100 || !(year%100) )
{
*(c + 1) += 1;
}
for (i = 2; i <= *(c + month - 1); i++)
{
if (q == 27)
{
q = 3;
w++;
}
else
{
q += 4;
}
if(i == day)
{
gotoxy(q - 1, w);
printf("[%d]",j++);
}
else
{
gotoxy(q, w);
printf("%d",j++);
}
}
}
void show1()
{
printf(" Permanent Calendar Code: By/Wai1216 \n");
printf(" Enter q is Exit || \n");
gotoxy(3, 7);
printf("S");
gotoxy(7, 7);
printf("M");
gotoxy(11, 7);
printf("T");
gotoxy(15, 7);
printf("W");
gotoxy(19, 7);
printf("T");
gotoxy(23, 7);
printf("F");
gotoxy(27, 7);
printf("S");
}
main()
{
int year, month, day;
int k;
int t = 1;
char s;
scanf("%d%d%d", &year, &month, &day);
while( t )
{
int days = 0;
clrscr();
k = days_1( year, month, days);
show1();
show2( k, year, month, day);
printf("\n");
printf("\n");
printf(" You put date is:%d-%d-%d\n", year, month, day);
printf(" So tell me next loop:");
s = getch();
if(s == 'q')
{
t = 0;
}
else if(s == 0x48)
{
year--;
}
else if(s == 0x50)
{
year++;
}
else if(s == 0x4B)
{
month--;
if(month <= 0)
{
month = 12;
year--;
}
}
else if(s == 0x4D)
{
month++;
if(month > 12)
{
month = 1;
year++;
}
}
}
}
页:
[1]