changeset 5:348c59c36703

Fixed a LOT of mistakes I made with atof and ftoa...
author Jonathan Pevarnek <pevarnj@gmail.com>
date Wed, 02 Mar 2011 15:43:05 -0500
parents 90cd3d9a6ca3
children 8676cf44c307
files src/init.c src/operations.c src/std.c
diffstat 3 files changed, 25 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/init.c	Wed Mar 02 00:37:32 2011 -0500
+++ b/src/init.c	Wed Mar 02 15:43:05 2011 -0500
@@ -44,9 +44,7 @@
 		double toStore;
 		char input[30];
 		sPrint("Please enter an operation or value: ");
-		sGet(input, 30);
-		opVal = arrayLookup(input, operationNames, MAXOP);
-//		opVal = arrayLookup(sGet(input, 30), operationNames, MAXOP); //TODO figure out what is wrong with this...
+		opVal = arrayLookup(sGet(input, 30), operationNames, MAXOP);
 		if(opVal != MAXOP) {
 			(*operation[opVal]) (stack); //call the function array
 		} else {
--- a/src/operations.c	Wed Mar 02 00:37:32 2011 -0500
+++ b/src/operations.c	Wed Mar 02 15:43:05 2011 -0500
@@ -9,8 +9,8 @@
 void op_print(struct Stack *stack)
 {
 	eltType val = pop(stack);
-	char output[10];
-	sPrint(ftoa(val, output, 4));
+	char output[20];
+	sPrint(ftoa(val, output, 10));
 	sPrint("\n");
 }
 
--- a/src/std.c	Wed Mar 02 00:37:32 2011 -0500
+++ b/src/std.c	Wed Mar 02 15:43:05 2011 -0500
@@ -28,19 +28,25 @@
 	int n = (int) x; //integer part
 	double d = x - (double) n; //fractional part;
 	itoa(n, a);
-	if(prec) {
-		while(*a && *++a);
-		int i;
-		*a++ = '.';
+	if(prec) { //only do the decimal part if decimal parts were asked for
+		while(*a && *++a); //get to the null character from itoa
+		int i; //counter variable for the for loop
+		*a++ = '.'; //put the decimal in place
 		for(i = 0; i < prec; i++) {
-			d = d*10;
-			*a++ = ((int) d) + '0';
-			d -= (int) d;
+			d = d*10; //the integer part is the decimal digit
+			*a++ = ((int) d) + '0'; //add the integer part of d to the string
+			d -= (int) d; //chop off the integer part
+		} a--; //move back to the last digit
+		while(*a != '.') {
+			if(*a == '0') {
+				a--;
+				continue;
+			} else if(*a > '0' && *a <= '9') {
+				a++;
+				break;
+			}
 		}
-		a--; //move back to the last digit
-//		while(*a == '0') a--; //TODO figure out why this "works" even with the other version of init.c
-		while(*a == '0' || *a == '.') a--; //get to the first non-zero decimal digit
-		*(a + 1) = '\0';
+		*a = '\0';
 	}
 	return ret;
 }
@@ -64,8 +70,11 @@
 	int n = atoi(a);
 	double x = 0;
 	double dec = .1;
-	while(*a++ != '.');
-	while(*a != '\0') {
+	while(*a != '.') {
+		if(*a == '\0') return n;
+		a++;
+	} a++; //a will be immediately after the decimal point
+	while(*a >= '0' && *a <= '9') { //goes through the decimal part
 		x += (*a - '0')*dec;
 		dec *= .1;
 		a++;