Mercurial > sos > sos
diff src/std.c @ 3:0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Yeah, I really should have done this in seperate commits...
My mistake in operations.h was fixed, the name table is now defined in
operations.c
All operations have been modified to start with op_ so I can identify
the functions easier
Functions ftoa and atof were defined (their functions are kind of obvious)
Functions ftoa and itoa now return the location of the string they create
iPrint was removed, it was useless
The function append was created, it appends two strings
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Tue, 01 Mar 2011 23:33:51 -0500 |
parents | b6182f00de82 |
children | 90cd3d9a6ca3 |
line wrap: on
line diff
--- a/src/std.c Tue Mar 01 02:39:27 2011 -0500 +++ b/src/std.c Tue Mar 01 23:33:51 2011 -0500 @@ -1,5 +1,8 @@ -void itoa(int n, char *a) +#include <std.h> + +char* itoa(int n, char *a) { + char *ret = a; if(n < 0) { *a++ = '-'; n *= -1; @@ -16,6 +19,27 @@ *b = *a; *a = temp; } + return ret; +} + +char* ftoa(double x, char *a, unsigned int prec) +{ + char *ret = a; + int n = (int) x; //integer part + double d = x - (double) n; //fractional part; + itoa(n, a); + if(prec) { + while(*a && *++a); + int i; + *a++ = '.'; + for(i = 0; i < prec; i++) { + d = d*10; + *a++ = ((int) d) + '0'; + d -= (int) d; + } + *a = '\0'; + } + return ret; } int atoi(char *a) @@ -26,12 +50,26 @@ neg = 1; a++; } - while(*a >='0' && *a <= '9') + while(*a >= '0' && *a <= '9') n = n*10 + (*a++ - '0'); if(neg) n *= -1; return n; } +double atof(char *a) +{ + int n = atoi(a); + double x = 0; + double dec = .1; + while(*a++ != '.'); + while(*a != '\0') { + x += (*a - '0')*dec; + dec *= .1; + a++; + } + return n + x; +} + void sPrint(char *a) { char *b = a; @@ -39,14 +77,7 @@ putline(a, b - a); } -void iPrint(int n) -{ - char string[10]; - itoa(n, string); - sPrint(string); -} - -int strcmp(char *a, char *b) +int strcmp(const char *a, const char *b) { while(1) { if(*a - *b) return *a - *b; @@ -70,3 +101,11 @@ if(!strcmp(array[i], text)) return i; return last; } + +char* append(char *dest, char *src) +{ + char *ret = dest; + while(*dest&& *++dest); //get to null in first string + while((*dest++ = *src++)); + return ret; +}