标题:VC++6.0上写了个printf类似的程序,欢迎提出改进方法.
只看楼主
young
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:223
专家分:160
注 册:2004-9-5
结帖率:100%
 问题点数:0 回复次数:6 
VC++6.0上写了个printf类似的程序,欢迎提出改进方法.
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#ifndef NULL
#define NULL  (void *)0           /*  NULL    :   Null pointer */
#endif

#define putc_to_file(_c,_stream)  (--(_stream)->_cnt >= 0 ?\
 0xff & (*(_stream)->_ptr++ = (char)(_c)) :  _flsbuf((_c),(_stream)))

void outputch(char ch)
{
    putc_to_file(ch, stdout);

#if 0
    (*(volatile unsigned short *)(0x80130000)) = (unsigned short)ch;
#endif
}

void outputstr(char *str)
{
    puts(str);
}



void print_x(long l)
{
    unsigned long  t;
    signed long i, k ;
    signed char buf[8];

    if (l == 0)
    {
        outputch('0');
        return;
    }

    memset(buf,'0',8);
    t = l;
    k = 7;

    for(;;)
    {
        if (t > 15)
        {
            i = t % 16;
        }else
            i = t;

        if (i < 10){
            buf[k--] = i + '0';
        }
        else
            if(i < 16)
            {
               buf[k--] = i -10 +'a';
            }

        if (t < 15)
            break;
        t >>= 4;
    }

    for(i =0 ;i < 8 ; i ++)
        outputch(buf[i]);
}


void print_0nx(char ch2, long l)
{
    unsigned char ch;
    int i;

    ch2 = ch2 - '0';
    for (i = ch2 - 1; i >= 0; i--) {
        ch = (l >> (i * 4)) & 0x0f;
        if (ch < 10)
            outputch((char)(ch + '0'));
        else
            outputch((char)(ch - 10 + 'a'));
    }
}


void print_d(long l)
{
    unsigned long  t;
    int i, j, k ;
    char buf[8];

    if (l == 0)
    {
        outputch('0');
        return;
    }

    memset(buf,'0',8);
    t = l;
    k = 7;

    for(;;)
    {
        if (t > 9)
        {
            i = t % 10;
        }else
            i = t;

        if (i < 10){
            buf[k--] = i + '0';
        }

        if (t < 9)
            break;
        t /= 10;
    }

    j = 0;
    for(i =0 ;i < 8 ; i ++)
    {
        if(buf[i] != '0')
            j = 1;
        if (j)
            outputch(buf[i]);
    }
}


int print_u(char *s, ...)
{
    va_list ap;
    unsigned long arg;
    char *str;
    char ch1, ch2, ch3;

    va_start(ap, s);
    while (*s != 0) {
        if (*s == '%') {
            s++;
            ch1 = *s++;
            if (ch1 == 's') {
                str = va_arg(ap, char *);
                outputstr(str);
            }else if (ch1 == 'd') {
                arg = va_arg(ap, long);
                print_d(arg);
            }
             else if (ch1 == 'x') {
                arg = va_arg(ap, long);
                print_x(arg);
            }else {
                ch2 = *s++;
                ch3 = *s++;
                arg = va_arg(ap, long);
                print_0nx(ch2, arg);
            }
        } else
            outputch(*s++);
    }
    va_end(ap);
    return 0;
}


int debug_printf(char *s, ...)
{
    va_list ap;
    unsigned long arg;
    char *str;
    char ch1, ch2, ch3;

    va_start(ap, s);
    while (*s != 0) {
        if (*s == '%') {
            s++;
            ch1 = *s++;
            if (ch1 == 's') {
                str = va_arg(ap, char *);
                print_u(str);
            }else if (ch1 == 'd') {
                arg = va_arg(ap, long);
                print_d(arg);
            }
            else if (ch1 == 'x') {
                arg = va_arg(ap, long);
                print_x(arg);
            }else {
                ch2 = *s++;
                ch3 = *s++;
                arg = va_arg(ap, long);
                print_0nx(ch2, arg);
            }
        } else
            outputch(*s++);
    }
    va_end(ap);

    return 0;
}


void main(void)
{
    debug_printf("This is a printf test func, %s, %d \n", __FILE__, __LINE__);
    debug_printf("*************AUTHOR :Young ************\n");
}
搜索更多相关主题的帖子: printf void stream NULL 
2008-06-01 02:37
dongfly111
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-3-25
得分:0 
看不懂,学习中!
看不懂,学习中!
2008-06-01 09:01
蓝色神话
Rank: 2
等 级:论坛游民
威 望:1
帖 子:404
专家分:24
注 册:2006-5-11
得分:0 
没有注释,算不上好代码!
2008-06-01 10:12
learnerboy
Rank: 2
等 级:论坛游民
帖 子:246
专家分:22
注 册:2007-11-11
得分:0 
看不懂啊,还没见过了!
2008-06-01 11:35
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
得分:0 
挺不错的,作为可变参数示例
但是程序本身还有很多功能没有实现
比如
debug_printf("%d,%f,%lf,%e\n",23,344.5,4.667,343);试试
2008-06-02 12:32
lxf371777450
Rank: 1
来 自:云南昆明
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-6-7
得分:0 
看不懂 学习中
看不懂 学习中

有梦的日子是痛苦的,没梦的日子是苍白的!
2008-06-07 02:13
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
得分:0 
恩,在OpenGL中输出格式化文字也是用va_list来做

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2008-06-07 10:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-216954-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.077735 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved