dddaniu
crownN
采纳率100%
2015-04-28 14:48 阅读 1.7k
已采纳

小白 求懂的帮忙讲讲

int myprintf(const char *format, ...)
{
va_list ap;
int data;
char str[32] = {0};
char *p;

va_start(ap, format);

while(*format != '\0')
{
    if(*format == '%')
    {
        format++;
        switch(*format)
        {
            case 'x':
                break;
            case 'o':
                break;
            case '%':
                break;
            case 's':
                p = va_arg(ap, char *);
                myputs(p);
                break;
            case 'd':
                data = va_arg(ap, int);
                str[0] = '\0';
                itoa(data, str);
                myputs(str);
                break;
            case 'c':
                data = va_arg(ap, int);
                putchar(data);
                break;
        }
    }
    else
    {
        putchar(*format);
    }

    format++;
}
va_end(ap);

}

int main(void)
{
myprintf("[%c]hello[%d][%s]world\n",
'A', 123, "Jason");

return 0;

}

老师上课的给的一段代码 要把 x o % 下面补全 实在是没思路 谁能大概说下 这段代码编译了 大概理解什么意思 就是x o % 下面不知道怎么补

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2015-04-28 14:51

    对照上面的表,和你现有的代码,无非就是输出整数和%等。
    如果你能采纳我的回答,可以给一些代码

    点赞 1 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2015-04-28 14:50
     d / i  int 接受整数值并将它表示为有符号的十进制整数,i是老式写法
    o   unsigned int    无符号8进制整数(不输出前缀0)
    u
    unsigned int    无符号10进制整数
    x / X
    unsigned int    无符号16进制整数,x对应的是abcdef,X对应的是ABCDEF(不输出前缀0x)
    f(lf)
    float(double)   单精度浮点数用f,双精度浮点数用lf(尤其scanf不能混用)
    e / E
    double  科学计数法表示的数,此处"e"的大小写代表在输出时用的“e”的大小写
    g / G
    double  使用以上两种中最短的形式,大小写的使用同%e和%E
    c
    char    字符型。可以把输入的数字按照ASCII码相应转换为对应的字符
    s / S
    char * / wchar_t *  字符串。输出字符串中的字符直至字符串中的空字符(字符串以'\0‘结尾,这个'\0'即空字符)
    p
    void *  以16进制形式输出指针
    n   int *   到此字符之前为止,一共输出的字符个数,不输出文本
    %
    无输入   
    不进行转换,输出字符‘%’(百分号)本身
    m   无     打印errno值对应的出错内容,(例: printf("%m\n"); ) 
    
    点赞 1 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2015-04-28 17:33
     static int skip_atoi(const char **s)
    {
        int i = 0;
    
        while (isdigit(**s))
            i = i * 10 + *((*s)++) - '0';
        return i;
    }
    
    #define ZEROPAD    1        /* pad with zero */
    #define SIGN    2        /* unsigned/signed long */
    #define PLUS    4        /* show plus */
    #define SPACE    8        /* space if plus */
    #define LEFT    16        /* left justified */
    #define SMALL    32        /* Must be 32 == 0x20 */
    #define SPECIAL    64        /* 0x */
    
    #define __do_div(n, base) ({ \
    int __res; \
    __res = ((unsigned long) n) % (unsigned) base; \
    n = ((unsigned long) n) / (unsigned) base; \
    __res; })
    
    static char *number(char *str, long num, int base, int size, int precision,
             int type)
    {
        /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
        static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
    
        char tmp[66];
        char c, sign, locase;
        int i;
    
        /* locase = 0 or 0x20. ORing digits or letters with 'locase'
         * produces same digits or (maybe lowercased) letters */
        locase = (type & SMALL);
        if (type & LEFT)
            type &= ~ZEROPAD;
        if (base < 2 || base > 36)
            return NULL;
        c = (type & ZEROPAD) ? '0' : ' ';
        sign = 0;
        if (type & SIGN) {
            if (num < 0) {
                sign = '-';
                num = -num;
                size--;
            } else if (type & PLUS) {
                sign = '+';
                size--;
            } else if (type & SPACE) {
                sign = ' ';
                size--;
            }
        }
        if (type & SPECIAL) {
            if (base == 16)
                size -= 2;
            else if (base == 8)
                size--;
        }
        i = 0;
        if (num == 0)
            tmp[i++] = '0';
        else
            while (num != 0)
                tmp[i++] = (digits[__do_div(num, base)] | locase);
        if (i > precision)
            precision = i;
        size -= precision;
        if (!(type & (ZEROPAD + LEFT)))
            while (size-- > 0)
                *str++ = ' ';
        if (sign)
            *str++ = sign;
        if (type & SPECIAL) {
            if (base == 8)
                *str++ = '0';
            else if (base == 16) {
                *str++ = '0';
                *str++ = ('X' | locase);
            }
        }
        if (!(type & LEFT))
            while (size-- > 0)
                *str++ = c;
        while (i < precision--)
            *str++ = '0';
        while (i-- > 0)
            *str++ = tmp[i];
        while (size-- > 0)
            *str++ = ' ';
        return str;
    }
    
    int vsprintf(char *buf, const char *fmt, va_list args)
    {
        int len;
        unsigned long num;
        int i, base;
        char *str;
        const char *s;
    
        int flags;        /* flags to number() */
    
        int field_width;    /* width of output field */
        int precision;        /* min. # of digits for integers; max
                     number of chars for from string */
        int qualifier;        /* 'h', 'l', or 'L' for integer fields */
    
        for (str = buf; *fmt; ++fmt) {
            if (*fmt != '%') {
                *str++ = *fmt;
                continue;
            }
    
            /* process flags */
            flags = 0;
         repeat:
            ++fmt;        /* this also skips first '%' */
            switch (*fmt) {
            case '-':
                flags |= LEFT;
                goto repeat;
            case '+':
                flags |= PLUS;
                goto repeat;
            case ' ':
                flags |= SPACE;
                goto repeat;
            case '#':
                flags |= SPECIAL;
                goto repeat;
            case '0':
                flags |= ZEROPAD;
                goto repeat;
            }
    
            /* get field width */
            field_width = -1;
            if (isdigit(*fmt))
                field_width = skip_atoi(&fmt);
            else if (*fmt == '*') {
                ++fmt;
                /* it's the next argument */
                field_width = va_arg(args, int);
                if (field_width < 0) {
                    field_width = -field_width;
                    flags |= LEFT;
                }
            }
    
            /* get the precision */
            precision = -1;
            if (*fmt == '.') {
                ++fmt;
                if (isdigit(*fmt))
                    precision = skip_atoi(&fmt);
                else if (*fmt == '*') {
                    ++fmt;
                    /* it's the next argument */
                    precision = va_arg(args, int);
                }
                if (precision < 0)
                    precision = 0;
            }
    
            /* get the conversion qualifier */
            qualifier = -1;
            if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
                qualifier = *fmt;
                ++fmt;
            }
    
            /* default base */
            base = 10;
    
            switch (*fmt) {
            case 'c':
                if (!(flags & LEFT))
                    while (--field_width > 0)
                        *str++ = ' ';
                *str++ = (unsigned char)va_arg(args, int);
                while (--field_width > 0)
                    *str++ = ' ';
                continue;
    
            case 's':
                s = va_arg(args, char *);
                len = strnlen(s, precision);
    
                if (!(flags & LEFT))
                    while (len < field_width--)
                        *str++ = ' ';
                for (i = 0; i < len; ++i)
                    *str++ = *s++;
                while (len < field_width--)
                    *str++ = ' ';
                continue;
    
            case 'p':
                if (field_width == -1) {
                    field_width = 2 * sizeof(void *);
                    flags |= ZEROPAD;
                }
                str = number(str,
                     (unsigned long)va_arg(args, void *), 16,
                     field_width, precision, flags);
                continue;
    
            case 'n':
                if (qualifier == 'l') {
                    long *ip = va_arg(args, long *);
                    *ip = (str - buf);
                } else {
                    int *ip = va_arg(args, int *);
                    *ip = (str - buf);
                }
                continue;
    
            case '%':
                *str++ = '%';
                continue;
    
                /* integer number formats - set up the flags and "break" */
            case 'o':
                base = 8;
                break;
    
            case 'x':
                flags |= SMALL;
            case 'X':
                base = 16;
                break;
    
            case 'd':
            case 'i':
                flags |= SIGN;
            case 'u':
                break;
    
            default:
                *str++ = '%';
                if (*fmt)
                    *str++ = *fmt;
                else
                    --fmt;
                continue;
            }
            if (qualifier == 'l')
                num = va_arg(args, unsigned long);
            else if (qualifier == 'h') {
                num = (unsigned short)va_arg(args, int);
                if (flags & SIGN)
                    num = (short)num;
            } else if (flags & SIGN)
                num = va_arg(args, int);
            else
                num = va_arg(args, unsigned int);
            str = number(str, num, base, field_width, precision, flags);
        }
        *str = '\0';
        return str - buf;
    }
    
    int sprintf(char *buf, const char *fmt, ...)
    {
        va_list args;
        int i;
    
        va_start(args, fmt);
        i = vsprintf(buf, fmt, args);
        va_end(args);
        return i;
    }
    
    int printf(const char *fmt, ...)
    {
        char printf_buf[1024];
        va_list args;
        int printed;
    
        va_start(args, fmt);
        printed = vsprintf(printf_buf, fmt, args);
        va_end(args);
    
        puts(printf_buf);
    
        return printed;
    }
    
    点赞 1 评论 复制链接分享

相关推荐