changeset 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
files include/operations.h include/stack.h include/std.h src/init.c src/operations.c src/stack.c src/std.c
diffstat 7 files changed, 89 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/include/operations.h	Tue Mar 01 02:39:27 2011 -0500
+++ b/include/operations.h	Tue Mar 01 23:33:51 2011 -0500
@@ -5,13 +5,13 @@
 #include <stack.h>
 
 enum Operation {PRINT, ADD, SUB, MULT, DIV, DUP, MAXOP};
-const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "dup"};
+extern const char operationNames[MAXOP][10];
 
-void print(struct Stack *stack);
-void add(struct Stack *stack);
-void sub(struct Stack *stack);
-void mult(struct Stack *stack);
-void div(struct Stack *stack);
-void dup(struct Stack *stack);
+void op_print(struct Stack *stack);
+void op_add(struct Stack *stack);
+void op_sub(struct Stack *stack);
+void op_mult(struct Stack *stack);
+void op_div(struct Stack *stack);
+void op_dup(struct Stack *stack);
 
 #endif //__OPERATIONS_H
--- a/include/stack.h	Tue Mar 01 02:39:27 2011 -0500
+++ b/include/stack.h	Tue Mar 01 23:33:51 2011 -0500
@@ -1,7 +1,7 @@
 #ifndef __STACK_H
 #define __STACK_H
 
-typedef int eltType;
+typedef double eltType;
 
 struct Stack {
 	int isEmpty;
--- a/include/std.h	Tue Mar 01 02:39:27 2011 -0500
+++ b/include/std.h	Tue Mar 01 23:33:51 2011 -0500
@@ -1,15 +1,18 @@
 #ifndef __STD_H
 #define __STD_H
 
-extern void itoa(int n, char *a);
-extern int atoi(char *a);
+char* ftoa(double x, char *a, unsigned int prec);
+char* itoa(int n, char *a);
+int atoi(char *a);
+double atof(char *a);
 
-extern void sPrint(char *a);
-void iPrint(int n);
-extern void sGet(char *a, unsigned int n);
+void sPrint(char *a);
+void sGet(char *a, unsigned int n);
 
-extern int strcmp(char *a, char *b);
+int strcmp(const char *a, const char *b);
 
 int arrayLookup(char *text, const char array[][10], int last);
 
+char* append(char *dest, char *src);
+
 #endif
--- a/src/init.c	Tue Mar 01 02:39:27 2011 -0500
+++ b/src/init.c	Tue Mar 01 23:33:51 2011 -0500
@@ -37,27 +37,28 @@
 	*/
 
 	void (*operation[MAXOP])(struct Stack*) = {NULL};
-	operation[0] = print;
-	operation[1] = add;
-	operation[2] = sub;
-	operation[3] = mult;
-	operation[4] = div;
-	operation[5] = dup;
+	operation[0] = op_print;
+	operation[1] = op_add;
+	operation[2] = op_sub;
+	operation[3] = op_mult;
+	operation[4] = op_div;
+	operation[5] = op_dup;
 
 	struct Stack theStack;
 	struct Stack *stack = &theStack;
 	initStack(stack);
 	while(1) {
 		int opVal;
-		char input[20];
+		double toStore;
+		char input[30];
 		sPrint("Please enter an operation or value: ");
-		sGet(input, 20);
+		sGet(input, 30);
 		opVal = arrayLookup(input, operationNames, MAXOP);
 		if(opVal != MAXOP) {
 			(*operation[opVal]) (stack); //call the function array
 		} else {
-			opVal = atoi(input);
-			push(stack, opVal);
+			toStore = atof(input);
+			push(stack, toStore);
 		}
 	}
 
--- a/src/operations.c	Tue Mar 01 02:39:27 2011 -0500
+++ b/src/operations.c	Tue Mar 01 23:33:51 2011 -0500
@@ -1,45 +1,46 @@
-//#include <operations.h>
+#include <operations.h>
 #include <std.h>
 #include <stack.h>
 
-void print(struct Stack *stack)
+const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "dup"};
+
+void op_print(struct Stack *stack)
 {
 	eltType val = pop(stack);
 	char output[10];
-	itoa(val, output);
-	sPrint(output);
+	sPrint(ftoa(val, output, 4));
 	sPrint("\n");
 }
 
-void add(struct Stack *stack)
+void op_add(struct Stack *stack)
 {
 	eltType first = pop(stack);
 	eltType sec = pop(stack);
 	push(stack, sec + first); //yeah, this should be obvious
 }
 
-void sub(struct Stack *stack)
+void op_sub(struct Stack *stack)
 {
 	eltType first = pop(stack);
 	eltType sec = pop(stack);
 	push(stack, sec - first);
 }
 
-void mult(struct Stack *stack)
+void op_mult(struct Stack *stack)
 {
 	eltType first = pop(stack);
 	eltType sec = pop(stack);
 	push(stack, sec*first);
 }
 
-void div(struct Stack *stack)
+void op_div(struct Stack *stack)
 {
 	eltType first = pop(stack);
 	eltType sec = pop(stack);
 	push(stack, sec/first);
 }
 
-void dup(struct Stack *stack)
+void op_dup(struct Stack *stack)
 {
 	eltType val = pop(stack);
 	push(stack, val);
--- a/src/stack.c	Tue Mar 01 02:39:27 2011 -0500
+++ b/src/stack.c	Tue Mar 01 23:33:51 2011 -0500
@@ -1,4 +1,5 @@
 #include <stack.h>
+#include <std.h>
 
 eltType pop(struct Stack *stack)
 {
@@ -9,7 +10,7 @@
 		stack->isEmpty = 1;
 		return stack->values[stack->top];
 	} else if(stack->top < 0) {
-		return 0; //TODO figure out a better way to handle errors
+		return -1; //TODO figure out a better way to handle errors
 	} else {
 		return stack->values[stack->top--];
 	}
--- 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;
+}