C与指针第七章编程练习

#include <stdio.h>
#include <stdlib.h>
int hermite( int n, int x)
{
    int iRet = 0;
    if (n == 0)
        iRet += 1;
    else if (n == 1)
        iRet = iRet + (2 * x);
    else
    {
        iRet = 2 * x * hermite(n - 1, x) - 2 * (n - 1) * hermite(n - 2, x);
    }

    return iRet;
}
int main()
{
    printf("%d\n", hermite( 3, 2));
    system("pause");
    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
int gcd(int M, int N)
{
    int iRet = 0;
    if (M > 0 && N > 0)
    {
        int R = M % N;
        if (R == 0)
            iRet = N;
        else if (R < 0)
            iRet = 0;
        else
            iRet = gcd(M, R);
    }
    return iRet;
}
int main()
{
    printf("%d\n", gcd(25, 15));
    system("pause");
    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
// 递归是先进后出,所以这里不用递归
int ascii_to_integer(char *string)
{
    int iRet = 0;
    while (*string != '\000')
    {
        iRet = iRet * 10;
        if (*string >= '0' && *string <= '9')
        {
            iRet = iRet + (*string - '0');
        }
        ++string;
    }

    return iRet;
}
int main()
{
    char chArray[8] = {'z', '5', 'u', '4', 'd', 'p', '7', '\000'};
    int iR = ascii_to_integer(chArray);
    printf("%d\n", iR);
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>

int max_list(int *piArray)
{
    int iRet = 0;
    int iMax = 0;
    if (*(piArray + 1) >= 0)
    {
        iMax = (*piArray > *(piArray + 1)) ? *piArray : *(piArray + 1);
        iRet = max_list(++piArray);        
    }

    return iMax > iRet ? iMax : iRet;
}
int main()
{
    int iArray[8] = {1, 2, 8, 4, 9, 6, 7, -8};
    int iR = max_list(iArray);
    printf("%d\n", iR);
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>  //处理可变参数列表

void print_char(char chValue)
{
    putchar(chValue);
}

void print_integer(int iValue)
{
    //按位打印
    int iValLen = 0;
    int iVal = iValue;
    while (iVal > 0)
    {
        ++iValLen;
        iVal /= 10;
    }

    iVal = iValue;
    char *pchArray = (char *)malloc(sizeof(char) * iValLen);
    for (int i = 0; i < iValLen; ++i)
    {
        *(pchArray + iValLen - 1 - i) = iVal % 10 + '0';
        iVal /= 10;
    }

    for (int i = 0; i < iValLen; ++i)
    {
        putchar(*(pchArray + i));
    }
}

void print_float(float fValue)
{
    char chArray[8] = {'0','0','0','0','0','0','0','0'}; // 考虑到负号和小数点 所以是2 + 6 = 8
    // 取6位有效数字
    int iOffset = 0;
    if (fValue < 0) 
    {
        *(chArray + iOffset) = '-';
        ++iOffset;
        fValue = fValue * (-1);
    }
    int iVal = (int)fValue; // 强制转换得到整型值
    // 得到整型值的位数
    char chTemp[6];
    int i = 0;
    while (iVal > 0 || i > 5)
    {
        chTemp[i] = iVal % 10 + '0';
        iVal /= 10;
        ++i;
    }

    // 将余数赋值到数组
    while (i > 0)
    {
        --i;
        *(chArray + iOffset) = chTemp[i];
        ++iOffset;
        iVal /= 10;
    }
    // 加上小数点的条件
    if (iOffset < 6 || (*chArray == '-' && iOffset == 6))
    {
        *(chArray + iOffset) = '.';
        ++iOffset;
    }

    for (i = 0; i < (8 - 1 - iOffset); ++i)
    {
        fValue -= (int)fValue;
        fValue *= 10;
        *(chArray + iOffset) = (int)(fValue) + '0';
        ++iOffset;
    }
    *(chArray + iOffset) = '\000';
    // 取每一位的值保存到数组里
    puts(chArray);
}

void print_string(char *pchValue)
{
#if 1
    puts(pchValue);
#elif 0
    int i = 0;
    while (*(pchValue + i) != '\000')
    {
        putchar(*(pchValue + i));
        ++i;
    }
#endif
}
void My_Printf(char *pchFormat, ...)
{
    va_list listValue;
    char *pString;
    int iValue;
    float fValue;
    char chValue;

    va_start(listValue, pchFormat);
    int i = 0;
    char chDis = *(pchFormat + i);
    while (chDis != '\000')
    {
        if (chDis != '%')
        {
            putchar(chDis);
            if (chDis == '\n')
                break;
        }
        else
        {
            ++i;
            chDis = *(pchFormat + i);
            switch (chDis)
            {
            case 'c':
                chValue = va_arg(listValue, int);
                print_char(chValue);
                break;
            case 'd':
                iValue = va_arg(listValue, int);
                print_integer(iValue);
                break;
            case 'f':
                fValue = va_arg(listValue, double);
                print_float(fValue);
                break;
            case 's':
                pString = va_arg(listValue, char *);
                print_string(pString);
                break;
            default:
                break;
            }
        }
        ++i;
        chDis = *(pchFormat + i);
    }
    va_end(listValue);
}
int main()
{
    char chVal = 'M';
    int iVal = 10;
    double dVal = 5.3;
    char *pchVal = "World\000";
    My_Printf("Hello %c\n", chVal);
    My_Printf("What %d\n", iVal);
    My_Printf("Money %f\n", dVal);
    My_Printf("Go %s\n", pchVal);
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *st_pchUnit_One[20] = {
    "", "ONE ", "TWO ", "THREE ", "FOUR ", "FIVE ", "SIX ", "SEVEN ", "EIGHT ", "NINE ", "TEN ",
    "ELEVEN ", "TWELVE ", "THIRTEEN ", "FOURTEEN ", "FIVTEEN ", "SIXTEEN ", "SEVENTEEN ",
    "EIGHTEEN ", "NINTEEN "};

static char *st_pchUnit_Ten[10] = {
    "", "", "TWENTY ", "THIRTY ", "FORTY ", "FIFTY ", "SIXTY ", "SEVENTY ", "EIGHTY ", "NINETY "};

static char *st_pchUnit_Thousand[4] = {"", "THOUSAND ", "MILLION ", "BILLION "};

void GetBuffer(unsigned int amount, char **pchUnit_Thousand, char *buffer)
{
    // 区分是1000 100 10 以内
    unsigned int uiVal = amount / 1000;
    if (uiVal > 0) // 判断是否大于1000
        GetBuffer(uiVal, pchUnit_Thousand + 1, buffer);
    
    // 进入这里表示递归结束 amount剩下的一定是0-999的值
    amount = amount % 1000;
    uiVal = amount / 100;
    
    if (uiVal > 0 && uiVal < 10) // 判断是否大于100 100-999
    {
        strcat(buffer, st_pchUnit_One[uiVal]);
        strcat(buffer, "HUNDRED ");
        amount = amount % 100; // amount 10-99
    }

    if (amount > 19) // 判断是否大于10 10-99
    {
        strcat(buffer, st_pchUnit_Ten[amount / 10]);
        amount = amount  % 10;
    }

    strcat(buffer, st_pchUnit_One[amount]);
    strcat(buffer, *pchUnit_Thousand);
}

void written_amount(unsigned int amount, char *buffer)
{
    char **pchThousands = st_pchUnit_Thousand;
    if (amount == 0)
        strcpy(buffer, "ZERO");
    else
        GetBuffer(amount, pchThousands, buffer);
}

int main()
{
    int iAmount = 16000321;
    char chArray[1000];
    written_amount(iAmount, chArray);
    puts(chArray);

    system("pause");
    return EXIT_SUCCESS;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值