源程序如下:
#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std
int main()
{
int s[2][13] = { 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, }
int year = 0
int month = 0
int day = 0
int n, i, daytemp
int flag = 0
int nyear = 0, nmonth = 0, nday = 0
printf("输入年月日正旦\n")
scanf("%d%d%d", &year, &month, &day)
//printf("输入天数\n")
//scanf("%d",&n)
n = 1
if (year <0 || month <0 || month>12 || day <1 || n <0)
{
printf("核姿输入数据错误\n")
return 1
}
daytemp = day + n//累加天数
if ((year % 4 == 0 &&year % 100 != 0) || year % 400 == 0)//闰年置1
flag = 1
if (day >s[flag][month])
{
printf("输入日期与年月不符.\n")
return 1
}
if (daytemp <= s[flag][month])//当前日期加天数在本月
{
nyear = year
nmonth = month
nday = daytemp
}
else
{
i = month
nyear = year;
while (daytemp >s[flag][i])//循环递减,直到当前日期加天数在本月
{
daytemp = daytemp - s[flag][i]
i++
if (i >12)//超过一年,年累加
{
nyear++
if ((nyear % 4 == 0 &&nyear % 100 != 0) || nyear % 400 == 0)
flag = 1
else
flag = 0
i = i - 12
}
}
nmonth = i
nday = daytemp
}
printf("%d年%d月%d日第%d天后是:\n", year, month, day, n)
printf("%d年%d月%d日\n", nyear, nmonth, nday)
return 0
}
程序运行结果如下:
扩改清绝展资料:
其他实现方式:
int monthsize(int year, int month) {
int days
if (month == 2) {
if (year % 4 == 0 &&year % 100 != 0 || year % 400 == 0)
return 29
return 28
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days = 31break
case 4:
case 6:
case 9:
case 11: days = 30break
}
return days
}
int main()
int year, month, day, days
printf("年 月 日:")
while (scanf("%d%d%d", &year, &month, &day) == 3) {
days = monthsize(year, month)
if (days == day) {
if (month == 12) {
++year
month = 1
day = 1
}
else ++month
}
else ++day
printf("第二天是:%d/%02d/%02d\n", year, month, day)
printf("年 月 日(q to quit):")
}
return 0
}
先做一个函数,计算1900年1月1日到该天的天数,然后用膜除求出星期几并输出。
具体代码如下:
#include <stdio.h>#include <stdlib.h>
int Day(int y,int 镇如m,int d)
int main(int argc, char *argv[]) {
int i 御掘启 //循环变量
int year,month,day //读取用户输入的年月日
int days //储存从1900-1-1到该天的天数
int week=0 //储存求余后的星期
//字符串组合输出星期
char b[]={"星期"}
char c[7][4]={"日","一","二","三","四","五","六"}
loop:printf("请输入年 月 日:(空格分隔)\n")
scanf(" %d%d%d",&year,&month,&day)
days=Day(year,month,day)
//增强程序的健壮性
if(days==-1)
{
printf("输入格式有误,")
fflush(stdin) //防止用户输入字母等其他非数字字符
goto loop
}
else
{
week=days%7 //用求余计算星期几
printf("%d年%d月%d日是%s%s\n",year,month,day,b,c[week])
}
system("pause")
return 0
}
//函数功能:输入年月日,输出1900-1-1到该天的天数,散升-1位输入不合法
int Day(int y,int m,int d)
{
int i
int r=0 //储存1900到该天的闰年个数
int a[]={31,28,31,30,31,30,31,31,30,31,30} //m给每出月的天数
if(y>=1900 && m>0 && m<13 && d>0 && d<32)
{
for(i=1900i<=yi++) //计算闰年的个数
{
if(i%400==0||i%100!=0&&i%4==0)
r++
}
for(i=0i<m-1i++) //统计当年1月1日到当月的天数
{
d+=a[i]
}
if((y%400==0||y%100!=0&&y%4==0)&&m<3) //如果当年是闰年但不到二月就不能算进去
r--
return 365*(y-1900)+r+d
}
else
return -1
}
下面是这段代码,配合注释体会一下吧#include <stdio.h>
void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
int main()
{
int year,yearDay,month,day
scanf("%d%d",&year,&yearDay)//毁笑输入年份和天数
MonthDay(year,yearDay,&month,&day)
printf("%d-%d-%d",year,month,day)
return 0
}
void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}//这个数组保存每月的天数
if(year%4==0&&year%100!=0||year%400==0)//判断是否为闰年
days[1]=29//闰年的二月有29天
*pMonth=1//从一月开始看
while(yearDay>days[*pMonth-1])//如纤老含果哪一天超含乱出这个月
{
yearDay-=days[*pMonth-1]//求出超出此月的天数
(*pMonth)++//继续考虑下一月
}
*pDay=yearDay
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)