-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyprintf.c
70 lines (54 loc) · 1.37 KB
/
myprintf.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
void hexadecimal_print(unsigned int n)
{
char str[] = "0123456789ABCDEF";
if (n/16 > 0) hexadecimal_print(n / 16);
print_char(str[n % 16]);
}
void octal_print(unsigned int n)
{
char str[]="01234567";
if(n/8 > 0) octal_print(n/8);
print_char(str[n % 8]);
}
void myprintf(char *fmt, ...)
{
char *p = (char*)&fmt;
while(*fmt){//fmtの最初が%から始まるときとそうでない時で分岐する
if(*fmt == '%'){
fmt++; //%をとばす
switch(*fmt){
case 'd': //10進数で出力する
p = (p + ((sizeof(fmt) + 3) / 4) * 4);
print_int(*(int*)p);
break;
case 's': //文字列で出力する
p = (p + ((sizeof(fmt) + 3) / 4) * 4);
print_string(*(char**)p);
break;
case 'c': //1文字で出力する
p = (p + ((sizeof(fmt) + 3) / 4) * 4);
print_char(*(char*)p);
break;
case 'x': //10進数を16進数に変換して出力する
p = (p + ((sizeof(fmt) + 3) / 4) * 4);
hexadecimal_print(*(int*)p);
break;
case 'o': //10進数を8進数に変換して出力する
p = (p + ((sizeof(fmt) + 3) / 4) * 4);
octal_print(*(int*)p);
break;
}
} else {
//そのまま*fmtの一文字を表示
print_char(*fmt);
}
fmt++;
}
}
int main()
{
char string[]= "ABC";
myprintf("test: %d, %s, %c, %x, %o\n", 10, string, 'a', 19, 10);
print_string("Done.\n");
return 0;
}