1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>
#include <ctype.h>
#include "mprintf.h"
#define DEFPRES 6
enum base { OCT = 8, DEC = 10, HEX = 16 };
struct flags {
unsigned int fw;
unsigned int pres;
unsigned int fwc;
unsigned int laligne : 1;
} flags;
void printnum(long long num, struct flags flags, int base);
void printflt(double num, struct flags flags);
void printstr(char *s, struct flags flags);
int reverse(long long *num, int base);
void mprintf(char *fmt, ...)
{
va_list ap;
char *p;
int dlim;
va_start(ap, fmt);
for (p = fmt; *p; ++p) {
if (*p != '%') {
putchar(*p);
continue;
}
flags.fwc = ' ';
flags.fw = dlim = flags.pres = 0;
while (!isalpha(*++p) && *p != '\0') {
if (isalnum(*p) && !dlim)
flags.fw = flags.fw * 10 + *p - '0';
else if (isalnum(*p) && dlim)
flags.pres = flags.pres * 10 + *p - '0';
else if (*p == '.')
dlim = 1;
else if (*p == '-')
flags.laligne = 1;
}
if (!dlim)
flags.pres = DEFPRES;
switch (*p) {
case 'd':
case 'i':
printnum(va_arg(ap, int), flags, DEC);
break;
case 'o':
printnum(va_arg(ap, int), flags, DEC);
break;
case 'u':
printnum(va_arg(ap, unsigned), flags, DEC);
break;
case 'x':
printnum(va_arg(ap, unsigned), flags, HEX);
break;
case 'c':
putchar(va_arg(ap, unsigned));
break;
case 'p':
mprintf("0x");
printnum(va_arg(ap, long long), flags, HEX);
break;
case 'f':
printflt(va_arg(ap, double), flags);
break;
case 's':
printstr(va_arg(ap, char *), flags);
break;
default:
putchar(*p);
break;
}
}
va_end(ap);
return;
}
void printstr(char *s, struct flags flags)
{
int blank;
blank = flags.fw - strlen(s);
for (; blank > 0 && !flags.laligne; --blank)
putchar(flags.fwc);
for (; *s; ++s)
putchar(*s);
for (; blank > 0 && flags.laligne; --blank)
putchar(flags.fwc);
}
void printnum(long long num, struct flags flags, int base)
{
int temp, blank;
if (num < 0) {
putchar('-');
num *= -1;
}
blank = flags.fw - reverse(&num, base);
for (; blank > 0 && !flags.laligne; --blank)
putchar(flags.fwc);
for (; num; num /= base) {
switch (temp = num % base) {
case 10:
putchar('a');
break;
case 11:
putchar('b');
break;
case 12:
putchar('c');
break;
case 13:
putchar('d');
break;
case 14:
putchar('e');
break;
case 15:
putchar('f');
break;
default:
putchar(temp + '0');
break;
}
}
for (; blank > 0 && flags.laligne; --blank)
putchar(flags.fwc);
}
void printflt(double num, struct flags flags)
{
printnum((int) num, flags, DEC);
if (flags.pres) {
flags.fwc = '0';
putchar('.');
printnum((long long) ((num - (int) num) *
powl(10, flags.pres)), flags, DEC);
}
}
int reverse(long long *num, int base)
{
long long temp = *num;
int len;
for (*num = len = 0; temp; temp /= base, ++len)
*num = *num * base + temp % base;
return len;
}
|