Use the linked list to solve the problem of too many if statements (C/C ++ implementation)

Source: Internet
Author: User

After implementation with the design pattern, I suddenly found that the so-called design pattern is actually not a linked list in the C language? If the current node can be processed, it cannot be processed by the next node. Not to mention, design message of the code message class. h copy the Code # ifndef MESSAGE_H # define TRUE 1 # define FALSE 0 typedef struct {int sender; int isSend; int isCharge; char date [8];} Message; message * makeMessage (const int sender, const char * date); void setSendFlag (Message * const message); void setChargeFlag (Message * const message ); int isSameDate (const Message * const message, const char * con St date); char * format (const Message * const message); const char * boolStr (const int value); # endif copy code message. c copy the Code # include <assert. h> # include <string. h> # include <stdlib. h> # include <stdio. h> # include "message. h "Message * makeMessage (const int sender, const char * date) {Message * message = (Message *) malloc (sizeof (Message); assert (message! = NULL); message-> sender = sender; message-> isSend = FALSE; message-> isCharge = FALSE; strncpy (message-> date, date, 8 ); return message;} const char * boolStr (const int value) {return value = TRUE? "TRUE": "FALSE";} char * format (const Message * const message) {# define BUF_SIZE 1024 static char buffer [BUF_SIZE]; memset (& buffer, 0, BUF_SIZE); snprintf (char *) & buffer, BUF_SIZE, "Message <% d isSend: % s isCharge: % s> \ n", \ message-> sender, boolStr (message-> isSend), boolStr (message-> isCharge); return (char *) buffer;} void setSendFlag (Message * const message) {message-> isSend = TRUE;} void setCharg EFlag (Message * const message) {message-> isCharge = TRUE;} int isSameDate (const Message * const message, const char * const date) {if (strncmp (message-> date, date, 8) = 0) {return TRUE;} else {return FALSE ;}} copy the code testMessage. c copy the Code # include <stdio. h> # include "message. h "# include" gtest/gtest. h "TEST (MESSAGE, makeMessage) {Message * message = makeMessage (1," 20131212 "); EXPECT_EQ (1, message-> sender); EXPECT_STREQ ("Message <1 isSend: FALSE isCharge: FALSE> \ n", format (message);} copy the implementation node of the code linked list class. h copy the Code # ifndef NOTE_H # define NOTE_H typedef struct Node {void * ptr; struct Node * next;} Node; Node * makeListWithArray (void * array [], int length ); void foreach (Node * list, void (* process) (Node *); # endif copies the code node. c copy the Code # include <stdlib. h> # include <assert. h> # include "node. h "Node * makeListWithArray (void * arra Y [], int length) {int I; Node * last = NULL; assert (array! = NULL & length> 0); for (I = length-1; I> = 0; I --) {Node * node = (Node *) malloc (sizeof (Node); node-> ptr = array [I]; node-> next = last; last = node;} return last ;} void foreach (Node * list, void (* process) (Node *) {Node * current = NULL; assert (list! = NULL & process! = NULL); for (current = list; current! = NULL; current = current-> next) {process (current) ;}} copy the code testNode. c copy the Code # include <stdio. h> # include "node. h "# include" gtest/gtest. h "void printNode (Node * node) {static int I = 0; int data [] = {1, 2, 3}; EXPECT_EQ (data [I], * (int *) node-> ptr); I ++;} TEST (NODE, makeListWithArray) {int I; int data [] = {1, 2, 3 }; void * aSet [] = {& data [0], & data [1], & data [2]}; Node * list = makeListWithArray (aSet, 3 ); foreach (list, printNode);} copy the code program entry implementation (main. c) copy the Code # include <stdio. h> # include <string. h> # include "message. h "# include" node. h "# define FALSE 0 # define TRUE 1 typedef int BOOL; typedef BOOL (* FuncIsAllowSend) (Message *, Node *); BOOL isAllowSendCheckDate (Message * message, Node * node) {FuncIsAllowSend isAllowSend = NULL; if (strcmp (message-> date, "20130101") = 0) {return FALSE;} isAllowSend = (FuncIsAllowSend) node-> next-> ptr; return isAllowSend (message, node-> next);} BOOL isAllowSendCheckWhiteList (Message * message, Node * node) {FuncIsAllowSend isAllowSend = NULL; if (message-> sender = 10) {return TRUE;} isAllowSend = (FuncIsAllowSend) node-> next-> ptr; return isAllowSend (message, node-> next );} BOOL isAllowSendWithDefault (Message * message, Node * node) {setChargeFlag (message); return TRUE;} int main () {Message * message = makeMessage (1, "20131212 "); void * actionList [] = {(void *) & isAllowSendCheckDate, (void *) & isAllowSendCheckWhiteList, (void *) & isAllowSendWithDefault}; Node * theList = makeListWithArray (actionList, sizeof (actionList)/4); FuncIsAllowSend isAllowSend = (FuncIsAllowSend) theList-> ptr; if (isAllowSend (message, theList) = TRUE) {setSendFlag (message );} printf ("% s \ n", format (message);} the copy code style is actually C, however, to use gtest, you have to use g ++ to compile and debug the program. The command is as follows: copy the Code # prerequisites: I have compiled gtest into a library and placed it in the system directory g ++-c message. cg ++-c testMessage. cg ++ message. o testMessage. o-lgtest-lpthread. /. out g ++-c node. cg ++-c testNode. cg ++ node. o testNode. o-lgtest-lpthread. /. out g ++-c main. cg ++ message. o node. o main. o. /. out

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.