-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_printf.c
147 lines (131 loc) · 2.96 KB
/
my_printf.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
#include<stdio.h>
#include<stdarg.h>
#include<unistd.h>
#include<stdlib.h>
//my oun strlen
int my_strlen(char* param_1)
{
int count = 0;
for(int i=0; param_1[i] != '\0'; i++)
{
count++;
}
return count;
}
//my oun putchar
int my_putchar(char c) {
write(1, &c, 1);
return 1;
}
// my own puts
int my_puts( char *str_a)
{
int i =0;
for(i = 0; i < my_strlen(str_a);i++){
my_putchar(str_a[i]);
}
return i;
}
//itoa implementation
char* itoaa(unsigned long num,int base,char mass[]){
int i = 48;
int remainder;
mass[i+1] = '\0';
if(num == 0){
mass[i--]= '0';
} else{
do{
remainder = num%base;
num/=base;
if (remainder>9){
mass[i] = 'a'+ remainder - 10;
}else{
mass[i]= remainder + '0';
}
i--;
}while(num!=0);
}
return &mass[i+1];
}
int my_printf(char*restrict format,...)
{
// my_printf arguments
void* kakoito;
char mas[50];
char *elements;
char *str_a;
int num;
int count = 0;
va_list arg;
va_start(arg, format);
// ///////////////////////
for(elements = format; *elements != '\0'; elements++)
{
if( *elements != '%' )
{
count ++;
my_putchar(*elements);
//elements++;
}
//elements++;
else{
elements++;
// %c %d %o %s %x %u
switch(*elements)
{
case 'c' : num = va_arg(arg,int); //char
count += my_putchar(num);
break;
case 'o': num = va_arg(arg,unsigned int);//Octal
count += my_puts(itoaa(num,8,mas));
break;
case 'x': num = va_arg(arg,unsigned int);//Hexadecimal
count += my_puts(itoaa(num,16,mas));
break;
case 'p': num = va_arg(arg,unsigned int);//P
kakoito = va_arg(arg, void*);
count += my_puts("0x");
count += my_puts(itoaa((unsigned long)kakoito,16,mas));
break;
case 's': str_a = va_arg(arg,char *);//string
if(str_a == NULL){
count += my_puts("(null)");
}
else{
count += my_puts(str_a);
}
break;
case 'd': num = va_arg(arg,int);
if(num < 0){
count += my_puts("-");
num = -num;
}
count += my_puts(itoaa(num,10,mas));
break;
case 'u' : num = va_arg(arg,unsigned int);// Decimal/Integer
// if(num <= 0)
// {
// num = -num;
// }
count += my_puts(itoaa(num,10,mas));
break;
}
}
}
va_end(arg);
return count;
}
int main()
{
my_printf("Hello\n");
my_printf("%c%c%c%c%c!\n", 'H', 'e', 'l', 'l', 'o');
my_printf("Hello %s!\n", "QWASAR.IO");
my_printf("NULL STRING %s!\n", (char*)NULL);
my_printf("%d - %d - %d!\n", 2048, 0, -1337);
my_printf("%o!\n", 100);
my_printf("%u!\n", 1337);
my_printf("%x!\n", 17);
my_printf("Hello world!\n");
my_printf("%c!\n", 'H');
return 0;
}