Help with Postfix Calculator Project

This is a discussion on Help with Postfix Calculator Project within the C Programming forums, part of the General Programming Boards category; Hello, I am new to posting on this board, but have been reading posts for a few weeks now. I ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    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:");
    	gets(ans2); // Get the answer
        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;
    	stack_element_t answer;
    	to_freep = sp->topp;
    	answer= to_freep->element;
    	sp->topp = to_freep->restp;
    	free(to_freep);
    
    	return (answer);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    Awesome, thanks!!!

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

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >3) What the hell is this:
    >int _tmain(int argc, _TCHAR* argv[])

    I think that is for unicode support or something.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  3. Just a postfix clarification.
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2005, 09:47 AM
  4. Infix to Postfix
    By dat in forum C Programming
    Replies: 6
    Last Post: 06-16-2003, 09:46 AM
  5. postfix
    By datainjector in forum C Programming
    Replies: 11
    Last Post: 11-20-2002, 08:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21