实现自己的printf函数(1)

my_printf.h:

[csharp]  view plain  copy
  1. extern void my_printf(const char *format,...);  


 

my_printf.c:

[csharp]  view plain  copy
  1. #include "my_printf.h"  
  2. #include "stdarg.h"  
  3. /********************************************************** 
  4.  
  5. ***********************************************************/  
  6. void printch(const char ch)  
  7. {  
  8.     putchar(ch);  
  9. }  
  10. /********************************************************** 
  11.  
  12. ***********************************************************/  
  13. void printint(const int dec)  
  14. {  
  15.     if(dec == 0)  
  16.     {  
  17.         return;  
  18.     }  
  19.     printint(dec / 10);  
  20.     putchar((char)(dec % 10 + '0'));  
  21. }  
  22. /********************************************************** 
  23.  
  24. ***********************************************************/  
  25. void printstr(const char *ptr)  
  26. {  
  27.     while(*ptr)  
  28.     {  
  29.         putchar(*ptr);  
  30.     ptr++;  
  31.     }  
  32. }  
  33. /********************************************************** 
  34.  
  35. ***********************************************************/  
  36. void printfloat(const float flt)  
  37. {  
  38.     int tmpint = (int)flt;  
  39.     int tmpflt = (int)(100000 * (flt - tmpint));  
  40.     if(tmpflt % 10 >= 5)  
  41.     {  
  42.         tmpflt = tmpflt / 10 + 1;  
  43.     }  
  44.     else  
  45.     {  
  46.         tmpflt = tmpflt / 10;  
  47.     }  
  48.     printint(tmpint);  
  49.     putchar('.');  
  50.     printint(tmpflt);  
  51.   
  52. }  
  53. /********************************************************** 
  54.  
  55. ***********************************************************/  
  56. void my_printf(const char *format,...)  
  57. {  
  58.     va_list ap;  
  59.     va_start(ap,format);  
  60.     while(*format)  
  61.     {  
  62.         if(*format != '%')  
  63.     {  
  64.             putchar(*format);  
  65.         format++;  
  66.     }  
  67.     else  
  68.     {  
  69.         format++;  
  70.         switch(*format)  
  71.         {  
  72.             case 'c':  
  73.             {  
  74.                 char valch = va_arg(ap,int);  
  75.             printch(valch);  
  76.             format++;  
  77.             break;  
  78.             }  
  79.             case 'd':  
  80.             {  
  81.                 int valint = va_arg(ap,int);  
  82.             printint(valint);  
  83.             format++;  
  84.             break;  
  85.             }  
  86.         case 's':  
  87.         {  
  88.             char *valstr = va_arg(ap,char *);  
  89.             printstr(valstr);  
  90.             format++;  
  91.             break;  
  92.         }  
  93.         case 'f':  
  94.         {  
  95.             float valflt = va_arg(ap,double);  
  96.             printfloat(valflt);  
  97.             format++;  
  98.             break;  
  99.         }  
  100.         default:  
  101.         {  
  102.             printch(*format);  
  103.             format++;  
  104.         }  
  105.         }  
  106.         va_end(ap);  
  107.     }  
  108.     }  
  109. }  
  110. /********************************************************** 
  111.  
  112. ***********************************************************/  
  113. int main()  
  114. {  
  115.     char ch = 'A';  
  116.     char *str = "holle world";  
  117.     int dec = 1234;  
  118.     float flt = 1234.3456789;   
  119.     my_printf("str = %s,dec = %d,ch = %c,flt = %f\n",str,dec,ch,flt);  
  120.     return 0;  
  121. }  

编译一下,运行一下,ok了。

(注:float型我这能输出小数点后4位,第5位四舍五入了)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值