annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
1 #include <std.h>
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
2
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
3 char* itoa(int n, char *a)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
4 {
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
5 char *ret = a;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
6 if(n < 0) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
7 *a++ = '-';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
8 n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
9 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
10 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
11 if(!n) *b++ = '0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
12 for(; n; b++) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
13 *b = n%10 + '0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
14 n = n/10;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
15 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
16 *b-- = '\0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
17 for(; a < b; a++, b--) { //reverse
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
18 char temp = *b;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
19 *b = *a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
20 *a = temp;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
21 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
22 return ret;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
23 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
24
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
25 char* ftoa(double x, char *a, unsigned int prec)
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
26 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
27 char *ret = a;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
28 int n = (int) x; //integer part
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
29 double d = x - (double) n; //fractional part;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
30 itoa(n, a);
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
31 if(prec) {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
32 while(*a && *++a);
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
33 int i;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
34 *a++ = '.';
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
35 for(i = 0; i < prec; i++) {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
36 d = d*10;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
37 *a++ = ((int) d) + '0';
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
38 d -= (int) d;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
39 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
40 *a = '\0';
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
41 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
42 return ret;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
43 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
44
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
45 int atoi(char *a)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
46 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
47 int neg = 0;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
48 int n = 0;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
49 if(*a == '-') {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
50 neg = 1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
51 a++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
52 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
53 while(*a >= '0' && *a <= '9')
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
54 n = n*10 + (*a++ - '0');
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
55 if(neg) n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
56 return n;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
57 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
58
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
59 double atof(char *a)
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
60 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
61 int n = atoi(a);
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
62 double x = 0;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
63 double dec = .1;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
64 while(*a++ != '.');
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
65 while(*a != '\0') {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
66 x += (*a - '0')*dec;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
67 dec *= .1;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
68 a++;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
69 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
70 return n + x;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
71 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
72
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
73 void sPrint(char *a)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
74 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
75 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
76 while(*b && *++b);
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
77 putline(a, b - a);
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
78 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
79
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
80 int strcmp(const char *a, const char *b)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
81 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
82 while(1) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
83 if(*a - *b) return *a - *b;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
84 if(*a == '\0') return 0;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
85 a++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
86 b++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
87 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
88 return -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
89 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
90
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
91 void sGet(char *a, unsigned int n) //TODO bug Jeff about getline
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
92 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
93 int length = getline(a, n);
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
94 a[(length < n)?length:n - 1] = '\0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
95 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
96
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
97 int arrayLookup(char *text, const char array[][10], int last)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
98 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
99 int i;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
100 for(i = 0; i < last; i++)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
101 if(!strcmp(array[i], text)) return i;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
102 return last;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
103 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
104
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
105 char* append(char *dest, char *src)
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
106 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
107 char *ret = dest;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
108 while(*dest&& *++dest); //get to null in first string
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
109 while((*dest++ = *src++));
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
110 return ret;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
111 }