限制程序中某类操作的执行次数的算法设计及C代码实现

需求描述
编写程序实现限制程序中某类操作的执行次数的需求。为了便于说明,要求程序每天创建一个与上一天不同的文件目录。如果欲创建的文件目录已存在,则不用再创建。文件目录的命名格式为:FileDir_YYYYMMDD,如:FileDir_20160830。

程序流程
对于此类需求,最关键的问题是要设定一个标识来限制操作的执行次数。也就是说,当程序执行完一次操作之后,要有机制来限制它执行第二次操作。

因为本需求要求每天执行一次操作,所有我们自然想到了用日期来限制程序的执行次数。我们可以用一个全局时间变量来存放每次创建文件目录的日期,如果当前日期和全局时间变量中的日期相等,就表示当天已经执行过操作了,不用再次执行。

基于上面的分析,我们可以画出如下的程序流程图:

C代码

/**********************************************************************
* 版权所有 (C)2016, Zhou Zhaoxiong
*
* 文件名称:DirCreateControl.c
* 文件标识:无
* 内容摘要:限制创建目录操作的执行次数的C代码实现
* 其它说明:无
* 当前版本:V1.0
* 作   者:ZhouZhaoxiong
* 完成日期:20160830
*
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>

// 重定义数据类型
typedef signed  int         INT32;
typedef unsigned int        UINT32;
typedef unsigned char       UINT8;
typedef unsigned short int  UINT16;
typedef long     int        LONG;

// 全局变量
UINT8 g_szLastExecDate[10] = {0};       // 上一次执行操作的日期, 格式为: 年月日(YYYYMMDD)

// 时间结构体
typedef struct
{
    UINT8   second;    /* 0-59 */
    UINT8   minute;    /* 0-59 */
    UINT8   hour;      /* 0-23 */
    UINT8   day;       /* 1-31 */
    UINT8   month;     /* 1-12 */
    UINT16  year;      /* 1994-2099 */
    UINT8   week;      /* 1-7 */
    UINT8   Count10ms; /* 0-99 */
} ClockStruc;

// 函数声明
void GetCurrentTime(ClockStruc *ptTime);
INT32 JudgeIfExecOper(ClockStruc *ptTime);
void CreateFileDir(UINT8 *pszFileDir);
void Sleep(UINT32 iCountMs);

/****************************************************************
* 功能描述: 主函数
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 0-执行完成
* 其他说明: 无
* 修改日期         版本号        修改人         修改内容
*-------------------------------------------------------------
* 20160830        V1.0     Zhou Zhaoxiong      创建
****************************************************************/
INT32 main(void)
{
    ClockStruc tTimeNow       = {0};      // 当前时间结构体
    UINT8      szFileDir[100] = {0};      // 文件路径变量
    INT32      iRetVal        = 0;        // 执行函数之后的返回值

    while (1)      // 循环执行创建目录的操作
    {
        Sleep(10 *1000);    // 程序执行间隔为10s

        // 首先判断当天是否已执行过操作
        iRetVal = JudgeIfExecOper(&tTimeNow);
        if (iRetVal != 0)    // 不用执行创建目录的操作
        {
            continue;
        }

        // 构造目录
       snprintf(szFileDir, sizeof(szFileDir)-1,"%s/zhouzx/Test/FileDir_%04d%02d%02d/", getenv("HOME"),                             tTimeNow.year, tTimeNow.month, tTimeNow.day);

        // 接着创建目录
       CreateFileDir(szFileDir);
    }

    return 0;
}

/**********************************************************************
* 功能描述:获取当前时间
* 输入参数:ptTime-当前时间结构体
* 输出参数:ptTime-当前时间结构体
* 返 回 值:无
* 其它说明:无
* 修改日期        版本号          修改人         修改内容
*-------------------------------------------------------------------
* 20160830       V1.0       Zhou Zhaoxiong     创建
***********************************************************************/
void GetCurrentTime(ClockStruc *ptTime)
{
    LONG     dt           = 0;
    struct   tm     *tm1  = NULL;
    struct   timeval tp   = {0};

    // 获取系统时间
    gettimeofday(&tp, NULL);
    dt  = tp.tv_sec;
    tm1 = localtime(&dt);

    ptTime->Count10ms = tp.tv_usec / 10000;
    ptTime->year      = (UINT16)(tm1->tm_year + 1900);
    ptTime->month     = (UINT8)tm1->tm_mon + 1;
    ptTime->day       = (UINT8)tm1->tm_mday;
    ptTime->hour      = (UINT8)tm1->tm_hour;
    ptTime->minute    = (UINT8)tm1->tm_min;
    ptTime->second    = (UINT8)tm1->tm_sec;
    ptTime->week      = (UINT8)tm1->tm_wday;

    if (ptTime->week == 0)   // 星期天
    {
        ptTime->week = 7;
    }
}

/**********************************************************************
* 功能描述:判断当天是否已执行过操作
* 输入参数:ptTime-当前时间结构体
* 输出参数:ptTime-当前时间结构体
* 返 回 值:1-已执行  0-未执行  -1-程序异常
* 其它说明:无
* 修改日期        版本号          修改人          修改内容
* -------------------------------------------------------------------
* 20160830       V1.0       Zhou Zhaoxiong       创建
***********************************************************************/
INT32 JudgeIfExecOper(ClockStruc *ptTime)
{
    UINT8szCurrentDate[10] = {0};

    if (ptTime == NULL)
    {
       printf("JudgeIfExecOper: ptTime is NULL!\n");

        return -1;
    }

   GetCurrentTime(ptTime);     // 获取当前时间

   snprintf(szCurrentDate, sizeof(szCurrentDate)-1,"%04d%02d%02d", ptTime->year, ptTime->month, ptTime->day);

    // 判断当前日期和上次执行日期是否相等, 如果相等, 则不用再执行操作了
    if (strcmp(szCurrentDate, g_szLastExecDate) != 0)  // 两者不相等
    {
       printf("JudgeIfExecOper: CurrentDate is not equal to LastExecDate,so go ahead!\n");

       memset(g_szLastExecDate, 0x00, sizeof(g_szLastExecDate));
       memcpy(g_szLastExecDate, szCurrentDate, strlen(szCurrentDate));   // 将上次执行日期更新为当前日期

        return 0;
    }
    else
    {
       printf("JudgeIfExecOper: CurrentDate is %s, LastExecDate is %s,they are equal, so do not go ahead!\n", szCurrentDate, g_szLastExecDate);

        return 1;
    }
}

/****************************************************************
* 功能描述: 创建目录
* 输入参数: pszFileDir-目录
* 输出参数: 无
* 返 回 值: 无
* 其他说明: 无
* 修改日期         版本号        修改人       修改内容
* -------------------------------------------------------------
* 20160830        V1.0   Zhou Zhaoxiong     创建
****************************************************************/
void CreateFileDir(UINT8 *pszFileDir)
{
    UINT8 szCmdbuf[100] = {0};

    if (pszFileDir ==NULL)
    {
        printf("CreateFileDir: pszFileDir is NULL!\n");

        return;
    }

    // 下面执行目录创建操作
    // 第一步判断目录是否存在
    // 第二步创建目录
    if (0 != access(pszFileDir, F_OK))   // 目录不存在
    {
        // 创建目录
        memset(szCmdbuf,0x00, sizeof(szCmdbuf));
        snprintf(szCmdbuf, sizeof(szCmdbuf)-1, "mkdir -p %s",pszFileDir);
        system(szCmdbuf);

        printf("CreateFileDir: %s has created successfully!\n",pszFileDir);
    }
    else        // 目录存在
    {
        printf("CreateFileDir:%s has already existed!\n", pszFileDir);
    }
}

/**********************************************************************
* 功能描述:程序休眠
* 输入参数:iCountMs-休眠时间(单位:ms)
* 输出参数:无
* 返 回 值:无
* 其它说明:无
* 修改日期         版本号         修改人       修改内容
* ------------------------------------------------------------------
* 20160830        V1.0     Zhou Zhaoxiong     创建
********************************************************************/
void Sleep(UINT32 iCountMs)
{
    struct timevalt_timeout = {0};

    if (iCountMs <1000)
    {
       t_timeout.tv_sec  = 0;
       t_timeout.tv_usec = iCountMs * 1000;
    }
    else
    {
       t_timeout.tv_sec  = iCountMs /1000;
       t_timeout.tv_usec = (iCountMs % 1000) * 1000;
    }
    select(0, NULL,NULL, NULL, &t_timeout);    // 调用select函数阻塞程序
}

程序编译及执行
我们把上面编写好的DirCreateControl.c文件上传到Linux机器上,使用“gcc -g -o DirCreateControlDirCreateControl.c”命令编译之后,生成DirCreateControl文件,然后执行“DirCreateControl”命令,可对程序进行测试。
1)当被创建的目录不存在时,程序执行结果如下:

> DirCreateControl
JudgeIfExecOper: CurrentDate is not equal to LastExecDate, sogo ahead!
CreateFileDir: /home/zhou/zhouzx/Test/FileDir_20160830/ hascreated successfully!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!

2)当被创建的目录已经存在时,程序执行结果如下:

> DirCreateControl
JudgeIfExecOper: CurrentDate is not equal to LastExecDate, sogo ahead!
CreateFileDir: /home/zhou/zhouzx/Test/FileDir_20160830/ hasalready existed!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!
JudgeIfExecOper: CurrentDate is 20160830, LastExecDate is20160830, they are equal, so do not go ahead!

可以看到,不管被创建的目录是否存在,程序在一天当中都只执行一次创建目录的操作,而当天剩下的大部分时间都是在检查第二天是否来到,如果没有来到,则继续等待;如果已经来到,则再次重复昨天的操作。

总结
在实际的软件开发项目中,我们经常遇到需要限制程序中某类操作的执行次数的情况,本文只是给出了其中的一种解决办法。在实际编写代码的时候,大家要根据程序的特点及需求要求采用合理的实现方法。

时间: 2024-09-20 02:52:58

限制程序中某类操作的执行次数的算法设计及C代码实现的相关文章

LINQ To SQL在N层应用程序中的CUD操作、批量删除、批量更新

原文:LINQ To SQL在N层应用程序中的CUD操作.批量删除.批量更新 0. 说明     Linq to Sql,以下简称L2S.    以下文中所指的两层和三层结构,分别如下图所示:       准确的说,这里的分层并不是特别明确:(1) 生成的DataContext(Linq t0 SQL Runtime)和Entity是放在一个文件中的,物理上不能切割开来:上图只是展示逻辑上的结构.(2) 拿上图右边的三层结构来说,鉴于第(1)点,UI层就可以跨越BusinessLogic层,直接

[请教]计算机中不装MS Word可以在程序中显示和操作.doc文档吗?

问题描述 就是说本地计算机中不安装微软的Word程序,但是可以在本地计算机的程序中显示和操作word的.doc文档,有这样的插件和方案吗??本人写了一个调用word程序来显示和处理word文档的程序,但是客户嫌使用这个程序要先装word很麻烦,所以希望不装word就能在本人的程序中显示和处理word文档,不知道是否有高手做过类似的东西,提供个思路也行,拜谢!!! 解决方案 解决方案二:网上找找看有没有第三方控件支持,支持显示的应该能找到,支持编辑的可能就比较难找了解决方案三:好像没有什么特别好的

将源目录中的文件按照前缀分发到不同目录中的算法设计及C代码实现

一.需求描述 在Linux系统的某个源目录中有一批后缀相同的文件,编写程序将这些文件按照前缀分发到不同的目录中. 例如,源目录SourceDir中存放有三个后缀相同的文件File1_1.txt.File2_1.txt和File3_1.txt,按照前缀File1_.File2_和File3_将它们分别移动(分发)到目录FileDir1.FileDir2和FileDir3中. 二.算法设计 基于需求,可以采用如图1所示的程序流程: 图1 程序总体流程 三.特殊流程考虑 在编写程序的过程中,对于某些特

C#中TreeView类操作全攻略(一)

treeview|攻略 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using com.prm.client.tools;using System.Data.OracleClient;using com.prm.client.common;using com.prm.client

C#中TreeView类操作全攻略(二)

treeview|攻略 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using com.prm.client.tools;using System.Data.OracleClient;//using com.prm.client.common; namespace com.prm

C#中TreeView类操作全攻略(三)

treeview|攻略 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using com.prm.client.tools;using System.Data.OracleClient;//using com.prm.client.common; namespace com.prm.client.forms{ ///

在AJAX程序中实现互斥揭秘

ajax|程序 随着AJAX范例得到越来越广泛的应用,浏览器页面可以在向后台服务器请求数据的同时保持前端用户界面的活跃性(因此在AJAX中称为异步).然而,当这两个活动同时访问共用的JavaScript和DOM数据结构时就会引发问题.JavaScript没有提供针对该并发程序问题的经典解决方案.本文描述了作者在互斥机制方面的新见解,该经过验证的互斥机制在JavaScript中能发挥良好的作用. 为什么需要互斥? 当多个程序逻辑线程同时访问相同数据的时候,问题便产生了.程序通常假定与其交互的数据在

浅析PHP程序中的目录遍历漏洞

目录遍历漏洞在国内外有许多不同的叫法,比如也可以叫做信息泄露漏洞,非授权文件包含漏洞.名称虽然多,可他们却有一个共同的成因,就是在程序中没有过滤用户输入的../和./之类的目录跳转符,导致恶意用户可以通过提交目录跳转来遍历服务器上的任意文件,其危害可想而知.这类漏洞大家比较熟悉的可能就是在一些邮件列表程序以及网络硬盘程序中,其实这类漏洞还广泛存在与一些国外的BLOG程序中,这类漏洞大概分两种下面就来通过实例来说明这类漏洞是如何产生以及该如何防范. 首先,我们来看一个国外的BLOG,前几天从网上下

asp.net中cookie的操作(删除,修改,查找,赋值)

下面分享一下对cookies的简单操作 1.添加cookies(用cookies方式去做sso,用户信息保存,修改都会依赖cookies)    代码如下 复制代码 #region##添加cookeis     ///<summary>     /// 添加cookeis     ///</summary>     public void AddCookies()     {         HttpCookie cookies = new HttpCookie("Por