DOC

C.Plus.Plus.ProgramSummary

By Kevin Mills,2014-11-25 12:20
12 views 0
C.Plus.Plus.ProgramSummary

    The programming summary

    (1). calculate the N! Method 1:

    Calculate the factorial by recursion function.

    1. long factorial(int n)

    2. {

    3. long result;

    4. if(n < 0)

    5. return 0;

    6. else if(n == 1||n == 0)

    7. return result=1;

    8. else

    9. {

    10. result=n*factorial(n-1);

    11. return result;

    12. }

    13. }

Method 2:

    Calculate the factorial by for cycle.

    14. long factor(int n)

    15. {

    16. long result=0;

    17. if(n<0)

    18. return 0;

    19. else if(n==0||n==1)

    20. return result=1;

    21. else

    22. {

    23. for(result=n;n>1;n--)

    24. {

    25. result=result*(n-1);

    26. }

    27. return result;

    28. }

    29. }

    Method 1 compare with Method 2: Method 2 is more efficienty than method 1 in this question.

    (2). Print the n of sequence:1,1,2,3,5,8,13,21,…… int sequence(int n)

    {

     int sn;

     if(n<=0)

     return 0;

     else if(n==1||n==2)

     return sn=1;

     else

     {

     sn=sequence(n-1)+sequence(n-2);

     return sn;

     }

    }

void print(int n)

    {

     int i, sn;

     for(i=1;i<=n;i++)

     {

     sn=sequence(i);

     printf("%d, ", sn);

     }

     printf("\n");

    }

void printlist(int n)

    {

     int i, j, sn;

     for(i=1;i<=n;i++)

     {

     for(j=1;j<=i;j++)

     {

     sn=sequence(j);

     printf("%d, ", sn);

     }

     printf("\n");

     }

     printf("\n");

    }

    (3). Rewrite the function strlen()

    int GetLenth(const char *p) {

     If(p==NULL)

     {

     Return 0;

    }

     int lenth=0;

     while(p[lenth]!='\0'){

     //stop circle when get to end

     lenth++;

     //lenth of array add 1

     }

     return lenth;

    }

    /***

    *strlen - return the length of a null-terminated string

*

    *Purpose:

    * Finds the length in bytes of the given string, not including

    * the final null character.

    *

    *Entry:

    * const char * str - string whose length is to be computed

    *

    *Exit:

    * length of the string "str", exclusive of the final null byte

    *

    *Exceptions:

    *

    *****************************************************************************

    **/

size_t __cdecl strlen (

     const char * str

     )

    {

     const char *eos = str;

     while( *eos++ ) ;

     return( eos - str - 1 );

    }

    (4). Rewrite the function strcpy() int StringCpy(char *dest, const char *sour)

    {

     int i, j;

     if(dest==NULL||sour==NULL)

     //judge destination or source pointer if or not null

     return -1;

     else

     {

     for(i=0,j=0;sour[j]!='\0';i++,j++)

     {

     dest[i] = sour[j];

     //copy sour into dest pointer

     }

     dest[i]='\0';

     //copy '\0' to end of dest pointer

     return 0;

     }

    }

    /***

    *char *strcpy(dst, src) - copy one string over another *

    *Purpose:

    * Copies the string src into the spot specified by * dest; assumes enough room.

*

    *Entry:

    * char * dst - string over which "src" is to be copied

    * const char * src - string to be copied over "dst"

    *

    *Exit:

    * The address of "dst"

    *

    *Exceptions:

    *****************************************************************************

    **/

char * __cdecl strcpy(char * dst, const char * src)

    {

     char * cp = dst;

     while( *cp++ = *src++ )

     ; /* Copy src over dst */

     return( dst );

    }

    Char * strcpy(char * dst, const char* src) {

     Char* cp = dst;

     While(*dst!=\0)

     {

     *cp++ = *src++;

    }

    *cp = \0;

     Return dst;

    }

    (5). Rewrite the function strcat()

    char * StringCat(char *str1, char *str2)

    {

     int i=0, j=0;

     char *p = new char[strlen(str1)+strlen(str2)+1];

     //apply memory sum of str1 and str2

     while(str1[j]!='\0')

     { //copy str1

     p[i++]=str1[j++];

     }

     j=0;

     while(str2[j]!='\0')

     { //copy str2

     p[i++]=str2[j++];

     }

     p[i]='\0';

     //copy '\0' to end

     return p;

    }

    /***

    *char *strcat(dst, src) - concatenate (append) one string to another

    *

    *Purpose:

    * Concatenates src onto the end of dest. Assumes enough

    * space in dest.

    *

    *Entry:

    * char *dst - string to which "src" is to be appended

    * const char *src - string to be appended to the end of "dst"

    *

    *Exit:

    * The address of "dst"

    *

    *Exceptions:

    *

    *****************************************************************************

    **/

char * __cdecl strcat (

     char * dst,

     const char * src

     )

    {

     char * cp = dst;

     while( *cp )

     cp++; /* find end of dst */

     while( *cp++ = *src++ ) ; /* Copy src to end of dst */

     return( dst ); /* return dst */

}

    Char* strcat(char* dst, const char* src) {

     If(dst==NULL||src==NULL)

     {

     Return NULL;

    }

     Char* cp = dst;

     While(*cp!=\0)

     {

     cp++;

    }

    While(*str!=\0)

    {

     *cp++ = *src++;

    }

    *cp=\0;

    Return dst;

    }

    (6). Rewrite the function tolower()

    int ToLower(int ch)

    {

     if(ch<='Z'&&ch>='A')

     //judge the input char

     return ch+32;

     else

     return -1; }

Char* ToLower(char* str)

    {

     If(str==NULL)

     Return NULL;

     Char* cp = str;

     While(*cp!=\0)

     {

     If(*cp>A&&*cp<Z)

     {

     *cp += 32;

    }

    cp++;

     }

     Return str;

    }

    (7). Reverse order of a list

    class Node

    {

     friend class List; public:

     Node():next(NULL)

     {

     }

     Node(int data):data(data),next(NULL)

     {

     printf("Node(int)\n");

     }

     ~Node()

     {

     printf("~Node()\n");

     }

private:

     int data;

     Node* next;

    };

class List

    {

    public:

     List():head(NULL)

     {

     printf("List()\n");

     }

     ~List() //destroy memory at heap

     {

     Node* cur = head;

     while(cur != NULL)

     {

     head = head->next;

     delete cur;

     cur = head;

     }

     printf("~List()\n");

     }

     void AddHead(int data) //add new node in front of head node

     {

     Node* node = new Node(data);

     node->next = head;

     head = node;

     }

     void AddTrail(int data) //add new node in end of list

     {

     Node* node = new Node(data);

     if(head == NULL) //if list empty

     {

     head = node;

     return;

     }

     Node* cur = head;

     Node* pre = NULL;

     while(cur != NULL) //spread all over

     {

     pre = cur;

     cur = cur->next;

     }

     pre->next = node; //add new node in end

     }

     void Reverse() //reverse order the list

     {

     Node* cur = head;

     Node* pre = NULL;

     if(cur == NULL||cur->next==NULL) //list empty OR only one node

     {

     return;

     }

     head = head->next; //put frist node into last node

     pre = head->next;

     head->next = cur;

     cur->next = NULL;

     cur = head;

     while(pre != NULL) //cycle and reverse each node

     {

     head = pre;

     pre = pre->next;

     head->next = cur;

     cur = head;

     }

     }

     void Print() //print the data in order

     {

     Node* cur = head;

     if(cur == NULL) //list empty

     {

     printf("The list is empty!\n");

     return;

     }

     while(cur != NULL) //cycle print

     {

     printf("%d,",cur->data);

     cur = cur->next;

     }

     printf("\n");

     }

private:

     Node* head;

    };

    (8). Order a arry by ebullition(冒泡法排序). void ebullitionSort()

    {

     int str[] = {89, 45, 65, 11, 23, 54, 87, 3, 65,

     34,34,56,76,345,213,54,65,765,76,85,24,13,42,97};

     int length = sizeof(str)/sizeof(int);

     int i, j;

     for(j=1;j<=length;j++)

     {

     for(i=0;i<length-j;i++) //

     {

     if(str[i] > str[i+1])

     {

     int temp = str[i];

     str[i] = str[i+1];

     str[i+1] = temp;

     }

     }

     }

     for(i=0;i<length;i++)

     {

     printf("%d,",str[i]);

     }

     printf("\n");

    }

    (9). Design a function, which can reverse order all letter in

    given string.

    Method one:

    Firstly, arr[0] swap with arr[n-1], and then arr[1] swap with arr[n-

    2],afterthen take turns swap till to the middle element.

    void ReverseArry(char* arr) //

    {

     if(arr == NULL)

     {

     return;

     }

     int length = strlen(arr);

     int i,j;

     for(i=0,j=length-1;i!=length/2;i++,j--)

     { //swap frist and last

     char temp = arr[i];

     arr[i] = arr[j];

     arr[j] = temp;

     }

    }

Method two:

    Using nestd for cycle, put the first element swap back of border upon

    element in turn, untill the last element. Repeat above process in

    outside cycle.

    void RevArry(char* arr)

    {

     if(arr == NULL)

     {

     return;

     }

     int lenth = strlen(arr);

     int i,j;

     for(i=1;i<lenth;i++)

     {

     for(j=0;j<lenth-i;j++)

Report this document

For any questions or suggestions please email
cust-service@docsford.com