第二十二课作业,重写万年历(结构体)
本帖最后由 wai1216 于 2016-6-3 23:11 编辑简单实现了一下基本功能,结构体的使用。
大家一定要看校长的视频,后面就一下通了的感觉。
第一个是wnl.c
#include "data.h"
void main()
{
Data obj;
intlist(&obj);
_getchar(&obj);
}
---------------
第二个是data.h
typedef struct
{
int y;
int m;
int d;
}Data;
void intlist(Data * obj);
void showwnl(Data * obj);
void _getchar(Data * obj);
---------------
第三个是data.c
#include "data.h"
int a = { { 0,31,28,31,30,31,30,31,31,30,31,30,31 },{ 0,31,29,31,30,31,30,31,31,30,31,30,31 } };
char c = { 'S','M','T','W','T','F','S' };
char d[] = {'w','s','a','d','q'};
int isleapyear(Data * obj)
{
return (!(obj->y % 4) && (obj->y % 100)) || !(obj->y % 400);
}
int check(Data * obj)
{
int lp = isleapyear(obj);
return (obj->m > 0 && obj->m <= 12) && (obj->d >0 && obj->d <= a);
}
int caldays(Data * obj)
{
int days = 0;
Data first = { 2000,0,0 };
for (; first.y < obj->y || first.m < obj->m; first.m++)
{
if (first.m == 13)
{
first.m = 0;
first.y++;
}
days += *(*(a + isleapyear(&first)) + first.m);
}
return days;
}
void intlist(Data * obj)
{
while (1)
{
printf("\nPlease enter the number:");
scanf("%d%d%d", &obj->y, &obj->m, &obj->d);
if (check(obj))
{
break;
}
}
}
void showwnl(Data * obj)
{
int i = 0, days = 0, k = 0, n = 1;
printf("func:w,s,a,d quit:q\n");
days = caldays(obj);
for (i = 0; i < 7; i++)
{
printf("%5c", *(c + i));
}
printf("\n");
for (i = 1, k = 1; k <= *(*(a + isleapyear(obj)) + obj->m); i++, n++)
{
if (i <= (days + 6) % 7)
{
printf("%5c", 32);
}
else
{
if (k == obj->d)
{
printf("[%3d]", k++);
}
else
{
printf("%5d", k++);
}
}
if (!(n % 7))
{
printf("\n");
}
}
printf("\nYou put the number:%d-%d-%d\n",obj->y,obj->m,obj->d);
printf("Tell me the next step is:");
}
int choose(char s)
{
int i = 0;
for(i = 0; i < 5; i++)
{
if(s == *(d + i))
{
return i;
}
}
return -1;
}
int returncho(Data * obj,int n)
{
int t = 1;
switch(n)
{
case 0:obj->y--;break;
case 1: obj->y++;break;
case 2:obj->m--;
if (obj->m <= 0)
{
obj->m = 12;
obj->y--;
}
break;
case 3:
obj->m++;
if (obj->m > 12)
{
obj->m= 1;
obj->y++;
}
break;
case 4:t = 0;break;
}
return t;
}
void _getchar(Data * obj)
{
int t = 1;
char a;
while (t)
{
clrscr();
showwnl(obj);
a = getch();
t = returncho(obj,choose(a));
}
clrscr();
printf("Thank you very much!enjoy it! By:wai1216\n");
}
页:
[1]