可以使用mktime()
函数来确定给定日期的星期几。 该函数有原型:
time_t mktime(struct tm *ptime);
参考以下代码:
#include <stdio.h>
#include <time.h>
int main(void){
const char *day[7] = {
Sunday , Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday
};
const char *month[12] = {
January, February, march, April,
May, June, July, August,
September, October, November, December
};
const char *suffix[] = { st, nd, rd, th };
enum sufindex { st, nd, rd, th } sufsel = th; // Suffix selector
struct tm birthday = {0}; // A birthday time structure
char name[30] = {'\0'};
printf(Enter a name: );
gets_s(name, sizeof(name));
printf(Enter the birthday for %s as day month year integers separated by spaces.
\ne.g. For 1st February 1985 enter 1 2 1985 : , name);
scanf( %d %d %d, &birthday.tm_mday, &birthday.tm_mon, &birthday.tm_year);
birthday.tm_mon -= 1; // Month zero-based
birthday.tm_year -= 1900; // Year relative to 1900
if(mktime(&birthday) == - 1)
{
fprintf(stderr, Operation Failed.\n);
return -1;
}
switch(birthday.tm_mday)
{
case 1: case 21: case 31:
sufsel= st;
break;
case 2: case 22:
sufsel= nd;
break;
case 3: case 23:
sufsel= rd;
break;
default:
sufsel= th;
break;
}
printf(%s was born on the %d%s %s %d, which was a %s.\n, name,
birthday.tm_mday, suffix[sufsel], month[birthday.tm_mon],
1900 + birthday.tm_year, day[birthday.tm_wday]);
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。