-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprints.c
108 lines (97 loc) · 2.61 KB
/
prints.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
#include "prints.h"
/* ftoa implementation based on https://www.geeksforgeeks.org/convert-floating-point-number-string */
int INT_PADDING = 1;
int FLOAT_DEC_POINTS = 6;
// Reverses a string 'str' of length 'len'
void reverse(char* str, int len, int start_point)
{
int i = start_point, j = len - 1, temp;
while (i < j) {
temp = str[i];
str[i++] = str[j];
str[j--] = temp;
}
}
// Converts a given integer x to string str[].
// d is the number of digits required in the output.
// If d is more than the number of digits in x,
// then 0s are added at the beginning.
int itoa(int x, char str[], int d)
{
int i = 0;
int neg = x >= 0 ? 0 : 1;
if (!x){
str[i++] = '0';
}
if (neg){
str[i++] = '-';
x = -x;
}
while (x) {
str[i++] = (x % 10) + '0';
x = x / 10;
}
// If number of digits required is more, then
// add 0s at the beginning
while (i < d)
str[i++] = '0';
reverse(str, i, !neg ? 0 : 1); // if the number if negative we do not want to reverse the signal
str[i] = '\0';
return i++;
}
// Converts a floating-point/double number to a string.
int ftoa(float n, char* res, int afterpoint)
{
// Extract integer part
int neg = n >= 0 ? 0 : 1;
//int neg = 0;
if ( neg ) {
n = -n;
res[0] = '-';
res++;
}
int ipart = (int)n;
//printf("%d %f\n", ipart, n);
// Extract floating part
float fpart = n - (float)ipart;
//fpart = roundf(fpart * powf(10, afterpoint)) / powf(10, afterpoint);
// convert integer part to string
int i = itoa(ipart, res, 0);
// check for display option after point
if (afterpoint != 0) {
res[i++] = '.'; // add dot
// Get the value of fraction part upto given no.
// of points after dot. The third parameter
// is needed to handle cases like 233.007
int j;
for (j = 0; j < afterpoint; j++) {
fpart *= 10;
int digit = (int)fpart;
res[i++] = digit + '0';
fpart -= (float)digit;
}
}
return neg ? i + 1 : i ;
}
int printChar(char* str, int offset, char c){
str[offset] = c;
offset+=sizeof(c);
str[offset+1]='\0';
return offset;
}
int printChars(char* str, int offset, const char* format){
strcpy(str+offset, format);
offset+=strlen(format);
str[offset+1]='\0';
return offset;
}
int printInt(char* str, int offset, int i){
offset += itoa(i, str+offset, INT_PADDING);
str[offset+1]='\0';
return offset;
}
int printFloat(char* str, int offset, float f){
offset += ftoa(f, str+offset, FLOAT_DEC_POINTS);
str[offset+1]='\0';
return offset;
}