歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C程序中唯一序列號的生成

C程序中唯一序列號的生成

日期:2017/3/1 9:41:46   编辑:Linux編程

在實際的軟件開發項目中,經常會涉及唯一序列號的生成。本文以一個實際的程序為例,介紹了唯一序列號的生成過程。

本文生成的序列號的樣式為:MMDDHHMINSS_XXXXXX。

C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm

讀C++ Primer 之構造函數陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm

讀C++ Primer 之智能指針 http://www.linuxidc.com/Linux/2011-08/40177.htm

讀C++ Primer 之句柄類 http://www.linuxidc.com/Linux/2011-08/40175.htm

將C語言梳理一下,分布在以下10個章節中:

  1. Linux-C成長之路(一):Linux下C編程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成長之路(二):基本數據類型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成長之路(三):基本IO函數操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成長之路(四):運算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成長之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成長之路(六):函數要義 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成長之路(七):數組與指針 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成長之路(八):存儲類,動態內存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成長之路(九):復合數據類型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成長之路(十):其他高級議題

程序如下

/**********************************************************************
* 版權所有 (C)2014, Zhou Zhaoxiong。
*
* 文件名稱: SerialNo.c
* 文件標識: 無
* 內容摘要: 用於演示序列號的創建方法
* 其它說明: 無
* 當前版本: V1.0
* 作 者: 周兆熊
* 完成日期: 20140603
*
* 修改記錄1:// 修改歷史記錄, 包括修改日期、版本號、修改人及修改內容
* 修改日期: 20140603
* 版 本 號: V1.0
* 修 改 人: Zhou Zhaoxiong
* 修改內容: 創建
**********************************************************************/

#include <afxinet.h>

// 數據類型
typedef unsigned char UINT8;
typedef unsigned char UINT16;
typedef unsigned int UINT32;
typedef signed int INT32;


// 時間信息結構體
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 GetCurTime(ClockStruc *pCurrentTime); // 獲取當前時間
INT32 CreateSerial(UINT8 *pSerialID, UINT32 iSerialSize); // 創建序列號
INT32 main(void); // 主函數


/**********************************************************************
* 功能描述: 獲取當前時間
* 輸入參數: 無
* 輸出參數: pCurrentTime-當前時間結構體
* 返 回 值: 無
* 其它說明: 無
* 修改日期 版本號 修改人 修改內容
* ----------------------------------------------------------------------------
* 20140603 V1.0 Zhou Zhaoxiong 創建
**********************************************************************/
void GetCurTime(ClockStruc *pCurrentTime)
{
SYSTEMTIME tCurrentTime;

GetLocalTime(&tCurrentTime);

pCurrentTime->month = (UINT8)tCurrentTime.wMonth;
pCurrentTime->day = (UINT8)tCurrentTime.wDay;
pCurrentTime->hour = (UINT8)tCurrentTime.wHour;
pCurrentTime->minute = (UINT8)tCurrentTime.wMinute;
pCurrentTime->second = (UINT8)tCurrentTime.wSecond;
pCurrentTime->week = (UINT8)tCurrentTime.wDayOfWeek;
if (pCurrentTime->week == 0) // 表示星期天
{
pCurrentTime->week = 7;
}
}


/**********************************************************************
* 功能描述: 創建序列號
* 輸入參數: iSerialSize: 序列號長度
* 輸出參數: pSerialID: 序列號
* 返 回 值: 0-成功 -1-失敗
* 其它說明: 序列號的樣式: MMDDHHMINSS_XXXXXX
* 修改日期 版本號 修改人 修改內容
* --------------------------------------------------------------
* 20140603 V1.0 Zhou Zhaoxiong 創建
***********************************************************************/
INT32 CreateSerial(UINT8 *pSerialID, UINT32 iSerialSize)
{
ClockStruc tClock = {0};
static UINT32 iTailNum = 0;

if (NULL == pSerialID)
{
printf("CreateSerial: input parameter is NULL.\n");
return -1;
}

GetCurTime(&tClock);


_snprintf((char *)pSerialID, iSerialSize, "%02d%02d%02d%02d%02d_%06d",
tClock.month, tClock.day, tClock.hour, tClock.minute, tClock.second, iTailNum);


iTailNum ++;
if (iTailNum > 999999)
{
iTailNum = 0;
}

return 0;
}


/****************************************************************
* 功能描述: 主函數
* 輸入參數: 無
* 輸出參數: 無
* 返 回 值: 0-執行成功 -1-執行失敗
* 其他說明: 無
* 修改日期 版本號 修改人 修改內容
* ------------------------------------------------------------------------
* 20140603 V1.0 Zhou Zhaoxiong 創建
****************************************************************/
INT32 main(void)
{
UINT32 iLoopFlag = 0; // 該變量用於表示循環標志
INT32 iRetValue = 0; // 該變量用於表示調用CreateSerial函數返回的值
UINT8 szSerialID[50] = {0}; // 該變量用於存放生成的序列號

// 生成10個序列號, 並打印出來
for (iLoopFlag = 0; iLoopFlag < 10; iLoopFlag ++)
{
iRetValue = CreateSerial(szSerialID, sizeof(szSerialID));
if (iRetValue != 0)
{
printf("exec CreateSerial failed.\n");
return -1;
}

printf("第%d個序列號為: %s\n", iLoopFlag + 1, szSerialID);
}

return 0;
}

程序運行的結果如下圖所示:

Copyright © Linux教程網 All Rights Reserved