以後要搞C了先自己練習一下,系統本身沒有太大的實際用途,只是用來磨練編程規范,不足之處還望大家多多指正
為了方便只使用了一個 book.c 文件,在vc6 下編譯通過,其他編譯器還沒試過
順便貼出代碼
1
#include <stdio.h>
#include <stdlib.h>
#include <.h>
#include <MATH.H>
#include <TIME.H>
MAX_MSG_LEN 100
MAX_CMD_TMP_LEN 100
MAX_BOOK_NAME_LEN 30
MAX_BOOK_COUNT 10
MIN_BOOK_NUMBER 1000
MAX_BOOK_NUMBER MIN_BOOK_NUMBER + MAX_BOOK_COUNT - 1
DEFAULT_BOOK_ID 0
END_ID_OF_MSG_SET -1
BEGIN_MSG_SET MSG_INFO g_szMsg[] = {
ADD_MSG(ID,MSG_STR) {ID,MSG_STR},
END_MSG_SET {END_ID_OF_MSG_SET,""}};
typedef unsigned UINT8;
typedef unsigned BookNumber;
typedef tagSysTime
{
UINT8 nHour;
UINT8 nMin;
UINT8 nSec;
}SYS_TIME;
typedef emBoolVal
{
TRUE,
FALSE
}BOOL;
typedef emBookState
{
BOOK_IN,
BOOK_OUT
}BOOK_STATE_EM;
typedef emBookOption
{
BOOK_OP_IN,
BOOK_OP_OUT
}BOOK_OP_EM;
typedef emMsgID
{
MSG_SYS_INNER_ERROR = ,
MSG_SYS_INIT_SUCCESS = ,
MSG_SYS_BAD_CMD = ,
MSG_BOOK_BAD_ID = ,
MSG_BOOK_NOT_IN = ,
MSG_BOOK_NOT_OUT = ,
MSG_BOOK_BRW_SCS = ,
MSG_BOOK_RTN_SCS = ,
MSG_BOOK_NOT_HAS = ,
MSG_BOOK_QRY_SCS = ,
MSG_BOOK_HIS_SCS =
}MSG_ID_EM;
typedef tagMsgInfo
{
MSG_ID_EM emID;
szMessage[MAX_MSG_LEN];
}MSG_INFO;
typedef tagBookInfo
{
BookNumber nNum;
BOOK_STATE_EM emState;
szName[MAX_BOOK_NAME_LEN + ];
} BOOK_INFO;
typedef tagBookHistInfo
{
BOOK_INFO *pstBookInfo ;
BOOK_OP_EM emBookOption;
SYS_TIME stOptionTime;
}BOOK_HIS_INFO;
typedef tagBookHisNode
{
BOOK_HIS_INFO stBookHisInfo;
tagBookHisNode *pstNext;
}BOOK_HIS_NODE;
BEGIN_MSG_SET
ADD_MSG(MSG_SYS_INNER_ERROR , )
ADD_MSG(MSG_SYS_INIT_SUCCESS, )
ADD_MSG(MSG_SYS_BAD_CMD , )
ADD_MSG(MSG_BOOK_BAD_ID , )
ADD_MSG(MSG_BOOK_NOT_IN , )
ADD_MSG(MSG_BOOK_NOT_OUT , )
ADD_MSG(MSG_BOOK_BRW_SCS , )
ADD_MSG(MSG_BOOK_RTN_SCS , )
ADD_MSG(MSG_BOOK_NOT_HAS , )
ADD_MSG(MSG_BOOK_QRY_SCS , )
ADD_MSG(MSG_BOOK_HIS_SCS , )
END_MSG_SET
2
g_szTimeStamp[];
*g_szBookStateString[] = {,,};
*g_szBookOptionString[] = {,}; BOOL GetLocalTime()
{
time_t ulTime ;
tm *stTime = NULL;
*pcTimeStr = NULL;
time(&ulTime);
stTime = localtime(&ulTime);
sprintf(g_szTimeStamp, , stTime->tm_hour, stTime->tm_min, stTime->tm_sec);
g_szTimeStamp[] = ;
TRUE;
}
apiPrintMsg(MSG_ID_EM emMsgID)
{
i;
*pcTimeStr = NULL;
( i = ; END_ID_OF_MSG_SET != g_szMsg[i].emID; i++ )
{
( g_szMsg[i].emID == emMsgID )
{
( TRUE == GetLocalTime() )
{
printf(, g_szTimeStamp, g_szMsg[i].szMessage);
}
;
}
}
}
apiPrintBookInfo(BOOK_INFO* pstBookInfo, iCount)
{
i = , iStateIndex = ;
( NULL == pstBookInfo || == iCount)
{
apiPrintMsg(MSG_BOOK_NOT_HAS);
;
}
apiPrintMsg(MSG_BOOK_QRY_SCS);
printf();
printf();
( i = ; i < iCount; i++)
{
(pstBookInfo[i].emState)
{
BOOK_IN : iStateIndex = ; ;
BOOK_OUT : iStateIndex = ; ;
: iStateIndex = ;
}
printf(,
pstBookInfo[i].nNum, g_szBookStateString[iStateIndex],
pstBookInfo[i].szName);
}
printf();
}
apiPrintBookHistoryInfo(BOOK_HIS_INFO *pstBookHistroyList, iCount)
{
i = , iOpID = -;
( > iCount)
{
apiPrintMsg(MSG_SYS_INNER_ERROR);
;
}
( NULL == pstBookHistroyList || == iCount)
{
apiPrintMsg(MSG_BOOK_NOT_HAS);
;
}
apiPrintMsg(MSG_BOOK_HIS_SCS);
printf();
printf();
( i = ; i < iCount; i++)
{
( pstBookHistroyList[i].emBookOption )
{
BOOK_OP_IN : iOpID = ; ;
BOOK_OP_OUT : iOpID = ; ;
: apiPrintMsg(MSG_SYS_INNER_ERROR);;
}
( NULL == pstBookHistroyList[i].pstBookInfo )
{
apiPrintMsg(MSG_SYS_INNER_ERROR);
;
}
printf(,
pstBookHistroyList[i].stOptionTime.nHour,
pstBookHistroyList[i].stOptionTime.nMin,
pstBookHistroyList[i].stOptionTime.nSec,
pstBookHistroyList[i].pstBookInfo->nNum,
g_szBookOptionString[iOpID],
pstBookHistroyList[i].pstBookInfo->szName);
}
printf();
}
3
BOOK_INFO g_szBooks[MAX_BOOK_COUNT];
BOOK_HIS_NODE *g_pstHisLogHead = NULL; SYS_TIME GetSysTime()
{
SYS_TIME stSysTime;
time_t ulTime ;
tm *stTime = NULL;
time(&ulTime);
stTime = localtime(&ulTime);
stSysTime.nHour = stTime->tm_hour;
stSysTime.nMin = stTime->tm_min;
stSysTime.nSec = stTime->tm_sec;
stSysTime;
}
FreeHisList(BOOK_HIS_NODE *pstHead)
{
BOOK_HIS_NODE *pstNode = NULL;
( NULL != pstHead)
{
pstNode = pstHead;
pstHead = pstHead->pstNext;
free( pstNode );
}
}
GetBookHisCount(BookNumber nBookBumber)
{
nSize = ;
BOOK_HIS_NODE *pstNode = g_pstHisLogHead;
( DEFAULT_BOOK_ID == nBookBumber )
{
( NULL != pstNode )
{
pstNode = pstNode->pstNext;
nSize ++ ;
}
nSize;
}
( NULL != pstNode )
{
( NULL == pstNode->stBookHisInfo.pstBookInfo )
{
-;
}
nSize += pstNode->stBookHisInfo.pstBookInfo->nNum == nBookBumber? : ;
pstNode = pstNode->pstNext;
}
nSize;
}
BOOK_HIS_INFO *GetHisNodeByBookID(BookNumber nBookNumber, *nCount)
{
i = ;
BOOK_HIS_INFO *pstHisArrHead = NULL;
BOOK_HIS_NODE *pstNode = g_pstHisLogHead;
(*nCount) = GetBookHisCount(nBookNumber);
( > (*nCount) )
{
NULL;
}
pstHisArrHead = (BOOK_HIS_INFO *)malloc((*nCount) * (BOOK_HIS_INFO));
( NULL == pstHisArrHead )
{
(*nCount) = -;
NULL;
}
( DEFAULT_BOOK_ID == nBookNumber )
{
( NULL != pstNode )
{
( NULL == pstNode->stBookHisInfo.pstBookInfo )
{
(*nCount) = -;
free(pstHisArrHead);
NULL;
}
pstHisArrHead[i++] = pstNode->stBookHisInfo;
pstNode = pstNode->pstNext;
}
pstHisArrHead;
}
( NULL != pstNode )
{
( NULL == pstNode->stBookHisInfo.pstBookInfo )
{
(*nCount) = -;
free(pstHisArrHead);
NULL;
}
( nBookNumber == (pstNode->stBookHisInfo.pstBookInfo->nNum) )
{
pstHisArrHead[i++] = pstNode->stBookHisInfo;
}
pstNode = pstNode->pstNext;
}
pstHisArrHead;
}
BOOL AddHisNode(BOOK_INFO *pstBookInfo, BOOK_OP_EM emOption)
{
BOOK_HIS_NODE *pstTmp = NULL;
BOOK_HIS_NODE *pstBookHisNode = (BOOK_HIS_NODE *)malloc((BOOK_HIS_NODE));
( NULL == pstBookHisNode )
{
FALSE;
}
pstBookHisNode->pstNext = NULL;
pstBookHisNode->stBookHisInfo.emBookOption = emOption;
pstBookHisNode->stBookHisInfo.pstBookInfo = pstBookInfo;
pstBookHisNode->stBookHisInfo.stOptionTime = GetSysTime();
( NULL == g_pstHisLogHead )
{
g_pstHisLogHead = pstBookHisNode;
TRUE;
}
pstTmp = g_pstHisLogHead;
( NULL != pstTmp->pstNext )
{
pstTmp = pstTmp->pstNext;
}
pstTmp->pstNext= pstBookHisNode;
TRUE;
}
BOOL IsBookNumRight(BookNumber nBookNumber)
{
( MAX_BOOK_NUMBER >= nBookNumber && MIN_BOOK_NUMBER <= nBookNumber )
{
TRUE;
}
FALSE;
}
BOOK_INFO* GetBookInfoByID(BookNumber nBookNumbe)
{
&(g_szBooks[nBookNumbe-MIN_BOOK_NUMBER]);
}
4
BOOK_INFO g_szBooks[MAX_BOOK_COUNT];
BOOK_HIS_NODE *g_pstHisLogHead = NULL; SYS_TIME GetSysTime()
{
SYS_TIME stSysTime;
time_t ulTime ;
tm *stTime = NULL;
time(&ulTime);
stTime = localtime(&ulTime);
stSysTime.nHour = stTime->tm_hour;
stSysTime.nMin = stTime->tm_min;
stSysTime.nSec = stTime->tm_sec;
stSysTime;
}
FreeHisList(BOOK_HIS_NODE *pstHead)
{
BOOK_HIS_NODE *pstNode = NULL;
( NULL != pstHead)
{
pstNode = pstHead;
pstHead = pstHead->pstNext;
free( pstNode );
}
}
GetBookHisCount(BookNumber nBookBumber)
{
nSize = ;
BOOK_HIS_NODE *pstNode = g_pstHisLogHead;
( DEFAULT_BOOK_ID == nBookBumber )
{
( NULL != pstNode )
{
pstNode = pstNode->pstNext;
nSize ++ ;
}
nSize;
}
( NULL != pstNode )
{
( NULL == pstNode->stBookHisInfo.pstBookInfo )
{
-;
}
nSize += pstNode->stBookHisInfo.pstBookInfo->nNum == nBookBumber? : ;
pstNode = pstNode->pstNext;
}
nSize;
}
BOOK_HIS_INFO *GetHisNodeByBookID(BookNumber nBookNumber, *nCount)
{
i = ;
BOOK_HIS_INFO *pstHisArrHead = NULL;
BOOK_HIS_NODE *pstNode = g_pstHisLogHead;
(*nCount) = GetBookHisCount(nBookNumber);
( > (*nCount) )
{
NULL;
}
pstHisArrHead = (BOOK_HIS_INFO *)malloc((*nCount) * (BOOK_HIS_INFO));
( NULL == pstHisArrHead )
{
(*nCount) = -;
NULL;
}
( DEFAULT_BOOK_ID == nBookNumber )
{
( NULL != pstNode )
{
( NULL == pstNode->stBookHisInfo.pstBookInfo )
{
(*nCount) = -;
free(pstHisArrHead);
NULL;
}
pstHisArrHead[i++] = pstNode->stBookHisInfo;
pstNode = pstNode->pstNext;
}
pstHisArrHead;
}
( NULL != pstNode )
{
( NULL == pstNode->stBookHisInfo.pstBookInfo )
{
(*nCount) = -;
free(pstHisArrHead);
NULL;
}
( nBookNumber == (pstNode->stBookHisInfo.pstBookInfo->nNum) )
{
pstHisArrHead[i++] = pstNode->stBookHisInfo;
}
pstNode = pstNode->pstNext;
}
pstHisArrHead;
}
BOOL AddHisNode(BOOK_INFO *pstBookInfo, BOOK_OP_EM emOption)
{
BOOK_HIS_NODE *pstTmp = NULL;
BOOK_HIS_NODE *pstBookHisNode = (BOOK_HIS_NODE *)malloc((BOOK_HIS_NODE));
( NULL == pstBookHisNode )
{
FALSE;
}
pstBookHisNode->pstNext = NULL;
pstBookHisNode->stBookHisInfo.emBookOption = emOption;
pstBookHisNode->stBookHisInfo.pstBookInfo = pstBookInfo;
pstBookHisNode->stBookHisInfo.stOptionTime = GetSysTime();
( NULL == g_pstHisLogHead )
{
g_pstHisLogHead = pstBookHisNode;
TRUE;
}
pstTmp = g_pstHisLogHead;
( NULL != pstTmp->pstNext )
{
pstTmp = pstTmp->pstNext;
}
pstTmp->pstNext= pstBookHisNode;
TRUE;
}
BOOL IsBookNumRight(BookNumber nBookNumber)
{
( MAX_BOOK_NUMBER >= nBookNumber && MIN_BOOK_NUMBER <= nBookNumber )
{
TRUE;
}
FALSE;
}
BOOK_INFO* GetBookInfoByID(BookNumber nBookNumbe)
{
&(g_szBooks[nBookNumbe-MIN_BOOK_NUMBER]);
}
5
BOOL GetUserCommand()
{
BookNumber nBookNumber = ;
scTmp[MAX_CMD_TMP_LEN + ] = {};
scanf(, scTmp);
scTmp[MAX_CMD_TMP_LEN] = ;
( == strcmp(scTmp, ) )
{
FALSE;
}
( == strcmp(scTmp,))
{
cmdBooksInit();
TRUE;
}
( == strcmp(scTmp, ) )
{
scanf(,&nBookNumber);
cmdBooksBorrow(nBookNumber);
TRUE;
}
( == strcmp(scTmp, ) )
{
scanf(,&nBookNumber);
cmdBooksReturn(nBookNumber);
TRUE;
}
( == strcmp(scTmp, ) )
{
scanf(,&nBookNumber);
cmdBooksQuery(nBookNumber);
TRUE;
}
( == strcmp(scTmp, ) )
{
scanf(,&nBookNumber);
cmdBooksHistory(nBookNumber);
TRUE;
}
apiPrintMsg(MSG_SYS_BAD_CMD);
TRUE;
}
main()
{
BOOL bIsRun = TRUE;
cmdBooksInit();
( TRUE == bIsRun )
{
bIsRun = GetUserCommand();
}
;
}
運行截圖