47 lines
622 B
C
47 lines
622 B
C
#include <stdbool.h>
|
|
#include "include/stdlib.h"
|
|
|
|
int atoi(const char* s) {
|
|
bool neg = false;
|
|
if(*s == '+') {
|
|
s++;
|
|
} else if(*s == '-') {
|
|
neg = true;
|
|
s++;
|
|
}
|
|
int n = 0;
|
|
while(*s >= '0' && *s <= '9') {
|
|
n *= 10;
|
|
n += (*s - '0');
|
|
s++;
|
|
}
|
|
return neg ? -n : n;
|
|
}
|
|
|
|
void itoa(int n, char* buf) {
|
|
if(n < 0) {
|
|
n = -n;
|
|
*buf = '-';
|
|
buf++;
|
|
} else if(n == 0) {
|
|
buf[0] = '0';
|
|
buf[1] = '\0';
|
|
return;
|
|
}
|
|
char* start = buf;
|
|
while(n > 0) {
|
|
*buf= n % 10 + '0';
|
|
n /= 10;
|
|
buf++;
|
|
}
|
|
*buf = '\0';
|
|
buf--;
|
|
while(start < buf) {
|
|
char tmp = *start;
|
|
*start = *buf;
|
|
*buf = tmp;
|
|
start++;
|
|
buf--;
|
|
}
|
|
}
|