当前位置:首页 » 编程语言 » c语言时间的表达和加减
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言时间的表达和加减

发布时间: 2022-07-05 03:21:10

1. c语言中的时间相加

思路:

1.将两个数分为前两位和后两位。

2.后两位先相加,如果超过60,则减去60,并进一位。

3.前两位相加还要加上进位,如果超过24,则减去24.

4.将前两位和后两位组合,形成最终要输出的形式

完整的程序代码如下,不懂之处可以看注解:

#include<stdio.h>

voidmain()

{

inta,b,c,d,e,f,ab,ce,df;

inti=0;

printf("Inputtwonumber: ");//输入两行数据,每行一个整数。

scanf("%d%d",&a,&b);//a代表第一个数,b代表第二个数

c=a/100;//c代表a的前两位数

d=a%100;//d代表a的后两位数

e=b/100;//e代表b的前两位数

f=b%100;//f代表b的后两位数

df=d+f;

if(df>=60)//如果后两位相加超过60,则减去60,并进一位。

{

df-=60;

i++;

}

ce=c+e+i;

if(ce>=24)//如果前两位相加超过24,则减去24。

{

ce-=24;

}

ab=ce*100+df;//最后需要输出的数据

printf("Theoutputis: %d ",ab);

}

ps:运行结果如下:

2. c语言中时间处理

稳定超频,要看是什么CPU了,主板,电源,散热器,都很重要,因为超频后功耗变高,对主板也是一种考验,看主板CPU供电相数,电源功率,功耗变高,温度也会变高了,好的散热器才能压的住,一般CPU超频都有一个最大电压值,一般来说,电压相对低的,能达到更高的倍频或者外频的CPU体质比较好,再一个是稳定性,超频后要经过各种测试,稳定的同时也要控制温度。一般CPU要注意的就是CPU电压,防掉电自动加压,倍频或者外频数值。

3. c语言如何计算两个时间相差多少

任意输入两个24小时制的时间,输出两个时间的时间差;

4. c语言中怎么将时间相加啊,输入两组时间,时分秒用空格隔开,输出的值也用空格隔开

时间相加,判断起来比较麻烦,也容易出错,我常用的方法是用mktime先转换成秒,然后相加或者相减,再把结果转换成时间就行

5. C语言输入两个时间(同一天的两个时和分),计算其时间差,输出相差几小时几分钟

/**

time.c

定义一个结构体实现两个时间的加减

*/

#include<stdio.h>

#include<string.h>

typedef struct

{

int seconds;

int minutes;

int hours;

}Time;

int checkTime(Time time);

void printTime(Time time);

void swap(Time *time1,Time *time2);//大的时间放在前面

Time subtract1(Time *first,Time *second);

Time subtract(Time *first,Time *second);//默认第一个时间比第二个大

int main()

{

Time time1;

Time time2;

Time time3;

char againch[5]="y";

while(strcmp(againch,"y")==0||strcmp(againch,"Y")==0)

{

int again=1;

while(again)

{

printf("输入时间1:");

scanf("%d:%d:%d",&time1.hours,&time1.minutes,&time1.seconds);

if(checkTime(time1))

{

printf("-----输入时间格式错误!请重新输入 ");

again=1;

}

else

again=0;

}

again=1;

while(again)

{

printf("输入时间2:");

scanf("%d:%d:%d",&time2.hours,&time2.minutes,&time2.seconds);

if(checkTime(time2))

{

printf("-----输入时间格式错误!请重新输入 ");

again=1;

}

else

again=0;

}

swap(&time1,&time2);

printf(" ");

printTime(time1);

printf(" - ");

printTime(time2);

time3=subtract(&time1,&time2);

printf(" = ");

printTime(time3);

printf(" ");

printf("继续[y/n]?:");

scanf("%s",againch);

}

return 0;

}

//检查时间的格式

int checkTime(Time time)

{

// printf("小时格式错误:%d ",(time.hours>=24||time.hours<0));

// printf("分钟格式错误:%d ",(time.minutes>=60||time.minutes<0));

// printf("秒格式错误 :%d ",(time.seconds>=60||time.minutes<0));

return ((time.hours>24||time.hours<0)||(time.minutes>=60||time.minutes<0)||(time.seconds>=60||time.minutes<0));

}

//输出按个数输出时间

void printTime(Time time)

{

printf("%d:%d:%d",time.hours,time.minutes,time.seconds);

}

//大的时间放到第一个变量,小的时间方法哦第二个变量

void swap(Time *time1,Time *time2)

{

//保证第一个时间永远大于第二个时间

if(time2->hours>time1->hours)//如果有time

{

//交换两个时间的小时

time2->hours^=time1->hours;

time1->hours^=time2->hours;

time2->hours^=time1->hours;

//交换两个时间的分钟:

time1->minutes^=time2->minutes;

time2->minutes^=time1->minutes;

time1->minutes^=time2->minutes;

//交换两个时间的秒:

time1->seconds^=time2->seconds;

time2->seconds^=time1->seconds;

time1->seconds^=time2->seconds;

}

else if(time2->minutes>time1->minutes&&time1->hours==time2->hours)

{

//交换两个时间的分钟:

time1->minutes^=time2->minutes;

time2->minutes^=time1->minutes;

time1->minutes^=time2->minutes;

//交换两个时间的秒:

time1->seconds^=time2->seconds;

time2->seconds^=time1->seconds;

time1->seconds^=time2->seconds;

}

else if(time2->seconds>time1->seconds&&time1->minutes==time2->minutes)

{

//交换两个时间的秒:

time1->seconds^=time2->seconds;

time2->seconds^=time1->seconds;

time1->seconds^=time2->seconds;

}

}

//计算两个时间的差

Time subtract(Time *first,Time *second)//默认第一个时间比第二个大

{

Time result;

//先对秒进行相减

if(first->seconds>=second->seconds)//如果第一个秒大于或者等于

{

result.seconds=first->seconds-second->seconds;

}

else//如果第一个的秒数小的话

{

first->minutes=first->minutes-1;//借位

first->seconds=first->seconds+60;

result.seconds=first->seconds-second->seconds;

}

//接着对分钟相减

if(first->minutes>=second->minutes)//如果第一个秒大于或者等于

{

result.minutes=first->minutes-second->minutes;

}

else//如果第一个的秒数小的话

{

first->hours=first->hours-1;//借位

first->minutes=first->minutes+60;

result.minutes=first->minutes-second->minutes;

}

//交换后 默认第一个小时会大于第一个,没有借位的情况,不用

result.hours=first->hours-second->hours;

return result;

6. c语言时间函数的具体使用方法,时间的加减

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));
return0;
}


说明:
time_t // 时间类型(time.h 定义)
struct tm { // 时间结构,time.h 定义如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
time ( &rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime ( &rawtime ); //转为当地时间,tm 时间结构
asctime() // 转为标准ASCII时间格式:
//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

7. 时间加减 C语言

在VC中我是这样弄的:

SYSTEMTIME time;
::GetSystemTime(&time);\\取当前时间,time为一个临时的变量
CTime now=time;\\将当前时间存到CTime变量中

\\输入要进行对比的时间存入time中,比如下面(对比时间是2006年8月20日):
time.wYear=2006;
time.wMonth=8;
time.wDay=20;

CTime cmptime=time;\\将要对比的时间放到另一个CTime变量中

CTimeSpan overtime=now-cmptime;\\比较时,两时间直接作差,保存在CTimeSpan变量中,这个值有正负,overtime虽然是CTimeSpan类型的,但是用法和CTime一样

int overdays=overtime.GetDays();\\取天数差距,本例中就为9(今天是8月29日),如果刚才now-cmptime换成cmptime-now,现在的值就是-9

int overdays=overtime.GetMonths();同上,取月差距,其他方法一样,年,分,秒都能比较

自己回去试一试吧,应该有满意的效果!

我也是琢磨了很长时间才弄出来,个人觉得算是比较方便的方法了,正如你所说,时间的计算比较复杂,容易出错,不如让提供好的MFC类对时间进行处理,省去不必要的麻烦!

8. c语言题目 计算时间相减

很简单嘛
#include<stdio.h>
int main()
{
int s_min,s_sec,e_min,e_sec;
printf("Please input the time\n");
scanf("%d:%d %d:%d",&s_min,&s_sec,&e_min,&e_sec);
printf("%d\n",abs((s_min-e_min)*60+s_sec-e_sec));//用个绝对值就OK啦
}

9. C语言中如何让时间相加

#include <stdio.h>
int main()
{
int AH,AM,AS,BH,BM,BS,CH=0,CM=0,CS=0;
printf("请输入时间A");
L1:
scanf("%d %d %d",&AH,&AM,&AS);
if(AH>60||AH<0||AM>60||AM<0||AS>60||AS<0)
{
printf("不合法的时间,请重新输入");
goto L1;

}
printf("请输入时间B");
L2:
scanf("%d %d %d",&BH,&BM,&BS);
if(BH>60||BH<0||BM>60||BM<0||BS>60||BS<0)
{
printf("不合法的时间,请重新输入");
goto L2;

}
CS=BS+AS;
if(CS>=60)
{
CS=CS-60;
CM=AM+BM+1;
}
else
{
CM=AM+BM;
}
if(CM>=60)
{
CM=CM-60;
CH=AH+BH+1;
}
else
CH=AH+BH;
printf("时间和为:");
printf("%d,%d,%d",CH,CM,CS);
system("pause");

}

10. 求一段 日期时间类(时间加减转换) 代码的设计思路,有源码更好(源码语言 类C语言为好).

这是一个输入输出相互转换的题型,年月日与让天数相互转换。
天数1.5为1900年1月1日 12:00时,1900年+1.5天
42576.85545表示2016/7/25 20:31.,用42576.85545除以365天约为116.648919041 +1900
可以这样1900年加数字等于现在的时间,
1, 把数字分开,整数是天数,小数是小时,
2,用整数,判断年月日,
2.1 if(190xx润年?&&整数>=366)1900++;整数-=366;
2.2 不够365开始判断月份,
。。。。。。