forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_7-3.c
94 lines (88 loc) · 2.56 KB
/
exercise_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
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
void minprintf(char* format, ...)
{
va_list args;
char* string_value;
int int_value;
double double_value;
int char_value;
void* pointer_value;
va_start(args, format);
char* cursor;
for (cursor = format; *cursor; ++cursor)
{
if (*cursor != '%')
{
putchar(*cursor);
continue;
}
switch (*++cursor)
{
case 'd':
case 'i':
int_value = va_arg(args, int);
printf("%d", int_value);
break;
case 'c':
char_value = va_arg(args, int);
putchar(char_value);
break;
case 'p':
pointer_value = va_arg(args, void*);
printf("%p", pointer_value);
break;
case 'e':
double_value = va_arg(args, double);
printf("%e", double_value);
break;
case 'E':
double_value = va_arg(args, double);
printf("%E", double_value);
break;
case 'o':
int_value = va_arg(args, int);
printf("%o", int_value);
break;
case 'x':
int_value = va_arg(args, int);
printf("%x", int_value);
break;
case 'X':
int_value = va_arg(args, int);
printf("%X", int_value);
break;
case 'a':
double_value = va_arg(args, double);
printf("%a", double_value);
break;
case 'A':
double_value = va_arg(args, double);
printf("%A", double_value);
break;
case 'g':
double_value = va_arg(args, double);
printf("%g", double_value);
break;
case 'G':
double_value = va_arg(args, double);
printf("%G", double_value);
break;
case 'f':
double_value = va_arg(args, double);
printf("%f", double_value);
break;
case 's':
for (string_value = va_arg(args, char*); *string_value; ++string_value)
{
putchar(*string_value);
}
break;
default:
putchar(*cursor);
break;
}
va_end(args);
}
}