2
|
1 void itoa(int n, char *a)
|
|
2 {
|
|
3 if(n < 0) {
|
|
4 *a++ = '-';
|
|
5 n *= -1;
|
|
6 }
|
|
7 char *b = a;
|
|
8 if(!n) *b++ = '0';
|
|
9 for(; n; b++) {
|
|
10 *b = n%10 + '0';
|
|
11 n = n/10;
|
|
12 }
|
|
13 *b-- = '\0';
|
|
14 for(; a < b; a++, b--) { //reverse
|
|
15 char temp = *b;
|
|
16 *b = *a;
|
|
17 *a = temp;
|
|
18 }
|
|
19 }
|
|
20
|
|
21 int atoi(char *a)
|
|
22 {
|
|
23 int neg = 0;
|
|
24 int n = 0;
|
|
25 if(*a == '-') {
|
|
26 neg = 1;
|
|
27 a++;
|
|
28 }
|
|
29 while(*a >='0' && *a <= '9')
|
|
30 n = n*10 + (*a++ - '0');
|
|
31 if(neg) n *= -1;
|
|
32 return n;
|
|
33 }
|
|
34
|
|
35 void sPrint(char *a)
|
|
36 {
|
|
37 char *b = a;
|
|
38 while(*b && *++b);
|
|
39 putline(a, b - a);
|
|
40 }
|
|
41
|
|
42 void iPrint(int n)
|
|
43 {
|
|
44 char string[10];
|
|
45 itoa(n, string);
|
|
46 sPrint(string);
|
|
47 }
|
|
48
|
|
49 int strcmp(char *a, char *b)
|
|
50 {
|
|
51 while(1) {
|
|
52 if(*a - *b) return *a - *b;
|
|
53 if(*a == '\0') return 0;
|
|
54 a++;
|
|
55 b++;
|
|
56 }
|
|
57 return -1;
|
|
58 }
|
|
59
|
|
60 void sGet(char *a, unsigned int n) //TODO bug Jeff about getline
|
|
61 {
|
|
62 int length = getline(a, n);
|
|
63 a[(length < n)?length:n - 1] = '\0';
|
|
64 }
|
|
65
|
|
66 int arrayLookup(char *text, const char array[][10], int last)
|
|
67 {
|
|
68 int i;
|
|
69 for(i = 0; i < last; i++)
|
|
70 if(!strcmp(array[i], text)) return i;
|
|
71 return last;
|
|
72 }
|