DOC

C.Plus.Plus.ProgramSummary

By Kevin Mills,2014-11-25 12:20
13 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!=