文章目录
- 1.给出年月日,计算该日是星期几
- 2.给出年份,计算该年元旦那天是星期几
- 3.计算某年某月某日是本年度第几天
- 4.求指定日期距离出生日期的天数
- 5.求指定日期距离1970年1月1日的天数
1.给出年月日,计算该日是星期几
#include
#include
int main(){
int year,month,day;
int numberOfDays;
int week;
int num;
printf("请输入日期(格式:2022 3 6):");
scanf("%d%d%d",&year,&month,&day);
switch(month){
case 12:
numberOfDays += 30;
case 11:
numberOfDays += 31;
case 10:
numberOfDays += 30;
case 9:
numberOfDays += 31;
case 8:
numberOfDays += 31;
case 7:
numberOfDays += 30;
case 6:
numberOfDays += 31;
case 5:
numberOfDays += 30;
case 4:
numberOfDays += 31;
case 3:
if((year % 4 == 0 && year / 100 != 0) || (year % 400 == 0)){
numberOfDays += 29;
}else{
numberOfDays += 28;
}
case 2:
numberOfDays += 31;
case 1:
numberOfDays += day;
break;
default:
printf("输入年份有误!\n");
exit(0);
}
num = year - 1 + (year - 1)/4 - (year - 1)/100 + (year - 1)/400 + numberOfDays;
week = num % 7;
printf("\n");
switch(week){
case 0:
printf("%d年%d月%d日,是星期天\n",year,month,day);
break;
case 1:
printf("%d年%d月%d日,是星期一\n",year,month,day);
break;
case 2:
printf("%d年%d月%d日,是星期二\n",year,month,day);
break;
case 3:
printf("%d年%d月%d日,是星期三\n",year,month,day);
break;
case 4:
printf("%d年%d月%d日,是星期四\n",year,month,day);
break;
case 5:
printf("%d年%d月%d日,是星期五\n",year,month,day);
break;
case 6:
printf("%d年%d月%d日,是星期六\n",year,month,day);
break;
}
return 0;
}
2.给出年份,计算该年元旦那天是星期几
#include
int main(){
int year;
int week;
printf("请输入年份(如:2022):");
scanf("%d",&year);
week = (year + (year - 1)/4 - (year - 1)/100 + (year - 1)/400)%7;
switch(week){
case 0:
printf("%d年,是星期天\n",year);
break;
case 1:
printf("%d年,是星期一\n",year);
break;
case 2:
printf("%d年,是星期二\n",year);
break;
case 3:
printf("%d年,是星期三\n",year);
break;
case 4:
printf("%d年,是星期四\n",year);
break;
case 5:
printf("%d年,是星期五\n",year);
break;
case 6:
printf("%d年,是星期六\n",year);
break;
}
return 0;
}
3.计算某年某月某日是本年度第几天
#include
#include
int sumDays(int year,int month,int day);
bool isLeap(int year);
int main(){
int year,month,day;
int numberOfDays;
printf("请输入年月日(如:2022 3 6):");
scanf("%d%d%d",&year,&month,&day);
numberOfDays = sumDays(year,month,day);
printf("\n%d年%d月%d日,是%d年的第%d天\n",year,month,day,year,numberOfDays);
return 0;
}
int sumDays(int year,int month,int day){
int daysOfTheMonth[12] = {0,31,28,31,30,31,30,31,31,30,31,30};
bool monthFlag = false;
bool dayFlag = false;
for(int i = 1;i <= 12;i++){
if(month == i){
monthFlag = true;
}
}
if(month == 1 || month == 3 || month == 5 ||
month == 7 || month == 8 || month == 10 || month == 12){
for(int i = 1; i <= 31;i++){
if(day == i){
dayFlag = true;
}
}
}else if(month == 2){
if(isLeap(year)){
for(int i = 1; i <= 29;i++){
if(day == i){
dayFlag = true;
}
}
}else{
for(int i = 1; i <= 28;i++){
if(day == i){
dayFlag = true;
}
}
}
}else{
for(int i = 1; i <= 30;i++){
if(day == i){
dayFlag = true;
}
}
}
if(!monthFlag || !dayFlag){
printf("传入参数有误!请重新检查!\n");
exit(0);
}
for(int i = 1; i < month;i++){
if(i == 2){
if(isLeap(year)){
day += 29;
continue;
}
}
day += daysOfTheMonth[i];
}
return day;
}
bool isLeap(int year){
return (year % 4 == 0 && year / 100 != 0) || (year % 400 == 0);
}
4.求指定日期距离出生日期的天数
#include
#include
typedef struct datatime{
int year;
int month;
int day;
}DATETIME;
int totalDays(DATETIME date,int birth_year);
int currentYearDays(DATETIME date);
bool isLeap(int year);
bool isMonthError(int month);
bool isDayError(int year,int month,int day);
int main(){
DATETIME specifiedDate;
DATETIME birthday;
printf("请输入现在的日期(如:2022-3-6):");
scanf("%d-%d-%d",&specifiedDate.year,&specifiedDate.month,&specifiedDate.day);
printf("\n请输入出生的日期(如:2000-8-21):");
scanf("%d-%d-%d",&birthday.year,&birthday.month,&birthday.day);
int overall_days;
overall_days = totalDays(specifiedDate,birthday.year);
int birth_before = currentYearDays(birthday);
int birth_to_today = overall_days - birth_before;
if(birth_to_today < 0){
printf("还未出生呢~!\n");
exit(0);
}
int age = birth_to_today/365;
printf("\n现在%d年%d月%d日,你已经%d岁了!(%d天)\n",specifiedDate.year,specifiedDate.month,
specifiedDate.day,age,birth_to_today);
return 0;
}
int totalDays(DATETIME date,int birth_year){
int total_day = 0;
for(int oldYear = birth_year;oldYear < date.year;oldYear++){
if(isLeap(date.year)){
total_day += 366;
}else{
total_day += 365;
}
}
total_day += currentYearDays(date);
return total_day;
}
int currentYearDays(DATETIME date){
int daysOfTheMonth[12] = {0,31,28,31,30,31,30,31,31,30,31,30};
int sum_days = 0;
if(!isMonthError(date.month) || !isDayError(date.year,date.month,date.day)){
printf("传入参数有误!请重新检查!\n");
exit(0);
}
for(int i = 1; i < date.month;i++){
if(i == 2){
if(isLeap(date.year)){
sum_days += 29;
continue;
}
}
sum_days += daysOfTheMonth[i];
}
sum_days += date.day;
return sum_days;
}
bool isMonthError(int month){
for(int i = 1;i <= 12;i++){
if(month == i){
return true;
}
}
return false;
}
bool isDayError(int year,int month,int day){
if(month == 1 || month == 3 || month == 5 ||
month == 7 || month == 8 || month == 10 || month == 12){
for(int i = 1; i <= 31;i++){
if(day == i){
return true;
}
}
}else if(month == 2){
if(isLeap(year)){
for(int i = 1; i <= 29;i++){
if(day == i){
return true;
}
}
}else{
for(int i = 1; i <= 28;i++){
if(day == i){
return true;
}
}
}
}else{
for(int i = 1; i <= 30;i++){
if(day == i){
return true;
}
}
}
return false;
}
bool isLeap(int year){
return (year % 4 == 0 && year / 100 != 0) || (year % 400 == 0);
}
5.求指定日期距离1970年1月1日的天数
#include
#include
typedef struct datatime{
int year;
int month;
int day;
}DATETIME;
long long totalDays(DATETIME date);
int currentYearDays(DATETIME date);
bool isLeap(int year);
bool isMonthError(int month);
bool isDayError(int year,int month,int day);
int main(){
DATETIME specifiedDate;
printf("请输入日期(如:2022-3-6):");
scanf("%d-%d-%d",&specifiedDate.year,&specifiedDate.month,&specifiedDate.day);
printf("\n%d年%d月%d日距离1970年1月1日,有%lld天\n",specifiedDate.year,
specifiedDate.month,specifiedDate.day,totalDays(specifiedDate));
printf("\n%d年%d月%d日距离1970年1月1日,有%lld毫秒\n",specifiedDate.year,
specifiedDate.month,specifiedDate.day,totalDays(specifiedDate)*24*60*60*1000);
return 0;
}
long long totalDays(DATETIME date){
int total_day = 0;
for(int oldYear = 1970;oldYear < date.year;oldYear++){
if(isLeap(date.year)){
total_day += 366;
}else{
total_day += 365;
}
}
total_day += currentYearDays(date);
return total_day;
}
int currentYearDays(DATETIME date){
int daysOfTheMonth[12] = {0,31,28,31,30,31,30,31,31,30,31,30};
int sum_days = 0;
if(!isMonthError(date.month) || !isDayError(date.year,date.month,date.day)){
printf("传入参数有误!请重新检查!\n");
exit(0);
}
for(int i = 1; i < date.month;i++){
if(i == 2){
if(isLeap(date.year)){
sum_days += 29;
continue;
}
}
sum_days += daysOfTheMonth[i];
}
sum_days += date.day;
return sum_days;
}
bool isMonthError(int month){
for(int i = 1;i <= 12;i++){
if(month == i){
return true;
}
}
return false;
}
bool isDayError(int year,int month,int day){
if(month == 1 || month == 3 || month == 5 ||
month == 7 || month == 8 || month == 10 || month == 12){
for(int i = 1; i <= 31;i++){
if(day == i){
return true;
}
}
}else if(month == 2){
if(isLeap(year)){
for(int i = 1; i <= 29;i++){
if(day == i){
return true;
}
}
}else{
for(int i = 1; i <= 28;i++){
if(day == i){
return true;
}
}
}
}else{
for(int i = 1; i <= 30;i++){
if(day == i){
return true;
}
}
}
return false;
}
bool isLeap(int year){
return (year % 4 == 0 && year / 100 != 0) || (year % 400 == 0);
}
完