现在的位置: 首页 > 综合 > 正文

c中printf实现atoi实现

2013年05月16日 ⁄ 综合 ⁄ 共 2689字 ⁄ 字号 评论关闭

 

#include <stdarg.h>
#include <stdio.h>

/*
 * Conver int to string based on radix (usually 2, 8, 10, and 16)
 */
char *itoa(int num, char *str, int radix)
{
    char string[] = "0123456789abcdefghijklmnopqrstuvwxyz";

    char* ptr = str;
    int i;
    int j;

    while (num)
    {
        *ptr++ = string[num % radix];
        num /= radix;

        if (num < radix)
        {
            *ptr++ = string[num];
            *ptr = '\0';
            break;
        }
    }

    j = ptr - str - 1;

    for (i = 0; i < (ptr - str) / 2; i++)
    {
        int temp = str[i];
        str[i] = str[j];
        str[j--] = temp;
    }

    return str;
}

/*
 * A simple printf function. Only support the following format:
 * Code Format
 * %c character
 * %d signed integers
 * %i signed integers
 * %s a string of characters
 * %o octal
 * %x unsigned hexadecimal
 */
int vprintf( const char* format, ...)
{
    va_list arg;
    int done = 0;

    va_start (arg, format);
    //done = vfprintf (stdout, format, arg);

    while( *format != '\0')
    {
        if( *format == '%')
        {
            if( *(format+1) == 'c' )
            {
                char c = (char)va_arg(arg, int);
                putc(c, stdout);
            } else if( *(format+1) == 'd' || *(format+1) == 'i')
            {
                char store[20];
                int i = va_arg(arg, int);
                char* str = store;
                itoa(i, store, 10);
                while( *str != '\0') putc(*str++, stdout);
            } else if( *(format+1) == 'o')
            {
                char store[20];
                int i = va_arg(arg, int);
                char* str = store;
                itoa(i, store, 8);
                while( *str != '\0') putc(*str++, stdout);
            } else if( *(format+1) == 'x')
            {
                char store[20];
                int i = va_arg(arg, int);
                char* str = store;
                itoa(i, store, 16);
                while( *str != '\0') putc(*str++, stdout);
            } else if( *(format+1) == 's' )
            {
                char* str = va_arg(arg, char*);
                while( *str != '\0') putc(*str++, stdout);
            }

            // Skip this two characters.

            format += 2;
        } else {
            putc(*format++, stdout);
        }
    }

    va_end (arg);

    return done;
}

int main(int argc, char* argv[])
{
    int n = 255;
    char str[] = "hello, world!";

    printf("n = %d\n", n);
    printf("n = %i\n", n);
    printf("n = %o\n", n);
    printf("n = %x\n", n);
    printf("first char = %c\n", str[0]);
    printf("str = %s\n", str);
    printf("%s\tn = %d\n", str, n);

    // Test vprintf function

    printf("---------------vprintf--------------\n");
   
    vprintf("n = %d\n", n);
    vprintf("n = %i\n", n);
    vprintf("n = %o\n", n);
    vprintf("n = %x\n", n);
    vprintf("first char = %c\n", str[0]);
    vprintf("str = %s\n", str);
    vprintf("%s\tn = %d\n", str, n);

    return 0;
}
 
---------------------------result:
n = 255
n = 255
n = 377
n = ff
first char = h
str = hello,
hello, n = 255
---------------my_printf--------------
n = 255
n = 255
n = 377
n = ff
first char = h
str = hello,
hello, n = 255

抱歉!评论已关闭.