zoukankan      html  css  js  c++  java
  • 嵌入式 printf函数

    来自:https://www.cnblogs.com/02xiaoma/archive/2012/06/22/2558618.html

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

    int main(void)
    {
      print("print: %c ", 'c');
      print("print %d ", 1234567);
      print("print: %f ", 1234567.1234567);
      print("print: %s ", "string test");
      print("print: %b ", 0x12345ff);
      print("print: %x ", 0xabcdef);
      print("print: %% ");
      return 0;
    }

    void print(char* fmt, ...)
    {
      double vargflt = 0;
      int vargint = 0;
      char* vargpch = NULL;
      char vargch = 0;
      char* pfmt = NULL;
      va_list vp;

      va_start(vp, fmt);
      pfmt = fmt;

    while(*pfmt)
    {
      if(*pfmt == '%')
      {
        switch(*(++pfmt))
        {

          case 'c':
          vargch = va_arg(vp, int);
          /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
              mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
          printch(vargch);
          break;
        case 'd':
        case 'i':
        vargint = va_arg(vp, int);
        printdec(vargint);
        break;
        case 'f':
        vargflt = va_arg(vp, double);
        /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
        mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
        printflt(vargflt);
        break;
        case 's':
          vargpch = va_arg(vp, char*);
          printstr(vargpch);
        break;
        case 'b':
        case 'B':
          vargint = va_arg(vp, int);
          printbin(vargint);
        break;
        case 'x':
        case 'X':
          vargint = va_arg(vp, int);
          printhex(vargint);
        break;
        case '%':
          printch('%');
        break;
        default:
        break;
      }
      pfmt++;
      }
      else
      {
        printch(*pfmt++);
      }
      }
      va_end(vp);
    }

    void printch(char ch)
    {
      console_print(ch);
    }

    void printdec(int dec)
    {
      if(dec==0)
      {
        return;
      }
      printdec(dec/10);

      {
        printch( (char)(dec%10 + '0'));
      }

    void printflt(double flt)
    {
      int icnt = 0;
      int tmpint = 0;

      tmpint = (int)flt;
      printdec(tmpint);
      printch('.');
      flt = flt - tmpint;
      tmpint = (int)(flt * 1000000);
      printdec(tmpint);
    }

    void printstr(char* str)
    {
      while(*str)
      {
        printch(*str++);
      }
    }

    void printbin(int bin)
    {
      if(bin == 0)
      {
        printstr("0b");
        return;
      }
      printbin(bin/2);
      printch( (char)(bin%2 + '0'));
    }

    void printhex(int hex)
    {
      if(hex==0)
      {
        printstr("0x");
        return;
      }
      printhex(hex/16);
      if(hex < 10)
      {
        printch((char)(hex%16 + '0'));
      }
      else
      {
        printch((char)(hex%16 - 10 + 'a' ));
      }
    }

  • 相关阅读:
    结对项目:四则运算
    Word Count(C语言)
    自我介绍+软工5问
    如何在博客园中使用markdown
    如何设计一门语言(九)——类型
    如何设计一门语言(八)——异步编程和CPS变换
    如何设计一门语言(七)——闭包、lambda和interface
    时隔多年我又再一次体验了一把跟大神聊天的感觉
    20199107 2019-2020-2 《网络攻防实践》第4周作业
    20199107 2019-2020-2 《网络攻防实践》第3周作业
  • 原文地址:https://www.cnblogs.com/qinshizhi/p/10673566.html
Copyright © 2011-2022 走看看