計算時間間隔的 副程式

只能 精確到 16ms


[code]
/*
for time1(), time2()
*/
// ----------------------------------------------

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>

#include <sys/timeb.h>
#include <time.h>
// ----------------------------------------------

#include "sj01.h"
// ----------------------------------------------

// time1( &t1);
void time1(int *t1)
{
struct _timeb timebuffer;
int t2, t3;
_ftime( &timebuffer );

t2= timebuffer.time;// Time in seconds since midnight (00:00:00),
// January 1, 1970, coordinated universal time (UTC).
t3= timebuffer.millitm;// Fraction of a second in milliseconds.

t2%= (1L*24L*60L*60L);// seconds in 1- day
t2*= 1000L;// in milliseconds

*t1= t2 + t3;
}// end of time1()
// ----------------------------------------------

// time2( t1, &dt);
void time2(int t1, double *dt)
{
int t2;

// get t2
time1(&t2);

*dt= (t2 - t1)/1000.0;

// (*dt) >= 0
if ((*dt) < 0) {
(*dt)+= (1.0*24.0*60.0*60.0);
}
}// end of time2()
// ----------------------------------------------

void main()
{
int no, sum, i, t1;
double xs, dt;
skip(1);

// by sum= 1 + 2 + . . . + no
no= 10;
int ct1= 1;
while (no>0) {
time1(&t1);
sum= 0;
for (i=1;i<=no;i++) {
sum+= i;
}
time2(t1, &dt);
// ----------------------------------------------

xs= (1.0 + no)*(no - 1.0 + 1.0)/2.0;// for check
printf("%2d, %12.2le, %12ld, %20.0lf, dt= %10.4lf\n",
ct1, (double) no, sum, xs, dt);

no*= 2;
ct1++;
}
}// end of main()

[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值