A. c语言中年月日之类的代码
int year = 1230;
int month = 4;
int day = 46;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(year%4==0&&year%100!=0||year%400==0)
a[2]++;
if(month>12||month<1||day<1||day>a[month])
{
printf("date error!\n");
return 0;
}
B. c语言中如何输入年月日
就这么写的,没调试,你自己调一下好吧
#include <stdio.h>
void main()
{
int year,month,date;
scanf("%d-%d-%d",&year,&month,&date);
printf("%d-%d-%d",year,month,date);
if(year<0||year>3000)
printf("Error!");
if(month>12||month<1)
printf("Error!");
switch(month)
{
case 1: if(date>31||date<1) printf("Error!");break;
case 2: if((year%4==0)&&(year%100!=0)||(year%400==0))
{
if(date>29||date<1) printf("Error!");
}
else
if(date>28||date<1) printf("Error!");
break;
case 3: if(date>31||date<1) printf("Error!");break;
case 4: if(date>30||date<1) printf("Error!");break;
case 5: if(date>31||date<1) printf("Error!");break;
case 6: if(date>30||date<1) printf("Error!");break;
case 7: if(date>31||date<1) printf("Error!");break;
case 8: if(date>31||date<1) printf("Error!");break;
case 9: if(date>30||date<1) printf("Error!");break;
case 10: if(date>31||date<1) printf("Error!");break;
case 11: if(date>30||date<1) printf("Error!");break;
case 12: if(date>31||date<1) printf("Error!");break;
}
}
C. 用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。
源程序如下:
#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("输入年月日
");
scanf("%d%d%d", &year, &month, &day);
//printf("输入天数
");
//scanf("%d",&n);
n = 1;
if (year < 0 || month < 0 || month>12 || day < 1 || n < 0)
{
printf("输入数据错误
");
return 1;
}
daytemp = day + n;//累加天数
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//闰年置1
flag = 1;
if (day > s[flag][month])
{
printf("输入日期与年月不符.
");
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天后是:
", year, month, day, n);
printf("%d年%d月%d日
", nyear, nmonth, nday);
return 0;
}
程序运行结果如下:
(3)c语言怎样写年月日代码扩展阅读:
其他实现方式:
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 = 31; break;
case 4:
case 6:
case 9:
case 11: days = 30; break;
}
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
", year, month, day);
printf("年 月 日(q to quit):");
}
return 0;
}
D. C语言数组实现输入年月日
根据题意可得代码:
#include<stdio.h>
intisLeapYear(intyear)
{
if((year%4==0&&year%100!=0)||year%400==0)return1;
return0;
}
intmain()
{
intdays_of_month[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}
};
intyear,month,date,days=0,i;
scanf("%d%d%d",&year,&month,&date);
if(isLeapYear(year)){
for(i=0;i<month-1;++i){
days+=days_of_month[1][i];
}
days+=date;
}
else{
for(i=0;i<month-1;++i){
days+=days_of_month[0][i];
}
days+=date;
}
printf("%d ",days);
return0;
}
E. C语言中如何把月日年输出为年月日
这要看原来的程序中,月日年是怎么组织的?如果分别存放在三个变量中,那么只要把这三个变量的输出顺序交换一下就可以了。
例如,
int y,m,dm,d;
printf("请输入月 日 年:");
scanf("%d%d%d",&m,&d,&y);
printf("%d年%d月%d日",y,m,d);
F. C语言编写程序实现:从键盘输入一个年月日,要求出该天是该年的第几天。(注意判断该年是否是闰年。)
#include<stdio.h>
int main(void)
{
int month,year,day,sum,i=0;
printf("输入年月日:\n");
scanf("%d %d %d",&year,&month,&day);
switch(month)
{
case 12: i=i+30;
case 11: i=i+31;
case 10: i=i+30;
case 9: i=i+31;
case 8: i=i+31;
case 7: i=i+30;
case 6: i=i+31;
case 5: i=i+30;
case 4: i=i+31;
case 3: i=i+28;
case 2: i=i+31;
case 1: ;
default:
;
}
sum=i+day;
if(((year%4==0&&year%100!=0)||(year%400==0))&&(month>2))
sum=sum+1;
printf("这一天是 %d 年 第 %d 天\n",year,sum);
}
G. C语言输入年份和天数输出对应的年月日
C语言输入年份和天数输出对应的年月日的源代码如下:
#include<iostream>
intday(int&year,int&month);
intmain()
{
intyear{};
intmonth{};
std::cout<<"请输入年和月(空格隔开):";
std::cin>>year>>month;
std::cout<<"该月天数:"<<day(year,month)<<' ';
return0;
}
(7)c语言怎样写年月日代码扩展阅读
1、C++ 标准库没有提供所谓的日期类型。C++ 继承了 C 语言用于日期和时间操作的结构和函数。为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。
2、有四个与时间相关的类型:clock_t、time_t、size_t和tm。类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数。
H. c语言怎么编写要求输入当前年月日,输入生日年月日,然后计算多少岁(要注意平年二月跟闰年二月的天数
#include<stadio.h>
main()
{ int year,year1,month,month1,day,day1,age;
printf("请输入您的生日:");
scanf("%d %d %d",&year,&month,&day);
printf("请输入当前日期:");
scanf("%d %d %d",&year1,&month1,&day1);
if(year1==year)
age=0;
else
{age=year1-year;
if(month1<month||(month1==month&&day1<day))
age=age-1;
}
printf("您的年龄是:%d",age);
}
大概就是这个样子,我没有调试,可能有错误,但应该都是小问题,你自己调试一下,觉得哪儿不妥,可以自己改动。
I. 编写一个c语言程序,输入一个年月日,输出这个日期是这一年的第几天。(考虑闰年)
#include <stdio.h>
void main()
{
int year=0,month=0,day=0,i=0,sum=0;
while(1)
{
printf("请输入年份:");
scanf("%d",&year);
printf("请输入月份:");
scanf("%d",&month);
printf("请输入日期:");
scanf("%d",&day);
i=0;//i变量是为了控制月和日的格式是否输入正确;
switch(month)//判断每个月的日期是否输入正确;
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day>0&&day<=31)
{
i=1;
}
break;
case 4:
case 6:
case 9:
case 11:
if(day>0&&day<=30)
{
i=1;
}
break;
case 2://2月闰年可以输入29日;
if((year%4==0&&year%100!=0)||year%400==0)
{
if(day>0&&day<=29)
{
i=1;
}
}
else
{
if(day>0&&day<=28)
{
i=1;
}
}
break;
default://如果输入不是1-12月份,那么月份输入有误;
i=0;
break;
}
if(i)//如果i变量为1,那么输入正确,否则重新输入;
{
break;
}
else
{
printf("请输入正确的年月日!!! ");
}
}
i=0;
switch(month)//把每个月的日期累加;
{
case 12:
i=i+30;
case 11:
i=i+31;
case 10:
i=i+30;
case 9:
i=i+31;
case 8:
i=i+31;
case 7:
i=i+30;
case 6:
i=i+31;
case 5:
i=i+30;
case 4:
i=i+31;
case 3:
if((year%4==0&&year%100!=0)||year%400==0)
{
i=i+29;
}
else
{
i=i+28;
}
case 2:
i=i+31;
case 1:
;
}
sum=i+day;
printf("%d年%d月%d日是%d年的第%d天 !",year,month,day,year,sum);
}
拓展资料
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。
尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。