编程题 输入年月日格式yyyymmdd,判断是否是周末,只要求对2005年判断。
在文本框中输入年月日格式yyyymmdd的生日,输出其生肖并判断是否是周末http://zhidao.baidu.com/question/97053204.html...
在文本框中输入年月日格式yyyymmdd的生日,输出其生肖并判断是否是周末
http://zhidao.baidu.com/question/97053204.html 展开
http://zhidao.baidu.com/question/97053204.html 展开
2个回答
展开全部
我以前写的代码,你参考一下即可,对于你这个问题用GetWeekDay应该就能解决了
#include <stdio.h>
#include <string.h>
#include <time.h>
const char *Week[]=
{
"星期天",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
};
#if 0
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
#endif
void GetWeekDay(int year,int num);
void GetSomeDay(int year, int mon, int day);
void GetNextTime(int year, int mon, int day,int num);
int main()
{
GetWeekDay(2007,337);
GetSomeDay(2007,12,4);
GetNextTime(2005,12,4,0);
return 0;
}
void GetSomeDay(int year, int mon, int day)
{
time_t t;//存储需要计算时间距1900的秒数
struct tm test ;//填充这个结构
memset(&test, 0, sizeof(tm));//先清空下
test.tm_year = year - 1900;
test.tm_mon = mon -1;
test.tm_mday = day ;
t = mktime(&test);// 转换成秒
struct tm *now = localtime(&t);//再转回tm
printf("%d\n", now->tm_yday);//第几天
printf("%s\n", Week[now->tm_wday]);//星期几
}
void GetNextTime(int year, int mon, int day,int num)
{
struct tm now, tmp;
time_t t;
memset(&now, 0 , sizeof(struct tm));
memset(&tmp, 0 , sizeof(struct tm));
now.tm_year = year-1900; //指定年
now.tm_mon = mon -1;//指定月份
now.tm_mday = day + num; //指定是哪一天
t = mktime(&now);//转换成秒
struct tm *pNext = localtime(&t);
printf("%s\n", asctime(pNext));//哪一天
printf("%s\n", Week[pNext->tm_wday]);//星期几
}
void GetWeekDay(int year,int num)
{
time_t t;//存储需要计算时间距1900的秒数
struct tm test ;//填充这个结构
memset(&test, 0, sizeof(tm));//先清空下
test.tm_year = year - 1900;
test.tm_mday = num +1;
t = mktime(&test);// 转换成秒
struct tm *now = localtime(&t);//再转回tm
printf("%d\n", now->tm_yday);//第几天
printf("%s\n", Week[now->tm_wday]);//星期几
}
#include <stdio.h>
#include <string.h>
#include <time.h>
const char *Week[]=
{
"星期天",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
};
#if 0
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
#endif
void GetWeekDay(int year,int num);
void GetSomeDay(int year, int mon, int day);
void GetNextTime(int year, int mon, int day,int num);
int main()
{
GetWeekDay(2007,337);
GetSomeDay(2007,12,4);
GetNextTime(2005,12,4,0);
return 0;
}
void GetSomeDay(int year, int mon, int day)
{
time_t t;//存储需要计算时间距1900的秒数
struct tm test ;//填充这个结构
memset(&test, 0, sizeof(tm));//先清空下
test.tm_year = year - 1900;
test.tm_mon = mon -1;
test.tm_mday = day ;
t = mktime(&test);// 转换成秒
struct tm *now = localtime(&t);//再转回tm
printf("%d\n", now->tm_yday);//第几天
printf("%s\n", Week[now->tm_wday]);//星期几
}
void GetNextTime(int year, int mon, int day,int num)
{
struct tm now, tmp;
time_t t;
memset(&now, 0 , sizeof(struct tm));
memset(&tmp, 0 , sizeof(struct tm));
now.tm_year = year-1900; //指定年
now.tm_mon = mon -1;//指定月份
now.tm_mday = day + num; //指定是哪一天
t = mktime(&now);//转换成秒
struct tm *pNext = localtime(&t);
printf("%s\n", asctime(pNext));//哪一天
printf("%s\n", Week[pNext->tm_wday]);//星期几
}
void GetWeekDay(int year,int num)
{
time_t t;//存储需要计算时间距1900的秒数
struct tm test ;//填充这个结构
memset(&test, 0, sizeof(tm));//先清空下
test.tm_year = year - 1900;
test.tm_mday = num +1;
t = mktime(&test);// 转换成秒
struct tm *now = localtime(&t);//再转回tm
printf("%d\n", now->tm_yday);//第几天
printf("%s\n", Week[now->tm_wday]);//星期几
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询