用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。

用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。,第1张

源程序如下:

#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

}

加入以下代码:

Private Sub Form_Load()

Text1.Text = Now

Timer1.Enabled = True

Timer1.Interval = 1000

End Sub

Private Sub Timer1_Timer()

Text1.Text = Now

End Sub

扩展资料:

VB获取系统当前时间并格式化输出

用到的函数:

Date函数:返回系统的日期

Now函数:返回系统的日期和时间

Format函数:根据格式表达式来格式化数据

实例:

Private Sub Form_Load()

'设置同步时间显示

Timer1.Interval = 1000

Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()

Label2.Caption = Format(Now, "hh:mm:ss")            '获取当前时间

Label4.Caption = Format(Now, "yyyy-mm-dd")          '获取当前日期

Label6.Caption = Format(Now, "yyyy-mm-dd hh:mm:ss") '日期和时间

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7927501.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存