comparison 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
comparison
equal deleted inserted replaced
2:b6182f00de82 3:0aa0ad9e1cc3
1 void itoa(int n, char *a) 1 #include <std.h>
2
3 char* itoa(int n, char *a)
2 { 4 {
5 char *ret = a;
3 if(n < 0) { 6 if(n < 0) {
4 *a++ = '-'; 7 *a++ = '-';
5 n *= -1; 8 n *= -1;
6 } 9 }
7 char *b = a; 10 char *b = a;
14 for(; a < b; a++, b--) { //reverse 17 for(; a < b; a++, b--) { //reverse
15 char temp = *b; 18 char temp = *b;
16 *b = *a; 19 *b = *a;
17 *a = temp; 20 *a = temp;
18 } 21 }
22 return ret;
23 }
24
25 char* ftoa(double x, char *a, unsigned int prec)
26 {
27 char *ret = a;
28 int n = (int) x; //integer part
29 double d = x - (double) n; //fractional part;
30 itoa(n, a);
31 if(prec) {
32 while(*a && *++a);
33 int i;
34 *a++ = '.';
35 for(i = 0; i < prec; i++) {
36 d = d*10;
37 *a++ = ((int) d) + '0';
38 d -= (int) d;
39 }
40 *a = '\0';
41 }
42 return ret;
19 } 43 }
20 44
21 int atoi(char *a) 45 int atoi(char *a)
22 { 46 {
23 int neg = 0; 47 int neg = 0;
24 int n = 0; 48 int n = 0;
25 if(*a == '-') { 49 if(*a == '-') {
26 neg = 1; 50 neg = 1;
27 a++; 51 a++;
28 } 52 }
29 while(*a >='0' && *a <= '9') 53 while(*a >= '0' && *a <= '9')
30 n = n*10 + (*a++ - '0'); 54 n = n*10 + (*a++ - '0');
31 if(neg) n *= -1; 55 if(neg) n *= -1;
32 return n; 56 return n;
57 }
58
59 double atof(char *a)
60 {
61 int n = atoi(a);
62 double x = 0;
63 double dec = .1;
64 while(*a++ != '.');
65 while(*a != '\0') {
66 x += (*a - '0')*dec;
67 dec *= .1;
68 a++;
69 }
70 return n + x;
33 } 71 }
34 72
35 void sPrint(char *a) 73 void sPrint(char *a)
36 { 74 {
37 char *b = a; 75 char *b = a;
38 while(*b && *++b); 76 while(*b && *++b);
39 putline(a, b - a); 77 putline(a, b - a);
40 } 78 }
41 79
42 void iPrint(int n) 80 int strcmp(const char *a, const char *b)
43 {
44 char string[10];
45 itoa(n, string);
46 sPrint(string);
47 }
48
49 int strcmp(char *a, char *b)
50 { 81 {
51 while(1) { 82 while(1) {
52 if(*a - *b) return *a - *b; 83 if(*a - *b) return *a - *b;
53 if(*a == '\0') return 0; 84 if(*a == '\0') return 0;
54 a++; 85 a++;
68 int i; 99 int i;
69 for(i = 0; i < last; i++) 100 for(i = 0; i < last; i++)
70 if(!strcmp(array[i], text)) return i; 101 if(!strcmp(array[i], text)) return i;
71 return last; 102 return last;
72 } 103 }
104
105 char* append(char *dest, char *src)
106 {
107 char *ret = dest;
108 while(*dest&& *++dest); //get to null in first string
109 while((*dest++ = *src++));
110 return ret;
111 }