这道简单的题怎么做,高人指点。

QQ472152323 2011-11-21 11:01:10
已知2011.11.1为星期二,编写一个程序,实现输出从2011.11.1到2020.12.31的日历(提示:要用到闰年的判断算法)。
...全文
127 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liutengfeigo 2011-11-21
  • 打赏
  • 举报
回复
是我的话也用系统api
自信男孩 2011-11-21
  • 打赏
  • 举报
回复
这样的问题自己可以尝试一下写一写,如果遇到什么问题自己也可以思考一下。遇到bug也可以调试一下,当然看完别人的代码你就需要跟着别人的思路去想问题了,这样我觉得不是很好。当然看别人的代码也能学到很多知识,和解决问题的方法。但是,没有自己动手写出来的收获多吧。
qq120848369 2011-11-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qq120848369 的回复:]

请用系统API。
C/C++ code
NAME
asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r - transform date and time to broken-down time or ASCII

SYNOPSIS
#include……
[/Quote]

time 获取当前time_t ,然后每次+24*60*60秒,格式化一次时间即可。
qq120848369 2011-11-21
  • 打赏
  • 举报
回复
请用系统API。
NAME
asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r - transform date and time to broken-down time or ASCII

SYNOPSIS
#include <time.h>

char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

time_t mktime(struct tm *tm);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

asctime_r(), ctime_r(), gmtime_r(), localtime_r():
_POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE

DESCRIPTION
The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar time. When interpreted as an
absolute time value, it represents the number of seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

The asctime() and mktime() functions both take an argument representing broken-down time which is a representation separated into year, month,
day, etc.

Broken-down time is stored in the structure tm which is defined in <time.h> as follows:

struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};

liu1317 2011-11-21
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

void OutPut(int,int,int); /*输出*/
int DayNum(int,int); /*计算从公元1月1日到所输入的时间的前一个月的最后一天的总天数*/
int Distinguish(int,int);/*判断月份的类型,确定此月的天数*/
char Content();/*输出一目录,提示用户选择后序的操作,并返回用户的选择*/

void main()
{
char r; //用于记录用户的选择。
int year,month;
printf("请输入年份:");
scanf("%d",&year);
printf("请输入月份:");
scanf("%d",&month);

r='?';
do
{
system("cls"); //清屏
if(year<1 || month<1 || month>12)
{
printf("输入有误,请重新输入!\n");
printf("请输入年份:");
scanf("%d",&year);
printf("请输入月份:");
scanf("%d",&month);
}
else
{
printf("%d年%d月:\n",year,month);
OutPut(DayNum(year,month),year,month);

r=Content();
switch(r)/*根据用户的选择进行相应的处理*/
{
case '>':
month=month+1;
if(month>=13)
{
year=year+1;
month=month%12;
}
break;
case '<':
month=month-1;
if(month<=0)
{
year=year-1;
month=month+12;
}
break;
case '?':
printf("\n\n");
printf("请输入年份:");
scanf("%d",&year);
printf("请输入月份:");
scanf("%d",&month);
break;
}
}
}while(r!='#');

system("cls");
printf("按任意键退出...\n");
getchar();
}

int DayNum(int year,int month)
{
int total;
int a,b,c=0,i;

a=(year-1)*365;
b=(year-1)/4-(year-1)/100+(year-1)/400; //闰年的个数。

total=a+b;

if(month==1)
return total;

for(i=1;i<=month-1;i++) //计算从一月到输入月份前一月的最后一天的总天数。
c+=Distinguish(year,i);

total=total+c;
return total;
}
void OutPut(int days,int year,int month)
{
int i,k=0,n;

printf("日\t一\t二\t三\t四\t五\t六\t\n");
printf("------------------------------------------------------------\n");

k=(int)(days+1)%7;

for(i=1;i<=k;i++)
printf(" \t");

n=Distinguish(year,month);

for(i=1;i<=n;i++)
{
printf("%d\t",i);
k=k+1;
if(k%7==0) //每行七个
{
printf("\n");
printf("------------------------------------------------------------\n");
}
}
printf("\n");
}
int Distinguish(int year,int month)
{
int c=0;

if(month==2)
{
if((year%4==0&&year%100!=0)||(year%400==0))//验证是否为闰年。
c=29;
else
c=28;
}

switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
c=31;
break;
case 4:
case 6:
case 9:
case 11:
c=30;
break;
}
return c;

}
char Content()
{
char ch;
printf("-----------------------\n");
printf(" 选项 \n");
printf(" > 下一个月 \n");
printf(" < 上一个月 \n");
printf(" ? 自定义年月 \n");
printf(" # 退出 \n");
printf("-----------------------\n\n");
printf("请选择:");

fflush(stdin);
ch=getchar();
return ch;
}


这是我们老师写的 不知道能不能对你有帮助 我也不会 一起探讨吧
心死 2011-11-21
  • 打赏
  • 举报
回复
用系统api吧

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧