本文原创,版权属作者个人所有,如需转载请联系作者本人。Q&微:155122733

--------------------------------------------------------------------------------------------------------

直接上代码

#include <stdio.h>
#include <time.h>
int fun1(int val_a, int val_b, int e_ret1)
{
        if(val_a+val_b == e_ret1)
        {
                return 0;
        }
        else
        {
                return -1;
        }
}
int fun2(int val_a, int val_b, int e_ret2)
{
        if((val_a-val_b)==e_ret2)
        {
                return 0;
        }
        else
        {
                return -1;
        }
}

#define E_TIME 3*60
#define RESULT_OK 0
time_t e_start;
//1 初始化
void init()
{
        e_start = time(NULL);
        printf("e_start = %ld\n",e_start);
        printf("e_start = %s\n",asctime(localtime(&e_start)));
}
//3 执行具体测试步骤
int test(int a, int b, int E_ret1, int E_ret2)
{
        if(fun1(a,b,E_ret1)!=RESULT_OK)
        {
                //也可以在这里增加获取时间记录,计算运行了多长时间
                return -1;
        }
        if(fun2(a,b,E_ret2)!= RESULT_OK)
        {
                //也可以在这里增加获取时间记录,计算运行了多长时间
                return -1;
        }
        return 0;
}
//2 在一定时间内循环执行
int loop()
{
        time_t cur;
        int result;
        long int cost, sec, min, hour, day;
        while(1)
        {
                cur = time(NULL);//获取当前日期时间
                if((cur - e_start)> E_TIME)//如果大于期望的运行时间,则推出循环;
                {
                        break;
                }
                else
                {
                        result = test(5,3,8,2);//具体执行
                        if(result != 0)//如果运行失败,计算运行了多长时间;
                        {
                                cost = (cur-e_start);
                               //两种方式计算时间都正确
                                /*
                                sec = cost%60;
                                min = ((cost - sec)/60)%60; 
                                hour = ((cost - sec - min*60)/60/60)%24;
                                day = ((cost- sec - min*60 - hour*60*60)/60/60/24);
                                */

                                day = cost/60/60/24;
                                hour = ((cost/60/60)%24);
                                min = (cost/60%60);
                                sec = cost%60;

                                printf("already run %ld days %ld hours %ld minutes %ld seconds\n", day, hour, min, sec);
                                return result;
                                break;
                        }
                }
                return 0;
        }
}
//4 finish()/
void finish()
{

}
int main(void)
{
        int result;
        init();
        result = loop();
        finish();
        return 0;
}