猫史档案馆


【python模仿分享】中仿print函数

用户:程序侯程序侯查看:5 回复:5 评论:5 创建时间:2020-08-05T23:45:47


(其实,还有两个参数没我没都填进去,一个是输出的位置,一个是我也不懂,因为没法实现和不懂没有加进去)

def printf(var):
    #我们假设就用C编写python
    print(var, end = '')
def Print(*var, sep = ' ', end = '\n'):
    for i in range(len(var)):
        printf(var[i])
        if i < len(var) - 1:
            printf(sep)
        else:
            printf(end)


回复

上一页1 页 / 共 1下一页
程序侯程序侯

Print(1, 2, 3, 4, 5, 6, 7, 8, 9, sep = '+', end = '=45')

能看懂吗哈哈

点赞0


评论


蒟蒻OIer1048576蒟蒻OIer1048576

def Print(*args,sep=" ",end="\n"):
    for i in args[:-1]:
        printf(i+sep)
    print(args[-1]+end)

点赞0


评论


程序侯程序侯

def Print(*args,sep=" ",end="\n"):
    for i in args[:-1]:
        printf(i)
        printf(sep)
    printf(args[-1])
    printf(end)

点赞0


评论


tq_xyytq_xyy

def print(*values, sep=' ', end='\n', file=sys.stdout, flush=False):
    file.write(sep.join([str(value) for value in values]) + end)
    if flush:
        file.flush()

print的完整写法

内部是调用C标准库的printf函数实现的。

static char sprint_buf[1024];
int printf(const char *fmt, ...)
{
    va_list args;
    int n;
    va_start(args, fmt);
    n = vsprintf(sprint_buf, fmt, args);
    va_end(args);
    if (console_ops.write)
        console_ops.write(sprint_buf, n);
    return n;
}

int vsprintf(char *buf, const char *fmt, va_list args)
{
    int len;
    unsigned long long num;
    int i, base;
    char * str;
    const char *s;
    int flags;
    int field_width;
    int precision;
    int qualifier; 
    for (str=buf ; *fmt ; ++fmt)
    {
        if (*fmt != '%') 
        {
            *str++ = *fmt;
            continue;
        }
        flags = 0;
        repeat:
            ++fmt; 
            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;
            }
        field_width = -1;
        if ('0' <= *fmt && *fmt <= '9')
            field_width = skip_atoi(&fmt);
        else if (*fmt == '*')
        {
            ++fmt;/*skip '*' */
            /* it's the next argument */
            field_width = va_arg(args, int);
            if (field_width < 0) {
                field_width = -field_width;
                flags |= LEFT;
            }
        }
        precision = -1;
        if (*fmt == '.')
        {
            ++fmt;
            if ('0' <= *fmt && *fmt <= '9')
                precision = skip_atoi(&fmt);
            else if (*fmt == '*')
            {
                ++fmt;
                /* it's the next argument */
                precision = va_arg(args, int);
            }
            if (precision < 0)
                precision = 0;
        }
        qualifier = -1;
        if (*fmt == 'l' && *(fmt + 1) == 'l')
        {
            qualifier = 'q';
            fmt += 2;
        }
        else if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L'|| *fmt == 'Z')
        {
            qualifier = *fmt;
            ++fmt;
        }
        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 *);
                if (!s)
                    s = "";
                    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 if (qualifier == 'Z')
            {
                size_t * ip = va_arg(args, size_t *);
                *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 |= LARGE;
            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);
            if (flags & SIGN)
                num = (signed long) num;
        }
        else if (qualifier == 'q')
        {
            num = va_arg(args, unsigned long long);
            if (flags & SIGN)
                num = (signed long long) num;
        }
        else if (qualifier == 'Z')
        {
            num = va_arg(args, size_t);
        }
        else if (qualifier == 'h')
        {
            num = (unsigned short) va_arg(args, int);
            if (flags & SIGN)
                num = (signed short) num;
        }
        else
        {
            num = va_arg(args, unsigned int);
            if (flags & SIGN)
                num = (signed int) num;
        }
        str = number(str, num, base, field_width, precision, flags);
    }
    *str = '/0';
    return str-buf;
}

c函数库printf的实现

点赞0


评论


lsk666666lsk666666

其实还可以用sys.stdout.write,也是不会进行换行的

点赞0


评论