-
Notifications
You must be signed in to change notification settings - Fork 37
/
7-3.c
175 lines (159 loc) · 4.09 KB
/
7-3.c
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
166
167
168
169
170
171
172
173
174
175
/*
* Exercise 7-3. Revise minprintf to handle more of the other facilities of
* printf.
* Note: all flags (except for left adjust) and variable width/length are not implemented.
* By Faisal Saadatmand
*/
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUMB 20
#define FLT_PRCISION 6
#define FIELD_WIDTH 1
/* macros */
#define PRINT_N(la,x,w,v, nc) (la) ? (nc += printf("%-*" # x, w, v)) : \
(nc += printf("%*" #x, w, v))
#define PRINT_D(la,x,w,p,v,nc) (la) ? (nc += printf("%-*.*" # x, w, p, v)) : \
(nc += printf("%*.*" # x, w, p, v))
/* functionst */
int minprintf(char *, ...);
/* minprintf: minimal printf with variable argument list */
int minprintf(char *fmt, ...)
{
va_list ap; /* points to each unnamed arg in turn */
char *p, *sval, number[MAXNUMB];
int ival, i, width, precision, nchar = 0;
double dval;
unsigned uval;
void *pval;
int leftAdjust, *pival;
va_start(ap, fmt); /* make ap point to 1st unnamed arg */
for (p = fmt; *p; p++) {
if (*p != '%') {
putchar(*p);
++nchar;
continue;
}
leftAdjust = 0;
width = 1;
precision = -1;
if (*++p == '-') {
leftAdjust = 1; /* left adjust */
++p;
}
if (isdigit(*p)) { /* get width */
for (i = 0; isdigit(*p) && i < MAXNUMB; i++)
number[i] = *p++;
number[i] = '\0';
width = atoi(number);
}
if (*p == '.') {
++p;
for (i = 0; isdigit(*p) && i < MAXNUMB; i++) /* get precision pts */
number[i] = *p++;
number[i] = '\0';
precision = atoi(number);
}
switch (*p) {
case 'd':
ival = va_arg(ap, int);
PRINT_N(leftAdjust, d, width, ival, nchar);
break;
case 'i':
ival = va_arg(ap, int);
PRINT_N(leftAdjust, i, width, ival, nchar);
break;
case 'o':
uval = va_arg(ap, int);
PRINT_N(leftAdjust, o, width, uval, nchar);
break;
case 'x':
uval = va_arg(ap, int);
PRINT_N(leftAdjust, x, width, uval, nchar);
break;
case 'X':
uval = va_arg(ap, int);
PRINT_N(leftAdjust, X, width, uval, nchar);
break;
case 'u':
uval = va_arg(ap, int);
PRINT_N(leftAdjust, u, width, uval, nchar);
break;
case 'c':
ival = va_arg(ap, int);
PRINT_N(leftAdjust, c, width, ival, nchar);
break;
case 's':
sval = va_arg(ap, char *);
if (precision < 0)
precision = strlen(sval);
PRINT_D(leftAdjust, s, width, precision, sval, nchar);
break;
case 'f':
dval = va_arg(ap, double);
if (precision < 0)
precision = FLT_PRCISION;
PRINT_D(leftAdjust, f, width, precision, dval, nchar);
break;
case 'e':
dval = va_arg(ap, double);
if (precision < 0)
precision = FLT_PRCISION;
PRINT_D(leftAdjust, e, width, precision, dval, nchar);
break;
case 'E':
dval = va_arg(ap, double);
if (precision < 0)
precision = FLT_PRCISION;
PRINT_D(leftAdjust, E, width, precision, dval, nchar);
break;
case 'g':
dval = va_arg(ap, double);
PRINT_N(leftAdjust, g, width, dval, nchar);
break;
case 'G':
dval = va_arg(ap, double);
PRINT_N(leftAdjust, G, width, dval, nchar);
break;
case 'p':
pval = va_arg(ap, void *);
PRINT_N(leftAdjust, p, width, pval, nchar);
break;
case 'n':
pival = va_arg(ap, int *);
*pival = nchar;
break;
default:
putchar(*p);
++nchar;
break;
}
}
va_end(ap); /* clean up when done */
return nchar;
}
int main(void)
{
minprintf("minprintf() output:\n");
minprintf("%s\n", "hello, world");
minprintf("%10s\n", "hello, world");
minprintf("%.10s\n", "hello, world");
minprintf("%-10s\n", "hello, world");
minprintf("%.15s\n", "hello, world");
minprintf("%-15s\n", "hello, world");
minprintf("%15.10s\n", "hello, world");
minprintf("%-15.10s\n", "hello, world");
printf("\n");
printf("printf() output:\n");
printf("%s\n", "hello, world");
printf("%10s\n", "hello, world");
printf("%.10s\n", "hello, world");
printf("%-10s\n", "hello, world");
printf("%.15s\n", "hello, world");
printf("%-15s\n", "hello, world");
printf("%15.10s\n", "hello, world");
printf("%-15.10s\n", "hello, world");
return 0;
}