1 #ifndef     __PRINT_H_
2 #define __PRINT_H_
3
4 void print(char* fmt, ...);
5 void printch(char ch);
6 void printdec(int dec);
7 void printflt(double flt);
8 void printbin(int bin);
9 void printhex(int hex);
10 void printstr(char* str);
11
12 #define console_print(ch) putchar(ch)
13
14 #endif /*#ifndef __PRINT_H_*/
 #include <stdio.h>
2 #include <stdarg.h>
3 #include "print.h"
4
5 int main(void)
6 {
7 print("print: %c\n", 'c');
8 print("print %d\n", 1234567);
9 print("print: %f\n", 1234567.1234567);
10 print("print: %s\n", "string test");
11 print("print: %b\n", 0x12345ff);
12 print("print: %x\n", 0xabcdef);
13 print("print: %%\n");
14 return 0;
15 }
16
17 void print(char* fmt, ...)
18 {
19 double vargflt = 0;
20 int vargint = 0;
21 char* vargpch = NULL;
22 char vargch = 0;
23 char* pfmt = NULL;
24 va_list vp;
25
26 va_start(vp, fmt);
27 pfmt = fmt;
28
29 while(*pfmt)
30 {
31 if(*pfmt == '%')
32 {
33 switch(*(++pfmt))
34 {
35
36 case 'c':
37 vargch = va_arg(vp, int);
38 /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
39 mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
40 printch(vargch);
41 break;
42 case 'd':
43 case 'i':
44 vargint = va_arg(vp, int);
45 printdec(vargint);
46 break;
47 case 'f':
48 vargflt = va_arg(vp, double);
49 /* va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
50 mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
51 printflt(vargflt);
52 break;
53 case 's':
54 vargpch = va_arg(vp, char*);
55 printstr(vargpch);
56 break;
57 case 'b':
58 case 'B':
59 vargint = va_arg(vp, int);
60 printbin(vargint);
61 break;
62 case 'x':
63 case 'X':
64 vargint = va_arg(vp, int);
65 printhex(vargint);
66 break;
67 case '%':
68 printch('%');
69 break;
70 default:
71 break;
72 }
73 pfmt++;
74 }
75 else
76 {
77 printch(*pfmt++);
78 }
79 }
80 va_end(vp);
81 }
82
83 void printch(char ch)
84 {
85 console_print(ch);
86 }
87
88 void printdec(int dec)
89 {
90 if(dec==0)
91 {
92 return;
93 }
94 printdec(dec/10);
95 printch( (char)(dec%10 + '0'));
96 }
97
98 void printflt(double flt)
99 {
100 int icnt = 0;
101 int tmpint = 0;
102
103 tmpint = (int)flt;
104 printdec(tmpint);
105 printch('.');
106 flt = flt - tmpint;
107 tmpint = (int)(flt * 1000000);
108 printdec(tmpint);
109 }
110
111 void printstr(char* str)
112 {
113 while(*str)
114 {
115 printch(*str++);
116 }
117 }
118
119 void printbin(int bin)
120 {
121 if(bin == 0)
122 {
123 printstr("0b");
124 return;
125 }
126 printbin(bin/2);
127 printch( (char)(bin%2 + '0'));
128 }
129
130 void printhex(int hex)
131 {
132 if(hex==0)
133 {
134 printstr("0x");
135 return;
136 }
137 printhex(hex/16);
138 if(hex < 10)
139 {
140 printch((char)(hex%16 + '0'));
141 }
142 else
143 {
144 printch((char)(hex%16 - 10 + 'a' ));
145 }
146 }

最新文章

  1. Java jdbc 连接oracle之三(封装工具类)
  2. (OSP)外包工单关工单失败
  3. 中文系统下,UTF-8编码文本文件读取导致的错误
  4. SVN的命令行操作
  5. EntityFramework批量Insert
  6. final关键字细节
  7. 解决问题:CA_ERROR证书出错,请登录微信支付商户平台下载证书-企业付款到零钱接口(原创)
  8. 小甲鱼零基础python课后题 P22 021函数:lambda表达式
  9. Wpf ViewModel中 ObservableCollection不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改
  10. U68641 划水(swim.pas/c/cpp)
  11. 分布式CAP定理,为什么不能同时满足三个特性?
  12. BZOJ1068 [SCOI2007]压缩 区间动态规划 字符串
  13. python学习笔记_week6_面向对象
  14. PHP ini 配置无效的坑给自己记录
  15. javaScript高级教程(九) ------javascript对象字面量--------困扰已久的问题
  16. Ionic Js十三:平台
  17. Fiddler2 中文手册
  18. SPOJ - VLATTICE
  19. 【题解】CQOI2017老C的键盘
  20. 【51Nod 1363】最小公倍数之和(欧拉函数)

热门文章

  1. C# autocomplete
  2. margin外边距属性
  3. 【[AHOI2005]洗牌 题解
  4. 初学高级程序设计 shell编程
  5. python基础——1、python背景及特点——(YZ)
  6. gtest 参数化
  7. linux下用ctrl+r快速搜索history命令
  8. 用vs2010打开使用vs2013升级后的WP工程
  9. 我发起了一个 用 C# 写 的 浏览器 开源项目 HtmlCore
  10. Dubbo和Spring Cloud