changeset 8:25b2b501a5fa

Cleaned up the stack operations The stack operations now handle error checking correctly (and are no longer a tangled mass of duplicated code) atoi now will work correctly if there is a "+" symbol in front of the number atof will no longer treat something like "aoe235.357" as ".357"
author Jonathan Pevarnek <pevarnj@gmail.com>
date Wed, 09 Mar 2011 12:08:48 -0500
parents 089efc170bb0
children f8493aa1b207
files include/math.h include/operations.h include/stack.h src/init.c src/math.c src/operations.c src/stack.c src/std.c
diffstat 8 files changed, 91 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/include/math.h	Sun Mar 06 01:55:09 2011 -0500
+++ b/include/math.h	Wed Mar 09 12:08:48 2011 -0500
@@ -7,4 +7,9 @@
 double math_pow(double base, double exp);
 double math_abs(double num);
 
+double math_add(double first, double sec);
+double math_sub(double first, double sec);
+double math_mult(double first, double sec);
+double math_div(double first, double sec);
+
 #endif //__MATH_H
--- a/include/operations.h	Sun Mar 06 01:55:09 2011 -0500
+++ b/include/operations.h	Wed Mar 09 12:08:48 2011 -0500
@@ -8,6 +8,9 @@
 extern const char operationNames[MAXOP][10];
 extern void (*operation[MAXOP])(struct Stack*);
 
+void op_math1(struct Stack *stack, eltType (*mathop)(eltType));
+void op_math2(struct Stack *stack, eltType (*mathop)(eltType, eltType));
+
 void op_print(struct Stack *stack);
 void op_add(struct Stack *stack);
 void op_sub(struct Stack *stack);
--- a/include/stack.h	Sun Mar 06 01:55:09 2011 -0500
+++ b/include/stack.h	Wed Mar 09 12:08:48 2011 -0500
@@ -2,14 +2,19 @@
 #define __STACK_H
 
 typedef double eltType;
+typedef struct eltTypeCon eltCon;
 
 struct Stack {
-	int isEmpty;
-	unsigned int top;
+	int top;
 	eltType values[100];
 };
 
-eltType pop(struct Stack *stack);
+struct eltTypeCon { //contains an eltType and an error value
+	eltType val;
+	short error;
+};
+
+eltCon pop(struct Stack *stack);
 void push(struct Stack *stack, eltType val);
 void initStack(struct Stack *stack);
 
--- a/src/init.c	Sun Mar 06 01:55:09 2011 -0500
+++ b/src/init.c	Wed Mar 09 12:08:48 2011 -0500
@@ -41,15 +41,13 @@
 	initStack(stack);
 	while(1) {
 		int opVal;
-		double toStore;
 		char input[30];
 		sPrint("Please enter an operation or value: ");
 		opVal = arrayLookup(sGet(input, 30), operationNames, MAXOP);
 		if(opVal != MAXOP) {
 			(*operation[opVal]) (stack); //call the function array
 		} else {
-			toStore = atof(input);
-			push(stack, toStore);
+			push(stack, atof(input));
 		}
 	}
 
--- a/src/math.c	Sun Mar 06 01:55:09 2011 -0500
+++ b/src/math.c	Wed Mar 09 12:08:48 2011 -0500
@@ -68,3 +68,23 @@
 	if(num < 0) return num*-1;
 	return num;
 }
+
+double math_add(double first, double sec)
+{
+	return first + sec;
+}
+
+double math_sub(double first, double sec)
+{
+	return first - sec;
+}
+
+double math_mult(double first, double sec)
+{
+	return first*sec;
+}
+
+double math_div(double first, double sec)
+{
+	return first/sec;
+}
--- a/src/operations.c	Sun Mar 06 01:55:09 2011 -0500
+++ b/src/operations.c	Wed Mar 09 12:08:48 2011 -0500
@@ -7,54 +7,72 @@
 void (*operation[MAXOP])(struct Stack*) = {op_print, op_add, op_sub, op_mult,
 	op_div, op_pow, op_dup, op_drop, op_log, op_exp};
 
+void op_math1(struct Stack *stack, eltType (*mathop)(eltType))
+{
+	if(stack->top < 0) sPrint("ERROR\n");
+	else {
+		eltCon con = pop(stack);
+		if(con.error) sPrint("ERROR\n");
+		else push(stack, mathop(con.val));
+	}
+}
+
+void op_math2(struct Stack *stack, eltType (*mathop)(eltType, eltType))
+{
+	if(stack->top < 1) sPrint("ERROR\n");
+	else {
+		eltCon con2 = pop(stack);
+		eltCon con1 = pop(stack);
+		if(con1.error || con2.error) sPrint("ERROR\n");
+		else push(stack, mathop(con1.val, con2.val));
+	}
+}
+
 void op_print(struct Stack *stack)
 {
-	eltType val = pop(stack);
-	char output[20];
-	sPrint(ftoa(val, output, 10));
-	sPrint("\n");
+	eltCon con = pop(stack);
+	if(con.error) sPrint("ERROR\n");
+	else {
+		eltType val = con.val;
+		char output[30];
+		sPrint(append(ftoa(val, output, 10), "\n"));
+	}
 }
 
 void op_add(struct Stack *stack)
 {
-	eltType first = pop(stack);
-	eltType sec = pop(stack);
-	push(stack, sec + first); //yeah, this should be obvious
+	op_math2(stack, math_add);
 }
 
 void op_sub(struct Stack *stack)
 {
-	eltType first = pop(stack);
-	eltType sec = pop(stack);
-	push(stack, sec - first);
+	op_math2(stack, math_sub);
 }
 
 void op_mult(struct Stack *stack)
 {
-	eltType first = pop(stack);
-	eltType sec = pop(stack);
-	push(stack, sec*first);
+	op_math2(stack, math_mult);
 }
 
 void op_div(struct Stack *stack)
 {
-	eltType first = pop(stack);
-	eltType sec = pop(stack);
-	push(stack, sec/first);
+	op_math2(stack, math_div);
 }
 
 void op_pow(struct Stack *stack)
 {
-	eltType first = pop(stack);
-	eltType sec = pop(stack);
-	push(stack, math_pow(sec, first));
+	op_math2(stack, math_pow);
 }
 
 void op_dup(struct Stack *stack)
 {
-	eltType val = pop(stack);
-	push(stack, val);
-	push(stack, val);
+	eltCon con = pop(stack);
+	if(con.error) sPrint("ERROR\n");
+	else {
+		eltType val = con.val;
+		push(stack, val);
+		push(stack, val);
+	}
 }
 
 void op_drop(struct Stack *stack)
@@ -64,12 +82,10 @@
 
 void op_log(struct Stack *stack)
 {
-	eltType val = pop(stack);
-	push(stack, math_log(val));
+	op_math1(stack, math_log);
 }
 
 void op_exp(struct Stack *stack)
 {
-	eltType val = pop(stack);
-	push(stack, math_exp(val));
+	op_math1(stack, math_exp);
 }
--- a/src/stack.c	Sun Mar 06 01:55:09 2011 -0500
+++ b/src/stack.c	Wed Mar 09 12:08:48 2011 -0500
@@ -1,33 +1,24 @@
 #include <stack.h>
 #include <std.h>
 
-eltType pop(struct Stack *stack)
+eltCon pop(struct Stack *stack)
 {
-	if(stack->isEmpty) {
-		sPrint("ERROR: STACK IS EMPTY.\n");
-		return -1;
-	} else if(stack->top == 0) {
-		stack->isEmpty = 1;
-		return stack->values[stack->top];
-	} else if(stack->top < 0) {
-		return -1; //TODO figure out a better way to handle errors
+	eltCon ret;
+	if(stack->top == -1) {
+		ret.error = 1;
 	} else {
-		return stack->values[stack->top--];
+		ret.error = 0;
+		ret.val = stack->values[stack->top--];
 	}
+	return ret;
 }
 
 void push(struct Stack *stack, eltType val)
 {
-	if(stack->isEmpty) {
-		stack->isEmpty = 0;
-		stack->values[stack->top] = val;
-	} else {
-		stack->values[++stack->top] = val;
-	}
+	if(stack->top < 100) stack->values[++stack->top] = val;
 }
 
 void initStack(struct Stack *stack)
 {
-	stack->isEmpty = 1;
-	stack->top = 0;
+	stack->top = -1;
 }
--- a/src/std.c	Sun Mar 06 01:55:09 2011 -0500
+++ b/src/std.c	Wed Mar 09 12:08:48 2011 -0500
@@ -53,12 +53,12 @@
 
 int atoi(char *a)
 {
-	int neg = 0;
+	short neg = 0;
 	int n = 0;
 	if(*a == '-') {
 		neg = 1;
 		a++;
-	}
+	} else if(*a == '+') a++;
 	while(*a >= '0' && *a <= '9')
 		n = n*10 + (*a++ - '0');
 	if(neg) n *= -1;
@@ -71,7 +71,7 @@
 	double x = 0;
 	double dec = .1;
 	while(*a != '.') {
-		if(*a == '\0') return n;
+		if(!(*a >= '0' && *a <= '9')) return n;
 		a++;
 	} a++; //a will be immediately after the decimal point
 	while(*a >= '0' && *a <= '9') { //goes through the decimal part