Hi there everyone! I am doing calculator that analysses a math term in a postfix notation. My code seems nice, but I suspect it doesnt calculate at all ! It only writes back imput .. Maybe theres error in string/char conversion for all that I know. But please have a look at it and tell me what is wrong with it...

#include <iostream>
#include <math.h>
#include <string.h>

bool err = false;	
//stack : 
const int SIZE = 100;
double stack[SIZE]; 
int nOperandu = 0; 

bool push(double x)
{
	if (nOperandu<SIZE)
	{
		stack[nOperandu++] = x;
		return true;
	}
	return false;
}

bool pop(double &x)
{
	if (nOperandu>0)
	{
		x = stack[--nOperandu];
		return true;
	}
	return false;
}

int getCount()
{
	return nOperandu;
}

bool isEmpty()
{
	return nOperandu==0;
}

//end of stack

// functions: 
void add() 
{
	double a;
	double b;
	pop(b);		 
	pop(a);

	push(a+b);
}
void minus() 
{
	double a;
	double b;
	pop(b);		 
	pop(a);

	push(a-b);
}
void multiply() 
{
	double a;
	double b;
	pop(b);		 
	pop(a);

	push(a*b);
}
void divi() 
{
	double a;
	double b;
	pop(b);		 
	pop(a);
	if (b == 0)
		{
			printf("Cant div. by 0.\n");
			err = true;
		}
	else push(a/b);
}
void mod()
{
	double a;
	double b;
	pop(b);		
	pop(a);
	if (b == 0)
		{
			printf("Cant div. by 0.\n");
			err = true;
		}
	else push(a / b);
}

void powa() 
{
	double a;
	double b;
	pop(b);		
	pop(a);
	if ((a == 0) && (b < 0)) 
		{
			printf("Cant pow. with negative number.\n");
			err = true;  
		}
	else push(pow(a,b));
}
void full() 
{
	double a;
	pop(a);

	if (a < 0) push(ceil(a));
		else push(floor(a));
}			

// end of functions

// main alg: 
void alg()
{
	char line[256];
	char *cast = NULL;
	char *nextCast = NULL;
	double result;

	printf("Add numbers in postfix:\n ");
	gets_s(line,255);	// loads line
	cast = strtok_s(line," \n", &nextCast); 
	while ((cast != NULL) && !(err)) // loop till error 
        {
		if ((cast[0]!='+') || (cast[0]!='-') || (cast[0]!='*') ||
		    (cast[0]!='/') || (cast[0]!='%') || (cast[0]!='^') ||
		    (cast[0]!='f'))  // if first char isnt operator then convert to number and add to stack  
		{
			push(atof(cast));
		}
		else
		{
			switch (cast[0]) // chceks if there are enough operands in cases
			{
				case '+': 
					if (getCount() >= 2) add(); 
						else 
						{
							printf("Not enough operators.\n");
							err = true; 
						}
					break;
				case '-': 
					if (getCount() >= 2) minus(); 
						else 
						{
							printf("Not enough operators.\n");
							err = true; 
						}
					break;
				case '*': 
					if (getCount() >= 2) multiply(); 
						else 
						{
							printf("Not enough operators.\n");
							err = true; 
						}
					break;
				case '/': 
					if (getCount() >= 2) divi(); 
						else 
						{
							printf("Not enough operators.\n");
							err = true; 
						}
					break;
				case '%': 
					if (getCount() >= 2) mod(); 
						else 
						{
							printf("Not enough operators.\n");
							err = true; 
						}
					break;
				case '^': 
					if (getCount() >= 2) powa(); 
						else 
						{
							printf("Not enough operators.\n");
							err = true; 
						}
					break;
				case 'f': 
					if (getCount() >= 1) full(); 
						else 
						{
							printf("Not enough operators.\n");
							err = true; 
						}
					break;
			}
		}
        	cast = strtok_s( NULL, " \n", &nextCast); // jumps to next part of the line
    	}
	/*
	if (isEmpty()) printf("Lack of operators\n"); 
	else
	if (getCount() > 1) printf("Too many operators\n"); 
	else*/
	if (!err)
	{
		pop(result);
		printf("Result is: %0.5lf.\n",result); // writes result with 5 decimal
	}
}
//and of alg
int main()
{
	alg();
	//return 0;
	system("PAUSE");
}

Thanks for your effort ;)

First, welcome to the board.

Second, thanks for figuring out how to post code using code tags. Much appreciated.

Third, except for line #1 your code is written in classic C style, not C++ so posting to the C board may get you more responses. As some of my concerns with your program have more to do with logic or involve protocols that are the same in C as in C++ I will address some of them below.

Fourth, you would be wise to limit your enthusiasm for writing whole programs without compiling as you are doomed to repeating the same mistakes over and over again if you do. In addition it is more difficult to debug a program all at once as opposed to a few lines, or at most a single task/function, at a time. Compiling after every few lines can save a whole lot of hassles, even if it means extra time along the way.

Fifth, to see if what strings/tokens you are generating with strok() place an out statement between line 132 and 133. It's been a long time since I used C I/O so this may well not be syntactically correct, though it should given you an idea of what to do:

printf(%s, cast);

Sixth: In lines 135 - 137 I think you want to use the && operator instead of the ||. I think you want to say, the current value of cast is an operand if and only if it isn't an operator like addition and it isn't an operator like subtraction and it isn't an operator like multiplication, etc.

Seventh: Once you have determined the current string/token in cast is an operator how will the functions you call know what values to use in the operation unless you pass them to the function. You can either pass the stack and pop the appropriate numbers to use off the stack, popping the result of the algebraic process done in the function back onto the stack before returning to the calling function, or you can do the popping in the calling function, pass the appropriate number of operands to the function to do the algebraic expression and return the result back to the calling function where it is then pushed back onto the stack before proceeding.

Third, except for line #1 your code is written in classic C style, not C++ so posting to the C board may get you more responses.

Not so. pop() argument is passed by reference, which addresses your seventh bullet.

printf("%s", cast);

Just to avoid a possible confusion.

And stack[] and nOperandu are declared with global scope, which I forgot to even look for before because I loathe use of global variables so much. Therefore I retract my seventh bullet point as nezachem correctly points out I was in error on that part.

I also appreciate nezachem's correction on the printf() statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.