Ⅰ c语言 输入一个时间(年、月、日、时、分、秒),判断时间是否合法,输出下一秒的时间
#include<stdio.h>
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
int main()
{
void inputDate(); /*输入年-月-日 时:分:秒*/
void nextSceond(); /*计算下一秒的时间*/
int leapYear(int year); /*判断是否为闰年*/
int dayMonth(int month); /*返回每个月份对应的天数*/
inputDate();
leapYear(year);
dayMonth(month);
nextSceond();
system("PAUSE");
return 0;
}
/*函数inputDate()输入年-月-日 时:分:秒*/
void inputDate()
{
int loop;
for(loop = 0; loop < 3; loop++)
{
printf("请输入年-月-日 时:分:秒:");
scanf("%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
if(month < 1 || month > 12)
{
printf(" 月份输入错误! ");
continue;
}
else if(day < 1 || day > dayMonth(month))
{
printf(" 日期输入错误! ");
continue;
}
else if(hour < 0 || hour > 23)
{
printf(" 小时输入错误! ");
continue;
}
else if(minute < 0 || minute > 59)
{
printf(" 分钟输入错误! ");
continue;
}
else if(second < 0 || second > 59)
{
printf(" 秒数输入错误! ");
continue;
}
else
{
break;
}
}
}
/*函数nextSecond()计算下一秒的时间*/
void nextSceond()
{
if(59 == second)
{
minute += 1;
second = 0;
if(60 == minute)
{
hour += 1;
minute = 0;
if(24 == hour)
{
day += 1;
hour = 0;
if(day > dayMonth(month))
{
month += 1;
day = 1;
if(13 == month)
{
year += 1;
month = 1;
}
}
}
}
}
else
{
second += 1;
}
printf("%d-%d-%d %d:%d:%d ",year, month, day, hour, minute, second);
}
/*函数leapYear(int year)判断是否为闰年*/
int leapYear(int year)
{
if(0 == (year % 4 && 0 != year % 100) || 0 == year % 400)
{
return 1;
}
else
{
return 0;
}
}
/*函数名dayMonth(int month)返回每个月份对应的天数*/
int dayMonth(int month)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if(0 == (year % 4 && 0 != year % 100) || 0 == year %400)
{
return 29;
}
else
{
return 28;
}
case 4:
case 6:
case 9:
case 11:
return 30;
}
}
请放心使用
有问题的话请追问
满意请及时采纳,谢谢
Ⅱ 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;
}
Ⅳ C语言;输入任意年月日,得出周几
#include"stdio.h"
struct Date
{
int year;
int month;
int day;
};
int f(int x)
{
if(x%4==0&&x%100!=0||x%400==0) return 366;
else return 365;
}
int isleap(int x)
{
if(x%4==0&&x%100!=0||x%400==0) return 1;
else return 0;
}
int s(int a,int b,int c,int d,int year)
{
int array[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}};
int cha=0,i;
if(a<c||a==c&&b<d)
{
for(i=a;i<c;i++)
cha+=array[isleap(year)][i-1];
cha+=d-b;
return cha;
}
else
{
for(i=c;i<a;i++)
cha+=array[isleap(year)][i-1];
cha+=b-d;
return -cha;
}
}
int tianshu(struct Date a,struct Date b)//计算两个日期相差的天数
{
int i,tian=0;
if(a.year<b.year)
{
for(i=a.year;i<b.year;i++)
tian+=f(i);
tian+=s(a.month,a.day,b.month,b.day,b.year);
}
else
{
for(i=a.year;i<b.year;i++)
tian-=f(i);
tian+=s(a.month,a.day,b.month,b.day,b.year);
}
return tian;
}
void jieguo(int a)
{
if(a==0) printf("这一天是星期天\n");
if(a==1) printf("这一天是星期一\n");
if(a==2) printf("这一天是星期二\n");
if(a==3) printf("这一天是星期三\n");
if(a==4) printf("这一天是星期四\n");
if(a==5) printf("这一天是星期五\n");
if(a==6) printf("这一天是星期六\n");
if(a==7) printf("这一天是星期天\n");
}
int main()
{
int tian;
struct Date now={2009,4,12};//选择一天作为基准
struct Date chaxun;
printf("请输入你想查询的日期\n");
scanf("%d%d%d",&chaxun.year,&chaxun.month,&chaxun.day);
tian=tianshu(now,chaxun);
if(tian>=0)
{
jieguo(tian%7);
}
else
{
jieguo(7-((-tian)%7));
}
return 0;
}
Ⅳ c语言中这么输入一个时间数据,用汉字表示输出时间
#include <stdio.h>
#include <windows.h>
int main()
{
SYSTEMTIME Stime,Etime;
GetLocalTime(&Stime);//取得开始计时时间
Sleep(5000);//停5秒
Ⅵ 用C语言编写一个程序输出任意时间下一秒的时间
#include "stdio.h"
#include "conio.h"
int main(void)
{
int s;
int f;
int m;
printf("请输入一个时间: ");
scanf("%i %i %i",&s,&f,&m);
if(m==59){
m=0;
f=f++;
printf("%i %i %i ",s,f,m);
}else {
m=m++;
printf("%i %i %i ",s,f,m);
}
getch();
}
或:
#include <stdio.h>
#include <time.h>
int main(void)
{
int i=123456789;
clock_t start, end;
start = clock();
while(i--);
end = clock();
printf("The time was: %d ", (end - start));//单位是毫秒,注意是%d,不再是%f
printf("The time was: %f ", (double)(end - start) / CLK_TCK); //单位是秒
return 0;
}
(6)c语言可以随意输入时间吗扩展阅读:
C的数据类型包括:整型(short,int,long,long long)、字符型(char)、实型或浮点型(单精度float和双精度double)、枚举类型(enum)、数组类型、结构体类型(struct)、共用体类型(union)、指针类型和空类型(void)。
变量是以某标识符为名字,其值可以改变的量。标识符是以字母或下划线开头的一串由字母、数字或下划线构成的序列,请注意第一个字符必须为字母或下划线,否则为不合法的变量名。变量在编译时为其分配相应存储单元。
Ⅶ c语言中时间怎么输入
Sleep()//可以用这个函数,是暂时执行...()内为要暂停的时间,Sleep(500)为0.5秒
Ⅷ C语言,计算旅途的时间,输入任意时间~找错~麻烦大仙了
#include<stdio.h>
#include<iostream.h>
int main()
{
int time1,time2,hours,mins;
int h1,h2,m1,m2;
printf("请输入两个时间\n");
scanf("%d%d",&time1,&time2);
h1=time1/60;
h2=time2/60;
m1=time1%60;
m2=time2%60;
if(h1>h2)
{
if(m1<m2)
{
hours=h1-h2-1;
mins=m1-m2+60;
}
else
{
hours=h1-h2;
mins=m1-m2;
}
}
else
{
if(m1>m2)
{
if(h1==h2)
{
hours=h2-h1,mins=m1-m2;
}
else
{
hours=h2-h1-1,mins=m2-m1+60;
}
}
else
{
if(h1==h2)
{
hours=h2-h1,mins=m2-m1;
}
else
{
hours=h2-h1,mins=m2-m1;
}
}
}
printf("%d,%d\n",hours,mins);
system("pause");
return 0;
}
楼主你看看,差点被你误导了,是除以60,不是100!!!!
Ⅸ C语言怎么输入日期求大神指导!
scanf("%d-%d-%d", &y, &m, &d);
printf("%d/%d/%d\n", y, m, d);
Ⅹ 关于C语言中,如何对时间进行输入,处理和输出。
#include <stdio.h>
#include <windows.h>
int main()
{
SYSTEMTIME Stime,Etime;
GetLocalTime(&Stime);//取得开始计时时间
Sleep(5000);//停5秒
GetLocalTime(&Etime);//结束计时时间
//结束减去开始就是间隔了
printf("间隔:%d分%d秒\n",Etime.wMinute-Stime.wMinute,Etime.wSecond-Stime.wSecond);
return 0;
}//本程序是调用WindowsAPI实现的