# Thread: Help with Postfix Calculator Project

1. ## Help with Postfix Calculator Project

Hello, I am new to posting on this board, but have been reading posts for a few weeks now. I have searched through the FAQ and old posts and can't find the answer to my question.

I have to create a postfix calcuator in C using linked lists, along with push and pop functions. I am able to get output with the code that I have done thus far, but it's not right. If someone could please provide me some pointers on this, I would greatly appreciate it! Thank you very much!

Code:
```// Calc2.cpp : Defines the entry point for the console application.
//

#include "stdio.h"
#include "stdlib.h"
#include "stdafx.h"
#include "conio.h"
#include "malloc.h"
#include "ctype.h"
#include "string.h"

#define LINE_LEN 81

typedef int stack_element_t;

typedef struct stack_node_s {
stack_element_t element;
struct stack_node_s *restp;
} stack_node_t;

typedef struct {
stack_node_t *topp;
} stack_t;

void push(stack_t *sp, stack_element_t c);
stack_element_t pop(stack_t *sp);

int _tmain(int argc, _TCHAR* argv[])
{

char ans2[LINE_LEN];
stack_t ans={NULL};
int w, x, y, z, solution;

printf("Enter a postfix notation expression");
printf("\n:");
w=0;

for (z=0;z<=80;z++)
if (isdigit(ans2[z])){
w=(w*10)+ans2[z];

} else if (ans2[z] == '*' || ans2[z] == '+' || ans2[z] == '/' || ans2[z] == '%' || ans2[z] == '-' ) {
x=pop(&ans); // pop the first portion of the stack (number)
y=pop(&ans); // pop the second portion of the stack (number)
switch(ans2[z])
{
case '+': solution = x + y; break;
case '-': solution = x - y; break;
case '/': solution = x / y; break;
case '*': solution = x * y; break;
case '%': solution = x % y; break;
}
printf("=%d", solution);
} else if (ans2[z]==' '){
push(&ans, w);/* Builds the stack based on the user's entry */
w=0;
}else if (ans2[z]=='\0'){
printf("\nPress any key...");
getch();
return 0;
}

}

void push(stack_t *sp, stack_element_t c)
{
stack_node_t *newp;
/* pointer to new stack node */

newp = (stack_node_t *) malloc(sizeof (stack_node_t));
newp->element =c;
newp->restp = sp->topp;
sp->topp = newp;
}

stack_element_t pop(stack_t *sp)
{
stack_node_t *to_freep;
to_freep = sp->topp;
sp->topp = to_freep->restp;
free(to_freep);

}```

2. 1) .cpp is usually a C++ extension. Just use .c
2) "" on your includes means to check the local current directory. You do not want this usually. It is best to use "" for your own created headers. Use < > for standard headers.

3) What the hell is this:
int _tmain(int argc, _TCHAR* argv[])

4) Main returns an int, so return one.

5) Don't use gets.

6) Do provide an actual input string and the example output. I'm not going to compile random code for the hell of it just to see what it does.

Quzah.

3. ## Hi again

I'm not sure if this will explain the reason why some of the code may have looked a bit different, but I am using Visual Studio .NET edition's version of Visual C++ to make this program. When I do that, it automatically generates the main function as int _tmain(int argc, _TCHAR* argv[])

Thanks for letting me about the distinction between the "" and <> for the header files. I thought that could be used interchangably...

I chose to use gets because that the was the only that I was aware of to be able to grab an entire line of user input as one string. (Other than fgets, which I don't fully grasp). If there is a better method, I am open to that suggestion.

In terms of sample output, here goes:

User inputs:

1 1 +

The user gets:

=98

User inputs:

2 3 *

The user gets:

2550

I hope that this helps.. thanks again..

4. Code:
```#include <stdio.h>

int main( void )
{
printf("1 + 1 is: %d\n", '1' + '1' );
printf("2 * 3 is: %d\n", '2' * '3' );
printf("Notice, you are using the decimal value of the character.\n");
printf("You want:\n");
printf("1 + 1 is: %d\n", ('1'-'0') + ('1'-'0') );
printf("2 * 3 is: %d\n", ('2'-'0') * ('3'-'0') );
printf("See?\n");

return 0;
}```
Presto. Problem solved..

Quzah.

5. ## Awesome, thanks!!!

Thank you very very much.. that works great! I really do appreciate the help!

6. >3) What the hell is this:
>int _tmain(int argc, _TCHAR* argv[])

I think that is for unicode support or something.