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); }



LinkBack URL
About LinkBacks
If someone could please provide me some pointers on this, I would greatly appreciate it! Thank you very much!


