C语言输入年份和天数输出对应的年月日

C语言输入年份和天数输出对应的年月日,第1张

C语言输入年份和天数输出对应的年月日的源代码如下:

#include <iostream>

int day(int &year, int &month);

int main()

{

int year{};

int month{};

std::cout << "请输入年和月(空格隔开):";

std::cin >> year >> month;

std::cout << "该月天数:" << day(year, month) << '\n';

return 0;

}

扩展资料

1、C++ 标准库没有提供所谓的日期类型。C++ 继承了 C 语言用于日期和时间 *** 作的结构和函数。为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。

2、有四个与时间相关的类型:clock_t、time_t、size_t 和 tm。类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数。

用switch选择的:

#include<stdioh>

int main()

{

int y,m;

printf("请依次输入年,月:\n"); 

scanf("%d,%d",&y,&m); //上机时注意打逗号,也可修改为其他格式

switch(m)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:printf("这个月有31天\n");break;

case 2 : if(y%4==0&&y%100!=0||y%400==0) //最基本的判断闰年的条件 

printf("这个月有29天\n");

else printf("这个月有28天\n");break;

case 4:

case 6:

case 9 :

case 11:printf("这个月有30天\n");break;

}

return 0;

}

用指针型列举的:

#include<stdioh>

int a[]={31,28,31,30,31,30,31,31,30,31,30,31},p1,p2,p3,i=1,k=0;

int main()

{

int p1,p2;

printf("请依次输入年·月:\n");

scanf("%d%d",&p1,&p2);

if(p1%4==0&&p1%100!=0||p1%400==0)

a[1]=29;

if(p2>12)

printf("您的输入有误!\n");

printf("这个月的天数为:%d\n",a[p2-1]);

return 0;

}#include <stdioh>

main()

{int a,b;

printf("请输入年份和月份:\n");

scanf("%d%d",&a,&b);

if (a%4==0&&a%100!=0)

if(b==2)

printf("这个月有29天\n");

else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)

printf("这个月有31天\n");

else 

printf("这个月有30天\n");

else if(b==2)

printf("这个月有28天\n");

else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)

printf("这个月有31天\n");

else 

printf("这个月有30天\n");}

普通

#include <stdioh>

main(){

int a,b;

printf("请输入年份和月份:\n");

scanf("%d%d",&a,&b);

if (a%4==0&&a%100!=0)

if(b==2)

printf("这个月有29天\n");

else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)

printf("这个月有31天\n");

else 

printf("这个月有30天\n");

else if(b==2)

printf("这个月有28天\n");

else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)

printf("这个月有31天\n");

else 

printf("这个月有30天\n");}

扩展资料

switch 语句可以处理多分支选择问题,根据其中break 语句的使用方法,一般分三种情况。

在switch 语句的每个语句段中都使用break 语句,这是switch 语句的主要使用方法,一般形式为:

switch (表达式)

case 常量表达式1: 语句段1; break;

case 常量表达式2: 语句段2; break;

case常量表达式n: 语句段n; break;

case常量表达式n+1:语句段n+ 1; break;

default :

参考资料c语言百度经验

获得日期和时间

这里说的日期和时间就是我们平时所说的年、月、日、时、分、秒等信息。从第2节我们已经知道这些信息都保存在一个名为tm的结构体中,那么如何将一个日历时间保存为一个tm结构的对象呢?

其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:

struct

tm

gmtime(const

time_t

timer);

struct

tm

localtime(const

time_t

timer);

其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数

是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,那么我用

localtime()函数在中国地区获得的本地时间会比世界标准时间晚8个小时,即2005年7月30日15点18分20秒。下面是个例子:

#include

"timeh"

#include

"stdioh"

int

main(void)

{

struct

tm

local;

time_t

t;

t=time(NUL);

local=localtime(&t);

printf("Local

hour

is:

%d\n",local->tm_hour);

local=gmtime(&t);

printf("UTC

hour

is:

%d\n",local->tm_hour);

return

0;

}

运行结果是:

Local

hour

is:

15

UTC

hour

is:

7

固定的时间格式

我们可以通过asctime()函数和ctime()函数将时间以固定的格式显示出来,两者的返回值都是char型的字符串。返回的时间格式为:

星期几

月份

日期

时:分:秒

年\n{postcontent}

例如:Wed

Jan

02

02:03:55

1980\n{postcontent}

其中\n是一个换行符,{postcontent}是一个空字符,表示字符串结束。下面是两个函数的原型:

Char

asctime(const

struct

tm

timeptr);

char

ctime(const

time_t

timer);

其中asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样的

话,asctime()函数只是把tm结构对象中的各个域填到时间字符串的相应位置就行了,而ctime()函数需要先参照本地的时间设置,把日历时间转

化为本地时间,然后再生成格式化后的字符串。在下面,如果t是一个非空的time_t变量的话,那么:

printf(ctime(&t));

等价于:

struct

tm

ptr;

ptr=localtime(&t);

printf(asctime(ptr));

那么,下面这个程序的两条printf语句输出的结果就是不同的了(除非你将本地时区设为世界标准时间所在的时区):

#include

"timeh"

#include

"stdioh"

int

main(void)

{

struct

tm

ptr;

time_t

lt;

lt

=time(NUL);

ptr=gmtime(<);

printf(asctime(ptr));

printf(ctime(<));

return

0;

}

运行结果:

Sat

Jul

30

08:43:03

2005

Sat

Jul

30

16:43:03

2005

#include <stdioh>

int main()

{

int year,month,day;//年月日

int judge,i;

int sum=0;//标记天数

int date[2][12]={

{31,28,31,30,31,30,31,31,30,31,30,31},

{31,29,31,30,31,30,31,31,30,31,30,31}};//储存闰年和非闰年每一月的天数

printf("输入年月日:");

scanf("%d %d %d",&year,&month,&day);

judge=(year%4==0)||(year%400==0 && year%100!=0);

//判断年份是不是闰年 是闰年judge就是1;否则就是0

for(i=0;i<month-1;i++)//加month之前的所有天数

sum+=date[judge][i];

sum+=day;//加当天月份的天数day

printf("它是%d年中的第%d天\n",year,sum);

return 0;

}

以上就是关于C语言输入年份和天数输出对应的年月日全部的内容,包括:C语言输入年份和天数输出对应的年月日、C语言编程 从键盘输入一个年份和月份,输出该月有多少天(考虑闰年)、在c语言中如何使用系统函数得到当前的日期等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/web/10072380.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存