展开全部
需要做一些合理的假设:
1 输入时间采用这样的格式 yyyy-mm-dd hh:mm:ss 即年月日.时分秒
2 系统时间是指本地(本时区的时间)
基于这些,代码如下:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char Time[256] = "";
printf("Please input a time in this format yyyy-mm-dd hh:mm:ss:\n");
scanf("%s",Time);
unsigned int year,mon,day,hour,min,sec;
sscanf(Time,"%d-%d-%d %d:%d:%d",&year,&mon,&day,&hour,&min,&sec);
time_t timep;
struct tm *p;
time(&timep);//获取当前时间
p = localtime(&timep); //取得当地时间
unsigned int dayin,daysys,tickin,ticksys;
dayin = year*10000+mon*100+day;
daysys = p->tm_year*10000+p->tm_mon*100+p->tm_mday;
if(dayin > daysys) {
printf("%s is big\n",Time);
return 0;
}else if(dayin<daysys) {
printf("%d-%d-%d %d:%d:%d is big\n",
p->tm_year,p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
return 0;
}
//日期相等
tickin = hour*3600+min*60+sec;
ticksys = p->tm_hour*3600+p->tm_min*60+p->tm_sec;
if(tickin>ticksys){
printf("%s is big\n",Time);
return 0;
}else{
printf("%d-%d-%d %d:%d:%d is big\n",
p->tm_year,p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
return 0;
}
//时间也相等
printf("time is equal!\n");
return 0;
}
展开全部
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class Ctime//时间的类
{
private:
int hour;
int minute;
int second;
public:
Ctime();
void settime(int h,int m,int s);//时间设置函数
void settime(char *p_time);
void comptime(const Ctime system_time);//比较两个时间大小,并且输出较大的时间
};
Ctime::Ctime()
{
hour=0;
minute=0;
second=0;
}
void Ctime::settime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void Ctime::settime(char *p_time)
{
char *ph,*pm,*ps;
ph=strtok(p_time,":");//strtok函数的作用是获取p_time内存位置处“:”之前的字符
pm=strtok(0,":");//由于字符数组已经被拆分,所以头位置为当前位置为0;
ps=strtok(0," ");
hour=atoi(ph);//将字符转换成int型
minute=atoi(pm);
second=atoi(ps);
}
void Ctime::comptime(const Ctime system_time)
{
if(hour>system_time.hour)
cout<<"较大的时间是输入的时间:"<<hour<<":"<<minute<<":"<<second<<endl;
else if(system_time.hour>hour)
cout<<"较大的时间是系统时间:"<<__TIME__<<endl;
else if(minute>system_time.minute)
cout<<"较大的时间是输入的时间:"<<hour<<":"<<minute<<":"<<second<<endl;
else if(system_time.minute>minute)
cout<<"较大的时间是系统时间:"<<__TIME__<<endl;
else if(second>system_time.second)
cout<<"较大的时间是输入的时间:"<<hour<<":"<<minute<<":"<<second<<endl;
else if(system_time.second>second)
cout<<"较大的时间是系统时间:"<<__TIME__<<endl;
}
void main()
{
char time[10];
char *ph,*pm,*ps;
int h,m,s;
Ctime time1,time2;
for(bool i=false;i==false;)//判断时间是否在正常范围内
{
cout<<"请输入要比较的时间:(输入格式:XX:XX:XX:)"<<endl;
cin>>time;
ph=strtok(time,":");
pm=strtok(0,":");
ps=strtok(0," ");
h=atoi(ph);
m=atoi(pm);
s=atoi(ps);
if(h<=24&&h>=0&&m<60&&m>=0&&s<60&&s>=0)
{
time1.settime(h,m,s);
i=true;
}
if(i==false)
cout<<"你输入的时间不正确,请重新输入。"<<endl;
}
strcpy(time,__TIME__);//__TIME__是获取系统时间,格式:XX:XX:XX,获取系统日期是__DATE__
time2.settime(time);
time1.comptime(time2);
}
//试过了运行无误
#include<cstring>
#include<cstdlib>
using namespace std;
class Ctime//时间的类
{
private:
int hour;
int minute;
int second;
public:
Ctime();
void settime(int h,int m,int s);//时间设置函数
void settime(char *p_time);
void comptime(const Ctime system_time);//比较两个时间大小,并且输出较大的时间
};
Ctime::Ctime()
{
hour=0;
minute=0;
second=0;
}
void Ctime::settime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void Ctime::settime(char *p_time)
{
char *ph,*pm,*ps;
ph=strtok(p_time,":");//strtok函数的作用是获取p_time内存位置处“:”之前的字符
pm=strtok(0,":");//由于字符数组已经被拆分,所以头位置为当前位置为0;
ps=strtok(0," ");
hour=atoi(ph);//将字符转换成int型
minute=atoi(pm);
second=atoi(ps);
}
void Ctime::comptime(const Ctime system_time)
{
if(hour>system_time.hour)
cout<<"较大的时间是输入的时间:"<<hour<<":"<<minute<<":"<<second<<endl;
else if(system_time.hour>hour)
cout<<"较大的时间是系统时间:"<<__TIME__<<endl;
else if(minute>system_time.minute)
cout<<"较大的时间是输入的时间:"<<hour<<":"<<minute<<":"<<second<<endl;
else if(system_time.minute>minute)
cout<<"较大的时间是系统时间:"<<__TIME__<<endl;
else if(second>system_time.second)
cout<<"较大的时间是输入的时间:"<<hour<<":"<<minute<<":"<<second<<endl;
else if(system_time.second>second)
cout<<"较大的时间是系统时间:"<<__TIME__<<endl;
}
void main()
{
char time[10];
char *ph,*pm,*ps;
int h,m,s;
Ctime time1,time2;
for(bool i=false;i==false;)//判断时间是否在正常范围内
{
cout<<"请输入要比较的时间:(输入格式:XX:XX:XX:)"<<endl;
cin>>time;
ph=strtok(time,":");
pm=strtok(0,":");
ps=strtok(0," ");
h=atoi(ph);
m=atoi(pm);
s=atoi(ps);
if(h<=24&&h>=0&&m<60&&m>=0&&s<60&&s>=0)
{
time1.settime(h,m,s);
i=true;
}
if(i==false)
cout<<"你输入的时间不正确,请重新输入。"<<endl;
}
strcpy(time,__TIME__);//__TIME__是获取系统时间,格式:XX:XX:XX,获取系统日期是__DATE__
time2.settime(time);
time1.comptime(time2);
}
//试过了运行无误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<time.h>
#include<stdio.h>
int main(void)
{
struct tm *local;
struct tm compare;
time_t t;
char comtemp[128];
t=time(NULL);
local=localtime(&t);
printf("输入时间和日期(这个时间和日期是和系统时间做比较的,格式 2010-12-28 12:15:60)\n");
printf("输入时:");
scanf("%d",&compare.tm_hour);
printf("输入分:");
scanf("%d",&compare.tm_min);
printf("输入秒:");
scanf("%d",&compare.tm_sec);
if(compare.tm_hour>local->tm_hour)
{
printf("输入时间大于系统时间\n");
}
else if(compare.tm_hour==local->tm_hour)
{
if(compare.tm_min>local->tm_min)
{
printf("输入时间大于系统时间\n");
}
else if(compare.tm_min==local->tm_min)
{
if(compare.tm_sec>local->tm_sec)
{
printf("输入时间大于系统时间\n");
}
else if(compare.tm_sec==local->tm_sec)
{
printf("输入时间等于系统时间\n");
}
else
{
printf("输入时间小于系统时间\n");
}
}
else
{
printf("输入时间小于系统时间\n");
}
}
else
{
printf("输入时间小于系统时间\n");
}
printf("当前时间和日期: H:M:S %d:%d:%d\n",local->tm_hour,local->tm_min,local->tm_sec);
return 0;
}
#include<stdio.h>
int main(void)
{
struct tm *local;
struct tm compare;
time_t t;
char comtemp[128];
t=time(NULL);
local=localtime(&t);
printf("输入时间和日期(这个时间和日期是和系统时间做比较的,格式 2010-12-28 12:15:60)\n");
printf("输入时:");
scanf("%d",&compare.tm_hour);
printf("输入分:");
scanf("%d",&compare.tm_min);
printf("输入秒:");
scanf("%d",&compare.tm_sec);
if(compare.tm_hour>local->tm_hour)
{
printf("输入时间大于系统时间\n");
}
else if(compare.tm_hour==local->tm_hour)
{
if(compare.tm_min>local->tm_min)
{
printf("输入时间大于系统时间\n");
}
else if(compare.tm_min==local->tm_min)
{
if(compare.tm_sec>local->tm_sec)
{
printf("输入时间大于系统时间\n");
}
else if(compare.tm_sec==local->tm_sec)
{
printf("输入时间等于系统时间\n");
}
else
{
printf("输入时间小于系统时间\n");
}
}
else
{
printf("输入时间小于系统时间\n");
}
}
else
{
printf("输入时间小于系统时间\n");
}
printf("当前时间和日期: H:M:S %d:%d:%d\n",local->tm_hour,local->tm_min,local->tm_sec);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <time.h>
int main()
{
unsigned int hour,minu;
///////////////////////////////输入部分
int key=0;
printf("input a time:\t");
do
{
key=scanf("%u:%u",&hour,&minu);
if (hour>24 || minu>60)
{
printf("input error!\n");
fflush(stdin);
printf("input a time:\t");
key=0;
}
}
while(key!=2);
/////////////////////////////比较部分
{
time_t systime;
struct tm * timeinfo;
time(&systime);
timeinfo = localtime( &systime );
printf("your input is:%d:%d\n",hour,minu);
printf("system time is:%d %d\n",timeinfo->tm_hour,timeinfo->tm_min);
if (hour>timeinfo->tm_hour)
{
printf("your inpur is bigger!\n");
return 1;
}
if (hour==timeinfo->tm_hour)
{
if (minu>timeinfo->tm_min)
{
printf("your inpur is bigger!\n");
return 1;
}
if (minu==timeinfo->tm_min)
{
printf("euqual!!\n");
return 1;
}
else
{
printf("system time is bigger!\n");
return 1;
}
}
printf("system time is bigger!\n");
return 1;
}
}
#include <time.h>
int main()
{
unsigned int hour,minu;
///////////////////////////////输入部分
int key=0;
printf("input a time:\t");
do
{
key=scanf("%u:%u",&hour,&minu);
if (hour>24 || minu>60)
{
printf("input error!\n");
fflush(stdin);
printf("input a time:\t");
key=0;
}
}
while(key!=2);
/////////////////////////////比较部分
{
time_t systime;
struct tm * timeinfo;
time(&systime);
timeinfo = localtime( &systime );
printf("your input is:%d:%d\n",hour,minu);
printf("system time is:%d %d\n",timeinfo->tm_hour,timeinfo->tm_min);
if (hour>timeinfo->tm_hour)
{
printf("your inpur is bigger!\n");
return 1;
}
if (hour==timeinfo->tm_hour)
{
if (minu>timeinfo->tm_min)
{
printf("your inpur is bigger!\n");
return 1;
}
if (minu==timeinfo->tm_min)
{
printf("euqual!!\n");
return 1;
}
else
{
printf("system time is bigger!\n");
return 1;
}
}
printf("system time is bigger!\n");
return 1;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int h1, m1, s1;
int h2, m2, s2;
int times1, times2;
cout<<"请输入一个时间: ";
scanf("%d:%d:%d",&h1,&m1,&s1);
cout<<"请输入一个时间: ";
scanf("%d:%d:%d",&h2,&m2,&s2);
times1=h1*3600+m1*60+s1;
times2=h2*3600+m2*60+s2;
if(times1>times2)
{
cout<<h1<<":"<<m1<<":"<<s1;
cout<<" > ";
cout<<h2<<":"<<m2<<":"<<s2<<endl;
}
else if(times1<times2)
{
cout<<h1<<":"<<m1<<":"<<s1;
cout<<" < ";
cout<<h2<<":"<<m2<<":"<<s2<<endl;
}
else
{
cout<<h1<<":"<<m1<<":"<<s1;
cout<<" = ";
cout<<h2<<":"<<m2<<":"<<s2<<endl;
}
return 0;
}
#include<cstdio>
using namespace std;
int main()
{
int h1, m1, s1;
int h2, m2, s2;
int times1, times2;
cout<<"请输入一个时间: ";
scanf("%d:%d:%d",&h1,&m1,&s1);
cout<<"请输入一个时间: ";
scanf("%d:%d:%d",&h2,&m2,&s2);
times1=h1*3600+m1*60+s1;
times2=h2*3600+m2*60+s2;
if(times1>times2)
{
cout<<h1<<":"<<m1<<":"<<s1;
cout<<" > ";
cout<<h2<<":"<<m2<<":"<<s2<<endl;
}
else if(times1<times2)
{
cout<<h1<<":"<<m1<<":"<<s1;
cout<<" < ";
cout<<h2<<":"<<m2<<":"<<s2<<endl;
}
else
{
cout<<h1<<":"<<m1<<":"<<s1;
cout<<" = ";
cout<<h2<<":"<<m2<<":"<<s2<<endl;
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询