Software Engineering: Summary of C coding practices, and summary of c Coding

Source: Internet
Author: User
Tags sub command

Software Engineering: Summary of C coding practices, and summary of c Coding
Experiment 4: the reusable linked list module is used to implement the experiment requirements of the command line menu applet V2.5.

The Reusable linked list module is used to implement the command line menu applet. When executing a command, a specific function is called as the execution action. The interface design of the linked list module must be general enough, the function of the command line menu applet remains unchanged; the general Linktable module can be integrated into our menu program;
Interface specifications;
Experiment Process

1. Create a remote repository and download it to your local device.

git clone https://github.com/libaoquan95/seClass_lab4.git

Go to the version library and create the menu. c linktable. h linktable. c file.

2 linktable. h

#ifndef _LINK_TABLE_H_#define _LINK_TABLE_H_#include 
 
  #define  SUCCESS 0#define  FAILURE (-1)/* * LinkTableNode Node Type */typedef struct LinkTableNode{    struct LinkTableNode *pNext;}tLinkTableNode;/* * LinkTableNode Type */typedef struct LinkTable{    tLinkTableNode * pHead;    tLinkTableNode * pTail;    int              SumOfNode;}tLinkTable;tLinkTable * CreateLinkTable();int          DelLinkTable(tLinkTable* pLinkTable);int          AddLinkTable(tLinkTable* pLinkTable,tLinkTableNode *pNode);int          DelLinkTableNode(tLinkTable* pLinkTable,tLinkTableNode *pNode);tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable);tLinkTableNode * GetNextLinkTableNode(tLinkTable* pLinkTable,tLinkTableNode *pNode);#endif
 

3 linktable. c

#include 
 
  #include 
  
   #include 
   
    #include "linktable.h"tLinkTable * CreateLinkTable(){    tLinkTable *pLinkTable=(tLinkTable*)malloc(sizeof(tLinkTable));    if (pLinkTable == NULL)    {        return NULL;        printf("create linktable failure! ");    }    pLinkTable->pHead = NULL;    pLinkTable->pTail = NULL;    pLinkTable->SumOfNode = 0;    return pLinkTable;}int DelLinkTable(tLinkTable* pLinkTable){    if(pLinkTable == NULL)    {        return FAILURE;    }    while(pLinkTable->pHead != NULL)    {        tLinkTableNode *p = pLinkTable->pHead;        pLinkTable->pHead = p->pNext;        free(p);    }    pLinkTable->pHead = NULL;    pLinkTable->pTail = NULL;    pLinkTable->SumOfNode = 0;    free(pLinkTable);    return SUCCESS;}int AddLinkTable(tLinkTable* pLinkTable,tLinkTableNode *pNode){     if(pLinkTable == NULL || pNode == NULL)    {        return FAILURE;    }    if(pLinkTable->pHead == NULL && pLinkTable->pTail == NULL)    {        pLinkTable->pHead = pNode;        pLinkTable->pTail = pNode;        pLinkTable->pTail = NULL;        pLinkTable->SumOfNode = 1;    }    else    {        pLinkTable->pTail->pNext = pNode;        pLinkTable->pTail = pNode;        pLinkTable->pTail->pNext = NULL;        pLinkTable->SumOfNode ++;    }    return SUCCESS;}int DelLinkTableNode(tLinkTable* pLinkTable,tLinkTableNode *pNode){    if (pLinkTable == NULL)    {        return FAILURE;    }    tLinkTableNode *pWork = pLinkTable->pHead;    tLinkTableNode *pre = pWork;    if (pLinkTable->pHead == pNode)    {        pLinkTable->pHead = pWork->pNext;        free(pWork);        return SUCCESS;    }    while (pWork != NULL)    {        if(pWork == pNode)        {            pre->pNext = pWork->pNext;            free(pWork);            return SUCCESS;        }        pre = pWork;        pWork = pWork->pNext;    }    return FAILURE;}tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable){    if (pLinkTable->pHead == NULL)    {        printf("LinkTable is empty\n");        return NULL;    }    return pLinkTable->pHead;}tLinkTableNode * GetNextLinkTableNode(tLinkTable* pLinkTable,tLinkTableNode *pNode){    if (pLinkTable == NULL || pNode == NULL)    {        printf("LinkTable is empty\n");        return NULL;    }    tLinkTableNode *pWork = pLinkTable->pHead;    while(pWork != NULL)    {        if(pWork == pNode)            return pWork->pNext;        pWork = pWork->pNext;    }    return NULL;}
   
  
 

4 menu. c

/** Copyright (C) libaoquan95@github.com, 2017-2018 ** File name: menu. c * Principal author: libaoquan95 * Subsystem name: menu * Module name: menu * Language: C * Date of first release: Pushed /10/09 * Deacription: This is a menu program */# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include "linktable. h "# define CMD_MAX_LEN 128 # define DESC_LEN 1024 # define CMD_NUM 10int PrintCommand (); int PrintSystemTime (); int PrintCurrentWorkingDirectory (); int Add (); int Sub (); int Mul (); int Div (); int Quit (); typedef struct DataNode {tLinkTableNode * pNext; char * cmd; char * desc; int (* handler )();} tDataNode; tDataNode * FindCmd (tLinkTable * head, char * cmd) {tDataNode * pNode = (tDataNode *) GetLinkTa BleHead (head); while (pNode! = NULL) {if (strcmp (pNode-> cmd, cmd) = 0) {return pNode;} pNode = (tDataNode *) GetNextLinkTableNode (head, (tLinkTableNode *) pNode);} return NULL;} int ShowAllCmd (tLinkTable * head) {tDataNode * pNode = (tDataNode *) GetLinkTableHead (head); printf ("counter \ n "); while (pNode! = NULL) {printf ("\ t % s: \ t % s \ n", pNode-> cmd, pNode-> desc ); pNode = (tDataNode *) GetNextLinkTableNode (head, (tLinkTableNode *) pNode);} printf ("records \ n"); return 0 ;} static tDataNode menu [] ={{ (tLinkTableNode *) & menu [1], "help", "Print all command of menu", PrintCommand}, {(tLinkTableNode *) & menu [2], "time", "Show system time", PrintSyst EmTime}, {(tLinkTableNode *) & menu [3], "pwd", "Show current working directory", PrintCurrentWorkingDirectory}, {(tLinkTableNode *) & menu [4], "add", "Calculate the summarize of the two integer numbers", Add}, {(tLinkTableNode *) & menu [5], "sub ", "Calculate the subtractions of the two integer numbers", Sub}, {(tLinkTableNode *) & menu [6], "mul", "Calculate the multiplication of the two integer numbers ", mul}, {(TLinkTableNode *) & menu [7], "p", "Calculate the pision of the two integer numbers", Div}, {(tLinkTableNode *) NULL, "quit", "Exit menu program", Quit }}; int InitMenuData (tLinkTable ** ppLinkTable) {* ppLinkTable = CreateLinkTable (); (* ppLinkTable) -> pHead = (tLinkTableNode *) & menu [0]; (* ppLinkTable)-> pTail = (tLinkTableNode *) & menu [7]; (* ppLinkTable) -> SumOfNode = 8;} tLinkTable * head = NULL; int main () {I NitMenuData (& head); while (1) {char cmd [CMD_MAX_LEN]; printf ("$ menu>"); scanf ("% s", cmd ); tDataNode * p = FindCmd (head, cmd); if (p = NULL) {printf ("ERROR: This is not a command, you can input 'help' to find command \ n "); continue;} if (p-> handler! = NULL) {p-> handler () ;}}/*** print all command and it's information * @ param none * @ return none */int PrintCommand () {ShowAllCmd (head); return 0;}/*** print current time * @ param none * @ return none */int PrintSystemTime () {struct tm * ptr; time_t it; it = time (NULL); ptr = localtime (& it); printf ("% 4d % 02d % 02d % d: % d \ n ", ptr-> maid + 1900, ptr-> tm_mon + 1, ptr-> tm_mday, ptr-> tm_hour, pt R-> tm_min, ptr-> tm_sec); return 0;}/*** print current working directory path * @ param none * @ return none */int PrintCurrentWorkingDirectory () {char buf [256]; getcwd (buf, sizeof (buf); printf ("current path: % s \ n", buf); return 0 ;} /*** calculate add of two interage numbers * @ param none * @ return calculate result */int Add () {int num1 = 0, num2 = 0; printf ("input number1:"); scanf ("% d", & num1); printf (" Input number2: "); scanf (" % d ", & num2); printf (" % d + % d = % d \ n ", num1, num2, num1 + num2); return 0;}/*** calculate sub of two interage numbers * @ param none * @ return calculate result */int Sub () {int num1 = 0, num2 = 0; printf ("input number1:"); scanf ("% d", & num1); printf ("input number2:"); scanf ("% d ", & num2); printf ("% d-% d = % d \ n", num1, num2, num1-num2); return 0 ;}/ *** calculate mul Of two interage numbers * @ param none * @ return calculate result */int Mul () {int num1 = 0, num2 = 0; printf ("input number1 :"); scanf ("% d", & num1); printf ("input number2:"); scanf ("% d", & num2 ); printf ("% d * % d = % d \ n", num1, num2, num1 * num2); return 0 ;} /*** calculate p of two interage numbers * @ param none * @ return calculate result */int Div () {int num1 = 0, num2 = 0; printf ("input numbe R1: "); scanf (" % d ", & num1); printf (" input number2: "); scanf (" % d ", & num2); if (num2! = 0) {printf ("% d/% d = % d \ n", num1, num2, num1/num2);} else {printf ("ERROR: don't pision 0 \ n ");} return 0;}/*** quit command * @ param none * @ return none */int Quit () {exit (0); return 0 ;}
     
    
   
  
 

5. Compile and run the program
5.1 compile

gcc linktable.h linktable.c menu.c -o menu./menu


5.2 run
Help Command

Time Command

Pwd command

Add command

Sub command

Mul command

P command

Incorrect command

Quit command

6. upload to the remote version Library

Experiment Summary

Through the modular design of menu program, I learned a lot of software engineering ideas and realized the great role of modularization.

In this experiment, a general linked list module is designed, and the specific data types are specified during the call, which reflects the idea of high clustering and low coupling, and the interfaces are more standardized, make our code more universal and easier to manage.

In the experiment, we realized the importance of functional interface specifications for reusable and some methods of code design specifications. In the compilation process, I also learned the link knowledge and gained a better understanding of the gcc compiler.

Related Article

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.