This commit is contained in:
Benjamin Kyd
2019-04-25 21:39:31 +01:00
parent 4bc5392ca7
commit da858dfe76
14 changed files with 137 additions and 26 deletions

37
lib/std/stdlib.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "stdlib.h"
#include "string.h"
void swap(int* x, int* y) {
int* t = x;
x = y;
y = t;
}
void reverse(char s[]) {
int i;
int j = strlen(s) - 1;
char c;
for (i = 0; i < j; i++) {
j--;
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void itoa(int i, char[] s) {
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}